parent
56b10b9c15
commit
01cad2159a
After Width: | Height: | Size: 811 B |
@ -0,0 +1,84 @@
|
||||
|
||||
import * as vscode from 'vscode';
|
||||
import { getConfigs, UPDATE_LISTENERS } from './config';
|
||||
import type { Connection, ConnectionManager } from './connection';
|
||||
import { FileSystemConfig, getGroups } from './fileSystemConfig';
|
||||
import type { SSHPseudoTerminal } from './pseudoTerminal';
|
||||
import type { SSHFileSystem } from './sshFileSystem';
|
||||
import { formatItem } from './ui-utils';
|
||||
|
||||
type PendingConnection = [string, FileSystemConfig | undefined];
|
||||
type TreeData = Connection | PendingConnection | SSHFileSystem | SSHPseudoTerminal;
|
||||
export class ConnectionTreeProvider implements vscode.TreeDataProvider<TreeData> {
|
||||
protected onDidChangeTreeDataEmitter = new vscode.EventEmitter<TreeData | void>();
|
||||
public onDidChangeTreeData: vscode.Event<TreeData | void> = this.onDidChangeTreeDataEmitter.event;
|
||||
constructor(protected readonly manager: ConnectionManager) {
|
||||
manager.onConnectionAdded(() => this.onDidChangeTreeDataEmitter.fire());
|
||||
manager.onConnectionRemoved(() => this.onDidChangeTreeDataEmitter.fire());
|
||||
manager.onConnectionUpdated(con => this.onDidChangeTreeDataEmitter.fire(con));
|
||||
}
|
||||
public refresh() {
|
||||
this.onDidChangeTreeDataEmitter.fire();
|
||||
}
|
||||
public getTreeItem(element: TreeData): vscode.TreeItem | Thenable<vscode.TreeItem> {
|
||||
if ('onDidChangeFile' in element || 'handleInput' in element) { // SSHFileSystem | SSHPseudoTerminal
|
||||
return { ...formatItem(element), collapsibleState: vscode.TreeItemCollapsibleState.None }
|
||||
} else if (Array.isArray(element)) { // PendingConnection
|
||||
const [name, config] = element;
|
||||
if (!config) return { label: name, description: 'Connecting...' };
|
||||
return {
|
||||
...formatItem(config),
|
||||
contextValue: 'pendingconnection',
|
||||
collapsibleState: vscode.TreeItemCollapsibleState.Collapsed,
|
||||
// Doesn't seem to actually spin, but still gets rendered properly otherwise
|
||||
iconPath: new vscode.ThemeIcon('loading~spin'),
|
||||
};
|
||||
}
|
||||
// Connection
|
||||
return { ...formatItem(element), collapsibleState: vscode.TreeItemCollapsibleState.Collapsed };
|
||||
}
|
||||
public getChildren(element?: TreeData): vscode.ProviderResult<TreeData[]> {
|
||||
if (!element) return [
|
||||
...this.manager.getActiveConnections(),
|
||||
...this.manager.getPendingConnections(),
|
||||
];
|
||||
if ('onDidChangeFile' in element) return []; // SSHFileSystem
|
||||
if ('handleInput' in element) return []; // SSHPseudoTerminal
|
||||
if (Array.isArray(element)) return []; // PendingConnection
|
||||
return [...element.terminals, ...element.filesystems]; // Connection
|
||||
}
|
||||
}
|
||||
|
||||
export class ConfigTreeProvider implements vscode.TreeDataProvider<FileSystemConfig | string> {
|
||||
protected onDidChangeTreeDataEmitter = new vscode.EventEmitter<FileSystemConfig | string | void>();
|
||||
public onDidChangeTreeData: vscode.Event<FileSystemConfig | string | void> = this.onDidChangeTreeDataEmitter.event;
|
||||
constructor() {
|
||||
// Would be very difficult (and a bit useless) to pinpoint the exact
|
||||
// group/config that changes, so let's just update the whole tree
|
||||
UPDATE_LISTENERS.push(() => this.onDidChangeTreeDataEmitter.fire());
|
||||
// ^ Technically a memory leak, but there should only be one ConfigTreeProvider that never gets discarded
|
||||
}
|
||||
public getTreeItem(element: FileSystemConfig | string): vscode.TreeItem | Thenable<vscode.TreeItem> {
|
||||
if (typeof element === 'string') {
|
||||
return {
|
||||
label: element.replace(/^.+\./, ''), contextValue: 'group',
|
||||
collapsibleState: vscode.TreeItemCollapsibleState.Collapsed,
|
||||
iconPath: vscode.ThemeIcon.Folder,
|
||||
};
|
||||
}
|
||||
return { ...formatItem(element), collapsibleState: vscode.TreeItemCollapsibleState.None };
|
||||
}
|
||||
public getChildren(element: FileSystemConfig | string = ''): vscode.ProviderResult<(FileSystemConfig | string)[]> {
|
||||
if (typeof element !== 'string') return []; // Configs don't have children
|
||||
const configs = getConfigs();
|
||||
const matching = configs.filter(({ group }) => (group || '') === element);
|
||||
matching.sort((a, b) => a.name > b.name ? 1 : -1);
|
||||
let groups = getGroups(configs, true);
|
||||
if (element) {
|
||||
groups = groups.filter(g => g.startsWith(element) && g[element.length] === '.' && !g.includes('.', element.length + 1));
|
||||
} else {
|
||||
groups = groups.filter(g => !g.includes('.'));
|
||||
}
|
||||
return [...matching, ...groups.sort()];
|
||||
}
|
||||
}
|
@ -0,0 +1,126 @@
|
||||
|
||||
import * as vscode from 'vscode';
|
||||
import { getConfigs } from './config';
|
||||
import type { Connection } from './connection';
|
||||
import type { FileSystemConfig } from './fileSystemConfig';
|
||||
import type { Manager } from './manager';
|
||||
import type { SSHPseudoTerminal } from './pseudoTerminal';
|
||||
import type { SSHFileSystem } from './sshFileSystem';
|
||||
|
||||
export interface FormattedItem extends vscode.QuickPickItem, vscode.TreeItem {
|
||||
item: any;
|
||||
label: string;
|
||||
description?: string;
|
||||
}
|
||||
|
||||
export function formatAddress(config: FileSystemConfig): string {
|
||||
const { username, host, port } = config;
|
||||
return `${username ? `${username}@` : ''}${host}${port ? `:${port}` : ''}`;
|
||||
}
|
||||
|
||||
export let asAbsolutePath: vscode.ExtensionContext['asAbsolutePath'] | undefined;
|
||||
export const setAsAbsolutePath = (value: typeof asAbsolutePath) => asAbsolutePath = value;
|
||||
|
||||
/** 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 {
|
||||
if ('handleInput' in item) { // SSHPseudoTerminal
|
||||
return {
|
||||
item, contextValue: 'terminal',
|
||||
label: `${iconInLabel ? '$(terminal) ' : ''}${item.terminal?.name || 'Unnamed terminal'}`,
|
||||
iconPath: new vscode.ThemeIcon('terminal'),
|
||||
command: {
|
||||
title: 'Focus',
|
||||
command: 'sshfs.focusTerminal',
|
||||
arguments: [item],
|
||||
},
|
||||
};
|
||||
} else if ('client' in item) { // Connection
|
||||
const { label, name, group } = item.config;
|
||||
const description = group ? `${group}.${name} ` : (label && name);
|
||||
const detail = formatAddress(item.actualConfig);
|
||||
return {
|
||||
item, description, detail, tooltip: detail,
|
||||
label: `${iconInLabel ? '$(plug) ' : ''}${label || name} `,
|
||||
iconPath: new vscode.ThemeIcon('plug'),
|
||||
contextValue: 'connection',
|
||||
};
|
||||
} else if ('onDidChangeFile' in item) { // SSHFileSystem
|
||||
return {
|
||||
item, description: item.root, contextValue: 'filesystem',
|
||||
label: `${iconInLabel ? '$(root-folder) ' : ''}ssh://${item.authority}/`,
|
||||
iconPath: asAbsolutePath?.('resources/icon.svg'),
|
||||
}
|
||||
}
|
||||
// FileSystemConfig
|
||||
const { label, name, group, putty } = item;
|
||||
const description = group ? `${group}.${name} ` : (label && name);
|
||||
const detail = putty === true ? 'PuTTY session (decuded from config)' :
|
||||
(typeof putty === 'string' ? `PuTTY session '${putty}'` : formatAddress(item));
|
||||
return {
|
||||
item: item, description, detail, tooltip: detail, contextValue: 'config',
|
||||
label: `${iconInLabel ? '$(settings-gear) ' : ''}${item.label || item.name} `,
|
||||
iconPath: new vscode.ThemeIcon('settings-gear'),
|
||||
}
|
||||
}
|
||||
|
||||
type QuickPickItemWithValue = vscode.QuickPickItem & { value?: any };
|
||||
|
||||
export interface PickComplexOptions {
|
||||
/** If true and there is only one or none item is available, immediately resolve with it/undefined */
|
||||
immediateReturn?: boolean;
|
||||
/** If true, add all connections. If this is a string, filter by config name first */
|
||||
promptConnections?: boolean | string;
|
||||
/** If true, add all configurations. If this is a string, filter by config name first */
|
||||
promptConfigs?: boolean | string;
|
||||
/** If true, add all terminals. If this is a string, filter by config name first */
|
||||
promptTerminals?: boolean | string;
|
||||
/** If set, filter the connections/configs by (config) name first */
|
||||
nameFilter?: string;
|
||||
}
|
||||
|
||||
export async function pickComplex(manager: Manager, options: PickComplexOptions):
|
||||
Promise<FileSystemConfig | Connection | SSHPseudoTerminal | undefined> {
|
||||
return new Promise((resolve) => {
|
||||
const { promptConnections, promptConfigs, promptTerminals, immediateReturn, nameFilter } = options;
|
||||
const items: QuickPickItemWithValue[] = [];
|
||||
const toSelect: string[] = [];
|
||||
if (promptConnections) {
|
||||
let cons = manager.connectionManager.getActiveConnections();
|
||||
if (typeof promptConnections === 'string') cons = cons.filter(con => con.actualConfig.name === promptConnections);
|
||||
if (nameFilter) cons = cons.filter(con => con.actualConfig.name === nameFilter);
|
||||
items.push(...cons.map(con => formatItem(con, true)));
|
||||
toSelect.push('connection');
|
||||
}
|
||||
if (promptConfigs) {
|
||||
let configs = getConfigs();
|
||||
if (typeof promptConfigs === 'string') configs = configs.filter(config => config.name === promptConfigs);
|
||||
if (nameFilter) configs = configs.filter(config => config.name === nameFilter);
|
||||
items.push(...configs.map(config => formatItem(config, true)));
|
||||
toSelect.push('configuration');
|
||||
}
|
||||
if (promptTerminals) {
|
||||
let cons = manager.connectionManager.getActiveConnections();
|
||||
if (typeof promptConnections === 'string') cons = cons.filter(con => con.actualConfig.name === promptConnections);
|
||||
if (nameFilter) cons = cons.filter(con => con.actualConfig.name === nameFilter);
|
||||
const terminals = cons.reduce((all, con) => [...all, ...con.terminals], []);
|
||||
items.push(...terminals.map(config => formatItem(config, true)));
|
||||
toSelect.push('terminal');
|
||||
}
|
||||
if (immediateReturn && items.length <= 1) return resolve(items[0]?.value);
|
||||
const quickPick = vscode.window.createQuickPick<QuickPickItemWithValue>();
|
||||
quickPick.items = items;
|
||||
quickPick.title = 'Select ' + toSelect.join(' / ');
|
||||
quickPick.onDidAccept(() => {
|
||||
const value = quickPick.activeItems[0]?.value;
|
||||
if (!value) return;
|
||||
quickPick.hide();
|
||||
resolve(value);
|
||||
});
|
||||
quickPick.onDidHide(() => resolve());
|
||||
quickPick.show();
|
||||
});
|
||||
}
|
||||
|
||||
export const pickConfig = (manager: Manager) => pickComplex(manager, { promptConfigs: true }) as Promise<FileSystemConfig | undefined>;
|
||||
export const pickConnection = (manager: Manager, name?: string) => pickComplex(manager, { promptConnections: name || true }) as Promise<Connection | undefined>;
|
||||
export const pickTerminal = (manager: Manager) => pickComplex(manager, { promptTerminals: true }) as Promise<SSHPseudoTerminal | undefined>;
|
Loading…
Reference in new issue