Skip to content

Commit

Permalink
Fix relative path for inserted url()
Browse files Browse the repository at this point in the history
  • Loading branch information
mjbvz committed Jul 29, 2024
1 parent b755f23 commit de3c6e2
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

import * as path from 'path';
import * as vscode from 'vscode';
import { getDocumentDir, Mimes, Schemes } from './shared';
import { Mimes, Schemes } from './shared';
import { UriList } from './uriList';

class DropOrPasteResourceProvider implements vscode.DocumentDropEditProvider, vscode.DocumentPasteEditProvider {
Expand Down Expand Up @@ -115,17 +115,17 @@ class DropOrPasteResourceProvider implements vscode.DocumentDropEditProvider, vs
}

function getRelativePath(file: vscode.Uri): string | undefined {
const dir = getDocumentDir(file);
if (dir && dir.scheme === file.scheme && dir.authority === file.authority) {
const workspaceUri = vscode.workspace.getWorkspaceFolder(file)?.uri;
if (workspaceUri && workspaceUri.scheme === file.scheme && workspaceUri.authority === file.authority) {
if (file.scheme === Schemes.file) {
// On windows, we must use the native `path.relative` 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
const relativePath = path.relative(dir.fsPath, file.fsPath);
const relativePath = path.relative(workspaceUri.fsPath, file.fsPath);
return path.posix.normalize(relativePath.split(path.sep).join(path.posix.sep));
}

return path.posix.relative(dir.path, file.path);
return path.posix.relative(workspaceUri.path, file.path);
}

return undefined;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
*--------------------------------------------------------------------------------------------*/

import * as vscode from 'vscode';
import { Utils } from 'vscode-uri';

export const Schemes = Object.freeze({
file: 'file',
Expand All @@ -17,16 +16,7 @@ export const Mimes = Object.freeze({
uriList: 'text/uri-list',
});


export function getDocumentDir(uri: vscode.Uri): vscode.Uri | undefined {
const docUri = getParentDocumentUri(uri);
if (docUri.scheme === Schemes.untitled) {
return vscode.workspace.workspaceFolders?.[0]?.uri;
}
return Utils.dirname(docUri);
}

function getParentDocumentUri(uri: vscode.Uri): vscode.Uri {
export function getParentDocumentUri(uri: vscode.Uri): vscode.Uri {
if (uri.scheme === Schemes.notebookCell) {
// is notebook documents necessary?
for (const notebook of vscode.workspace.notebookDocuments) {
Expand Down

0 comments on commit de3c6e2

Please sign in to comment.