Unnamed repository; edit this file 'description' to name the repository.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
import * as vscode from "vscode";
import { strict as nativeAssert } from "assert";
import { exec, spawn, type SpawnOptionsWithoutStdio, type ExecOptions } from "child_process";
import { inspect } from "util";
import type { CargoRunnableArgs, ShellRunnableArgs } from "./lsp_ext";

export function assert(condition: boolean, explanation: string): asserts condition {
    try {
        nativeAssert(condition, explanation);
    } catch (err) {
        log.error(`Assertion failed:`, explanation);
        throw err;
    }
}

export type Env = {
    [name: string]: string | undefined;
};

class Log {
    private readonly output = vscode.window.createOutputChannel("rust-analyzer Extension", {
        log: true,
    });

    trace(...messages: [unknown, ...unknown[]]): void {
        this.output.trace(this.stringify(messages));
    }

    debug(...messages: [unknown, ...unknown[]]): void {
        this.output.debug(this.stringify(messages));
    }

    info(...messages: [unknown, ...unknown[]]): void {
        this.output.info(this.stringify(messages));
    }

    warn(...messages: [unknown, ...unknown[]]): void {
        this.output.warn(this.stringify(messages));
    }

    error(...messages: [unknown, ...unknown[]]): void {
        this.output.error(this.stringify(messages));
        this.output.show(true);
    }

    private stringify(messages: unknown[]): string {
        return messages
            .map((message) => {
                if (typeof message === "string") {
                    return message;
                }
                if (message instanceof Error) {
                    return message.stack || message.message;
                }
                return inspect(message, { depth: 6, colors: false });
            })
            .join(" ");
    }
}

export const log = new Log();

export function sleep(ms: number) {
    return new Promise((resolve) => setTimeout(resolve, ms));
}

export type RustDocument = vscode.TextDocument & { languageId: "rust" };
export type RustEditor = vscode.TextEditor & { document: RustDocument };

export function isRustDocument(document: vscode.TextDocument): document is RustDocument {
    // Prevent corrupted text (particularly via inlay hints) in diff views
    // by allowing only `file` schemes
    // unfortunately extensions that use diff views not always set this
    // to something different than 'file' (see ongoing bug: #4608)
    return document.languageId === "rust" && document.uri.scheme === "file";
}

export function isCargoTomlDocument(document: vscode.TextDocument): document is RustDocument {
    // ideally `document.languageId` should be 'toml' but user maybe not have toml extension installed
    return document.uri.scheme === "file" && document.fileName.endsWith("Cargo.toml");
}

export function isCargoRunnableArgs(
    args: CargoRunnableArgs | ShellRunnableArgs,
): args is CargoRunnableArgs {
    return (args as CargoRunnableArgs).executableArgs !== undefined;
}

export function isRustEditor(editor: vscode.TextEditor): editor is RustEditor {
    return isRustDocument(editor.document);
}

export function isCargoTomlEditor(editor: vscode.TextEditor): editor is RustEditor {
    return isCargoTomlDocument(editor.document);
}

export function isDocumentInWorkspace(document: RustDocument): boolean {
    const workspaceFolders = vscode.workspace.workspaceFolders;
    if (!workspaceFolders) {
        return false;
    }
    for (const folder of workspaceFolders) {
        if (document.uri.fsPath.startsWith(folder.uri.fsPath)) {
            return true;
        }
    }
    return false;
}

/** Sets ['when'](https://code.visualstudio.com/docs/getstarted/keybindings#_when-clause-contexts) clause contexts */
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export function setContextValue(key: string, value: any): Thenable<void> {
    return vscode.commands.executeCommand("setContext", key, value);
}

/**
 * Returns a higher-order function that caches the results of invoking the
 * underlying async function.
 */
export function memoizeAsync<Ret, TThis, Param extends string>(
    func: (this: TThis, arg: Param) => Promise<Ret>,
) {
    const cache = new Map<string, Ret>();

    return async function (this: TThis, arg: Param) {
        const cached = cache.get(arg);
        if (cached) return cached;

        const result = await func.call(this, arg);
        cache.set(arg, result);

        return result;
    };
}

/** Awaitable wrapper around `child_process.exec` */
export function execute(command: string, options: ExecOptions): Promise<string> {
    log.info(`running command: ${command}`);
    return new Promise((resolve, reject) => {
        exec(command, options, (err, stdout, stderr) => {
            if (err) {
                log.error("error:", err);
                reject(err);
                return;
            }

            if (stderr) {
                reject(new Error(stderr));
                return;
            }

            resolve(stdout.trimEnd());
        });
    });
}

export class LazyOutputChannel implements vscode.OutputChannel {
    constructor(name: string) {
        this.name = name;
    }

    name: string;
    _channel: vscode.OutputChannel | undefined;

    get channel(): vscode.OutputChannel {
        if (!this._channel) {
            this._channel = vscode.window.createOutputChannel(this.name);
        }
        return this._channel;
    }

    append(value: string): void {
        this.channel.append(value);
    }

    appendLine(value: string): void {
        this.channel.appendLine(value);
    }

    replace(value: string): void {
        this.channel.replace(value);
    }

    clear(): void {
        if (this._channel) {
            this._channel.clear();
        }
    }

    show(columnOrPreserveFocus?: vscode.ViewColumn | boolean, preserveFocus?: boolean): void {
        if (typeof columnOrPreserveFocus === "boolean") {
            this.channel.show(columnOrPreserveFocus);
        } else {
            this.channel.show(columnOrPreserveFocus, preserveFocus);
        }
    }

    hide(): void {
        if (this._channel) {
            this._channel.hide();
        }
    }

    dispose(): void {
        if (this._channel) {
            this._channel.dispose();
        }
    }
}

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

interface SpawnAsyncReturns {
    stdout: string;
    stderr: string;
    status: number | null;
    error?: Error | undefined;
}

export async function spawnAsync(
    path: string,
    args?: ReadonlyArray<string>,
    options?: SpawnOptionsWithoutStdio,
): Promise<SpawnAsyncReturns> {
    const child = spawn(path, args, options);
    const stdout: Array<Buffer> = [];
    const stderr: Array<Buffer> = [];
    try {
        const res = await new Promise<{ stdout: string; stderr: string; status: number | null }>(
            (resolve, reject) => {
                child.stdout.on("data", (chunk) => stdout.push(Buffer.from(chunk)));
                child.stderr.on("data", (chunk) => stderr.push(Buffer.from(chunk)));
                child.on("error", (error) =>
                    reject({
                        stdout: Buffer.concat(stdout).toString("utf8"),
                        stderr: Buffer.concat(stderr).toString("utf8"),
                        error,
                    }),
                );
                child.on("close", (status) =>
                    resolve({
                        stdout: Buffer.concat(stdout).toString("utf8"),
                        stderr: Buffer.concat(stderr).toString("utf8"),
                        status,
                    }),
                );
            },
        );

        return {
            stdout: res.stdout,
            stderr: res.stderr,
            status: res.status,
        };
        // eslint-disable-next-line @typescript-eslint/no-explicit-any
    } catch (e: any) {
        return {
            stdout: e.stdout,
            stderr: e.stderr,
            status: e.status,
            error: e.error,
        };
    }
}

export const isWindows = process.platform === "win32";

export function isWindowsDriveLetter(code: number): boolean {
    // Copied from https://github.com/microsoft/vscode/blob/02c2dba5f2669b924fd290dff7d2ff3460791996/src/vs/base/common/extpath.ts#L265-L267
    return (
        (code >= /* CharCode.A */ 65 && code <= /* CharCode.Z */ 90) ||
        (code >= /* CharCode.a */ 97 && code <= /* CharCode.z */ 122)
    );
}
export function hasDriveLetter(path: string, isWindowsOS: boolean = isWindows): boolean {
    // Copied from https://github.com/microsoft/vscode/blob/02c2dba5f2669b924fd290dff7d2ff3460791996/src/vs/base/common/extpath.ts#L324-L330
    if (isWindowsOS) {
        return (
            isWindowsDriveLetter(path.charCodeAt(0)) &&
            path.charCodeAt(1) === /* CharCode.Colon */ 58
        );
    }

    return false;
}
export function normalizeDriveLetter(path: string, isWindowsOS: boolean = isWindows): string {
    // Copied from https://github.com/microsoft/vscode/blob/02c2dba5f2669b924fd290dff7d2ff3460791996/src/vs/base/common/labels.ts#L140-L146
    if (hasDriveLetter(path, isWindowsOS)) {
        return path.charAt(0).toUpperCase() + path.slice(1);
    }

    return path;
}

export const RUST_TOOLCHAIN_FILES = ["rust-toolchain.toml", "rust-toolchain"] as const;

export async function findRustToolchainFiles(): Promise<vscode.Uri[]> {
    const found: vscode.Uri[] = [];
    const workspaceFolders = vscode.workspace.workspaceFolders;
    if (!workspaceFolders) {
        return found;
    }

    for (const folder of workspaceFolders) {
        for (const filename of RUST_TOOLCHAIN_FILES) {
            const toolchainUri = vscode.Uri.joinPath(folder.uri, filename);
            try {
                await vscode.workspace.fs.stat(toolchainUri);
                found.push(toolchainUri);
                // Only add the first toolchain file found per workspace folder
                break;
            } catch {
                // File doesn't exist, continue
            }
        }
    }
    return found;
}