Add when clause contexts

pull/285/head
Kelvin Schoofs 3 years ago
parent 36a440df3b
commit b311fec87f

@ -8,7 +8,7 @@ import { Logging, setDebug } from './logging';
import { Manager } from './manager';
import type { SSHPseudoTerminal } from './pseudoTerminal';
import { ConfigTreeProvider, ConnectionTreeProvider } from './treeViewManager';
import { pickComplex, PickComplexOptions, pickConnection, setAsAbsolutePath } from './ui-utils';
import { pickComplex, PickComplexOptions, pickConnection, setAsAbsolutePath, setupWhenClauseContexts } from './ui-utils';
function getVersion(): string | undefined {
const ext = vscode.extensions.getExtension('Kelvin.vscode-sshfs');
@ -59,6 +59,8 @@ export function activate(context: vscode.ExtensionContext) {
subscribe(vscode.tasks.registerTaskProvider('ssh-shell', manager));
subscribe(vscode.window.registerTerminalLinkProvider(manager));
setupWhenClauseContexts(manager.connectionManager);
function registerCommandHandler(name: string, handler: CommandHandler) {
const callback = async (arg?: string | FileSystemConfig | Connection | SSHPseudoTerminal | vscode.Uri) => {
if (handler.promptOptions && (!arg || typeof arg === 'string')) {

@ -1,7 +1,7 @@
import * as vscode from 'vscode';
import { getConfigs } from './config';
import type { Connection } from './connection';
import type { Connection, ConnectionManager } from './connection';
import { parseConnectionString, FileSystemConfig } from './fileSystemConfig';
import type { Manager } from './manager';
import type { SSHPseudoTerminal } from './pseudoTerminal';
@ -18,6 +18,25 @@ export function formatAddress(config: FileSystemConfig): string {
return `${username ? `${username}@` : ''}${host}${port ? `:${port}` : ''}`;
}
export function setWhenClauseContext(key: string, value: any) {
return vscode.commands.executeCommand('setContext', `sshfs.${key}`, value);
}
export function setupWhenClauseContexts(connectionManager: ConnectionManager): Promise<void> {
async function refresh() {
const active = connectionManager.getActiveConnections();
const pending = connectionManager.getPendingConnections();
await setWhenClauseContext('openConnections', active.length + pending.length);
await setWhenClauseContext('openTerminals', active.reduce((tot, con) => tot + con.terminals.length, 0));
await setWhenClauseContext('openFileSystems', active.reduce((tot, con) => tot + con.filesystems.length, 0));
}
connectionManager.onConnectionAdded(refresh);
connectionManager.onConnectionRemoved(refresh);
connectionManager.onConnectionUpdated(refresh);
connectionManager.onPendingChanged(refresh);
return refresh();
}
export let asAbsolutePath: vscode.ExtensionContext['asAbsolutePath'] | undefined;
export const setAsAbsolutePath = (value: typeof asAbsolutePath) => asAbsolutePath = value;

Loading…
Cancel
Save