Skip to content

Commit 190a059

Browse files
bors[bot]vsrs
andauthored
Merge #4640
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 <[email protected]> Co-authored-by: vsrs <[email protected]>
2 parents fc29d0e + 6d0f1e2 commit 190a059

File tree

4 files changed

+86
-1
lines changed

4 files changed

+86
-1
lines changed

docs/user/readme.adoc

+10
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,16 @@ The server binary is stored in:
6565

6666
Note that we only support two most recent versions of VS Code.
6767

68+
==== Special `when` clause context for keybindings.
69+
You may use `inRustProject` context to configure keybindings for rust projects only. For example:
70+
[source,json]
71+
----
72+
{ "key": "ctrl+shift+f5", "command": "workbench.action.debug.restart", "when": "inDebugMode && !inRustProject"},
73+
{ "key": "ctrl+shift+f5", "command": "rust-analyzer.debug", "when": "inRustProject"},
74+
{ "key": "ctrl+i", "command": "rust-analyzer.toggleInlayHints", "when": "inRustProject" }
75+
----
76+
More about `when` clause contexts https://code.visualstudio.com/docs/getstarted/keybindings#_when-clause-contexts[here].
77+
6878
==== Updates
6979

7080
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.

editors/code/package.json

+65-1
Original file line numberDiff line numberDiff line change
@@ -694,6 +694,70 @@
694694
]
695695
}
696696
}
697-
]
697+
],
698+
"menus": {
699+
"commandPalette": [
700+
{
701+
"command": "rust-analyzer.syntaxTree",
702+
"when": "inRustProject"
703+
},
704+
{
705+
"command": "rust-analyzer.expandMacro",
706+
"when": "inRustProject"
707+
},
708+
{
709+
"command": "rust-analyzer.matchingBrace",
710+
"when": "inRustProject"
711+
},
712+
{
713+
"command": "rust-analyzer.parentModule",
714+
"when": "inRustProject"
715+
},
716+
{
717+
"command": "rust-analyzer.joinLines",
718+
"when": "inRustProject"
719+
},
720+
{
721+
"command": "rust-analyzer.run",
722+
"when": "inRustProject"
723+
},
724+
{
725+
"command": "rust-analyzer.debug",
726+
"when": "inRustProject"
727+
},
728+
{
729+
"command": "rust-analyzer.newDebugConfig",
730+
"when": "inRustProject"
731+
},
732+
{
733+
"command": "rust-analyzer.analyzerStatus",
734+
"when": "inRustProject"
735+
},
736+
{
737+
"command": "rust-analyzer.collectGarbage",
738+
"when": "inRustProject"
739+
},
740+
{
741+
"command": "rust-analyzer.reload",
742+
"when": "inRustProject"
743+
},
744+
{
745+
"command": "rust-analyzer.onEnter",
746+
"when": "inRustProject"
747+
},
748+
{
749+
"command": "rust-analyzer.ssr",
750+
"when": "inRustProject"
751+
},
752+
{
753+
"command": "rust-analyzer.serverVersion",
754+
"when": "inRustProject"
755+
},
756+
{
757+
"command": "rust-analyzer.toggleInlayHints",
758+
"when": "inRustProject"
759+
}
760+
]
761+
}
698762
}
699763
}

editors/code/src/main.ts

+6
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,13 @@ import { log, assert, isValidExecutable } from './util';
1212
import { PersistentState } from './persistent_state';
1313
import { fetchRelease, download } from './net';
1414
import { activateTaskProvider } from './tasks';
15+
import { setContextValue } from './util';
1516
import { exec } from 'child_process';
1617

1718
let ctx: Ctx | undefined;
1819

20+
const RUST_PROJECT_CONTEXT_NAME = "inRustProject";
21+
1922
export async function activate(context: vscode.ExtensionContext) {
2023
// Register a "dumb" onEnter command for the case where server fails to
2124
// start.
@@ -54,6 +57,8 @@ export async function activate(context: vscode.ExtensionContext) {
5457
// This a horribly, horribly wrong way to deal with this problem.
5558
ctx = await Ctx.create(config, context, serverPath, workspaceFolder.uri.fsPath);
5659

60+
setContextValue(RUST_PROJECT_CONTEXT_NAME, true);
61+
5762
// Commands which invokes manually via command palette, shortcut, etc.
5863

5964
// 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) {
109114
}
110115

111116
export async function deactivate() {
117+
setContextValue(RUST_PROJECT_CONTEXT_NAME, undefined);
112118
await ctx?.client.stop();
113119
ctx = undefined;
114120
}

editors/code/src/util.ts

+5
Original file line numberDiff line numberDiff line change
@@ -94,3 +94,8 @@ export function isValidExecutable(path: string): boolean {
9494

9595
return res.status === 0;
9696
}
97+
98+
/** Sets ['when'](https://code.visualstudio.com/docs/getstarted/keybindings#_when-clause-contexts) clause contexts */
99+
export function setContextValue(key: string, value: any): Thenable<void> {
100+
return vscode.commands.executeCommand('setContext', key, value);
101+
}

0 commit comments

Comments
 (0)