Skip to content

Commit 49f3bcb

Browse files
author
Jakob Werner
committed
Introduced caching
1 parent b6735df commit 49f3bcb

File tree

1 file changed

+75
-14
lines changed

1 file changed

+75
-14
lines changed

src/extension.ts

Lines changed: 75 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// Import the module and reference it with the alias vscode in your code below
33
import * as vscode from 'vscode';
44
var path = require('path');
5-
var glob = require('glob');
5+
var Glob = require('glob').Glob;
66

77
// this method is called when your extension is activated
88
// your extension is activated the very first time the command is executed
@@ -11,28 +11,91 @@ export function activate(context: vscode.ExtensionContext) {
1111
// Use the console to output diagnostic information (console.log) and errors (console.error)
1212
// This line of code will only be executed once when your extension is activated
1313
console.log('The extension "RelativePath" is now active!');
14+
const workspacePath: string = vscode.workspace.rootPath.replace(/\\/g, "/");
15+
var editor = vscode.window.activeTextEditor;
16+
let items: string[] = null;
17+
var myGlob = null;
18+
let paused: boolean = false;
19+
20+
function myActivate() {
21+
22+
// Show loading info box
23+
let info = vscode.window.showQuickPick([], { matchOnDescription: false, placeHolder: "Finding files... Please wait. (Press escape to cancel)" });
24+
info.then(
25+
(value?: string) => {
26+
myGlob.pause();
27+
paused = true;
28+
},
29+
(rejected?: any) => {
30+
myGlob.pause();
31+
paused = true;
32+
}
33+
);
34+
35+
//Search for files
36+
if (paused) {
37+
paused = false;
38+
myGlob.resume();
39+
} else {
40+
myGlob = new Glob(workspacePath + "/**/*.*",
41+
{ ignore: ["**/node_modules/**","**/*.dll","**/obj/**","**/objd/**"] },
42+
function(err, files) {
43+
if (err) {
44+
return;
45+
}
46+
47+
items = files;
48+
vscode.commands.executeCommand('extension.relativePath');
49+
});
50+
}
51+
}
52+
53+
// Initialize activation
54+
myActivate();
55+
56+
// Watch for file system changes - as we're caching the searched files
57+
let watcher: vscode.FileSystemWatcher = vscode.workspace.createFileSystemWatcher("**/*.*");
58+
watcher.ignoreChangeEvents = true;
59+
60+
// Add a file on creation
61+
watcher.onDidCreate((e: vscode.Uri) => {
62+
items.push(e.fsPath.replace(/\\/g, "/"));
63+
});
64+
65+
// Remove a file on deletion
66+
watcher.onDidDelete((e: vscode.Uri) => {
67+
let item = e.fsPath.replace(/\\/g, "/");
68+
let index = items.indexOf(item);
69+
if (index > -1) {
70+
items.splice(index, 1);
71+
}
72+
});
1473

1574
// The command has been defined in the package.json file
1675
// Now provide the implementation of the command with registerCommand
1776
// The commandId parameter must match the command field in package.json
1877
var disposable = vscode.commands.registerCommand('extension.relativePath', () => {
1978
// The code you place here will be executed every time your command is executed
79+
80+
// If there's no file opened
2081
var editor = vscode.window.activeTextEditor;
2182
if (!editor) {
22-
vscode.window.showInformationMessage("You need to have a file loaded.");
83+
vscode.window.showInformationMessage("You need to have a file opened.");
2384
return; // No open text editor
2485
}
2586

26-
const workspacePath: string = vscode.workspace.rootPath.replace(/\\/g, "/");
27-
console.log(workspacePath);
28-
glob(workspacePath + "/**/*.*", { ignore: ["**/node_modules/**","**/*.dll","**/obj/**","**/objd/**"] }, function(err, files) {
29-
if (err) {
30-
return;
31-
}
32-
33-
console.log(files.length);
34-
showQuickPick(files);
35-
});
87+
// If we canceled the file search
88+
if (paused) {
89+
myActivate();
90+
return;
91+
}
92+
93+
// If there are no items found
94+
if (!items) {
95+
return;
96+
}
97+
98+
showQuickPick(items);
3699

37100
// Show dropdown editor
38101
function showQuickPick(items: string[]): void {
@@ -54,9 +117,7 @@ export function activate(context: vscode.ExtensionContext) {
54117
function returnRelativeLink(item: vscode.QuickPickItem): void {
55118
if (item) {
56119
const targetPath = item.description;
57-
console.log(targetPath);
58120
const currentItemPath = editor.document.fileName.replace(/\\/g,"/").replace(workspacePath, "");
59-
console.log(currentItemPath);
60121
let relativeUrl: string = path.relative(currentItemPath, targetPath).replace(".", "").replace(/\\/g,"/");
61122
vscode.window.activeTextEditor.edit(
62123
(editBuilder: vscode.TextEditorEdit) => {

0 commit comments

Comments
 (0)