mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-09-26 11:59:49 +00:00
Tidy up vscode extension a bit
This commit is contained in:
parent
f9bb5476c3
commit
ff07caa9de
16 changed files with 143 additions and 171 deletions
|
@ -1,8 +1,7 @@
|
||||||
import * as vscode from "vscode";
|
import * as vscode from "vscode";
|
||||||
|
|
||||||
import type { Ctx, Disposable } from "./ctx";
|
import type { Ctx, Disposable } from "./ctx";
|
||||||
import { type RustEditor, isRustEditor } from "./util";
|
import { type RustEditor, isRustEditor, unwrapUndefinable } from "./util";
|
||||||
import { unwrapUndefinable } from "./undefinable";
|
|
||||||
|
|
||||||
// FIXME: consider implementing this via the Tree View API?
|
// FIXME: consider implementing this via the Tree View API?
|
||||||
// https://code.visualstudio.com/api/extension-guides/tree-view
|
// https://code.visualstudio.com/api/extension-guides/tree-view
|
||||||
|
|
|
@ -1,9 +1,9 @@
|
||||||
import * as vscode from "vscode";
|
import * as vscode from "vscode";
|
||||||
import * as os from "os";
|
import * as os from "os";
|
||||||
import type { Config } from "./config";
|
import type { Config } from "./config";
|
||||||
import { log, isValidExecutable } from "./util";
|
import { type Env, log } from "./util";
|
||||||
import type { PersistentState } from "./persistent_state";
|
import type { PersistentState } from "./persistent_state";
|
||||||
import { exec } from "child_process";
|
import { exec, spawnSync } from "child_process";
|
||||||
|
|
||||||
export async function bootstrap(
|
export async function bootstrap(
|
||||||
context: vscode.ExtensionContext,
|
context: vscode.ExtensionContext,
|
||||||
|
@ -13,7 +13,7 @@ export async function bootstrap(
|
||||||
const path = await getServer(context, config, state);
|
const path = await getServer(context, config, state);
|
||||||
if (!path) {
|
if (!path) {
|
||||||
throw new Error(
|
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).",
|
"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);
|
log.info("Using server binary at", path);
|
||||||
|
|
||||||
if (!isValidExecutable(path, config.serverExtraEnv)) {
|
if (!isValidExecutable(path, config.serverExtraEnv)) {
|
||||||
if (config.serverPath) {
|
throw new Error(
|
||||||
throw new Error(`Failed to execute ${path} --version. \`config.server.path\` or \`config.serverPath\` has been set explicitly.\
|
`Failed to execute ${path} --version.` + config.serverPath
|
||||||
Consider removing this config or making a valid server binary available at that path.`);
|
? `\`config.server.path\` or \`config.serverPath\` has been set explicitly.\
|
||||||
} else {
|
Consider removing this config or making a valid server binary available at that path.`
|
||||||
throw new Error(`Failed to execute ${path} --version`);
|
: "",
|
||||||
}
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
return path;
|
return path;
|
||||||
|
@ -54,27 +54,12 @@ async function getServer(
|
||||||
if (bundledExists) {
|
if (bundledExists) {
|
||||||
let server = bundled;
|
let server = bundled;
|
||||||
if (await isNixOs()) {
|
if (await isNixOs()) {
|
||||||
await vscode.workspace.fs.createDirectory(config.globalStorageUri).then();
|
server = await getNixOsServer(config, ext, state, bundled, server);
|
||||||
const dest = vscode.Uri.joinPath(config.globalStorageUri, `rust-analyzer${ext}`);
|
await state.updateServerVersion(config.package.version);
|
||||||
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;
|
|
||||||
}
|
}
|
||||||
await state.updateServerVersion(config.package.version);
|
|
||||||
return server.fsPath;
|
return server.fsPath;
|
||||||
}
|
}
|
||||||
|
|
||||||
await state.updateServerVersion(undefined);
|
|
||||||
await vscode.window.showErrorMessage(
|
await vscode.window.showErrorMessage(
|
||||||
"Unfortunately we don't ship binaries for your platform yet. " +
|
"Unfortunately we don't ship binaries for your platform yet. " +
|
||||||
"You need to manually clone the rust-analyzer repository and " +
|
"You need to manually clone the rust-analyzer repository and " +
|
||||||
|
@ -86,6 +71,45 @@ async function getServer(
|
||||||
return undefined;
|
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> {
|
async function isNixOs(): Promise<boolean> {
|
||||||
try {
|
try {
|
||||||
const contents = (
|
const contents = (
|
||||||
|
|
|
@ -3,73 +3,13 @@ import * as lc from "vscode-languageclient/node";
|
||||||
import * as vscode from "vscode";
|
import * as vscode from "vscode";
|
||||||
import * as ra from "../src/lsp_ext";
|
import * as ra from "../src/lsp_ext";
|
||||||
import * as Is from "vscode-languageclient/lib/common/utils/is";
|
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 * as diagnostics from "./diagnostics";
|
||||||
import { WorkspaceEdit } from "vscode";
|
import { WorkspaceEdit } from "vscode";
|
||||||
import { type Config, prepareVSCodeConfig } from "./config";
|
import { type Config, prepareVSCodeConfig } from "./config";
|
||||||
import { randomUUID } from "crypto";
|
|
||||||
import { sep as pathSeparator } from "path";
|
import { sep as pathSeparator } from "path";
|
||||||
import { unwrapUndefinable } from "./undefinable";
|
|
||||||
import { RaLanguageClient } from "./lang_client";
|
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(
|
export async function createClient(
|
||||||
traceOutputChannel: vscode.OutputChannel,
|
traceOutputChannel: vscode.OutputChannel,
|
||||||
outputChannel: vscode.OutputChannel,
|
outputChannel: vscode.OutputChannel,
|
||||||
|
@ -450,3 +390,32 @@ function isCodeActionWithoutEditsAndCommands(value: any): boolean {
|
||||||
candidate.command === void 0
|
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;
|
||||||
|
}
|
||||||
|
|
|
@ -24,12 +24,12 @@ import {
|
||||||
isRustEditor,
|
isRustEditor,
|
||||||
type RustEditor,
|
type RustEditor,
|
||||||
type RustDocument,
|
type RustDocument,
|
||||||
|
unwrapUndefinable,
|
||||||
} from "./util";
|
} from "./util";
|
||||||
import { startDebugSession, makeDebugConfig } from "./debug";
|
import { startDebugSession, makeDebugConfig } from "./debug";
|
||||||
import type { LanguageClient } from "vscode-languageclient/node";
|
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 type { DependencyId } from "./dependencies_provider";
|
||||||
import { unwrapUndefinable } from "./undefinable";
|
|
||||||
import { log } from "./util";
|
import { log } from "./util";
|
||||||
|
|
||||||
export * from "./ast_inspector";
|
export * from "./ast_inspector";
|
||||||
|
@ -1196,11 +1196,10 @@ export function newDebugConfig(ctx: CtxInit): Cmd {
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
export function linkToCommand(_: Ctx): Cmd {
|
export function hoverRefCommandProxy(_: Ctx): Cmd {
|
||||||
return async (commandId: string) => {
|
return async () => {
|
||||||
const link = LINKED_COMMANDS.get(commandId);
|
if (HOVER_REFERENCE_COMMAND) {
|
||||||
if (link) {
|
const { command, arguments: args = [] } = HOVER_REFERENCE_COMMAND;
|
||||||
const { command, arguments: args = [] } = link;
|
|
||||||
await vscode.commands.executeCommand(command, ...args);
|
await vscode.commands.executeCommand(command, ...args);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
|
@ -2,9 +2,7 @@ import * as Is from "vscode-languageclient/lib/common/utils/is";
|
||||||
import * as os from "os";
|
import * as os from "os";
|
||||||
import * as path from "path";
|
import * as path from "path";
|
||||||
import * as vscode from "vscode";
|
import * as vscode from "vscode";
|
||||||
import type { Env } from "./client";
|
import { type Env, log, unwrapUndefinable, expectNotUndefined } from "./util";
|
||||||
import { log } from "./util";
|
|
||||||
import { expectNotUndefined, unwrapUndefinable } from "./undefinable";
|
|
||||||
import type { JsonProject } from "./rust_project";
|
import type { JsonProject } from "./rust_project";
|
||||||
|
|
||||||
export type RunnableEnvCfgItem = {
|
export type RunnableEnvCfgItem = {
|
||||||
|
|
|
@ -6,8 +6,7 @@ import type * as ra from "./lsp_ext";
|
||||||
import { Cargo, getRustcId, getSysroot } from "./toolchain";
|
import { Cargo, getRustcId, getSysroot } from "./toolchain";
|
||||||
import type { Ctx } from "./ctx";
|
import type { Ctx } from "./ctx";
|
||||||
import { prepareEnv } from "./run";
|
import { prepareEnv } from "./run";
|
||||||
import { unwrapUndefinable } from "./undefinable";
|
import { isCargoRunnableArgs, unwrapUndefinable } from "./util";
|
||||||
import { isCargoRunnableArgs } from "./util";
|
|
||||||
|
|
||||||
const debugOutput = vscode.window.createOutputChannel("Debug");
|
const debugOutput = vscode.window.createOutputChannel("Debug");
|
||||||
type DebugConfigProvider = (
|
type DebugConfigProvider = (
|
||||||
|
@ -136,7 +135,7 @@ async function getDebugConfiguration(
|
||||||
const workspaceQualifier = isMultiFolderWorkspace ? `:${workspace.name}` : "";
|
const workspaceQualifier = isMultiFolderWorkspace ? `:${workspace.name}` : "";
|
||||||
function simplifyPath(p: string): string {
|
function simplifyPath(p: string): string {
|
||||||
// see https://github.com/rust-lang/rust-analyzer/pull/5513#issuecomment-663458818 for why this is needed
|
// 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);
|
const env = prepareEnv(runnable.label, runnableArgs, ctx.config.runnablesExtraEnv);
|
||||||
|
|
|
@ -4,7 +4,7 @@ import * as fs from "fs";
|
||||||
import type { CtxInit } from "./ctx";
|
import type { CtxInit } from "./ctx";
|
||||||
import * as ra from "./lsp_ext";
|
import * as ra from "./lsp_ext";
|
||||||
import type { FetchDependencyListResult } from "./lsp_ext";
|
import type { FetchDependencyListResult } from "./lsp_ext";
|
||||||
import { unwrapUndefinable } from "./undefinable";
|
import { unwrapUndefinable } from "./util";
|
||||||
|
|
||||||
export class RustDependenciesProvider
|
export class RustDependenciesProvider
|
||||||
implements vscode.TreeDataProvider<Dependency | DependencyFile>
|
implements vscode.TreeDataProvider<Dependency | DependencyFile>
|
||||||
|
|
|
@ -8,7 +8,7 @@ import {
|
||||||
window,
|
window,
|
||||||
} from "vscode";
|
} from "vscode";
|
||||||
import type { Ctx } from "./ctx";
|
import type { Ctx } from "./ctx";
|
||||||
import { unwrapUndefinable } from "./undefinable";
|
import { unwrapUndefinable } from "./util";
|
||||||
|
|
||||||
export const URI_SCHEME = "rust-analyzer-diagnostics-view";
|
export const URI_SCHEME = "rust-analyzer-diagnostics-view";
|
||||||
|
|
||||||
|
|
|
@ -182,7 +182,7 @@ function createCommands(): Record<string, CommandFactory> {
|
||||||
applySnippetWorkspaceEdit: { enabled: commands.applySnippetWorkspaceEditCommand },
|
applySnippetWorkspaceEdit: { enabled: commands.applySnippetWorkspaceEditCommand },
|
||||||
debugSingle: { enabled: commands.debugSingle },
|
debugSingle: { enabled: commands.debugSingle },
|
||||||
gotoLocation: { enabled: commands.gotoLocation },
|
gotoLocation: { enabled: commands.gotoLocation },
|
||||||
linkToCommand: { enabled: commands.linkToCommand },
|
hoverRefCommandProxy: { enabled: commands.hoverRefCommandProxy },
|
||||||
resolveCodeAction: { enabled: commands.resolveCodeAction },
|
resolveCodeAction: { enabled: commands.resolveCodeAction },
|
||||||
runSingle: { enabled: commands.runSingle },
|
runSingle: { enabled: commands.runSingle },
|
||||||
showReferences: { enabled: commands.showReferences },
|
showReferences: { enabled: commands.showReferences },
|
||||||
|
|
|
@ -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\``);
|
|
||||||
}
|
|
|
@ -6,9 +6,8 @@ import * as tasks from "./tasks";
|
||||||
import type { CtxInit } from "./ctx";
|
import type { CtxInit } from "./ctx";
|
||||||
import { makeDebugConfig } from "./debug";
|
import { makeDebugConfig } from "./debug";
|
||||||
import type { Config, RunnableEnvCfg, RunnableEnvCfgItem } from "./config";
|
import type { Config, RunnableEnvCfg, RunnableEnvCfgItem } from "./config";
|
||||||
import { unwrapUndefinable } from "./undefinable";
|
|
||||||
import type { LanguageClient } from "vscode-languageclient/node";
|
import type { LanguageClient } from "vscode-languageclient/node";
|
||||||
import type { RustEditor } from "./util";
|
import { unwrapUndefinable, type RustEditor } from "./util";
|
||||||
import * as toolchain from "./toolchain";
|
import * as toolchain from "./toolchain";
|
||||||
|
|
||||||
const quickPickButtons = [
|
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];
|
||||||
const target = vscode.workspace.workspaceFolders![0]; // safe, see main activate()
|
|
||||||
const exec = await tasks.targetToExecution(definition, config.cargoRunner, true);
|
const exec = await tasks.targetToExecution(definition, config.cargoRunner, true);
|
||||||
const task = await tasks.buildRustTask(
|
const task = await tasks.buildRustTask(
|
||||||
target,
|
target,
|
||||||
|
|
|
@ -1,7 +1,6 @@
|
||||||
import * as vscode from "vscode";
|
import * as vscode from "vscode";
|
||||||
|
|
||||||
import { assert } from "./util";
|
import { assert, unwrapUndefinable } from "./util";
|
||||||
import { unwrapUndefinable } from "./undefinable";
|
|
||||||
|
|
||||||
export type SnippetTextDocumentEdit = [vscode.Uri, (vscode.TextEdit | vscode.SnippetTextEdit)[]];
|
export type SnippetTextDocumentEdit = [vscode.Uri, (vscode.TextEdit | vscode.SnippetTextEdit)[]];
|
||||||
|
|
||||||
|
|
|
@ -1,7 +1,6 @@
|
||||||
import * as vscode from "vscode";
|
import * as vscode from "vscode";
|
||||||
import type { Config } from "./config";
|
import type { Config } from "./config";
|
||||||
import { log } from "./util";
|
import { log, unwrapUndefinable } from "./util";
|
||||||
import { unwrapUndefinable } from "./undefinable";
|
|
||||||
import * as toolchain from "./toolchain";
|
import * as toolchain from "./toolchain";
|
||||||
|
|
||||||
// This ends up as the `type` key in tasks.json. RLS also uses `cargo` and
|
// This ends up as the `type` key in tasks.json. RLS also uses `cargo` and
|
||||||
|
|
|
@ -3,9 +3,7 @@ import * as os from "os";
|
||||||
import * as path from "path";
|
import * as path from "path";
|
||||||
import * as readline from "readline";
|
import * as readline from "readline";
|
||||||
import * as vscode from "vscode";
|
import * as vscode from "vscode";
|
||||||
import { execute, log, memoizeAsync } from "./util";
|
import { execute, log, memoizeAsync, unwrapNullable, unwrapUndefinable } from "./util";
|
||||||
import { unwrapNullable } from "./nullable";
|
|
||||||
import { unwrapUndefinable } from "./undefinable";
|
|
||||||
|
|
||||||
interface CompilationArtifact {
|
interface CompilationArtifact {
|
||||||
fileName: string;
|
fileName: string;
|
||||||
|
@ -157,7 +155,7 @@ export function cargoPath(): Promise<string> {
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Mirrors `toolchain::get_path_for_executable()` implementation */
|
/** Mirrors `toolchain::get_path_for_executable()` implementation */
|
||||||
export const getPathForExecutable = memoizeAsync(
|
const getPathForExecutable = memoizeAsync(
|
||||||
// We apply caching to decrease file-system interactions
|
// We apply caching to decrease file-system interactions
|
||||||
async (executableName: "cargo" | "rustc" | "rustup"): Promise<string> => {
|
async (executableName: "cargo" | "rustc" | "rustup"): Promise<string> => {
|
||||||
{
|
{
|
||||||
|
|
|
@ -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\``);
|
|
||||||
}
|
|
|
@ -1,9 +1,8 @@
|
||||||
import * as vscode from "vscode";
|
import * as vscode from "vscode";
|
||||||
import { strict as nativeAssert } from "assert";
|
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 { inspect } from "util";
|
||||||
import type { CargoRunnableArgs, ShellRunnableArgs } from "./lsp_ext";
|
import type { CargoRunnableArgs, ShellRunnableArgs } from "./lsp_ext";
|
||||||
import type { Env } from "./client";
|
|
||||||
|
|
||||||
export function assert(condition: boolean, explanation: string): asserts condition {
|
export function assert(condition: boolean, explanation: string): asserts condition {
|
||||||
try {
|
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 {
|
export const log = new (class {
|
||||||
private enabled = true;
|
private enabled = true;
|
||||||
private readonly output = vscode.window.createOutputChannel("Rust Analyzer Client");
|
private readonly output = vscode.window.createOutputChannel("Rust Analyzer Client");
|
||||||
|
@ -101,20 +104,6 @@ export function isDocumentInWorkspace(document: RustDocument): boolean {
|
||||||
return false;
|
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 */
|
/** Sets ['when'](https://code.visualstudio.com/docs/getstarted/keybindings#_when-clause-contexts) clause contexts */
|
||||||
export function setContextValue(key: string, value: any): Thenable<void> {
|
export function setContextValue(key: string, value: any): Thenable<void> {
|
||||||
return vscode.commands.executeCommand("setContext", key, value);
|
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\``);
|
||||||
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue