-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathextension.js
114 lines (95 loc) · 3.27 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
103
104
105
106
107
108
109
110
111
112
113
114
const vscode = require("vscode");
const path = require("path");
const fs = require("fs");
function activate(context) {
let baseName = _fileName => {
return _fileName.split(".")[0];
};
let extName = _fileName => {
let [first, ...rest] = _fileName.split(".");
return rest.join(".");
};
let openFile = _fullPath => {
vscode.workspace.openTextDocument(vscode.Uri.file(_fullPath)).then(doc => {
console.log(doc);
vscode.window.showTextDocument(doc, { preview: false });
});
};
let disposableOpen = vscode.commands.registerCommand(
"openRelatedFiles.open",
function() {
let config = vscode.workspace.getConfiguration("openRelatedFiles");
/** @type {Array} */
let ignoreExtArr = config.get("ignoreExt");
let fileExtMap = config.get("openFileMap");
let editor = vscode.window.activeTextEditor;
if (!editor) {
return;
}
let hasIgnoredExt = fileName => {
return ignoreExtArr.some(ext => {
return fileName.endsWith(ext);
});
};
let currentFileName = path.basename(editor.document.fileName);
let currentFileDir = path.dirname(editor.document.fileName);
let currentFileExt = "." + extName(currentFileName);
fs.readdir(currentFileDir, (err, files) => {
let relevantFiles = files.filter(file => {
return (
!hasIgnoredExt(file) &&
baseName(file) == baseName(currentFileName) &&
file != currentFileName
);
});
let otherInterestedFiles = fileExtMap.hasOwnProperty(currentFileExt)
? fileExtMap[currentFileExt].map(ext => {
return baseName(currentFileName) + ext;
})
: [];
let allFiles = relevantFiles.concat(otherInterestedFiles);
allFiles.forEach(file => {
let fullPath = [currentFileDir, file].join(path.sep);
if (fs.existsSync(fullPath)) openFile(fullPath);
});
});
}
);
let disposableCreate = vscode.commands.registerCommand(
"openRelatedFiles.create",
function() {
let config = vscode.workspace.getConfiguration("openRelatedFiles");
let fileExtMap = config.get("createFileMap");
let editor = vscode.window.activeTextEditor;
if (!editor) {
return;
}
let currentFileName = path.basename(editor.document.fileName);
let currentFileDir = path.dirname(editor.document.fileName);
let currentFileExt = "." + extName(currentFileName);
if (fileExtMap.hasOwnProperty(currentFileExt)) {
/** @type {Array} */
let interestedExts = fileExtMap[currentFileExt];
let interestedFiles = interestedExts.map(ext => {
return baseName(currentFileName) + ext;
});
interestedFiles.forEach(file => {
let fullPath = [currentFileDir, file].join(path.sep);
fs.open(fullPath, "wx", (err, fd) => {
if (!err) {
fs.close(fd, err => {
if (err) throw err;
});
}
openFile(fullPath);
});
});
}
}
);
context.subscriptions.push(disposableOpen);
context.subscriptions.push(disposableCreate);
}
exports.activate = activate;
function deactivate() {}
exports.deactivate = deactivate;