-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathextension.js
More file actions
230 lines (210 loc) · 7.69 KB
/
Copy pathextension.js
File metadata and controls
230 lines (210 loc) · 7.69 KB
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
// The module 'vscode' contains the VS Code extensibility API
const vscode = require('vscode');
const fs = require('fs');
const nodePath = require('path');
const jsonic = require('jsonic');
const sep = nodePath.sep;
const altSep = sep === '\\' ? '/' : '\\';
function fileExists(path) {
if (typeof path === 'string') {
try {
fs.accessSync(path);
return true;
} catch (ex) {
// Unable to access the file
}
}
return false;
}
function sanitizedWorkspaceName(str) {
return str.replace(/\s(\[.+\])$/, '').replace(/\s(\([^()]+\))$/, '');
}
function isCodeWorkspaceFile(path) {
if (typeof path === 'string') {
let wsObject = getWorkspaceObject(path);
if (typeof wsObject === 'object') {
let projFolders = vscode.workspace.workspaceFolders;
if (wsObject.folders && wsObject.folders.length === projFolders.length) {
return true;
}
}
return false;
}
throw new Error('Invalid argument path, expected string but got ' + typeof path);
}
function getWorkspacePath() {
let name = sanitizedWorkspaceName(vscode.workspace.name);
let workspaceFile = name + '.code-workspace';
// First, check the settings
let config = vscode.workspace.getConfiguration('workspacesort');
if (config.has('workspaceDirectory')) {
let workspaceDir = config.get('workspaceDirectory');
// Does the directory contain the .code-workspace file?
if (workspaceDir && workspaceDir.length > 0) {
// If the setting is a file, just get the directory
if (workspaceDir.indexOf('.') > -1) {
workspaceDir = nodePath.dirname(workspaceDir);
}
let path = nodePath.normalize(workspaceDir + sep + workspaceFile);
if (isCodeWorkspaceFile(path)) {
return path;
}
}
}
// No setting active? Search the file ourselves
// First, try to get the path through the new workspace-information
if (fileExists(vscode.workspace.workspaceFile.fsPath)) {
return vscode.workspace.workspaceFile.fsPath;
}
if (fileExists(vscode.workspace.workspaceFile.path)) {
return vscode.workspace.workspaceFile.path;
}
// Get it from the folder-structure
let visited = [];
let projFolders = vscode.workspace.workspaceFolders;
// Walk the project tree
for (let projFolder of projFolders) {
let folder = projFolder.uri.fsPath;
// Visit all parent folders until we find the .code-workspace file
while (folder.indexOf(sep) !== -1) {
if (!visited.includes(folder)) {
visited.push(folder);
let path = nodePath.normalize(folder + sep + workspaceFile);
if (isCodeWorkspaceFile(path)) {
return path;
}
folder = nodePath.normalize(folder + sep + '..' + sep);
} else {
break;
}
}
}
return '';
}
function getWorkspaceObject(path) {
if (fileExists(path)) {
let fileContent = fs.readFileSync(path);
try {
return jsonic(jsonic(fileContent));
// return JSON.parse(fileContent);
} catch (error) {
let linified = linifyParseError(fileContent, error);
vscode.window.showErrorMessage(linified);
throw error;
}
}
return undefined;
}
function linifyParseError(fileContent, error) {
let newMessage = null;
try {
let positionPart = /[at position] [\d]+$/;
if (positionPart.test(error.message)) {
let getPosition = /[\d]+$/;
let positionString = getPosition.exec(error.message);
let position = parseInt(positionString[0], 10);
if (Number.isInteger(position)) {
let contentString = fileContent.toString('utf8');
let subString = contentString.substr(0, position);
let breakCount = subString.split('\n').length;
newMessage = error.message + ` (Line: ${breakCount})`;
}
}
} catch (e) {
// We aren't interested in this error
}
return newMessage || error.message;
}
function sortPaths(wsObject) {
let changed = false;
if (wsObject.folders && wsObject.folders.length > 1) {
let prev = wsObject.folders.slice();
wsObject.folders = wsObject.folders.sort(alphabetical);
changed = !isSameArrayContent(prev, wsObject.folders);
}
return changed;
}
function topLevelDirectory(path) {
if (typeof path === 'string') {
let split = path.split(sep).filter(it => Boolean(it.length));
split = split[split.length - 1].split(altSep).filter(it => Boolean(it.length));
return split[split.length - 1];
}
throw new Error('Invalid argument path, expected string but got ' + typeof path);
}
function getShownName(folderObj) {
if (!folderObj.path) {
throw new Error('Invalid argument folderObj, expected an object with at least a path-property.');
}
let name = folderObj.name;
if (!name) {
name = topLevelDirectory(folderObj.path).toLocaleLowerCase();
}
return name;
}
function alphabetical(a, b) {
let nameA = getShownName(a);
let nameB = getShownName(b);
return nameA.localeCompare(nameB);
}
function isSameArrayContent(a, b) {
let result = true;
if (a.length !== b.length) {
result = false;
} else {
a.forEach((item, index) => {
if (item !== b[index]) {
result = false;
return;
}
});
}
return result;
}
function sortWorkspaceFolders(notifySuccess) {
let path = getWorkspacePath();
let wsObject = getWorkspaceObject(path);
if (typeof wsObject === 'object') {
if (sortPaths(wsObject)) {
fs.writeFileSync(path, JSON.stringify(wsObject, undefined, '\t'));
let name = vscode.workspace.name;
if (notifySuccess !== false) {
vscode.window.showInformationMessage('Sorted ' + name);
}
}
} else {
vscode.window.showErrorMessage('The .code-workspace file for this workspace was not found. Please use the setting workspacesort.workspaceDirectory to tell WorkspaceSort where it can be found.');
}
}
function onDidChangeWorkspaceFolders() {
let config = vscode.workspace.getConfiguration('workspacesort');
let sortAutomatically = true;
if (config.has('sortAutomatically')) {
sortAutomatically = config.get('sortAutomatically');
}
if (sortAutomatically) {
sortWorkspaceFolders(false);
}
}
// this method is called when your extension is activated
// your extension is activated the very first time the command is executed
function activate(context) {
// The command has been defined in the package.json file
// Now provide the implementation of the command with registerCommand
// The commandId parameter must match the command field in package.json
let disposable = vscode.commands.registerCommand('extension.sort', sortWorkspaceFolders);
context.subscriptions.push(disposable);
vscode.workspace.onDidChangeWorkspaceFolders(onDidChangeWorkspaceFolders);
}
exports.activate = activate;
// this method is called when your extension is deactivated
function deactivate() {}
exports.deactivate = deactivate;
exports.fileExists = fileExists;
exports.sanitizedWorkspaceName = sanitizedWorkspaceName;
exports.sortPaths = sortPaths;
exports.topLevelDirectory = topLevelDirectory;
exports.alphabetical = alphabetical;
exports.isSameArrayContent = isSameArrayContent;
exports.getWorkspaceObject = getWorkspaceObject;
exports.linifyParseError = linifyParseError;