add normalizeDriveLetter

This commit is contained in:
Clouds Flowing 2025-04-14 11:23:31 +08:00
parent 8365cf853e
commit f66a3413db
3 changed files with 29 additions and 12 deletions

View file

@ -2,7 +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 { expectNotUndefined, log, unwrapUndefinable } from "./util";
import { expectNotUndefined, log, normalizeDriveLetter, unwrapUndefinable } from "./util";
import type { Env } from "./util";
import type { Disposable } from "vscode";
@ -498,7 +498,7 @@ function computeVscodeVar(varName: string): string | null {
// user has opened on Editor startup. Could lead to
// unpredictable workspace selection in practice.
// It's better to pick the first one
folder.uri.fsPath;
normalizeDriveLetter(folder.uri.fsPath);
return fsPath;
};
// https://code.visualstudio.com/docs/editor/variables-reference

View file

@ -6,7 +6,7 @@ import type * as ra from "./lsp_ext";
import { Cargo } from "./toolchain";
import type { Ctx } from "./ctx";
import { createTaskFromRunnable, prepareEnv } from "./run";
import { execute, isCargoRunnableArgs, unwrapUndefinable, log } from "./util";
import { execute, isCargoRunnableArgs, unwrapUndefinable, log, normalizeDriveLetter } from "./util";
import type { Config } from "./config";
// Here we want to keep track on everything that's currently running
@ -127,20 +127,14 @@ async function getDebugConfiguration(
firstWorkspace;
const workspace = unwrapUndefinable(maybeWorkspace);
let wsFolder = path.normalize(workspace.uri.fsPath);
if (os.platform() === "win32") {
// in windows, the drive letter can vary in casing for VSCode, so we gotta normalize that first
wsFolder = wsFolder.replace(/^[a-z]:\\/, (c) => c.toUpperCase());
}
const wsFolder = normalizeDriveLetter(path.normalize(workspace.uri.fsPath));
const workspaceQualifier = isMultiFolderWorkspace ? `:${workspace.name}` : "";
function simplifyPath(p: string): string {
// in windows, the drive letter can vary in casing for VSCode, so we gotta normalize that first
if (os.platform() === "win32") {
p = p.replace(/^[a-z]:\\/, (c) => c.toUpperCase());
}
p = normalizeDriveLetter(path.normalize(p));
// 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 p.replace(wsFolder, `\${workspaceFolder${workspaceQualifier}}`);
}
const executable = await getDebugExecutable(

View file

@ -299,3 +299,26 @@ export async function spawnAsync(
};
}
}
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;
}