-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathextension.js
320 lines (278 loc) · 10.9 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
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
// The module 'vscode' contains the VS Code extensibility API
// Import the module and reference it with the alias vscode in your code below
const vscode = require('vscode');
// 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.autoalign', function () {
const aligner = new Aligner();
aligner.movableItemsList = getMoveableItemsArray().sort(sortLengthDesc);
aligner.nonMovableItemsList = getNonMoveableItemsArray().sort(sortLengthDesc);
aligner.skippableEndingItemsArray = getSkippableEndingItemsArray().sort(sortLengthDesc);
// remove moveable items from non-moveable list
aligner.nonMovableItemsList = getDifference(aligner.nonMovableItemsList, aligner.movableItemsList);
aligner.minSeparationLeft = vscode.workspace.getConfiguration().get('autoalign.minSeparationLeft');
aligner.separationRight = vscode.workspace.getConfiguration().get('autoalign.separationRight');
aligner.columnWidth = vscode.workspace.getConfiguration().get('autoalign.columnWidth');
let editor = vscode.window.activeTextEditor;
let selections = editor.selections; // handle multiple selections the same
if ( ! isSomethingSelected(editor) ) {
vscode.window.showInformationMessage("Auto-Align only works on selections.");
return;
}
editor.edit((editBuilder) => {
selections.forEach((selection) => {
let aligned = aligner.align(getTextFromSelection(editor, selection));
replaceEditBuilderSelectionWith(editBuilder, selection, aligned);
});
});
});
context.subscriptions.push(disposable);
}
exports.activate = activate;
// this method is called when your extension is deactivated
function deactivate() {
}
exports.deactivate = deactivate;
function getMoveableItemsArray() {
let config= vscode.workspace.getConfiguration().get('autoalign.moveableItems') || [];
let additional =vscode.workspace.getConfiguration().get('autoalign.moveableItemsAdditional') || [];
return config.concat(additional);
}
function getNonMoveableItemsArray() {
let config= vscode.workspace.getConfiguration().get('autoalign.nonMoveableItems') || [];
let additional =vscode.workspace.getConfiguration().get('autoalign.nonMoveableItemsAdditional') || [];
return config.concat(additional);
}
function getSkippableEndingItemsArray() {
let config= vscode.workspace.getConfiguration().get('autoalign.skipLinesEndingWithItems') || [];
let additional =vscode.workspace.getConfiguration().get('autoalign.skipLinesEndingWithItemsAdditional') || [];
return config.concat(additional);
}
function getTextFromSelection(editor, selection) {
if ( ! editor || ! editor.document || ! selection) {
return null;
}
return editor.document.getText(rangeFromSelection(selection));
}
function getDifference(a, b) {
// give us everything in a which is not in b
return a.filter(x => !b.includes(x));
}
function replaceEditBuilderSelectionWith(editBuilder, selection, newText) {
if ( ! editBuilder || ! selection) {
return;
}
editBuilder.replace(rangeFromSelection(selection), newText);
}
function rangeFromSelection(selection, includeFullLine=true) {
let startChar = includeFullLine ? 0 : selection.start.character;
let endChar = includeFullLine ? 5000 : selection.end.character;
return new vscode.Range(selection.start.line, startChar, selection.end.line, endChar);
}
function isSomethingSelected(editor) {
if ( ! editor) {
return false;
}
let selections = editor.selections; // handle multiple selections the same
let foundSomething = false;
selections.forEach(selection => {
if (selection.start.line !== selection.end.line) {
foundSomething = true;
}
});
return foundSomething;
}
function sortLengthDesc(a,b) {
// ASC -> a.length - b.length
// DESC -> b.length - a.length
return b.length - a.length;
}
/**
* - - - - - - - - - - - - - -
* ALIGNER
* - - - - - - - - - - - - - -
* Main action class
*/
class Aligner {
constructor() {
this.movableItemsList = [];
this.nonMovableItemsList = [];
this.skippableEndingItemsArray = [];
this.minSeparationLeft = 3;
this.minSeparationRight = 1;
this.columnWidth = undefined;
}
align(text) {
if (!text) {
return text;
}
const lines = text.split(/\r\n|\r|\n/);
// NOT ENOUGH TO COMPARE
if (lines.length < 2) {
return text;
}
// GET FARTHEST ALIGNABLE ELEMENT
let farthestAlignablePosition = 0;
lines.forEach(line => {
let alignable = this._getAlignablePositionAndItem(line);
let leftStringTrimed = line.substr(0, alignable.position).trimRight();
let lineAlignPosition = leftStringTrimed.length;
if (lineAlignPosition >= farthestAlignablePosition) {
// this is the farthest we've been!
farthestAlignablePosition = lineAlignPosition;
// now, figure out how much white-space is "to the left" of the character
let leftOfCharacter = line.substr(0, lineAlignPosition-1);
let unTrimmedLength = leftOfCharacter.length;
let trimmendLength = this._trimEnd(leftOfCharacter).length;
let whiteAtEndCount = (unTrimmedLength - trimmendLength);
// we need to make sure it is the "minimumLeft" white-space
if (whiteAtEndCount < this.minSeparationLeft) {
let addAtEndCount = this.minSeparationLeft - whiteAtEndCount;
farthestAlignablePosition += addAtEndCount;
}
// NEAREST FACTOR OF
if (this.columnWidth > 1 && farthestAlignablePosition % this.columnWidth) {
let numberOfWholeColumns = ~~(farthestAlignablePosition / this.columnWidth);
farthestAlignablePosition = (numberOfWholeColumns + 1) * this.columnWidth;
}
}
});
// WE HAVE THINGS TO ALIGN
if (farthestAlignablePosition > 0) {
for (let i = 0; i < lines.length; i++) {
lines[i] = this._alignToPosition(lines[i], farthestAlignablePosition);
}
}
return lines.join('\n');
}
//** INTERNALS */
_alignToPosition(line, position) {
let moveable = this._getAlignablePositionAndItem(line);
// BAIL : nothing is movable
if (moveable.position < 0 || moveable.position >= line.length) {
return line;
}
// create parts
let leftLine = line.substr(0, moveable.position).trimRight();
let rightLine = line.substr(moveable.position + moveable.item.length).trim();
let leftInsertString = ' '.repeat(position - leftLine.length);
let rightInsertString = ' '.repeat(Math.max(this.minSeparationRight, 1));
return leftLine + leftInsertString + moveable.item + rightInsertString + rightLine;
}
_getAlignablePositionAndItem(line) {
let reply = {
position : -1,
item : undefined
}
let moveable = this._getNearestMovablePositionAndItem(line);
// BAIL : nothing is movable
if (moveable.position < 0) {
return reply;
}
let nonMoveable = this._getNearestNonMovablePositionAndItem(line);
// WE HAVE NON-MOVABLES FIRST : nothing is movable
if (nonMoveable.position > -1 && nonMoveable.position <= moveable.position ) {
return reply;
}
reply.position = moveable.position;
reply.item = moveable.item;
return reply;
}
_getNearestMovablePositionAndItem(line) {
return this._getNearestOccurancePositionAndItem(line, this.movableItemsList);
}
_getNearestNonMovablePositionAndItem(line) {
return this._getNearestOccurancePositionAndItem(line, this.nonMovableItemsList);
}
_getNearestOccurancePositionAndItem(line, itemList) {
/**
* will return a structure so that a caller can tell
* where the item is in the line but also what was found
* {
* position : 5,
* item : '=='
* }
*
*/
let reply = {
position : -1,
item : undefined
}
// SKIPPABLE
if (this._isLineSkippable(line)) {
return reply;
}
let nearestPosition = 1000000; // simply huge to start
let foundItem = undefined;
for (let i = 0; i < itemList.length; i++) {
const item = itemList[i];
let positionOfItem = line.indexOf(item);
if (positionOfItem > -1 && positionOfItem < nearestPosition) {
foundItem = item;
nearestPosition = Math.min(nearestPosition, line.indexOf(item));
}
}
if (foundItem) {
reply.position = nearestPosition;
reply.item = foundItem;
}
return reply;
}
_isLineSkippable(line) {
if ( ! line || line.length < 1 ) {
return true;
}
let trimmedLine = this._trimEnd(line);
for (let i = 0; i < this.skippableEndingItemsArray.length; i++) {
const item = this.skippableEndingItemsArray[i];
if (this._endsWith(trimmedLine, item)) {
return true;
}
}
return false;
}
_splice(text, start, delCount, insertText) {
return text.slice(0, start) + insertText + text.slice(start + Math.abs(delCount));
}
_trimEnd(text) {
if ( !text || text.length < 1) {
return text;
}
return text.replace(/[\s\uFEFF\xA0]+$/g, '');
}
_regexLastIndexOf(text, regex, startpos) {
// alteration of answer : https://stackoverflow.com/a/274094/473501
if ( ! text || ! regex) {
return -1;
}
regex = (regex.global) ? regex : new RegExp(regex.source, "g" + (regex.ignoreCase ? "i" : "") + (regex.multiLine ? "m" : ""));
if (typeof(startpos) === "undefined") {
startpos = text.length;
}
else if(startpos < 0) {
startpos = 0;
}
let stringToWorkWith = text.substring(0, startpos + 1);
let lastIndexOf = -1;
let nextStop = 0;
let result;
while((result = regex.exec(stringToWorkWith)) != null) {
lastIndexOf = result.index;
regex.lastIndex = ++nextStop;
}
return lastIndexOf;
}
_endsWith(text, lookFor, caseInsensitive) {
if ( ! text || text.length < 1 || ! lookFor || lookFor.length < 1 ) {
return false;
}
if (caseInsensitive) {
return text.toLowerCase().slice(-lookFor.length) === lookFor.toLowerCase();
}
return text.slice(-lookFor.length) === lookFor;
}
}