Tidy up vscode extension a bit

This commit is contained in:
Lukas Wirth 2024-06-19 09:36:22 +02:00
parent f9bb5476c3
commit ff07caa9de
16 changed files with 143 additions and 171 deletions

View file

@ -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\``);
}