From a62879bb46c84d0936798cf06debf5f92b04e4b4 Mon Sep 17 00:00:00 2001 From: Matt Bierner Date: Sun, 13 Nov 2022 15:03:05 -0800 Subject: [PATCH] Try computing windows paths correctly on markdown drop (#166077) Fixes #165352 --- .../src/languageFeatures/dropIntoEditor.ts | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/extensions/markdown-language-features/src/languageFeatures/dropIntoEditor.ts b/extensions/markdown-language-features/src/languageFeatures/dropIntoEditor.ts index 92916dba9ba..6cdf2b00174 100644 --- a/extensions/markdown-language-features/src/languageFeatures/dropIntoEditor.ts +++ b/extensions/markdown-language-features/src/languageFeatures/dropIntoEditor.ts @@ -80,9 +80,7 @@ export function createUriListSnippet(document: vscode.TextDocument, uris: readon const snippet = new vscode.SnippetString(); uris.forEach((uri, i) => { - const mdPath = dir && dir.scheme === uri.scheme && dir.authority === uri.authority - ? encodeURI(path.posix.relative(dir.path, uri.path)) - : uri.toString(false); + const mdPath = getMdPath(dir, uri); const ext = URI.Utils.extname(uri).toLowerCase().replace('.', ''); const insertAsImage = typeof options?.insertAsImage === 'undefined' ? imageFileExtensions.has(ext) : !!options.insertAsImage; @@ -102,6 +100,21 @@ export function createUriListSnippet(document: vscode.TextDocument, uris: readon return snippet; } +function getMdPath(dir: vscode.Uri | undefined, file: vscode.Uri) { + if (dir && dir.scheme === file.scheme && dir.authority === file.authority) { + if (file.scheme === Schemes.file) { + // On windows, we must use the native `path.resolve` to generate the relative path + // so that drive-letters are resolved cast insensitively. However we then want to + // convert back to a posix path to insert in to the document. + return encodeURI(path.posix.normalize(path.relative(dir.fsPath, file.fsPath))); + } + + return encodeURI(path.posix.relative(dir.path, file.path)); + } + + return file.toString(false); +} + function getDocumentDir(document: vscode.TextDocument): vscode.Uri | undefined { const docUri = getParentDocumentUri(document); if (docUri.scheme === Schemes.untitled) {