-
Notifications
You must be signed in to change notification settings - Fork 26
/
extension.js
347 lines (293 loc) · 9.14 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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
var vscode = require('vscode')
const GREMLINS = 'gremlins'
const GREMLINS_LEVELS = {
NONE: 'none',
INFO: 'info',
WARNING: 'warning',
ERROR: 'error',
}
const GREMLINS_SEVERITIES = {
[GREMLINS_LEVELS.INFO]: vscode.DiagnosticSeverity.Information,
[GREMLINS_LEVELS.WARNING]: vscode.DiagnosticSeverity.Warning,
[GREMLINS_LEVELS.ERROR]: vscode.DiagnosticSeverity.Error,
}
const gremlinsDefaultColor = 'rgba(169, 68, 66, .75)'
const eventListeners = []
let decorationTypes = {}
let processedDocuments = {}
const icons = {
light: null,
dark: null,
}
let diagnosticCollection = null
function configureDiagnosticsCollection(showDiagnostics) {
if (showDiagnostics && !diagnosticCollection) {
diagnosticCollection = diagnosticCollection =
vscode.languages.createDiagnosticCollection(GREMLINS)
} else if (!showDiagnostics && diagnosticCollection) {
diagnosticCollection.clear()
diagnosticCollection.dispose()
diagnosticCollection = null
}
return diagnosticCollection
}
function disposeDecorationTypes() {
Object.entries(decorationTypes).forEach(([key, decorationType]) => {
decorationType.dispose()
})
decorationTypes = {}
}
/**
*
* @param {vscode.ExtensionContext} context
*/
function loadIcons(context) {
icons.light = context.asAbsolutePath('images/gremlins-light.svg')
icons.dark = context.asAbsolutePath('images/gremlins-dark.svg')
}
/**
*
* @param {vscode.TextDocument} document
*/
function loadConfiguration(document) {
const gremlinsConfiguration = vscode.workspace.getConfiguration(
GREMLINS,
document,
)
const gremlins = gremlinsFromConfig(gremlinsConfiguration)
const showDiagnostics = gremlinsConfiguration.showInProblemPane
const diagnosticCollection = configureDiagnosticsCollection(showDiagnostics)
let regexpWithAllChars = new RegExp(
Object.keys(gremlins)
.map((char) => `${char}+`)
.join('|'),
'g',
)
return {
gremlins,
regexpWithAllChars,
diagnosticCollection,
}
}
function gremlinsFromConfig(gremlinsConfiguration) {
const gremlinsLevels = {
[GREMLINS_LEVELS.INFO]: gremlinsConfiguration.color_info,
[GREMLINS_LEVELS.WARNING]: gremlinsConfiguration.color_warning,
[GREMLINS_LEVELS.ERROR]: gremlinsConfiguration.color_error,
}
const gremlinsCharacters = gremlinsConfiguration.characters
const gutterIconSize = gremlinsConfiguration.gutterIconSize
const hexCodePointsRangeRegex = /^([0-9a-f]+)(?:-([0-9a-f]+))?$/i
const lightIcon = {
gutterIconPath: icons.light,
gutterIconSize: gutterIconSize,
}
const darkIcon = {
gutterIconPath: icons.dark,
gutterIconSize: gutterIconSize,
}
const gremlins = {}
for (const [hexCodePoint, config] of Object.entries(gremlinsCharacters)) {
const severityLevel = config.level
? config.level.toLowerCase()
: GREMLINS_LEVELS.ERROR
if (severityLevel === GREMLINS_LEVELS.NONE) {
// Ignore gremlins marked as "none"
continue
}
let decorationType = {
light: config.hideGutterIcon ? {} : lightIcon,
dark: config.hideGutterIcon ? {} : darkIcon,
overviewRulerColor: config.overviewRulerColor || gremlinsDefaultColor,
overviewRulerLane: vscode.OverviewRulerLane.Right,
}
if (config.zeroWidth) {
decorationType.borderWidth = '1px'
decorationType.borderStyle = 'solid'
decorationType.borderColor = gremlinsLevels[severityLevel]
} else {
decorationType.backgroundColor = gremlinsLevels[severityLevel]
}
let hexCodePointsRange = hexCodePoint.match(hexCodePointsRangeRegex)
if (hexCodePointsRange[2] !== undefined) {
// This is a range of characters
// Lets create all characters of the range, with the same configuration
let firstChar = parseInt(`0x${hexCodePointsRange[1]}`, 16)
let lastChar = parseInt(`0x${hexCodePointsRange[2]}`, 16)
for (var index = firstChar; index <= lastChar; ++index) {
let thisHexCodePoint = index.toString(16)
gremlins[String.fromCharCode(index)] = Object.assign({}, config, {
thisHexCodePoint,
decorationType: cachedDecorationType(decorationType),
})
}
} else {
// This is a single character
gremlins[charFromHex(hexCodePoint)] = Object.assign({}, config, {
hexCodePoint,
decorationType: cachedDecorationType(decorationType),
})
}
}
return gremlins
}
function cachedDecorationType(decorationType) {
const cacheKey = JSON.stringify(decorationType)
if (!decorationTypes[cacheKey]) {
decorationTypes[cacheKey] =
vscode.window.createTextEditorDecorationType(decorationType)
}
return decorationTypes[cacheKey]
}
function charFromHex(hexCodePoint) {
return String.fromCodePoint(`0x${hexCodePoint}`)
}
/**
*
* @param {vscode.TextEditor} activeTextEditor
* @param {*} gremlins
* @param {RegExp} regexpWithAllChars
* @param {vscode.DiagnosticCollection} diagnosticCollection
*/
function checkForGremlins(activeTextEditor) {
if (!activeTextEditor) {
return
}
const doc = activeTextEditor.document
let { gremlins, regexpWithAllChars, diagnosticCollection } =
loadConfiguration(doc)
const decorationOption = {}
for (const char in gremlins) {
decorationOption[char] = []
}
/** vscode.Diagnostic[] */
let diagnostics = []
for (let lineNum = 0; lineNum < doc.lineCount; lineNum++) {
let lineText = doc.lineAt(lineNum)
let line = lineText.text
let match
while ((match = regexpWithAllChars.exec(line))) {
const matchedCharacter = match[0][0]
const gremlin = gremlins[matchedCharacter]
let startPos = new vscode.Position(lineNum, match.index)
let endPos = new vscode.Position(lineNum, match.index + match[0].length)
const decoration = {
range: new vscode.Range(startPos, endPos),
hoverMessage:
match[0].length +
' ' +
gremlin.description +
(match[0].length > 1 ? 's' : '') +
' (unicode U+' +
gremlin.hexCodePoint +
') here',
}
decorationOption[matchedCharacter].push(decoration)
if (diagnosticCollection) {
const severity = GREMLINS_SEVERITIES[gremlin.level]
const diagnostic = {
range: decoration.range,
message: decoration.hoverMessage,
severity: severity,
source: 'Gremlins tracker',
}
diagnostics.push(diagnostic)
}
}
}
const decorations = groupDecorationsByType(gremlins, decorationOption)
drawDecorations(activeTextEditor, decorations)
if (diagnosticCollection) {
diagnosticCollection.set(activeTextEditor.document.uri, diagnostics)
}
processedDocuments[activeTextEditor.document.uri] = { decorations }
}
function groupDecorationsByType(gremlins, decorationOption) {
return Object.entries(gremlins).reduce((obj, [char, gremlin]) => {
const decorationType = gremlin.decorationType,
options = decorationOption[char]
if (!obj.hasOwnProperty(decorationType.key)) {
obj[decorationType.key] = {
decorationType: decorationType,
options: options,
}
} else {
obj[decorationType.key].options =
obj[decorationType.key].options.concat(options)
}
return obj
}, {})
}
function drawDecorations(activeTextEditor, decorations) {
for (const { decorationType, options } of Object.values(decorations)) {
activeTextEditor.setDecorations(decorationType, options)
}
}
/**
*
* @param {vscode.ExtensionContext} context
*/
function activate(context) {
loadIcons(context)
eventListeners.push(
vscode.workspace.onDidChangeConfiguration(
(event) => {
if (event.affectsConfiguration(GREMLINS)) {
disposeDecorationTypes()
processedDocuments = {}
vscode.window.visibleTextEditors.forEach((editor) =>
checkForGremlins(editor),
)
}
},
null,
context.subscriptions,
),
)
eventListeners.push(
vscode.window.onDidChangeActiveTextEditor(
(editor) => {
if (editor) {
const processedDocument = processedDocuments[editor.document.uri]
if (!processedDocument) {
checkForGremlins(editor)
} else {
drawDecorations(editor, processedDocument.decorations)
}
}
},
null,
context.subscriptions,
),
)
eventListeners.push(
vscode.workspace.onDidChangeTextDocument(
(_event) => checkForGremlins(vscode.window.activeTextEditor),
null,
context.subscriptions,
),
)
eventListeners.push(
vscode.workspace.onDidCloseTextDocument(
(textDocument) => {
diagnosticCollection && diagnosticCollection.delete(textDocument.uri)
delete processedDocuments[textDocument.uri]
},
null,
context.subscriptions,
),
)
checkForGremlins(vscode.window.activeTextEditor)
}
exports.activate = activate
// this method is called when your extension is deactivated
function deactivate() {
if (diagnosticCollection) {
diagnosticCollection.clear()
diagnosticCollection.dispose()
}
disposeDecorationTypes()
eventListeners.forEach((listener) => listener.dispose())
eventListeners.length = 0
}
exports.deactivate = deactivate