Skip to content

Commit bc34419

Browse files
Added options to exclude filename extension and to exclude leading dot
1 parent be25a86 commit bc34419

File tree

3 files changed

+141
-133
lines changed

3 files changed

+141
-133
lines changed

.vscode/launch.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
"args": ["--extensionDevelopmentPath=${workspaceRoot}" ],
1111
"stopOnEntry": false,
1212
"sourceMaps": true,
13-
"outDir": "out/src",
13+
"outDir": "${workspaceRoot}/out/src",
1414
"preLaunchTask": "npm"
1515
},
1616
{
@@ -21,7 +21,7 @@
2121
"args": ["--extensionDevelopmentPath=${workspaceRoot}", "--extensionTestsPath=${workspaceRoot}/out/test" ],
2222
"stopOnEntry": false,
2323
"sourceMaps": true,
24-
"outDir": "out/test",
24+
"outDir": "${workspaceRoot}/out/test",
2525
"preLaunchTask": "npm"
2626
}
2727
]

src/extension.ts

Lines changed: 138 additions & 130 deletions
Original file line numberDiff line numberDiff line change
@@ -1,138 +1,146 @@
11
// The module 'vscode' contains the VS Code extensibility API
22
// Import the module and reference it with the alias vscode in your code below
3-
import * as vscode from 'vscode';
4-
var path = require('path');
5-
var Glob = require('glob').Glob;
3+
import * as vscode from "vscode";
4+
let path = require("path");
5+
let 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
99
export function activate(context: vscode.ExtensionContext) {
1010

11-
// Use the console to output diagnostic information (console.log) and errors (console.error)
12-
// This line of code will only be executed once when your extension is activated
13-
console.log('The extension "RelativePath" is now active!');
14-
const workspacePath: string = vscode.workspace.rootPath.replace(/\\/g, "/");
15-
var configuration = vscode.workspace.getConfiguration("relativePath");
16-
var editor = vscode.window.activeTextEditor;
17-
let emptyItem: vscode.QuickPickItem = { label: "", description: "No files found" };
18-
let items: string[] = null;
19-
var myGlob = null;
20-
let paused: boolean = false;
21-
22-
function myActivate() {
23-
24-
// Show loading info box
25-
let info = vscode.window.showQuickPick([emptyItem], { matchOnDescription: false, placeHolder: "Finding files... Please wait. (Press escape to cancel)" });
26-
info.then(
27-
(value?: any) => {
28-
myGlob.pause();
29-
paused = true;
30-
},
31-
(rejected?: any) => {
32-
myGlob.pause();
33-
paused = true;
34-
}
35-
);
36-
37-
//Search for files
38-
if (paused) {
39-
paused = false;
40-
myGlob.resume();
41-
} else {
42-
myGlob = new Glob(workspacePath + "/**/*.*",
43-
{ ignore: configuration.get("ignore") },
44-
function(err, files) {
45-
if (err) {
46-
return;
47-
}
48-
49-
items = files;
50-
vscode.commands.executeCommand('extension.relativePath');
51-
});
52-
myGlob.on("end", function() {
53-
paused = false;
54-
})
55-
}
56-
}
57-
58-
// Initialize activation
59-
myActivate();
60-
61-
// Watch for file system changes - as we're caching the searched files
62-
let watcher: vscode.FileSystemWatcher = vscode.workspace.createFileSystemWatcher("**/*.*");
63-
watcher.ignoreChangeEvents = true;
64-
65-
// Add a file on creation
66-
watcher.onDidCreate((e: vscode.Uri) => {
67-
items.push(e.fsPath.replace(/\\/g, "/"));
68-
});
69-
70-
// Remove a file on deletion
71-
watcher.onDidDelete((e: vscode.Uri) => {
72-
let item = e.fsPath.replace(/\\/g, "/");
73-
let index = items.indexOf(item);
74-
if (index > -1) {
75-
items.splice(index, 1);
76-
}
77-
});
78-
79-
// The command has been defined in the package.json file
80-
// Now provide the implementation of the command with registerCommand
81-
// The commandId parameter must match the command field in package.json
82-
var disposable = vscode.commands.registerCommand('extension.relativePath', () => {
83-
// The code you place here will be executed every time your command is executed
84-
85-
// If there's no file opened
86-
var editor = vscode.window.activeTextEditor;
87-
if (!editor) {
88-
vscode.window.showInformationMessage("You need to have a file opened.");
89-
return; // No open text editor
90-
}
91-
92-
// If we canceled the file search
93-
if (paused) {
94-
myActivate();
95-
return;
96-
}
97-
98-
// If there are no items found
99-
if (!items) {
100-
return;
101-
}
102-
103-
showQuickPick(items);
104-
105-
// Show dropdown editor
106-
function showQuickPick(items: string[]): void {
107-
if (items) {
108-
let paths: vscode.QuickPickItem[] = items.map((val: string) => {
109-
let item: vscode.QuickPickItem = { description: val.replace(workspacePath, ""), label: val.split("/").pop() };
110-
return item;
111-
});
112-
113-
let pickResult: Thenable<vscode.QuickPickItem>;
114-
pickResult = vscode.window.showQuickPick(paths, { matchOnDescription: true, placeHolder: "Filename" });
115-
pickResult.then(returnRelativeLink);
116-
} else {
117-
vscode.window.showInformationMessage("No files to show.");
118-
}
119-
}
120-
121-
// Get the picked item
122-
function returnRelativeLink(item: vscode.QuickPickItem): void {
123-
if (item) {
124-
const targetPath = item.description;
125-
const currentItemPath = editor.document.fileName.replace(/\\/g,"/").replace(workspacePath, "");
126-
let relativeUrl: string = path.relative(currentItemPath, targetPath).replace(".", "").replace(/\\/g,"/");
127-
vscode.window.activeTextEditor.edit(
128-
(editBuilder: vscode.TextEditorEdit) => {
129-
let position: vscode.Position = vscode.window.activeTextEditor.selection.end;
130-
editBuilder.insert(position, relativeUrl);
131-
}
132-
);
133-
}
134-
}
135-
});
136-
137-
context.subscriptions.push(disposable);
11+
// Use the console to output diagnostic information (console.log) and errors (console.error)
12+
// This line of code will only be executed once when your extension is activated
13+
console.log("The extension \"RelativePath\" is now active!");
14+
const workspacePath: string = vscode.workspace.rootPath.replace(/\\/g, "/");
15+
let configuration: any = vscode.workspace.getConfiguration("relativePath");
16+
let editor = vscode.window.activeTextEditor;
17+
let emptyItem: vscode.QuickPickItem = { label: "", description: "No files found" };
18+
let items: string[] = null;
19+
let myGlob = null;
20+
let paused: boolean = false;
21+
22+
function myActivate() {
23+
24+
// Show loading info box
25+
let info = vscode.window.showQuickPick([emptyItem], { matchOnDescription: false, placeHolder: "Finding files... Please wait. (Press escape to cancel)" });
26+
info.then(
27+
(value?: any) => {
28+
myGlob.pause();
29+
paused = true;
30+
},
31+
(rejected?: any) => {
32+
myGlob.pause();
33+
paused = true;
34+
}
35+
);
36+
37+
// Search for files
38+
if (paused) {
39+
paused = false;
40+
myGlob.resume();
41+
} else {
42+
myGlob = new Glob(workspacePath + "/**/*.*",
43+
{ ignore: configuration.get("ignore") },
44+
function(err, files) {
45+
if (err) {
46+
return;
47+
}
48+
49+
items = files;
50+
vscode.commands.executeCommand("extension.relativePath");
51+
});
52+
myGlob.on("end", function() {
53+
paused = false;
54+
});
55+
}
56+
}
57+
58+
// Initialize activation
59+
myActivate();
60+
61+
// Watch for file system changes - as we're caching the searched files
62+
let watcher: vscode.FileSystemWatcher = vscode.workspace.createFileSystemWatcher("**/*.*");
63+
watcher.ignoreChangeEvents = true;
64+
65+
// Add a file on creation
66+
watcher.onDidCreate((e: vscode.Uri) => {
67+
items.push(e.fsPath.replace(/\\/g, "/"));
68+
});
69+
70+
// Remove a file on deletion
71+
watcher.onDidDelete((e: vscode.Uri) => {
72+
let item = e.fsPath.replace(/\\/g, "/");
73+
let index = items.indexOf(item);
74+
if (index > -1) {
75+
items.splice(index, 1);
76+
}
77+
});
78+
79+
// The command has been defined in the package.json file
80+
// Now provide the implementation of the command with registerCommand
81+
// The commandId parameter must match the command field in package.json
82+
let disposable = vscode.commands.registerCommand("extension.relativePath", () => {
83+
// The code you place here will be executed every time your command is executed
84+
85+
// If there's no file opened
86+
let editor = vscode.window.activeTextEditor;
87+
if (!editor) {
88+
vscode.window.showInformationMessage("You need to have a file opened.");
89+
return; // No open text editor
90+
}
91+
92+
// If we canceled the file search
93+
if (paused) {
94+
myActivate();
95+
return;
96+
}
97+
98+
// If there are no items found
99+
if (!items) {
100+
return;
101+
}
102+
103+
showQuickPick(items);
104+
105+
// Show dropdown editor
106+
function showQuickPick(items: string[]): void {
107+
if (items) {
108+
let paths: vscode.QuickPickItem[] = items.map((val: string) => {
109+
let item: vscode.QuickPickItem = { description: val.replace(workspacePath, ""), label: val.split("/").pop() };
110+
return item;
111+
});
112+
113+
let pickResult: Thenable<vscode.QuickPickItem>;
114+
pickResult = vscode.window.showQuickPick(paths, { matchOnDescription: true, placeHolder: "Filename" });
115+
pickResult.then(returnRelativeLink);
116+
} else {
117+
vscode.window.showInformationMessage("No files to show.");
118+
}
119+
}
120+
121+
// Get the picked item
122+
function returnRelativeLink(item: vscode.QuickPickItem): void {
123+
if (item) {
124+
const targetPath = item.description;
125+
const currentItemPath = editor.document.fileName.replace(/\\/g, "/").replace(workspacePath, "");
126+
let relativeUrl: string = path.relative(currentItemPath, targetPath).replace(".", "").replace(/\\/g, "/");
127+
128+
if (configuration.removeExtension) {
129+
relativeUrl = relativeUrl.substring(0, relativeUrl.lastIndexOf("."));
130+
}
131+
if (configuration.removeLeadingDot && relativeUrl.startsWith("./../")) {
132+
relativeUrl = relativeUrl.substring(2, relativeUrl.length);
133+
}
134+
135+
vscode.window.activeTextEditor.edit(
136+
(editBuilder: vscode.TextEditorEdit) => {
137+
let position: vscode.Position = vscode.window.activeTextEditor.selection.end;
138+
editBuilder.insert(position, relativeUrl);
139+
}
140+
);
141+
}
142+
}
143+
});
144+
145+
context.subscriptions.push(disposable);
138146
}

tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"compilerOptions": {
33
"module": "commonjs",
4-
"target": "ES5",
4+
"target": "es5",
55
"outDir": "out",
66
"noLib": true,
77
"sourceMap": true

0 commit comments

Comments
 (0)