4640: Add `inRustProject` when-clause for commands in vscode r=vsrs a=vsrs

At the moment all rust-analyzer commands always visible in the command palette, even if there is no rust project opened.

This PR adds special [when-clause](https://code.visualstudio.com/docs/getstarted/keybindings#_when-clause-contexts) context. This context also might be used in key bindings.


Co-authored-by: vsrs <vit@conrlab.com>
Co-authored-by: vsrs <62505555+vsrs@users.noreply.github.com>
This commit is contained in:
bors[bot] 2020-05-28 13:37:37 +00:00 committed by GitHub
commit 190a0595a4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 86 additions and 1 deletions

View file

@ -65,6 +65,16 @@ The server binary is stored in:
Note that we only support two most recent versions of VS Code. Note that we only support two most recent versions of VS Code.
==== Special `when` clause context for keybindings.
You may use `inRustProject` context to configure keybindings for rust projects only. For example:
[source,json]
----
{ "key": "ctrl+shift+f5", "command": "workbench.action.debug.restart", "when": "inDebugMode && !inRustProject"},
{ "key": "ctrl+shift+f5", "command": "rust-analyzer.debug", "when": "inRustProject"},
{ "key": "ctrl+i", "command": "rust-analyzer.toggleInlayHints", "when": "inRustProject" }
----
More about `when` clause contexts https://code.visualstudio.com/docs/getstarted/keybindings#_when-clause-contexts[here].
==== Updates ==== Updates
The extension will be updated automatically as new versions become available. It will ask your permission to download the matching language server version binary if needed. The extension will be updated automatically as new versions become available. It will ask your permission to download the matching language server version binary if needed.

View file

@ -694,6 +694,70 @@
] ]
} }
} }
] ],
"menus": {
"commandPalette": [
{
"command": "rust-analyzer.syntaxTree",
"when": "inRustProject"
},
{
"command": "rust-analyzer.expandMacro",
"when": "inRustProject"
},
{
"command": "rust-analyzer.matchingBrace",
"when": "inRustProject"
},
{
"command": "rust-analyzer.parentModule",
"when": "inRustProject"
},
{
"command": "rust-analyzer.joinLines",
"when": "inRustProject"
},
{
"command": "rust-analyzer.run",
"when": "inRustProject"
},
{
"command": "rust-analyzer.debug",
"when": "inRustProject"
},
{
"command": "rust-analyzer.newDebugConfig",
"when": "inRustProject"
},
{
"command": "rust-analyzer.analyzerStatus",
"when": "inRustProject"
},
{
"command": "rust-analyzer.collectGarbage",
"when": "inRustProject"
},
{
"command": "rust-analyzer.reload",
"when": "inRustProject"
},
{
"command": "rust-analyzer.onEnter",
"when": "inRustProject"
},
{
"command": "rust-analyzer.ssr",
"when": "inRustProject"
},
{
"command": "rust-analyzer.serverVersion",
"when": "inRustProject"
},
{
"command": "rust-analyzer.toggleInlayHints",
"when": "inRustProject"
}
]
}
} }
} }

View file

@ -12,10 +12,13 @@ import { log, assert, isValidExecutable } from './util';
import { PersistentState } from './persistent_state'; import { PersistentState } from './persistent_state';
import { fetchRelease, download } from './net'; import { fetchRelease, download } from './net';
import { activateTaskProvider } from './tasks'; import { activateTaskProvider } from './tasks';
import { setContextValue } from './util';
import { exec } from 'child_process'; import { exec } from 'child_process';
let ctx: Ctx | undefined; let ctx: Ctx | undefined;
const RUST_PROJECT_CONTEXT_NAME = "inRustProject";
export async function activate(context: vscode.ExtensionContext) { export async function activate(context: vscode.ExtensionContext) {
// Register a "dumb" onEnter command for the case where server fails to // Register a "dumb" onEnter command for the case where server fails to
// start. // start.
@ -54,6 +57,8 @@ export async function activate(context: vscode.ExtensionContext) {
// This a horribly, horribly wrong way to deal with this problem. // This a horribly, horribly wrong way to deal with this problem.
ctx = await Ctx.create(config, context, serverPath, workspaceFolder.uri.fsPath); ctx = await Ctx.create(config, context, serverPath, workspaceFolder.uri.fsPath);
setContextValue(RUST_PROJECT_CONTEXT_NAME, true);
// Commands which invokes manually via command palette, shortcut, etc. // Commands which invokes manually via command palette, shortcut, etc.
// Reloading is inspired by @DanTup maneuver: https://github.com/microsoft/vscode/issues/45774#issuecomment-373423895 // Reloading is inspired by @DanTup maneuver: https://github.com/microsoft/vscode/issues/45774#issuecomment-373423895
@ -109,6 +114,7 @@ export async function activate(context: vscode.ExtensionContext) {
} }
export async function deactivate() { export async function deactivate() {
setContextValue(RUST_PROJECT_CONTEXT_NAME, undefined);
await ctx?.client.stop(); await ctx?.client.stop();
ctx = undefined; ctx = undefined;
} }

View file

@ -94,3 +94,8 @@ export function isValidExecutable(path: string): boolean {
return res.status === 0; return res.status === 0;
} }
/** Sets ['when'](https://code.visualstudio.com/docs/getstarted/keybindings#_when-clause-contexts) clause contexts */
export function setContextValue(key: string, value: any): Thenable<void> {
return vscode.commands.executeCommand('setContext', key, value);
}