-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathextension.js
102 lines (84 loc) · 3.28 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
/**
* @name Functional Cursors
* @author Steven O'Riley
* @desc Automatically replaces patterns at the cursor with specified symbols
*/
const vscode = require('vscode');
const safeEval = require('safe-eval')
let fs = [];
let lastText = "(e, i) => ";
function activate(context) {
let config = vscode.workspace.getConfiguration("functional-cursors");
fs = config.get("functions");
vscode.workspace.onDidChangeConfiguration(() => {
let config = vscode.workspace.getConfiguration("functional-cursors");
fs = config.get("functions");
});
var disposable = vscode.commands.registerCommand("extension.jsCursors", function() {
vscode.window.showInputBox({ "value": lastText, "valueSelection": [lastText.length, lastText.length] })
.then(uinput => {
if (!uinput) return;
let code = '(function(){\n';
for (let i in fs) code += `let ${i}=(${fs[i]})\n`;
if (uinput.endsWith(";")) uinput = uinput.substring(0, uinput.length - 1);
code += `return (${uinput})})();`;
try {
let f = safeEval(code);
let test = f("", 0, [""]);
lastText = uinput;
update(f, test);
}
catch (e) {}
});
});
context.subscriptions.push(disposable);
}
exports.activate = activate;
function update(f, testResult) {
let editor = vscode.window.activeTextEditor;
let document = editor.document;
let selections = editor.selections;
selections.sort(function(a, b) {
return a.start.compareTo(b.start);
});
let content = [];
for (let index in selections) {
let selection = selections[index];
let range = new vscode.Range(selection.start.line, selection.start.character, selection.end.line, selection.end.character);
let text = document.getText(range);
content.push([index, text, selection.start.line, selection.start.character, selection.end.line, selection.end.character]);
}
if (content.length == editor.selections.length) {
let replacements;
let raw = content.map(stuff => stuff[1]);
if (testResult instanceof Array) {
replacements = f("", 0, raw);
editor.edit(function(edit) {
content.forEach((info, i) => {
let range = new vscode.Selection(info[2], info[3], info[4], info[5]);
let replacement = replacements[i % replacements.length] + "";
edit.replace(range, replacement);
});
});
}
else {
editor.edit(function(edit) {
content.forEach((info, i) => {
let range = new vscode.Selection(info[2], info[3], info[4], info[5]);
let replacement = f(info[1], i, raw) + "";
edit.replace(range, replacement);
});
});
}
}
}
function sorted(l, c) {
for (var i = 0; i < l.length - 1; i++) {
if (!c(l[i], l[i + 1])) return false;
}
return true;
}
// this method is called when your extension is deactivated
function deactivate() {
}
exports.deactivate = deactivate;