From f3e04fbbabe2887542a9bb57fffcabc7cb50d406 Mon Sep 17 00:00:00 2001 From: vsrs Date: Wed, 27 May 2020 19:40:13 +0300 Subject: Add `inRustProject` when-clause for commands. --- editors/code/package.json | 66 ++++++++++++++++++++++++++++++++++++++++++++++- editors/code/src/main.ts | 6 +++++ editors/code/src/util.ts | 5 ++++ 3 files changed, 76 insertions(+), 1 deletion(-) diff --git a/editors/code/package.json b/editors/code/package.json index acf3ca4b5..75dbafc05 100644 --- a/editors/code/package.json +++ b/editors/code/package.json @@ -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" + } + ] + } } } diff --git a/editors/code/src/main.ts b/editors/code/src/main.ts index 31ac81ee8..b7337621c 100644 --- a/editors/code/src/main.ts +++ b/editors/code/src/main.ts @@ -12,10 +12,13 @@ import { log, assert, isValidExecutable } from './util'; import { PersistentState } from './persistent_state'; import { fetchRelease, download } from './net'; import { activateTaskProvider } from './tasks'; +import { setContextValue } from './util'; import { exec } from 'child_process'; let ctx: Ctx | undefined; +const RUST_PROJECT_CONTEXT_NAME = "inRustProject"; + export async function activate(context: vscode.ExtensionContext) { // Register a "dumb" onEnter command for the case where server fails to // start. @@ -54,6 +57,8 @@ export async function activate(context: vscode.ExtensionContext) { // This a horribly, horribly wrong way to deal with this problem. 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. // 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() { + setContextValue(RUST_PROJECT_CONTEXT_NAME, undefined); await ctx?.client.stop(); ctx = undefined; } diff --git a/editors/code/src/util.ts b/editors/code/src/util.ts index 793c481fb..352ef9162 100644 --- a/editors/code/src/util.ts +++ b/editors/code/src/util.ts @@ -94,3 +94,8 @@ export function isValidExecutable(path: string): boolean { 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 { + return vscode.commands.executeCommand('setContext', key, value); +} -- cgit v1.2.3