Skip to content

Commit 909d210

Browse files
authored
Merge pull request #19578 from clouds56-contrib/drive-letter
add normalizeDriveLetter
2 parents 4d6f9e7 + 0c13a94 commit 909d210

File tree

3 files changed

+35
-12
lines changed

3 files changed

+35
-12
lines changed

editors/code/src/config.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import * as Is from "vscode-languageclient/lib/common/utils/is";
22
import * as os from "os";
33
import * as path from "path";
44
import * as vscode from "vscode";
5-
import { expectNotUndefined, log, unwrapUndefinable } from "./util";
5+
import { expectNotUndefined, log, normalizeDriveLetter, unwrapUndefinable } from "./util";
66
import type { Env } from "./util";
77
import type { Disposable } from "vscode";
88

@@ -498,7 +498,7 @@ function computeVscodeVar(varName: string): string | null {
498498
// user has opened on Editor startup. Could lead to
499499
// unpredictable workspace selection in practice.
500500
// It's better to pick the first one
501-
folder.uri.fsPath;
501+
normalizeDriveLetter(folder.uri.fsPath);
502502
return fsPath;
503503
};
504504
// https://code.visualstudio.com/docs/editor/variables-reference

editors/code/src/debug.ts

+4-10
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import type * as ra from "./lsp_ext";
66
import { Cargo } from "./toolchain";
77
import type { Ctx } from "./ctx";
88
import { createTaskFromRunnable, prepareEnv } from "./run";
9-
import { execute, isCargoRunnableArgs, unwrapUndefinable, log } from "./util";
9+
import { execute, isCargoRunnableArgs, unwrapUndefinable, log, normalizeDriveLetter } from "./util";
1010
import type { Config } from "./config";
1111

1212
// Here we want to keep track on everything that's currently running
@@ -127,20 +127,14 @@ async function getDebugConfiguration(
127127
firstWorkspace;
128128

129129
const workspace = unwrapUndefinable(maybeWorkspace);
130-
let wsFolder = path.normalize(workspace.uri.fsPath);
131-
if (os.platform() === "win32") {
132-
// in windows, the drive letter can vary in casing for VSCode, so we gotta normalize that first
133-
wsFolder = wsFolder.replace(/^[a-z]:\\/, (c) => c.toUpperCase());
134-
}
130+
const wsFolder = normalizeDriveLetter(path.normalize(workspace.uri.fsPath));
135131

136132
const workspaceQualifier = isMultiFolderWorkspace ? `:${workspace.name}` : "";
137133
function simplifyPath(p: string): string {
138134
// in windows, the drive letter can vary in casing for VSCode, so we gotta normalize that first
139-
if (os.platform() === "win32") {
140-
p = p.replace(/^[a-z]:\\/, (c) => c.toUpperCase());
141-
}
135+
p = normalizeDriveLetter(path.normalize(p));
142136
// see https://github.com/rust-lang/rust-analyzer/pull/5513#issuecomment-663458818 for why this is needed
143-
return path.normalize(p).replace(wsFolder, `\${workspaceFolder${workspaceQualifier}}`);
137+
return p.replace(wsFolder, `\${workspaceFolder${workspaceQualifier}}`);
144138
}
145139

146140
const executable = await getDebugExecutable(

editors/code/src/util.ts

+29
Original file line numberDiff line numberDiff line change
@@ -299,3 +299,32 @@ export async function spawnAsync(
299299
};
300300
}
301301
}
302+
303+
export const isWindows = process.platform === "win32";
304+
305+
export function isWindowsDriveLetter(code: number): boolean {
306+
// Copied from https://github.com/microsoft/vscode/blob/02c2dba5f2669b924fd290dff7d2ff3460791996/src/vs/base/common/extpath.ts#L265-L267
307+
return (
308+
(code >= /* CharCode.A */ 65 && code <= /* CharCode.Z */ 90) ||
309+
(code >= /* CharCode.a */ 97 && code <= /* CharCode.z */ 122)
310+
);
311+
}
312+
export function hasDriveLetter(path: string, isWindowsOS: boolean = isWindows): boolean {
313+
// Copied from https://github.com/microsoft/vscode/blob/02c2dba5f2669b924fd290dff7d2ff3460791996/src/vs/base/common/extpath.ts#L324-L330
314+
if (isWindowsOS) {
315+
return (
316+
isWindowsDriveLetter(path.charCodeAt(0)) &&
317+
path.charCodeAt(1) === /* CharCode.Colon */ 58
318+
);
319+
}
320+
321+
return false;
322+
}
323+
export function normalizeDriveLetter(path: string, isWindowsOS: boolean = isWindows): string {
324+
// Copied from https://github.com/microsoft/vscode/blob/02c2dba5f2669b924fd290dff7d2ff3460791996/src/vs/base/common/labels.ts#L140-L146
325+
if (hasDriveLetter(path, isWindowsOS)) {
326+
return path.charAt(0).toUpperCase() + path.slice(1);
327+
}
328+
329+
return path;
330+
}

0 commit comments

Comments
 (0)