Skip to content

Commit 75bb9e0

Browse files
author
Jakob Werner
committed
Merge pull request #5 from sebasrodriguez/master
Added options to exclude filename extension and to exclude leading dot
2 parents be25a86 + 6d83c45 commit 75bb9e0

File tree

4 files changed

+152
-135
lines changed

4 files changed

+152
-135
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
]

package.json

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
"publisher": "jakob101",
1010
"galleryBanner": {
1111
"color": "#373277",
12-
"theme": "dark"
12+
"theme": "dark"
1313
},
1414
"engines": {
1515
"vscode": "^0.10.1"
@@ -45,7 +45,17 @@
4545
"items": {
4646
"type": "string"
4747
}
48-
}
48+
},
49+
"relativePath.removeExtension": {
50+
"type": "boolean",
51+
"default": false,
52+
"description": "Excludes the extension from the relative path url (Useful for systemjs imports)."
53+
},
54+
"relativePath.removeLeadingDot": {
55+
"type": "boolean",
56+
"default": false,
57+
"description": "Removes the leading ./ character when the path is pointing to a parent folder."
58+
}
4959
}
5060
}
5161
},

src/extension.ts

Lines changed: 137 additions & 130 deletions
Original file line numberDiff line numberDiff line change
@@ -1,138 +1,145 @@
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("**/*.*", false, true, false);
63+
64+
// Add a file on creation
65+
watcher.onDidCreate((e: vscode.Uri) => {
66+
items.push(e.fsPath.replace(/\\/g, "/"));
67+
});
68+
69+
// Remove a file on deletion
70+
watcher.onDidDelete((e: vscode.Uri) => {
71+
let item = e.fsPath.replace(/\\/g, "/");
72+
let index = items.indexOf(item);
73+
if (index > -1) {
74+
items.splice(index, 1);
75+
}
76+
});
77+
78+
// The command has been defined in the package.json file
79+
// Now provide the implementation of the command with registerCommand
80+
// The commandId parameter must match the command field in package.json
81+
let disposable = vscode.commands.registerCommand("extension.relativePath", () => {
82+
// The code you place here will be executed every time your command is executed
83+
84+
// If there's no file opened
85+
let editor = vscode.window.activeTextEditor;
86+
if (!editor) {
87+
vscode.window.showInformationMessage("You need to have a file opened.");
88+
return; // No open text editor
89+
}
90+
91+
// If we canceled the file search
92+
if (paused) {
93+
myActivate();
94+
return;
95+
}
96+
97+
// If there are no items found
98+
if (!items) {
99+
return;
100+
}
101+
102+
showQuickPick(items);
103+
104+
// Show dropdown editor
105+
function showQuickPick(items: string[]): void {
106+
if (items) {
107+
let paths: vscode.QuickPickItem[] = items.map((val: string) => {
108+
let item: vscode.QuickPickItem = { description: val.replace(workspacePath, ""), label: val.split("/").pop() };
109+
return item;
110+
});
111+
112+
let pickResult: Thenable<vscode.QuickPickItem>;
113+
pickResult = vscode.window.showQuickPick(paths, { matchOnDescription: true, placeHolder: "Filename" });
114+
pickResult.then(returnRelativeLink);
115+
} else {
116+
vscode.window.showInformationMessage("No files to show.");
117+
}
118+
}
119+
120+
// Get the picked item
121+
function returnRelativeLink(item: vscode.QuickPickItem): void {
122+
if (item) {
123+
const targetPath = item.description;
124+
const currentItemPath = editor.document.fileName.replace(/\\/g, "/").replace(workspacePath, "");
125+
let relativeUrl: string = path.relative(currentItemPath, targetPath).replace(".", "").replace(/\\/g, "/");
126+
127+
if (configuration.removeExtension) {
128+
relativeUrl = relativeUrl.substring(0, relativeUrl.lastIndexOf("."));
129+
}
130+
if (configuration.removeLeadingDot && relativeUrl.startsWith("./../")) {
131+
relativeUrl = relativeUrl.substring(2, relativeUrl.length);
132+
}
133+
134+
vscode.window.activeTextEditor.edit(
135+
(editBuilder: vscode.TextEditorEdit) => {
136+
let position: vscode.Position = vscode.window.activeTextEditor.selection.end;
137+
editBuilder.insert(position, relativeUrl);
138+
}
139+
);
140+
}
141+
}
142+
});
143+
144+
context.subscriptions.push(disposable);
138145
}

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)