From 9615a0eb08b23fc609291325fb9e393975017892 Mon Sep 17 00:00:00 2001 From: ronny fretel Date: Fri, 21 Feb 2025 19:29:05 +0100 Subject: [PATCH 1/2] implement caching for quick pick items and update on folder creation --- package.json | 4 +++- src/extension.ts | 57 ++++++++++++++++++++++++++++++++++++++++-------- 2 files changed, 51 insertions(+), 10 deletions(-) diff --git a/package.json b/package.json index a17abc3..e66e8aa 100644 --- a/package.json +++ b/package.json @@ -16,7 +16,9 @@ "Other" ], "activationEvents": [ - "onCommand:extension.advancedNewFile" + "onCommand:extension.advancedNewFile", + "onStartupFinished", + "workspaceContains:**/*" ], "main": "./out/src/extension", "contributes": { diff --git a/src/extension.ts b/src/extension.ts index 5ab85da..70f99cc 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -32,6 +32,10 @@ declare module 'vscode' { } } +let cache: Cache = null; + +const CACHE_EXPIRATION_MINUTES = 60; + function isFolderDescriptor(filepath: string): boolean { return filepath.charAt(filepath.length - 1) === path.sep; } @@ -315,12 +319,18 @@ export async function dirQuickPickItems( roots: WorkspaceRoot[], cache: Cache): Promise { - const dirOptions = await Promise.all( - roots.map(async r => await subdirOptionsForRoot(r)) - ); - let quickPickItems = - dirOptions.reduce(flatten).map(o => buildQuickPickItem(o)); - + let quickPickItems = []; + const cachedItems = cache.get('quickPickItems'); + if (cachedItems && cachedItems.expires > Date.now()) { + quickPickItems = [...cachedItems.data]; + }else{ + const dirOptions = await Promise.all( + roots.map(async r => await subdirOptionsForRoot(r)) + ); + quickPickItems = dirOptions.reduce(flatten).map(o => buildQuickPickItem(o)); + cache.put('quickPickItems', { data: [...quickPickItems], expires: Date.now() + 1000 * 60 * CACHE_EXPIRATION_MINUTES }); + } + quickPickItems.unshift(...convenienceOptions(roots, cache)); return quickPickItems; @@ -360,12 +370,11 @@ export function rootForDir( } export async function command(context: vscode.ExtensionContext) { + const roots = workspaceRoots(); if (roots.length > 0) { - const cacheName = roots.map(r => r.rootPath).join(';'); - const cache = new Cache(context, `workspace:${cacheName}`); - + const sortedRoots = sortRoots(roots, cache.get('recentRoots') || []); const dirSelection = @@ -393,12 +402,42 @@ export async function command(context: vscode.ExtensionContext) { } export function activate(context: vscode.ExtensionContext) { + const roots = workspaceRoots(); + const cacheName = roots.map(r => r.rootPath).join(';'); + cache = new Cache(context, `workspace:${cacheName}`); + + const folderWatcher = vscode.workspace.createFileSystemWatcher('**/*',false,true,true); + folderWatcher.onDidCreate((uri) => updateCache(uri)); + let disposable = vscode.commands.registerCommand( 'extension.advancedNewFile', () => command(context) ); context.subscriptions.push(disposable); + context.subscriptions.push(folderWatcher); } export function deactivate() { } + +function updateCache(uri: vscode.Uri){ + if(!cache) return; + if(!fs.lstatSync(uri.path).isDirectory()) return; + + const cachedItems = cache.get('quickPickItems'); + if (cachedItems && cachedItems.data) { + const quickPickItems =[...cachedItems.data]; + const rootPath = workspaceRoots().find(r => uri.path.indexOf(r.rootPath) === 0); + const relativePath = uri.path.replace(rootPath.rootPath,''); + const newDir = buildQuickPickItem({ + displayText: relativePath, + fsLocation: { + relative: relativePath, + absolute: uri.path + } + }); + quickPickItems.push(newDir); + cachedItems.data = quickPickItems.sort((a,b) => a.label.localeCompare(b.label)); + cache.put('quickPickItems', cachedItems); + } +} From 8c449f8a3d8d89a2001672f3bb849db069586be0 Mon Sep 17 00:00:00 2001 From: ronny fretel Date: Fri, 21 Feb 2025 19:30:05 +0100 Subject: [PATCH 2/2] update tasks.json to version 2.0.0 and restructure npm task configuration --- .vscode/tasks.json | 43 ++++++++++++++++++++++++++++--------------- 1 file changed, 28 insertions(+), 15 deletions(-) diff --git a/.vscode/tasks.json b/.vscode/tasks.json index fb7f662..1c3bc6f 100644 --- a/.vscode/tasks.json +++ b/.vscode/tasks.json @@ -5,26 +5,39 @@ // ${fileDirname}: the current opened file's dirname // ${fileExtname}: the current opened file's extension // ${cwd}: the current working directory of the spawned process - // A task runner that calls a custom npm script that compiles the extension. { - "version": "0.1.0", - + "version": "2.0.0", // we want to run npm "command": "npm", - - // the command is a shell script - "isShellCommand": true, - - // show the output window only if unrecognized errors occur. - "showOutput": "silent", - // we run the custom script "compile" as defined in package.json - "args": ["run", "compile", "--loglevel", "silent"], - + "args": [ + "run", + "compile", + "--loglevel", + "silent" + ], // The tsc compiler is started in watching mode - "isWatching": true, - + "isBackground": true, // use the standard tsc in watch mode problem matcher to find compile problems in the output. - "problemMatcher": "$tsc-watch" + "problemMatcher": "$tsc-watch", + "tasks": [ + { + "label": "npm", + "type": "shell", + "command": "npm", + "args": [ + "run", + "compile", + "--loglevel", + "silent" + ], + "isBackground": true, + "problemMatcher": "$tsc-watch", + "group": { + "_id": "build", + "isDefault": false + } + } + ] } \ No newline at end of file