Skip to content

Commit 1d82f31

Browse files
authored
Fix relative path for inserted url() (microsoft#224194)
For microsoft#224190
1 parent e9e6eb4 commit 1d82f31

File tree

1 file changed

+9
-10
lines changed

1 file changed

+9
-10
lines changed

extensions/css-language-features/client/src/dropOrPaste/dropOrPasteResource.ts

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ class DropOrPasteResourceProvider implements vscode.DocumentDropEditProvider, vs
2222
return;
2323
}
2424

25-
const snippet = await this.createUriListSnippet(uriList);
25+
const snippet = await this.createUriListSnippet(document.uri, uriList);
2626
if (!snippet || token.isCancellationRequested) {
2727
return;
2828
}
@@ -47,7 +47,7 @@ class DropOrPasteResourceProvider implements vscode.DocumentDropEditProvider, vs
4747
return;
4848
}
4949

50-
const snippet = await this.createUriListSnippet(uriList);
50+
const snippet = await this.createUriListSnippet(document.uri, uriList);
5151
if (!snippet || token.isCancellationRequested) {
5252
return;
5353
}
@@ -78,15 +78,15 @@ class DropOrPasteResourceProvider implements vscode.DocumentDropEditProvider, vs
7878
return new UriList(uris.map(uri => ({ uri, str: uri.toString(true) })));
7979
}
8080

81-
private async createUriListSnippet(uriList: UriList): Promise<{ readonly snippet: vscode.SnippetString; readonly label: string } | undefined> {
81+
private async createUriListSnippet(docUri: vscode.Uri, uriList: UriList): Promise<{ readonly snippet: vscode.SnippetString; readonly label: string } | undefined> {
8282
if (!uriList.entries.length) {
8383
return;
8484
}
8585

8686
const snippet = new vscode.SnippetString();
8787
for (let i = 0; i < uriList.entries.length; i++) {
8888
const uri = uriList.entries[i];
89-
const relativePath = getRelativePath(uri.uri);
89+
const relativePath = getRelativePath(getDocumentDir(docUri), uri.uri);
9090
const urlText = relativePath ?? uri.str;
9191

9292
snippet.appendText(`url(${urlText})`);
@@ -114,18 +114,17 @@ class DropOrPasteResourceProvider implements vscode.DocumentDropEditProvider, vs
114114
}
115115
}
116116

117-
function getRelativePath(file: vscode.Uri): string | undefined {
118-
const dir = getDocumentDir(file);
119-
if (dir && dir.scheme === file.scheme && dir.authority === file.authority) {
120-
if (file.scheme === Schemes.file) {
117+
function getRelativePath(fromFile: vscode.Uri | undefined, toFile: vscode.Uri): string | undefined {
118+
if (fromFile && fromFile.scheme === toFile.scheme && fromFile.authority === toFile.authority) {
119+
if (toFile.scheme === Schemes.file) {
121120
// On windows, we must use the native `path.relative` to generate the relative path
122121
// so that drive-letters are resolved cast insensitively. However we then want to
123122
// convert back to a posix path to insert in to the document
124-
const relativePath = path.relative(dir.fsPath, file.fsPath);
123+
const relativePath = path.relative(fromFile.fsPath, toFile.fsPath);
125124
return path.posix.normalize(relativePath.split(path.sep).join(path.posix.sep));
126125
}
127126

128-
return path.posix.relative(dir.path, file.path);
127+
return path.posix.relative(fromFile.path, toFile.path);
129128
}
130129

131130
return undefined;

0 commit comments

Comments
 (0)