Skip to content

Commit 3a8e920

Browse files
authored
Show SSH Targets View Conditionally (#9856)
* Show SSH Targets View conditionally
1 parent ba0cc65 commit 3a8e920

File tree

6 files changed

+84
-23
lines changed

6 files changed

+84
-23
lines changed

Extension/package.json

+12-1
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,7 @@
252252
{
253253
"id": "CppSshTargetsView",
254254
"name": "%c_cpp.contributes.views.sshTargetsView.title%",
255-
"when": "showCppSshTargetsView"
255+
"when": "enableCppSshTargetsView"
256256
}
257257
]
258258
},
@@ -2761,6 +2761,17 @@
27612761
"default": false,
27622762
"markdownDescription": "%c_cpp.configuration.legacyCompilerArgsBehavior.markdownDescription%",
27632763
"scope": "resource"
2764+
},
2765+
"C_Cpp.sshTargetsView": {
2766+
"type": "string",
2767+
"enum": [
2768+
"enabled",
2769+
"disabled",
2770+
"default"
2771+
],
2772+
"default": "default",
2773+
"description": "%c_cpp.configuration.sshTargetsView.description%",
2774+
"scope": "window"
27642775
}
27652776
}
27662777
},

Extension/package.nls.json

+1
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,7 @@
239239
"c_cpp.configuration.debugger.useBacktickCommandSubstitution.markdownDescription": { "message": "If `true`, debugger shell command substitution will use obsolete backtick ``(`)``.", "comment": [ "Markdown text between `` should not be translated or localized (they represent literal text) and the capitalization, spacing, and punctuation (including the ``) should not be altered." ] },
240240
"c_cpp.configuration.legacyCompilerArgsBehavior.markdownDescription": "Enable pre-v1.10.0 behavior for how shell escaping is handled in compiler arg settings. Shell escaping is no longer expected or supported by default in arg arrays starting in v1.10.0.",
241241
"c_cpp.configuration.legacyCompilerArgsBehavior.deprecationMessage": "This setting is temporary to support transitioning to corrected behavior in v1.10.0.",
242+
"c_cpp.configuration.sshTargetsView.description": "Controls whether the SSH Targets view is visible. By default, enable the view when an SSH command is invoked.",
242243
"c_cpp.contributes.views.cppReferencesView.title": "C/C++: Other references results",
243244
"c_cpp.contributes.views.sshTargetsView.title": { "message": "Cpptools: SSH targets", "comment": [ "Do not localize `Cpptools`." ] },
244245
"c_cpp.contributes.viewsWelcome.contents": { "message": "To learn more about launch.json, see [Configuring C/C++ debugging](https://code.visualstudio.com/docs/cpp/launch-json-reference).", "comment": [ "Markdown text between () should not be altered: https://en.wikipedia.org/wiki/Markdown" ] },

Extension/src/Debugger/extension.ts

+62-17
Original file line numberDiff line numberDiff line change
@@ -12,20 +12,23 @@ import { CppdbgDebugAdapterDescriptorFactory, CppvsdbgDebugAdapterDescriptorFact
1212
import { DebuggerType } from './configurations';
1313
import * as nls from 'vscode-nls';
1414
import { getActiveSshTarget, initializeSshTargets, selectSshTarget, SshTargetsProvider } from '../SSH/TargetsView/sshTargetsProvider';
15-
import { addSshTarget, BaseNode, refreshCppSshTargetsView } from '../SSH/TargetsView/common';
15+
import { addSshTargetCmd, BaseNode, refreshCppSshTargetsViewCmd } from '../SSH/TargetsView/common';
1616
import { setActiveSshTarget, TargetLeafNode } from '../SSH/TargetsView/targetNodes';
1717
import { sshCommandToConfig } from '../SSH/sshCommandToConfig';
1818
import { getSshConfiguration, getSshConfigurationFiles, writeSshConfiguration } from '../SSH/sshHosts';
1919
import { pathAccessible } from '../common';
2020
import * as fs from 'fs';
2121
import { Configuration } from 'ssh-config';
22+
import { CppSettings } from '../LanguageServer/settings';
2223
import * as chokidar from 'chokidar';
2324

2425
// The extension deactivate method is asynchronous, so we handle the disposables ourselves instead of using extensionContext.subscriptions.
2526
const disposables: vscode.Disposable[] = [];
2627
const localize: nls.LocalizeFunc = nls.loadMessageBundle();
2728

28-
const fileWatchers: chokidar.FSWatcher[] = [];
29+
let sshTargetsViewEnabled: boolean = false;
30+
let sshTargetsViewSetting: string | undefined;
31+
let sshConfigWatcher: chokidar.FSWatcher | undefined;
2932

3033
export async function initialize(context: vscode.ExtensionContext): Promise<void> {
3134
// Activate Process Picker Commands
@@ -79,38 +82,80 @@ export async function initialize(context: vscode.ExtensionContext): Promise<void
7982
await initializeSshTargets();
8083
const sshTargetsProvider: SshTargetsProvider = new SshTargetsProvider();
8184
disposables.push(vscode.window.registerTreeDataProvider('CppSshTargetsView', sshTargetsProvider));
82-
disposables.push(vscode.commands.registerCommand(addSshTarget, addSshTargetImpl));
83-
disposables.push(vscode.commands.registerCommand('C_Cpp.removeSshTarget', removeSshTargetImpl));
84-
disposables.push(vscode.commands.registerCommand(refreshCppSshTargetsView, (node?: BaseNode) => sshTargetsProvider.refresh(node)));
85+
disposables.push(vscode.commands.registerCommand(addSshTargetCmd, () => enableSshTargetsViewAndRun(addSshTargetImpl)));
86+
disposables.push(vscode.commands.registerCommand('C_Cpp.removeSshTarget', (node?: BaseNode) => enableSshTargetsViewAndRun(removeSshTargetImpl, node)));
87+
disposables.push(vscode.commands.registerCommand(refreshCppSshTargetsViewCmd, (node?: BaseNode) => enableSshTargetsViewAndRun((node?: BaseNode) => sshTargetsProvider.refresh(node), node)));
8588
disposables.push(vscode.commands.registerCommand('C_Cpp.setActiveSshTarget', async (node: TargetLeafNode) => {
89+
await enableSshTargetsView();
8690
await setActiveSshTarget(node.name);
87-
await vscode.commands.executeCommand(refreshCppSshTargetsView);
91+
await vscode.commands.executeCommand(refreshCppSshTargetsViewCmd);
8892
}));
89-
disposables.push(vscode.commands.registerCommand('C_Cpp.selectSshTarget', selectSshTarget));
93+
disposables.push(vscode.commands.registerCommand('C_Cpp.selectSshTarget', () => enableSshTargetsViewAndRun(selectSshTarget)));
9094
disposables.push(vscode.commands.registerCommand('C_Cpp.selectActiveSshTarget', async () => {
95+
await enableSshTargetsView();
9196
const name: string | undefined = await selectSshTarget();
9297
if (name) {
9398
await setActiveSshTarget(name);
94-
await vscode.commands.executeCommand(refreshCppSshTargetsView);
99+
await vscode.commands.executeCommand(refreshCppSshTargetsViewCmd);
95100
}
96101
}));
97-
disposables.push(vscode.commands.registerCommand('C_Cpp.activeSshTarget', getActiveSshTarget));
102+
disposables.push(vscode.commands.registerCommand('C_Cpp.activeSshTarget', () => enableSshTargetsViewAndRun(getActiveSshTarget)));
98103
disposables.push(sshTargetsProvider);
99-
for (const sshConfig of getSshConfigurationFiles()) {
100-
fileWatchers.push(chokidar.watch(sshConfig, {ignoreInitial: true})
101-
.on('add', () => vscode.commands.executeCommand(refreshCppSshTargetsView))
102-
.on('change', () => vscode.commands.executeCommand(refreshCppSshTargetsView))
103-
.on('unlink', () => vscode.commands.executeCommand(refreshCppSshTargetsView)));
104+
105+
// Decide if we should show the SSH Targets View.
106+
sshTargetsViewSetting = (new CppSettings()).sshTargetsView;
107+
// Active SSH Target initialized in initializeSshTargets()
108+
if (sshTargetsViewSetting === 'enabled' || (sshTargetsViewSetting === 'default' && await getActiveSshTarget(false))) {
109+
// Don't wait
110+
enableSshTargetsView();
104111
}
105-
vscode.commands.executeCommand('setContext', 'showCppSshTargetsView', true);
112+
113+
disposables.push(vscode.workspace.onDidChangeConfiguration(async (e: vscode.ConfigurationChangeEvent) => {
114+
if (e.affectsConfiguration('C_Cpp.sshTargetsView')) {
115+
sshTargetsViewSetting = (new CppSettings()).sshTargetsView;
116+
if (sshTargetsViewSetting === 'enabled' || (sshTargetsViewSetting === 'default' && await getActiveSshTarget(false))) {
117+
await enableSshTargetsView();
118+
} else if (sshTargetsViewSetting === 'disabled') {
119+
await disableSshTargetsView();
120+
}
121+
}
122+
}));
106123
}
107124

108125
export function dispose(): void {
109-
// Don't wait
110-
fileWatchers.forEach(watcher => watcher.close().then(() => {}, () => {}));
126+
if (sshConfigWatcher) {
127+
sshConfigWatcher.close();
128+
sshConfigWatcher = undefined;
129+
}
111130
disposables.forEach(d => d.dispose());
112131
}
113132

133+
async function enableSshTargetsViewAndRun<T>(func: (...paras: any[]) => T | Promise<T>, ...args: any[]): Promise<T> {
134+
await enableSshTargetsView();
135+
return func(...args);
136+
}
137+
138+
async function enableSshTargetsView(): Promise<void> {
139+
if (sshTargetsViewEnabled || sshTargetsViewSetting === 'disabled') {
140+
return;
141+
}
142+
await vscode.commands.executeCommand('setContext', 'enableCppSshTargetsView', true);
143+
sshConfigWatcher = chokidar.watch(getSshConfigurationFiles(), { ignoreInitial: true })
144+
.on('add', () => vscode.commands.executeCommand(refreshCppSshTargetsViewCmd))
145+
.on('change', () => vscode.commands.executeCommand(refreshCppSshTargetsViewCmd))
146+
.on('unlink', () => vscode.commands.executeCommand(refreshCppSshTargetsViewCmd));
147+
sshTargetsViewEnabled = true;
148+
}
149+
150+
async function disableSshTargetsView(): Promise<void> {
151+
await vscode.commands.executeCommand('setContext', 'enableCppSshTargetsView', false);
152+
if (sshConfigWatcher) {
153+
sshConfigWatcher.close();
154+
sshConfigWatcher = undefined;
155+
}
156+
sshTargetsViewEnabled = false;
157+
}
158+
114159
async function addSshTargetImpl(): Promise<string> {
115160
const name: string | undefined = await vscode.window.showInputBox({
116161
title: localize('enter.ssh.target.name', 'Enter SSH Target Name'),

Extension/src/LanguageServer/settings.ts

+4
Original file line numberDiff line numberDiff line change
@@ -564,6 +564,10 @@ export class CppSettings extends Settings {
564564
&& vscode.workspace.getConfiguration("workbench").get<string>("colorTheme") !== "Default High Contrast";
565565
}
566566

567+
public get sshTargetsView(): string {
568+
return super.Section.get<string>("sshTargetsView") ?? 'default';
569+
}
570+
567571
public toggleSetting(name: string, value1: string, value2: string): void {
568572
const value: string | undefined = super.Section.get<string>(name);
569573
super.Section.update(name, value === value1 ? value2 : value1, getTarget());

Extension/src/SSH/TargetsView/common.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -36,5 +36,5 @@ export class LabelLeafNode implements BaseNode {
3636
}
3737
}
3838

39-
export const refreshCppSshTargetsView: string = 'C_Cpp.refreshCppSshTargetsView';
40-
export const addSshTarget: string = 'C_Cpp.addSshTarget';
39+
export const refreshCppSshTargetsViewCmd: string = 'C_Cpp.refreshCppSshTargetsView';
40+
export const addSshTargetCmd: string = 'C_Cpp.addSshTarget';

Extension/src/SSH/TargetsView/sshTargetsProvider.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
import { getSshConfigHostInfos } from '../sshHosts';
77
import * as vscode from 'vscode';
8-
import { addSshTarget, BaseNode, LabelLeafNode, refreshCppSshTargetsView } from './common';
8+
import { addSshTargetCmd, BaseNode, LabelLeafNode, refreshCppSshTargetsViewCmd } from './common';
99
import { TargetLeafNode, filesWritable, setActiveSshTarget, _activeTarget, workspaceState_activeSshTarget } from './targetNodes';
1010
import { extensionContext, ISshConfigHostInfo } from '../../common';
1111
import * as nls from 'vscode-nls';
@@ -97,7 +97,7 @@ export async function getActiveSshTarget(selectWhenNotSet: boolean = true): Prom
9797
throw Error(localize('active.ssh.target.selection.cancelled', 'Active SSH target selection cancelled.'));
9898
}
9999
await setActiveSshTarget(name);
100-
await vscode.commands.executeCommand(refreshCppSshTargetsView);
100+
await vscode.commands.executeCommand(refreshCppSshTargetsViewCmd);
101101
}
102102
return _activeTarget;
103103
}
@@ -113,7 +113,7 @@ export async function selectSshTarget(): Promise<string | undefined> {
113113
return undefined;
114114
}
115115
if (selection === addNewSshTarget) {
116-
return vscode.commands.executeCommand(addSshTarget);
116+
return vscode.commands.executeCommand(addSshTargetCmd);
117117
}
118118
return selection;
119119
}

0 commit comments

Comments
 (0)