Unnamed repository; edit this file 'description' to name the repository.
Tidy up vscode extension a bit
Lukas Wirth 2024-06-19
parent f9bb547 · commit ff07caa
-rw-r--r--editors/code/src/ast_inspector.ts3
-rw-r--r--editors/code/src/bootstrap.ts76
-rw-r--r--editors/code/src/client.ts91
-rw-r--r--editors/code/src/commands.ts13
-rw-r--r--editors/code/src/config.ts4
-rw-r--r--editors/code/src/debug.ts5
-rw-r--r--editors/code/src/dependencies_provider.ts2
-rw-r--r--editors/code/src/diagnostics.ts2
-rw-r--r--editors/code/src/main.ts2
-rw-r--r--editors/code/src/nullable.ts19
-rw-r--r--editors/code/src/run.ts6
-rw-r--r--editors/code/src/snippets.ts3
-rw-r--r--editors/code/src/tasks.ts3
-rw-r--r--editors/code/src/toolchain.ts6
-rw-r--r--editors/code/src/undefinable.ts19
-rw-r--r--editors/code/src/util.ts60
16 files changed, 143 insertions, 171 deletions
diff --git a/editors/code/src/ast_inspector.ts b/editors/code/src/ast_inspector.ts
index 688c53a9b1..35b705c477 100644
--- a/editors/code/src/ast_inspector.ts
+++ b/editors/code/src/ast_inspector.ts
@@ -1,8 +1,7 @@
import * as vscode from "vscode";
import type { Ctx, Disposable } from "./ctx";
-import { type RustEditor, isRustEditor } from "./util";
-import { unwrapUndefinable } from "./undefinable";
+import { type RustEditor, isRustEditor, unwrapUndefinable } from "./util";
// FIXME: consider implementing this via the Tree View API?
// https://code.visualstudio.com/api/extension-guides/tree-view
diff --git a/editors/code/src/bootstrap.ts b/editors/code/src/bootstrap.ts
index 6cf399599d..5a92b285ae 100644
--- a/editors/code/src/bootstrap.ts
+++ b/editors/code/src/bootstrap.ts
@@ -1,9 +1,9 @@
import * as vscode from "vscode";
import * as os from "os";
import type { Config } from "./config";
-import { log, isValidExecutable } from "./util";
+import { type Env, log } from "./util";
import type { PersistentState } from "./persistent_state";
-import { exec } from "child_process";
+import { exec, spawnSync } from "child_process";
export async function bootstrap(
context: vscode.ExtensionContext,
@@ -13,7 +13,7 @@ export async function bootstrap(
const path = await getServer(context, config, state);
if (!path) {
throw new Error(
- "Rust Analyzer Language Server is not available. " +
+ "rust-analyzer Language Server is not available. " +
"Please, ensure its [proper installation](https://rust-analyzer.github.io/manual.html#installation).",
);
}
@@ -21,12 +21,12 @@ export async function bootstrap(
log.info("Using server binary at", path);
if (!isValidExecutable(path, config.serverExtraEnv)) {
- if (config.serverPath) {
- throw new Error(`Failed to execute ${path} --version. \`config.server.path\` or \`config.serverPath\` has been set explicitly.\
- Consider removing this config or making a valid server binary available at that path.`);
- } else {
- throw new Error(`Failed to execute ${path} --version`);
- }
+ throw new Error(
+ `Failed to execute ${path} --version.` + config.serverPath
+ ? `\`config.server.path\` or \`config.serverPath\` has been set explicitly.\
+ Consider removing this config or making a valid server binary available at that path.`
+ : "",
+ );
}
return path;
@@ -54,27 +54,12 @@ async function getServer(
if (bundledExists) {
let server = bundled;
if (await isNixOs()) {
- await vscode.workspace.fs.createDirectory(config.globalStorageUri).then();
- const dest = vscode.Uri.joinPath(config.globalStorageUri, `rust-analyzer${ext}`);
- let exists = await vscode.workspace.fs.stat(dest).then(
- () => true,
- () => false,
- );
- if (exists && config.package.version !== state.serverVersion) {
- await vscode.workspace.fs.delete(dest);
- exists = false;
- }
- if (!exists) {
- await vscode.workspace.fs.copy(bundled, dest);
- await patchelf(dest);
- }
- server = dest;
+ server = await getNixOsServer(config, ext, state, bundled, server);
+ await state.updateServerVersion(config.package.version);
}
- await state.updateServerVersion(config.package.version);
return server.fsPath;
}
- await state.updateServerVersion(undefined);
await vscode.window.showErrorMessage(
"Unfortunately we don't ship binaries for your platform yet. " +
"You need to manually clone the rust-analyzer repository and " +
@@ -86,6 +71,45 @@ async function getServer(
return undefined;
}
+export function isValidExecutable(path: string, extraEnv: Env): boolean {
+ log.debug("Checking availability of a binary at", path);
+
+ const res = spawnSync(path, ["--version"], {
+ encoding: "utf8",
+ env: { ...process.env, ...extraEnv },
+ });
+
+ const printOutput = res.error ? log.warn : log.info;
+ printOutput(path, "--version:", res);
+
+ return res.status === 0;
+}
+
+async function getNixOsServer(
+ config: Config,
+ ext: string,
+ state: PersistentState,
+ bundled: vscode.Uri,
+ server: vscode.Uri,
+) {
+ await vscode.workspace.fs.createDirectory(config.globalStorageUri).then();
+ const dest = vscode.Uri.joinPath(config.globalStorageUri, `rust-analyzer${ext}`);
+ let exists = await vscode.workspace.fs.stat(dest).then(
+ () => true,
+ () => false,
+ );
+ if (exists && config.package.version !== state.serverVersion) {
+ await vscode.workspace.fs.delete(dest);
+ exists = false;
+ }
+ if (!exists) {
+ await vscode.workspace.fs.copy(bundled, dest);
+ await patchelf(dest);
+ }
+ server = dest;
+ return server;
+}
+
async function isNixOs(): Promise<boolean> {
try {
const contents = (
diff --git a/editors/code/src/client.ts b/editors/code/src/client.ts
index f679c88398..1c2a34b484 100644
--- a/editors/code/src/client.ts
+++ b/editors/code/src/client.ts
@@ -3,73 +3,13 @@ import * as lc from "vscode-languageclient/node";
import * as vscode from "vscode";
import * as ra from "../src/lsp_ext";
import * as Is from "vscode-languageclient/lib/common/utils/is";
-import { assert } from "./util";
+import { assert, unwrapUndefinable } from "./util";
import * as diagnostics from "./diagnostics";
import { WorkspaceEdit } from "vscode";
import { type Config, prepareVSCodeConfig } from "./config";
-import { randomUUID } from "crypto";
import { sep as pathSeparator } from "path";
-import { unwrapUndefinable } from "./undefinable";
import { RaLanguageClient } from "./lang_client";
-export interface Env {
- [name: string]: string;
-}
-
-// Command URIs have a form of command:command-name?arguments, where
-// arguments is a percent-encoded array of data we want to pass along to
-// the command function. For "Show References" this is a list of all file
-// URIs with locations of every reference, and it can get quite long.
-//
-// To work around it we use an intermediary linkToCommand command. When
-// we render a command link, a reference to a command with all its arguments
-// is stored in a map, and instead a linkToCommand link is rendered
-// with the key to that map.
-export const LINKED_COMMANDS = new Map<string, ra.CommandLink>();
-
-// For now the map is cleaned up periodically (I've set it to every
-// 10 minutes). In general case we'll probably need to introduce TTLs or
-// flags to denote ephemeral links (like these in hover popups) and
-// persistent links and clean those separately. But for now simply keeping
-// the last few links in the map should be good enough. Likewise, we could
-// add code to remove a target command from the map after the link is
-// clicked, but assuming most links in hover sheets won't be clicked anyway
-// this code won't change the overall memory use much.
-setInterval(
- function cleanupOlderCommandLinks() {
- // keys are returned in insertion order, we'll keep a few
- // of recent keys available, and clean the rest
- const keys = [...LINKED_COMMANDS.keys()];
- const keysToRemove = keys.slice(0, keys.length - 10);
- for (const key of keysToRemove) {
- LINKED_COMMANDS.delete(key);
- }
- },
- 10 * 60 * 1000,
-);
-
-function renderCommand(cmd: ra.CommandLink): string {
- const commandId = randomUUID();
- LINKED_COMMANDS.set(commandId, cmd);
- return `[${cmd.title}](command:rust-analyzer.linkToCommand?${encodeURIComponent(
- JSON.stringify([commandId]),
- )} '${cmd.tooltip}')`;
-}
-
-function renderHoverActions(actions: ra.CommandLinkGroup[]): vscode.MarkdownString {
- const text = actions
- .map(
- (group) =>
- (group.title ? group.title + " " : "") +
- group.commands.map(renderCommand).join(" | "),
- )
- .join(" | ");
-
- const result = new vscode.MarkdownString(text);
- result.isTrusted = true;
- return result;
-}
-
export async function createClient(
traceOutputChannel: vscode.OutputChannel,
outputChannel: vscode.OutputChannel,
@@ -450,3 +390,32 @@ function isCodeActionWithoutEditsAndCommands(value: any): boolean {
candidate.command === void 0
);
}
+
+// Command URIs have a form of command:command-name?arguments, where
+// arguments is a percent-encoded array of data we want to pass along to
+// the command function. For "Show References" this is a list of all file
+// URIs with locations of every reference, and it can get quite long.
+// So long in fact that it will fail rendering inside an `a` tag so we need
+// to proxy around that. We store the last hover's reference command link
+// here, as only one hover can be active at a time, and we don't need to
+// keep a history of these.
+export let HOVER_REFERENCE_COMMAND: ra.CommandLink | undefined = undefined;
+
+function renderCommand(cmd: ra.CommandLink): string {
+ HOVER_REFERENCE_COMMAND = cmd;
+ return `[${cmd.title}](command:rust-analyzer.hoverRefCommandProxy '${cmd.tooltip}')`;
+}
+
+function renderHoverActions(actions: ra.CommandLinkGroup[]): vscode.MarkdownString {
+ const text = actions
+ .map(
+ (group) =>
+ (group.title ? group.title + " " : "") +
+ group.commands.map(renderCommand).join(" | "),
+ )
+ .join(" | ");
+
+ const result = new vscode.MarkdownString(text);
+ result.isTrusted = true;
+ return result;
+}
diff --git a/editors/code/src/commands.ts b/editors/code/src/commands.ts
index 5048f820e4..c226aefeab 100644
--- a/editors/code/src/commands.ts
+++ b/editors/code/src/commands.ts
@@ -24,12 +24,12 @@ import {
isRustEditor,
type RustEditor,
type RustDocument,
+ unwrapUndefinable,
} from "./util";
import { startDebugSession, makeDebugConfig } from "./debug";
import type { LanguageClient } from "vscode-languageclient/node";
-import { LINKED_COMMANDS } from "./client";
+import { HOVER_REFERENCE_COMMAND } from "./client";
import type { DependencyId } from "./dependencies_provider";
-import { unwrapUndefinable } from "./undefinable";
import { log } from "./util";
export * from "./ast_inspector";
@@ -1196,11 +1196,10 @@ export function newDebugConfig(ctx: CtxInit): Cmd {
};
}
-export function linkToCommand(_: Ctx): Cmd {
- return async (commandId: string) => {
- const link = LINKED_COMMANDS.get(commandId);
- if (link) {
- const { command, arguments: args = [] } = link;
+export function hoverRefCommandProxy(_: Ctx): Cmd {
+ return async () => {
+ if (HOVER_REFERENCE_COMMAND) {
+ const { command, arguments: args = [] } = HOVER_REFERENCE_COMMAND;
await vscode.commands.executeCommand(command, ...args);
}
};
diff --git a/editors/code/src/config.ts b/editors/code/src/config.ts
index e676bc0826..1931cfe381 100644
--- a/editors/code/src/config.ts
+++ b/editors/code/src/config.ts
@@ -2,9 +2,7 @@ import * as Is from "vscode-languageclient/lib/common/utils/is";
import * as os from "os";
import * as path from "path";
import * as vscode from "vscode";
-import type { Env } from "./client";
-import { log } from "./util";
-import { expectNotUndefined, unwrapUndefinable } from "./undefinable";
+import { type Env, log, unwrapUndefinable, expectNotUndefined } from "./util";
import type { JsonProject } from "./rust_project";
export type RunnableEnvCfgItem = {
diff --git a/editors/code/src/debug.ts b/editors/code/src/debug.ts
index eef2f6f4ee..58fe1df51f 100644
--- a/editors/code/src/debug.ts
+++ b/editors/code/src/debug.ts
@@ -6,8 +6,7 @@ import type * as ra from "./lsp_ext";
import { Cargo, getRustcId, getSysroot } from "./toolchain";
import type { Ctx } from "./ctx";
import { prepareEnv } from "./run";
-import { unwrapUndefinable } from "./undefinable";
-import { isCargoRunnableArgs } from "./util";
+import { isCargoRunnableArgs, unwrapUndefinable } from "./util";
const debugOutput = vscode.window.createOutputChannel("Debug");
type DebugConfigProvider = (
@@ -136,7 +135,7 @@ async function getDebugConfiguration(
const workspaceQualifier = isMultiFolderWorkspace ? `:${workspace.name}` : "";
function simplifyPath(p: string): string {
// see https://github.com/rust-lang/rust-analyzer/pull/5513#issuecomment-663458818 for why this is needed
- return path.normalize(p).replace(wsFolder, "${workspaceFolder" + workspaceQualifier + "}");
+ return path.normalize(p).replace(wsFolder, `\${workspaceFolder${workspaceQualifier}}`);
}
const env = prepareEnv(runnable.label, runnableArgs, ctx.config.runnablesExtraEnv);
diff --git a/editors/code/src/dependencies_provider.ts b/editors/code/src/dependencies_provider.ts
index 863ace0780..203ef5cc85 100644
--- a/editors/code/src/dependencies_provider.ts
+++ b/editors/code/src/dependencies_provider.ts
@@ -4,7 +4,7 @@ import * as fs from "fs";
import type { CtxInit } from "./ctx";
import * as ra from "./lsp_ext";
import type { FetchDependencyListResult } from "./lsp_ext";
-import { unwrapUndefinable } from "./undefinable";
+import { unwrapUndefinable } from "./util";
export class RustDependenciesProvider
implements vscode.TreeDataProvider<Dependency | DependencyFile>
diff --git a/editors/code/src/diagnostics.ts b/editors/code/src/diagnostics.ts
index e31a1cdcef..9fb2993d12 100644
--- a/editors/code/src/diagnostics.ts
+++ b/editors/code/src/diagnostics.ts
@@ -8,7 +8,7 @@ import {
window,
} from "vscode";
import type { Ctx } from "./ctx";
-import { unwrapUndefinable } from "./undefinable";
+import { unwrapUndefinable } from "./util";
export const URI_SCHEME = "rust-analyzer-diagnostics-view";
diff --git a/editors/code/src/main.ts b/editors/code/src/main.ts
index 0af58fd781..788f9a0c14 100644
--- a/editors/code/src/main.ts
+++ b/editors/code/src/main.ts
@@ -182,7 +182,7 @@ function createCommands(): Record<string, CommandFactory> {
applySnippetWorkspaceEdit: { enabled: commands.applySnippetWorkspaceEditCommand },
debugSingle: { enabled: commands.debugSingle },
gotoLocation: { enabled: commands.gotoLocation },
- linkToCommand: { enabled: commands.linkToCommand },
+ hoverRefCommandProxy: { enabled: commands.hoverRefCommandProxy },
resolveCodeAction: { enabled: commands.resolveCodeAction },
runSingle: { enabled: commands.runSingle },
showReferences: { enabled: commands.showReferences },
diff --git a/editors/code/src/nullable.ts b/editors/code/src/nullable.ts
deleted file mode 100644
index e973e16290..0000000000
--- a/editors/code/src/nullable.ts
+++ /dev/null
@@ -1,19 +0,0 @@
-export type NotNull<T> = T extends null ? never : T;
-
-export type Nullable<T> = T | null;
-
-function isNotNull<T>(input: Nullable<T>): input is NotNull<T> {
- return input !== null;
-}
-
-function expectNotNull<T>(input: Nullable<T>, msg: string): NotNull<T> {
- if (isNotNull(input)) {
- return input;
- }
-
- throw new TypeError(msg);
-}
-
-export function unwrapNullable<T>(input: Nullable<T>): NotNull<T> {
- return expectNotNull(input, `unwrapping \`null\``);
-}
diff --git a/editors/code/src/run.ts b/editors/code/src/run.ts
index 1206137b6f..7a9049af0d 100644
--- a/editors/code/src/run.ts
+++ b/editors/code/src/run.ts
@@ -6,9 +6,8 @@ import * as tasks from "./tasks";
import type { CtxInit } from "./ctx";
import { makeDebugConfig } from "./debug";
import type { Config, RunnableEnvCfg, RunnableEnvCfgItem } from "./config";
-import { unwrapUndefinable } from "./undefinable";
import type { LanguageClient } from "vscode-languageclient/node";
-import type { RustEditor } from "./util";
+import { unwrapUndefinable, type RustEditor } from "./util";
import * as toolchain from "./toolchain";
const quickPickButtons = [
@@ -148,8 +147,7 @@ export async function createTaskFromRunnable(
};
}
- // eslint-disable-next-line @typescript-eslint/no-unnecessary-type-assertion
- const target = vscode.workspace.workspaceFolders![0]; // safe, see main activate()
+ const target = vscode.workspace.workspaceFolders?.[0];
const exec = await tasks.targetToExecution(definition, config.cargoRunner, true);
const task = await tasks.buildRustTask(
target,
diff --git a/editors/code/src/snippets.ts b/editors/code/src/snippets.ts
index b3982bdf2b..a469a9cd1f 100644
--- a/editors/code/src/snippets.ts
+++ b/editors/code/src/snippets.ts
@@ -1,7 +1,6 @@
import * as vscode from "vscode";
-import { assert } from "./util";
-import { unwrapUndefinable } from "./undefinable";
+import { assert, unwrapUndefinable } from "./util";
export type SnippetTextDocumentEdit = [vscode.Uri, (vscode.TextEdit | vscode.SnippetTextEdit)[]];
diff --git a/editors/code/src/tasks.ts b/editors/code/src/tasks.ts
index 870b1ffb71..6f4fbf9188 100644
--- a/editors/code/src/tasks.ts
+++ b/editors/code/src/tasks.ts
@@ -1,7 +1,6 @@
import * as vscode from "vscode";
import type { Config } from "./config";
-import { log } from "./util";
-import { unwrapUndefinable } from "./undefinable";
+import { log, unwrapUndefinable } from "./util";
import * as toolchain from "./toolchain";
// This ends up as the `type` key in tasks.json. RLS also uses `cargo` and
diff --git a/editors/code/src/toolchain.ts b/editors/code/src/toolchain.ts
index 060b0245d2..a48d2d90cc 100644
--- a/editors/code/src/toolchain.ts
+++ b/editors/code/src/toolchain.ts
@@ -3,9 +3,7 @@ import * as os from "os";
import * as path from "path";
import * as readline from "readline";
import * as vscode from "vscode";
-import { execute, log, memoizeAsync } from "./util";
-import { unwrapNullable } from "./nullable";
-import { unwrapUndefinable } from "./undefinable";
+import { execute, log, memoizeAsync, unwrapNullable, unwrapUndefinable } from "./util";
interface CompilationArtifact {
fileName: string;
@@ -157,7 +155,7 @@ export function cargoPath(): Promise<string> {
}
/** Mirrors `toolchain::get_path_for_executable()` implementation */
-export const getPathForExecutable = memoizeAsync(
+const getPathForExecutable = memoizeAsync(
// We apply caching to decrease file-system interactions
async (executableName: "cargo" | "rustc" | "rustup"): Promise<string> => {
{
diff --git a/editors/code/src/undefinable.ts b/editors/code/src/undefinable.ts
deleted file mode 100644
index 813bac5a12..0000000000
--- a/editors/code/src/undefinable.ts
+++ /dev/null
@@ -1,19 +0,0 @@
-export type NotUndefined<T> = T extends undefined ? never : T;
-
-export type Undefinable<T> = T | undefined;
-
-function isNotUndefined<T>(input: Undefinable<T>): input is NotUndefined<T> {
- return input !== undefined;
-}
-
-export function expectNotUndefined<T>(input: Undefinable<T>, msg: string): NotUndefined<T> {
- if (isNotUndefined(input)) {
- return input;
- }
-
- throw new TypeError(msg);
-}
-
-export function unwrapUndefinable<T>(input: Undefinable<T>): NotUndefined<T> {
- return expectNotUndefined(input, `unwrapping \`undefined\``);
-}
diff --git a/editors/code/src/util.ts b/editors/code/src/util.ts
index 868cb2b780..dd1cbe38ff 100644
--- a/editors/code/src/util.ts
+++ b/editors/code/src/util.ts
@@ -1,9 +1,8 @@
import * as vscode from "vscode";
import { strict as nativeAssert } from "assert";
-import { exec, type ExecOptions, spawnSync } from "child_process";
+import { exec, type ExecOptions } from "child_process";
import { inspect } from "util";
import type { CargoRunnableArgs, ShellRunnableArgs } from "./lsp_ext";
-import type { Env } from "./client";
export function assert(condition: boolean, explanation: string): asserts condition {
try {
@@ -14,6 +13,10 @@ export function assert(condition: boolean, explanation: string): asserts conditi
}
}
+export type Env = {
+ [name: string]: string;
+};
+
export const log = new (class {
private enabled = true;
private readonly output = vscode.window.createOutputChannel("Rust Analyzer Client");
@@ -101,20 +104,6 @@ export function isDocumentInWorkspace(document: RustDocument): boolean {
return false;
}
-export function isValidExecutable(path: string, extraEnv: Env): boolean {
- log.debug("Checking availability of a binary at", path);
-
- const res = spawnSync(path, ["--version"], {
- encoding: "utf8",
- env: { ...process.env, ...extraEnv },
- });
-
- const printOutput = res.error ? log.warn : log.info;
- printOutput(path, "--version:", res);
-
- 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);
@@ -206,3 +195,42 @@ export class LazyOutputChannel implements vscode.OutputChannel {
}
}
}
+
+export type NotNull<T> = T extends null ? never : T;
+
+export type Nullable<T> = T | null;
+
+function isNotNull<T>(input: Nullable<T>): input is NotNull<T> {
+ return input !== null;
+}
+
+function expectNotNull<T>(input: Nullable<T>, msg: string): NotNull<T> {
+ if (isNotNull(input)) {
+ return input;
+ }
+
+ throw new TypeError(msg);
+}
+
+export function unwrapNullable<T>(input: Nullable<T>): NotNull<T> {
+ return expectNotNull(input, `unwrapping \`null\``);
+}
+export type NotUndefined<T> = T extends undefined ? never : T;
+
+export type Undefinable<T> = T | undefined;
+
+function isNotUndefined<T>(input: Undefinable<T>): input is NotUndefined<T> {
+ return input !== undefined;
+}
+
+export function expectNotUndefined<T>(input: Undefinable<T>, msg: string): NotUndefined<T> {
+ if (isNotUndefined(input)) {
+ return input;
+ }
+
+ throw new TypeError(msg);
+}
+
+export function unwrapUndefinable<T>(input: Undefinable<T>): NotUndefined<T> {
+ return expectNotUndefined(input, `unwrapping \`undefined\``);
+}