-
Notifications
You must be signed in to change notification settings - Fork 6
/
extension.js
139 lines (128 loc) · 4.21 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
'use strict';
var vscode = require('vscode');
var Range = vscode.Range;
var Position = vscode.Position;
var syntaxRegex = /^([+\-]?[\da-fA-F]+(?:\.\d+)?)\s*([+\-]|(?:\+\+|\-\-))?\s*(\d+)?\s*(?:\:\s*(\d+))?\s*(?:\:\s*(\d+))?$/;
// this method is called when your extension is activated
function activate(context) {
var insertCmd = vscode.commands.registerCommand('insertSequentialNumbers', function () {
var editor = vscode.window.activeTextEditor;
var initialSelections = editor.selections.sort(sortSelection);
var inputOptions = {};
var undoStopBefore = true;
inputOptions.placeHolder = "<start> <operator> <step> : <digit> : <radix>";
inputOptions.validateInput = function (param) {
if (param === "") {
perform(initialSelections, editor, {}, { undoStopBefore: undoStopBefore, undoStopAfter: false });
return;
}
var test = parseInput(param);
if (!test) {
return 'Syntax error. The rule is "<start> <operator> <step> : <digit> : <radix>".';
}
// realtime simulate
perform(initialSelections, editor, test, { undoStopBefore: undoStopBefore, undoStopAfter: false });
undoStopBefore &= false;
};
vscode.window.showInputBox(inputOptions)
.then(function (value) {
if (!value && value !== 0) {
// undo
if (!undoStopBefore) {
vscode.commands.executeCommand("undo");
}
return;
}
// confirm input.
var options = parseInput(value);
perform(initialSelections, editor, options, { undoStopBefore: false, undoStopAfter: true });
});
});
context.subscriptions.push(insertCmd);
}
exports.activate = activate;
// this method is called when your extension is deactivated
function deactivate() {
}
exports.deactivate = deactivate;
function sortSelection(a, b) {
return a.anchor.line - b.anchor.line || a.anchor.character - b.anchor.character;
}
function perform(initial, editor, options, undoStop) {
var currentSelection = editor.selections.sort(sortSelection);
var replaceSelection = vscode.workspace.getConfiguration("sequence").replaceSelection;
editor.edit(function (builder) {
initial.forEach(function (selection, index) {
var endOfInitialSelection = replaceSelection ? selection.start.character : selection.end.character + (currentSelection[index].start.character - selection.start.character);
builder.replace(new Range(new Position(selection.end.line, endOfInitialSelection), currentSelection[index].end), calculate(index, options));
});
}, undoStop);
}
function parseInput(input) {
var matches = input.match(syntaxRegex);
if (!matches) {
return null;
}
var radix = matches[5] ? parseInt(matches[5], 10) : 10;
var start = parseInt(matches[1], radix);
var operator = matches[2] || "+";
var step = isNaN(matches[3]) ? 1 : parseInt(matches[3], 10);
var digit = parseInt(matches[4], 10);
if (isNaN(digit)) {
digit = (start.toString() === matches[1]) ? 0 : matches[1].length;
if (/^[+\-]/.test(matches[1])) {
digit = Math.max(digit - 1, 0);
}
}
return {
start: start,
digit: digit,
operator: operator,
step: step,
radix: radix,
input: input
};
}
function calculate(index, options) {
var value = NaN;
switch (options.operator) {
case "++":
value = options.start + index;
break;
case "--":
value = options.start - index;
break;
case "+":
value = options.start + (index * options.step);
break;
case "-":
value = options.start - (index * options.step);
break;
default:
return "";
}
value = paddingZero(value, options.digit, options.radix);
var hasAlpha = options.input.match(/([a-fA-F])/);
if (hasAlpha) {
// for hex.
return hasAlpha[1] == hasAlpha[1].toLowerCase() ? value.toLowerCase() : value.toUpperCase();
}
return value;
}
function paddingZero(num, dig, radix) {
if (!dig) {
dig = 0;
}
if (!radix) {
radix = 10;
}
var number = num.toString(radix);
var numAbs = number.replace("-", "");
var digit = Math.max(numAbs.length, dig);
var result = "";
if (0 <= number.indexOf("-")) {
result += "-";
}
result += (Array(digit).join("0") + numAbs).slice(digit * -1);
return result;
}