-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathextension.js
102 lines (88 loc) · 2.53 KB
/
extension.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
const vscode = require("vscode");
async function NavigateToDotsFile() {
const editor = vscode.window.activeTextEditor;
const selectedText = editor
? editor.document.getText(editor.selection)
: await vscode.window.showInputBox({
placeHolder: "Search query",
value: "",
});
const pathTo = selectedText
.split(".")
.map((e) => e.replace('"', ""))
.join("/");
vscode.commands.executeCommand("workbench.action.quickOpen", pathTo);
}
class DotLinkProvider {
async provideDocumentLinks(document, token) {
const links = [];
const text = document.getText();
const regex = /(\"|\')\b([\w]+)\.([\w.]+)\b(\"|\')/g;
let match;
while ((match = regex.exec(text))) {
const originalText = match[0];
const transformedText = originalText
.replace(/\"/g, "")
.replace(/\./g, "/");
const fileUri = await vscode.workspace.findFiles(
"*/**/" + transformedText + ".rb"
);
if (fileUri) {
links.push({
range: new vscode.Range(
document.positionAt(match.index),
document.positionAt(match.index + originalText.length)
),
target: fileUri[0],
});
}
}
return links;
}
async searchFileInDirectory(directoryUri, filename, fileExtension) {
const entries = await vscode.workspace.fs.readDirectory(directoryUri);
for (const [entryName, entryType] of entries) {
const entryPath = vscode.Uri.joinPath(directoryUri, entryName).path;
if (entryType === vscode.FileType.Directory) {
const fileUri = await this.searchFileInDirectory(
vscode.Uri.parse(entryPath),
filename,
fileExtension
);
if (fileUri) {
return fileUri;
}
} else if (
entryType === vscode.FileType.File &&
entryPath.toLocaleString().includes(`${filename}.${fileExtension}`)
) {
return vscode.Uri.parse(entryPath);
}
}
return null;
}
resolveDocumentLink(link, token) {
return link;
}
}
/**
* @param {vscode.ExtensionContext} context
*/
function activate(context) {
let disposable1 = vscode.commands.registerCommand(
"dot-navigation.navigate-to-dots",
function () {
NavigateToDotsFile();
}
);
const linkProvider = new DotLinkProvider();
context.subscriptions.push(
vscode.languages.registerDocumentLinkProvider("ruby", linkProvider)
);
context.subscriptions.push(disposable1);
}
function deactivate() {}
module.exports = {
activate,
deactivate,
};