Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 28 additions & 15 deletions .vscode/tasks.json
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
]
}
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@
"Other"
],
"activationEvents": [
"onCommand:extension.advancedNewFile"
"onCommand:extension.advancedNewFile",
"onStartupFinished",
"workspaceContains:**/*"
],
"main": "./out/src/extension",
"contributes": {
Expand Down
57 changes: 48 additions & 9 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -315,12 +319,18 @@ export async function dirQuickPickItems(
roots: WorkspaceRoot[],
cache: Cache): Promise<vscode.QuickPickItem[]> {

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;
Expand Down Expand Up @@ -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 =
Expand Down Expand Up @@ -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);
}
}