From 87807e9096d3dcc5cbafaa63040e0feec6d25ab3 Mon Sep 17 00:00:00 2001 From: Paul de Raaij Date: Fri, 11 Oct 2024 19:47:00 +0200 Subject: [PATCH] Process review comments --- package-lock.json | 14 ++++++++++- .../foam-vscode/src/core/model/workspace.ts | 25 ++++++++----------- yarn.lock | 7 +++--- 3 files changed, 27 insertions(+), 19 deletions(-) diff --git a/package-lock.json b/package-lock.json index 42b35216f..b353ce16e 100644 --- a/package-lock.json +++ b/package-lock.json @@ -21729,6 +21729,11 @@ "url": "https://opencollective.com/js-sdsl" } }, + "node_modules/js-sha1": { + "version": "0.7.0", + "resolved": "https://registry.npmjs.org/js-sha1/-/js-sha1-0.7.0.tgz", + "integrity": "sha512-oQZ1Mo7440BfLSv9TX87VNEyU52pXPVG19F9PL3gTgNt0tVxlZ8F4O6yze3CLuLx28TxotxvlyepCNaaV0ZjMw==" + }, "node_modules/js-tokens": { "version": "4.0.0", "resolved": "https://registry.npmjs.org/js-tokens/-/js-tokens-4.0.0.tgz", @@ -24517,6 +24522,11 @@ "tslib": "^2.0.3" } }, + "node_modules/path-browserify": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/path-browserify/-/path-browserify-1.0.1.tgz", + "integrity": "sha512-b7uo2UCUOYZcnF/3ID0lulOJi/bafxa1xPe7ZPsammBSpjSWQkjNxlt635YGS2MiR9GjvuXCtz2emr3jbsz98g==" + }, "node_modules/path-exists": { "version": "4.0.0", "resolved": "https://registry.npmjs.org/path-exists/-/path-exists-4.0.0.tgz", @@ -28296,17 +28306,19 @@ } }, "packages/foam-vscode": { - "version": "0.25.12", + "version": "0.26.0", "license": "MIT", "dependencies": { "dateformat": "4.5.1", "detect-newline": "^3.1.0", "github-slugger": "^1.4.0", "gray-matter": "^4.0.2", + "js-sha1": "^0.7.0", "lodash": "^4.17.21", "lru-cache": "^7.14.1", "markdown-it-regex": "^0.2.0", "mnemonist": "^0.39.8", + "path-browserify": "^1.0.1", "remark-frontmatter": "^2.0.0", "remark-parse": "^8.0.2", "remark-wiki-link": "^0.0.4", diff --git a/packages/foam-vscode/src/core/model/workspace.ts b/packages/foam-vscode/src/core/model/workspace.ts index 6fb502a32..00386879f 100644 --- a/packages/foam-vscode/src/core/model/workspace.ts +++ b/packages/foam-vscode/src/core/model/workspace.ts @@ -6,7 +6,7 @@ import { Emitter } from '../common/event'; import { ResourceProvider } from './provider'; import { IDisposable } from '../common/lifecycle'; import { IDataStore } from '../services/datastore'; -import { TrieMap } from 'mnemonist'; +import TrieMap from 'mnemonist/trie-map'; export class FoamWorkspace implements IDisposable { private onDidAddEmitter = new Emitter(); @@ -34,10 +34,9 @@ export class FoamWorkspace implements IDisposable { set(resource: Resource) { const old = this.find(resource.uri); - const normalizedPath = normalize(resource.uri.path); // store resource - this._resources.set(this.getReversedIdentifier(normalizedPath), resource); + this._resources.set(this.getTrieIdentifier(resource.uri.path), resource); isSome(old) ? this.onDidUpdateEmitter.fire({ old: old, new: resource }) @@ -46,8 +45,8 @@ export class FoamWorkspace implements IDisposable { } delete(uri: URI) { - const deleted = this._resources.get(this.getReversedIdentifier(uri)); - this._resources.delete(this.getReversedIdentifier(uri)); + const deleted = this._resources.get(this.getTrieIdentifier(uri)); + this._resources.delete(this.getTrieIdentifier(uri)); isSome(deleted) && this.onDidDeleteEmitter.fire(deleted); return deleted ?? null; @@ -79,19 +78,17 @@ export class FoamWorkspace implements IDisposable { } public listByIdentifier(identifier: string): Resource[] { - let needle = this.getReversedIdentifier(identifier); + let needle = this.getTrieIdentifier(identifier); const mdNeedle = getExtension(normalize(identifier)) !== this.defaultExtension - ? this.getReversedIdentifier(identifier + this.defaultExtension) + ? this.getTrieIdentifier(identifier + this.defaultExtension) : undefined; const resources: Resource[] = []; this._resources.find(needle).forEach(elm => { - if (elm[0].indexOf(getExtension(normalize(identifier))) > 0) { - resources.push(elm[1]); - } + resources.push(elm[1]); }); if (mdNeedle) { this._resources.find(mdNeedle).forEach(elm => resources.push(elm[1])); @@ -141,7 +138,7 @@ export class FoamWorkspace implements IDisposable { * * @param reference the URI path to reverse */ - private getReversedIdentifier(reference: URI | string): string { + private getTrieIdentifier(reference: URI | string): string { let path: string; if (reference instanceof URI) { path = (reference as URI).path; @@ -160,7 +157,7 @@ export class FoamWorkspace implements IDisposable { public find(reference: URI | string, baseUri?: URI): Resource | null { if (reference instanceof URI) { - return this._resources.get(this.getReversedIdentifier(reference)) ?? null; + return this._resources.get(this.getTrieIdentifier(reference)) ?? null; } let resource: Resource | null = null; const [path, fragment] = (reference as string).split('#'); @@ -174,9 +171,7 @@ export class FoamWorkspace implements IDisposable { : isSome(baseUri) ? baseUri.resolve(candidate).path : null; - resource = this._resources.get( - normalize(searchKey).split('/').reverse().join('/') - ); + resource = this._resources.get(this.getTrieIdentifier(searchKey)); if (resource) { break; } diff --git a/yarn.lock b/yarn.lock index 08a9b5fbf..02572d698 100644 --- a/yarn.lock +++ b/yarn.lock @@ -5422,7 +5422,7 @@ flatted@^3.1.0: integrity sha512-5nqDSxl8nn5BSNxyR3n4I6eDmbolI6WT+QqR547RwxQapgjQBmtktdP+HTBb/a/zLsbzERTONyUB5pefh5TtjQ== "foam-vscode@file:/Users/paulderaaij/projects/foam/packages/foam-vscode": - version "0.25.12" + version "0.26.0" resolved "file:packages/foam-vscode" dependencies: dateformat "4.5.1" @@ -5432,6 +5432,7 @@ flatted@^3.1.0: lodash "^4.17.21" lru-cache "^7.14.1" markdown-it-regex "^0.2.0" + mnemonist "^0.39.8" remark-frontmatter "^2.0.0" remark-parse "^8.0.2" remark-wiki-link "^0.0.4" @@ -7441,7 +7442,7 @@ js-sdsl@^4.1.4: js-sha1@^0.7.0: version "0.7.0" - resolved "https://registry.yarnpkg.com/js-sha1/-/js-sha1-0.7.0.tgz#fecaf5f36bb09a51b01da46b43a207c8452c9c1e" + resolved "https://registry.npmjs.org/js-sha1/-/js-sha1-0.7.0.tgz" integrity sha512-oQZ1Mo7440BfLSv9TX87VNEyU52pXPVG19F9PL3gTgNt0tVxlZ8F4O6yze3CLuLx28TxotxvlyepCNaaV0ZjMw== "js-tokens@^3.0.0 || ^4.0.0", js-tokens@^4.0.0: @@ -8845,7 +8846,7 @@ pascal-case@^3.1.2: path-browserify@^1.0.1: version "1.0.1" - resolved "https://registry.yarnpkg.com/path-browserify/-/path-browserify-1.0.1.tgz#d98454a9c3753d5790860f16f68867b9e46be1fd" + resolved "https://registry.npmjs.org/path-browserify/-/path-browserify-1.0.1.tgz" integrity sha512-b7uo2UCUOYZcnF/3ID0lulOJi/bafxa1xPe7ZPsammBSpjSWQkjNxlt635YGS2MiR9GjvuXCtz2emr3jbsz98g== path-exists@^3.0.0: