-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathextension.js
204 lines (196 loc) · 8.51 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
// 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) {
var discolored = vscode.workspace.getConfiguration("vscode-chez").get("discolorBracketInString");
activateRainbowBrackets(context, discolored);
context.subscriptions.push(vscode.languages.setLanguageConfiguration('scheme', configuration));
// vscode.workspace.getConfiguration().update('editor.tabSize', 2, vscode.ConfigurationTarget.Global);
}
function activateRainbowBrackets(context, discolored) {
var roundBracketsColor = ["#e6b422", "#c70067", "#00a960", "#fc7482"];
var squareBracketsColor = ["#33ccff", "#8080ff", "#0073a8"];
var squigglyBracketsColor = ["#d4d4aa", "#d1a075", "#9c6628"];
var roundBracketsDecorationTypes = [];
var squareBracketsDecorationTypes = [];
var squigglyBracketsDecorationTypes = [];
for (var index in roundBracketsColor) {
roundBracketsDecorationTypes.push(vscode.window.createTextEditorDecorationType({
color: roundBracketsColor[index]
}));
}
for (var index in squareBracketsColor) {
squareBracketsDecorationTypes.push(vscode.window.createTextEditorDecorationType({
color: squareBracketsColor[index]
}));
}
for (var index in squigglyBracketsColor) {
squigglyBracketsDecorationTypes.push(vscode.window.createTextEditorDecorationType({
color: squigglyBracketsColor[index]
}));
}
var isolatedRightBracketsDecorationTypes = vscode.window.createTextEditorDecorationType({
color: "#e2041b"
});
var activeEditor = vscode.window.activeTextEditor;
if (activeEditor) {
rainbowBrackets();
}
vscode.window.onDidChangeActiveTextEditor(function (editor) {
activeEditor = editor;
if (editor) {
rainbowBrackets();
}
}, null, context.subscriptions);
vscode.workspace.onDidChangeTextDocument(function (event) {
if (activeEditor && event.document === activeEditor.document) {
rainbowBrackets();
}
}, null, context.subscriptions);
function rainbowBrackets() {
if (!activeEditor) {
return;
}
var text = activeEditor.document.getText();
var regEx = /[\(\)\[\]\{\}]/g;
var match;
var roundBracketsColorCount = 0;
var squareBracketsColorCount = 0;
var squigglyBracketsColorCount = 0;
var leftRoundBracketsStack = [];
var leftSquareBracketsStack = [];
var leftSquigglyBracketsStack = [];
var roundBracketsDecorationTypeMap = {};
var squareBracketsDecorationTypeMap = {};
var squigglyBracketsDecorationTypeMap = {};
for (var index in roundBracketsDecorationTypes) {
roundBracketsDecorationTypeMap[index] = [];
};
for (var index in squareBracketsDecorationTypes) {
squareBracketsDecorationTypeMap[index] = [];
};
for (var index in squigglyBracketsDecorationTypes) {
squigglyBracketsDecorationTypeMap[index] = [];
};
var rightBracketsDecorationTypes = [];
var roundCalculate;
var squareCalculate;
var squigglyCalculate;
var stringIndexList = discolored ? getStringIndexList(text) : null;
while (match = regEx.exec(text)) {
if (match.index - 2 > -1 && text.substr(match.index - 2, 2) === "#\\") {
continue;
}
if (discolored && ifIndexInString(match.index, stringIndexList)) {
continue;
}
var startPos = activeEditor.document.positionAt(match.index);
var endPos = activeEditor.document.positionAt(match.index + 1);
var decoration = {
range: new vscode.Range(startPos, endPos),
hoverMessage: null
};
switch (match[0]) {
case '(':
roundCalculate = roundBracketsColorCount;
leftRoundBracketsStack.push(roundCalculate);
roundBracketsColorCount++;
if (roundBracketsColorCount >= roundBracketsColor.length) {
roundBracketsColorCount = 0;
}
roundBracketsDecorationTypeMap[roundCalculate].push(decoration);
break;
case ')':
if (leftRoundBracketsStack.length > 0) {
roundCalculate = leftRoundBracketsStack.pop();
roundBracketsColorCount = roundCalculate;
roundBracketsDecorationTypeMap[roundCalculate].push(decoration);
} else {
rightBracketsDecorationTypes.push(decoration);
}
break;
case '[':
squareCalculate = squareBracketsColorCount;
leftSquareBracketsStack.push(squareCalculate);
squareBracketsColorCount++;
if (squareBracketsColorCount >= squareBracketsColor.length) {
squareBracketsColorCount = 0;
}
squareBracketsDecorationTypeMap[squareCalculate].push(decoration);
break;
case ']':
if (leftSquareBracketsStack.length > 0) {
squareCalculate = leftSquareBracketsStack.pop();
squareBracketsColorCount = squareCalculate;
squareBracketsDecorationTypeMap[squareCalculate].push(decoration);
} else {
rightBracketsDecorationTypes.push(decoration);
}
break;
case '{':
squigglyCalculate = squigglyBracketsColorCount;
leftSquigglyBracketsStack.push(squigglyCalculate);
squigglyBracketsColorCount++;
if (squigglyBracketsColorCount >= squigglyBracketsColor.length) {
squigglyBracketsColorCount = 0;
}
squigglyBracketsDecorationTypeMap[squigglyCalculate].push(decoration);
break;
case '}':
if (leftSquigglyBracketsStack.length > 0) {
squigglyCalculate = leftSquigglyBracketsStack.pop();
squigglyBracketsColorCount = squigglyCalculate;
squigglyBracketsDecorationTypeMap[squigglyCalculate].push(decoration);
} else {
rightBracketsDecorationTypes.push(decoration);
}
break;
default:
}
}
for (var index in roundBracketsDecorationTypes) {
activeEditor.setDecorations(roundBracketsDecorationTypes[index], roundBracketsDecorationTypeMap[index]);
}
for (var index in squareBracketsDecorationTypes) {
activeEditor.setDecorations(squareBracketsDecorationTypes[index], squareBracketsDecorationTypeMap[index]);
}
for (var index in squigglyBracketsDecorationTypes) {
activeEditor.setDecorations(squigglyBracketsDecorationTypes[index], squigglyBracketsDecorationTypeMap[index]);
}
activeEditor.setDecorations(isolatedRightBracketsDecorationTypes, rightBracketsDecorationTypes);
}
}
function getStringIndexList(text) {
// var regEx = /(?<!\\)"[\s\S]*?(?<!\\)"/g;
text = text.replace(/#\\"/ig, "iii").replace(/\\\\"/ig, "ii\"").replace(/\\"/ig, "ii")
var regEx = /"[\s\S]*?"/g;
var match;
var lst = []
while (match = regEx.exec(text)) {
lst.push([match.index, match.index + match[0].length - 1])
}
return lst;
}
function ifIndexInString(index, indexList) {
if (!indexList)
return false;
for (var i in indexList) {
var ary = indexList[i];
if (index > ary[0] && index < ary[1])
return true;
}
return false;
}
var configuration = {
wordPattern: /[\w\-\.:<>\*][\w\d\.\\/\-\?<>\*!]+/,
indentationRules: {
decreaseIndentPattern: undefined,
increaseIndentPattern: /^\s*\(.*[^)]\s*$/
}
}
exports.activate = activate;
// this method is called when your extension is deactivated
function deactivate() {}
exports.deactivate = deactivate;