refactor🎨: (阅读代码):ui-utils.ts增加注释

master
yetao 2 weeks ago
parent 4501fe40d0
commit 86aec22b0e

@ -1,104 +1,255 @@
// 导入 common/fileSystemConfig 模块中的 FileSystemConfig 类型和 parseConnectionString 函数
import { FileSystemConfig, parseConnectionString } from 'common/fileSystemConfig'; import { FileSystemConfig, parseConnectionString } from 'common/fileSystemConfig';
// 导入 vscode 模块
import * as vscode from 'vscode'; import * as vscode from 'vscode';
// 从./config 模块中导入 getConfigs 函数
import { getConfigs } from './config'; import { getConfigs } from './config';
// 从./connection 模块中导入 Connection 和 ConnectionManager 类型
import type { Connection, ConnectionManager } from './connection'; import type { Connection, ConnectionManager } from './connection';
// 从./manager 模块中导入 Manager 类型
import type { Manager } from './manager'; import type { Manager } from './manager';
// 从./pseudoTerminal 模块中导入 SSHPseudoTerminal 类型
import type { SSHPseudoTerminal } from './pseudoTerminal'; import type { SSHPseudoTerminal } from './pseudoTerminal';
// 从./sshFileSystem 模块中导入 SSHFileSystem 类型
import type { SSHFileSystem } from './sshFileSystem'; import type { SSHFileSystem } from './sshFileSystem';
/**
* vscode.QuickPickItem vscode.TreeItem
*
*/
export interface FormattedItem extends vscode.QuickPickItem, vscode.TreeItem { export interface FormattedItem extends vscode.QuickPickItem, vscode.TreeItem {
/**
*
* 使
*/
item: any; item: any;
/**
*
*/
label: string; label: string;
/**
*
*
*/
description?: string; description?: string;
} }
/**
*
* @param config -
* @returns
* @description
*
*
*/
export function formatAddress(config: FileSystemConfig): string { export function formatAddress(config: FileSystemConfig): string {
const { username, host, port } = config; const { username, host, port } = config;
return `${username ? `${username}@` : ''}${host}${port ? `:${port}` : ''}`; return `${username ? `${username}@` : ''}${host}${port ? `:${port}` : ''}`;
} }
export function setWhenClauseContext(key: string, value: any) { export function setWhenClauseContext(key: string, value: any) {
// 执行 setContext 命令,设置名为 sshfs.key 的上下文,其值为 value
return vscode.commands.executeCommand('setContext', `sshfs.${key}`, value); return vscode.commands.executeCommand('setContext', `sshfs.${key}`, value);
} }
/**
*
* @param connectionManager -
* @returns Promise
* @description
* VS Code
*
*/
export function setupWhenClauseContexts(connectionManager: ConnectionManager): Promise<void> { export function setupWhenClauseContexts(connectionManager: ConnectionManager): Promise<void> {
async function refresh() { async function refresh() {
// 获取所有活动连接
const active = connectionManager.getActiveConnections(); const active = connectionManager.getActiveConnections();
// 获取所有待处理连接
const pending = connectionManager.getPendingConnections(); const pending = connectionManager.getPendingConnections();
// 设置上下文 'openConnections' 的值为活动连接和待处理连接的总数
await setWhenClauseContext('openConnections', active.length + pending.length); await setWhenClauseContext('openConnections', active.length + pending.length);
// 设置上下文 'openTerminals' 的值为所有活动连接中终端的总数
await setWhenClauseContext('openTerminals', active.reduce((tot, con) => tot + con.terminals.length, 0)); await setWhenClauseContext('openTerminals', active.reduce((tot, con) => tot + con.terminals.length, 0));
// 设置上下文 'openFileSystems' 的值为所有活动连接中文件系统的总数
await setWhenClauseContext('openFileSystems', active.reduce((tot, con) => tot + con.filesystems.length, 0)); await setWhenClauseContext('openFileSystems', active.reduce((tot, con) => tot + con.filesystems.length, 0));
} }
/**
* refresh 便
* @param connectionManager -
*/
connectionManager.onConnectionAdded(refresh); connectionManager.onConnectionAdded(refresh);
connectionManager.onConnectionRemoved(refresh); connectionManager.onConnectionRemoved(refresh);
connectionManager.onConnectionUpdated(refresh); connectionManager.onConnectionUpdated(refresh);
connectionManager.onPendingChanged(refresh); connectionManager.onPendingChanged(refresh);
/**
* refresh
* @returns Promise refresh
*/
return refresh(); return refresh();
} }
/**
*
* @type {vscode.ExtensionContext['asAbsolutePath'] | undefined}
*/
export let asAbsolutePath: vscode.ExtensionContext['asAbsolutePath'] | undefined; export let asAbsolutePath: vscode.ExtensionContext['asAbsolutePath'] | undefined;
/**
*
* @param {typeof asAbsolutePath} value -
*/
export const setAsAbsolutePath = (value: typeof asAbsolutePath) => asAbsolutePath = value; export const setAsAbsolutePath = (value: typeof asAbsolutePath) => asAbsolutePath = value;
/**
* SSH SSH
* @param item - SSH SSH
* @param iconInLabel -
* @returns
* @description
* 便
*
*/
/** Converts the supported types to something basically ready-to-use as vscode.QuickPickItem and vscode.TreeItem */ /** Converts the supported types to something basically ready-to-use as vscode.QuickPickItem and vscode.TreeItem */
export function formatItem(item: FileSystemConfig | Connection | SSHFileSystem | SSHPseudoTerminal, iconInLabel = false): FormattedItem { export function formatItem(item: FileSystemConfig | Connection | SSHFileSystem | SSHPseudoTerminal, iconInLabel = false): FormattedItem {
/**
* item SSHPseudoTerminal
*
*/
if ('handleInput' in item) { // SSHPseudoTerminal if ('handleInput' in item) { // SSHPseudoTerminal
// 返回一个格式化的终端项
return { return {
item, contextValue: 'terminal', // 原始项
label: `${iconInLabel ? '$(terminal) ' : ''}${item.terminal?.name || 'Unnamed terminal'}`, item,
iconPath: new vscode.ThemeIcon('terminal'), // 上下文值,用于标识该项是一个终端
contextValue: 'terminal',
// 标签包含终端的名称如果没有名称则显示为“Unnamed terminal”
label: `${iconInLabel? '$(terminal) ' : ''}${item.terminal?.name || 'Unnamed terminal'}`,
// 图标路径,使用 VS Code 的主题图标“terminal”
iconPath: new vscode.ThemeIcon('terminal'),
// 命令,当该项被选中时执行的命令
command: { command: {
title: 'Focus', // 命令标题
command: 'sshfs.focusTerminal', title: 'Focus',
arguments: [item], // 命令 ID
command: 'sshfs.focusTerminal',
// 命令参数,包含要聚焦的终端项
arguments: [item],
}, },
}; };
} else if ('client' in item) { // Connection } else if ('client' in item) { // Connection
// 从 item.config 中提取 label、name 和 group 属性
const { label, name, group } = item.config; const { label, name, group } = item.config;
// 根据 group 是否存在来生成描述信息
const description = group ? `${group}.${name} ` : (label && name); const description = group ? `${group}.${name} ` : (label && name);
// 使用 formatAddress 函数格式化 item.actualConfig 来生成详细信息
const detail = formatAddress(item.actualConfig); const detail = formatAddress(item.actualConfig);
// 返回一个格式化的项对象
return { return {
item, description, detail, tooltip: detail, // 原始项
label: `${iconInLabel ? '$(plug) ' : ''}${label || name} `, item,
iconPath: new vscode.ThemeIcon('plug'), // 描述信息
collapsibleState: vscode.TreeItemCollapsibleState.Expanded, description,
contextValue: 'connection', // 详细信息
detail,
// 工具提示信息
tooltip: detail,
// 标签,包含一个图标(如果 iconInLabel 为真)和标签或名称
label: `${iconInLabel ? '$(plug) ' : ''}${label || name} `,
// 图标路径,使用 VS Code 的主题图标“plug”
iconPath: new vscode.ThemeIcon('plug'),
// 可折叠状态,设置为展开
collapsibleState: vscode.TreeItemCollapsibleState.Expanded,
// 上下文值,用于标识该项是一个连接
contextValue: 'connection',
}; };
} else if ('onDidChangeFile' in item) { // SSHFileSystem } else if ('onDidChangeFile' in item) { // SSHFileSystem
// 从 item.config 中提取 label、name 和 group 属性
const { label, name, group } = item.config; const { label, name, group } = item.config;
// 根据 group 是否存在来生成描述信息
const description = group ? `${group}.${name} ` : (label && name); const description = group ? `${group}.${name} ` : (label && name);
// 返回一个格式化的项对象
return { return {
item, description, contextValue: 'filesystem', // 原始项
label: `${iconInLabel ? '$(root-folder) ' : ''}ssh://${item.authority}/`, item,
iconPath: asAbsolutePath?.('resources/icon.svg'), // 描述信息
description,
// 上下文值,用于标识该项是一个文件系统
contextValue: 'filesystem',
// 标签,包含一个图标(如果 iconInLabel 为真)和标签或名称
label: `${iconInLabel ? '$(root-folder) ' : ''}ssh://${item.authority}/`,
// 图标路径,使用 VS Code 的主题图标“root-folder”
iconPath: asAbsolutePath?.('resources/icon.svg'),
} }
} }
// FileSystemConfig // FileSystemConfig
const { label, name, group, putty } = item; const { label, name, group, putty } = item;
// 根据 group 是否存在来生成描述信息
const description = group ? `${group}.${name} ` : (label && name); const description = group ? `${group}.${name} ` : (label && name);
// 根据 putty 的值生成详细信息
const detail = putty === true ? 'PuTTY session (decuded from config)' : const detail = putty === true ? 'PuTTY session (decuded from config)' :
(typeof putty === 'string' ? `PuTTY session '${putty}'` : formatAddress(item)); (typeof putty === 'string' ? `PuTTY session '${putty}'` : formatAddress(item));
// 返回一个格式化的项对象
return { return {
item: item, description, detail, tooltip: detail, contextValue: 'config', // 原始项
label: `${iconInLabel ? '$(settings-gear) ' : ''}${item.label || item.name} `, item: item,
iconPath: new vscode.ThemeIcon('settings-gear'), // 描述信息
description,
// 详细信息
detail,
// 工具提示信息
tooltip: detail,
// 上下文值,用于标识该项是一个配置
contextValue: 'config',
// 标签,包含一个图标(如果 iconInLabel 为真)和标签或名称
label: `${iconInLabel ? '$(settings-gear) ' : ''}${item.label || item.name} `,
// 图标路径,使用 VS Code 的主题图标“settings-gear”
iconPath: new vscode.ThemeIcon('settings-gear'),
} }
} }
// 定义了一个扩展了 vscode.QuickPickItem 的类型,增加了一个名为 item 的属性。
type QuickPickItemWithItem = vscode.QuickPickItem & { item: any }; type QuickPickItemWithItem = vscode.QuickPickItem & { item: any };
/**
*
*
*/
export interface PickComplexOptions { export interface PickComplexOptions {
/** If true and there is only one or none item is available, immediately resolve with it/undefined */ /** If true and there is only one or none item is available, immediately resolve with it/undefined */
/**
* /
* @type {boolean}
*/
immediateReturn?: boolean; immediateReturn?: boolean;
/** If true, add all connections. If this is a string, filter by config name first */ /** If true, add all connections. If this is a string, filter by config name first */
/**
*
* @type {boolean | string}
*/
promptConnections?: boolean | string; promptConnections?: boolean | string;
/** If true, add an option to enter a connection string */ /** If true, add an option to enter a connection string */
/**
*
* @type {boolean}
*/
promptInstantConnection?: boolean; promptInstantConnection?: boolean;
/** If true, add all configurations. If this is a string, filter by config name first */ /** If true, add all configurations. If this is a string, filter by config name first */
/**
*
* @type {boolean | string}
*/
promptConfigs?: boolean | string; promptConfigs?: boolean | string;
/** If true, add all terminals. If this is a string, filter by config name first */ /** If true, add all terminals. If this is a string, filter by config name first */
/**
*
* @type {boolean | string}
*/
promptTerminals?: boolean | string; promptTerminals?: boolean | string;
/** If set, filter the connections/configs by (config) name first */ /** If set, filter the connections/configs by (config) name first */
/**
* /
* @type {string}
*/
nameFilter?: string; nameFilter?: string;
} }

Loading…
Cancel
Save