From e7ee143544b986e70a1e9e322da52dd2e4eac7a7 Mon Sep 17 00:00:00 2001 From: Riccardo Date: Tue, 12 Nov 2024 22:51:59 +0100 Subject: [PATCH] Improved URI handling for virtual FS (#1409) * Improved URI handling for virtual FS * Ensure virtual filesystem is accepted as mdSelector --------- Co-authored-by: Paul de Raaij --- packages/foam-vscode/src/core/model/uri.ts | 22 +++++++++++++++------ packages/foam-vscode/src/services/editor.ts | 5 +++-- 2 files changed, 19 insertions(+), 8 deletions(-) diff --git a/packages/foam-vscode/src/core/model/uri.ts b/packages/foam-vscode/src/core/model/uri.ts index 16010d1c0..1c8d95b31 100644 --- a/packages/foam-vscode/src/core/model/uri.ts +++ b/packages/foam-vscode/src/core/model/uri.ts @@ -381,10 +381,20 @@ function encodeURIComponentMinimal(path: string): string { * TODO this probably needs to be moved to the workspace service */ export function asAbsoluteUri(uri: URI, baseFolders: URI[]): URI { - return URI.file( - pathUtils.asAbsolutePaths( - uri.path, - baseFolders.map(f => f.path) - )[0] - ); + const path = uri.path; + if (pathUtils.isAbsolute(path)) { + return uri; + } + let tokens = path.split('/'); + const firstDir = tokens[0]; + if (baseFolders.length > 1) { + for (const folder of baseFolders) { + const lastDir = folder.path.split('/').pop(); + if (lastDir === firstDir) { + tokens = tokens.slice(1); + return folder.joinPath(...tokens); + } + } + } + return baseFolders[0].joinPath(...tokens); } diff --git a/packages/foam-vscode/src/services/editor.ts b/packages/foam-vscode/src/services/editor.ts index 5ff0ed19d..7edf810b9 100644 --- a/packages/foam-vscode/src/services/editor.ts +++ b/packages/foam-vscode/src/services/editor.ts @@ -54,6 +54,7 @@ export function formatMarkdownTooltip(content: string): MarkdownString { export const mdDocSelector = [ { language: 'markdown', scheme: 'file' }, + { language: 'markdown', scheme: 'vscode-vfs' }, { language: 'markdown', scheme: 'untitled' }, ]; @@ -219,9 +220,9 @@ export async function createMatcherAndDataStore(excludes: string[]): Promise<{ let files: Uri[] = []; for (const folder of workspace.workspaceFolders) { const uris = await workspace.findFiles( - new RelativePattern(folder.uri.path, '**/*'), + new RelativePattern(folder.uri, '**/*'), new RelativePattern( - folder.uri.path, + folder.uri, `{${excludePatterns.get(folder.name).join(',')}}` ) );