-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathextension.ts
executable file
·203 lines (176 loc) · 5.4 KB
/
extension.ts
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
// The module 'vscode' contains the VS Code extensibility API
// Import the module and reference it with the alias vscode in your code below
import * as vscode from 'vscode';
import * as _ from 'lodash';
interface SnakeRange {
opacityIndex: number,
range: vscode.Range,
rangeLength: number,
text: string
};
let activeEditor
let snakeOptions;
let queuedTimeout;
let snakeRanges: Array<SnakeRange> = [];
let opacities = [];
let snakeDecorations;
export function enable() {
initialiseSnakeTrail();
snakeOptions.enabled = true;
}
export function disable() {
snakeOptions.enabled = false;
snakeRanges = [];
snakeDecorations.forEach((snakeDecoration) => {
if (activeEditor) {
activeEditor.setDecorations(snakeDecoration, []);
}
});
}
export function refresh() {
initialiseSnakeTrail();
}
function dpRounder(number) {
return Math.round( number * 100 ) / 100;
}
function initialiseSnakeTrail() {
try {
let snakeOptionsOverrides = vscode.workspace.getConfiguration('snakeTrail');
snakeOptions = _.clone(snakeOptionsOverrides);
opacities = [];
let snakeFade = snakeOptions.fadeStart;
while (snakeFade >= snakeOptions.fadeEnd) {
opacities.push(snakeFade.toString());
snakeFade -= snakeOptions.fadeStep;
snakeFade = dpRounder(snakeFade);
}
opacities.reverse();
// Create the decorators
let newSnakeDecorations = [];
for (let i = 0; i < opacities.length; ++i) {
newSnakeDecorations.push(
vscode.window.createTextEditorDecorationType({
light: {
backgroundColor: 'rgba('+ (snakeOptions.colorLight || snakeOptions.color) + ',' + opacities[i] + ')'
},
dark: {
backgroundColor: 'rgba('+ (snakeOptions.colorDark || snakeOptions.color) + ',' + opacities[i] + ')'
}
})
);
}
snakeDecorations = newSnakeDecorations;
} catch(err) {
console.log(err);
}
}
// this method is called when vs code is activated
export function activate(context: vscode.ExtensionContext) {
console.log('snake-trail is activated');
initialiseSnakeTrail();
var commands = [
vscode.commands.registerCommand('snakeTrail.enable', enable),
vscode.commands.registerCommand('snakeTrail.disable', disable),
vscode.commands.registerCommand('snakeTrail.refresh', refresh)
];
commands.forEach(function (command) {
context.subscriptions.push(command);
});
activeEditor = vscode.window.activeTextEditor;
if (activeEditor) {
triggerUpdateDecorations();
}
// Register for Active Editor changes
vscode.window.onDidChangeActiveTextEditor(editor => {
if (activeEditor !== editor) {
snakeRanges = [];
}
activeEditor = editor;
if (editor) {
triggerUpdateDecorations();
}
}, null, context.subscriptions);
// Register for Text Editor changes
vscode.workspace.onDidChangeTextDocument(event => {
if (activeEditor && event.document === activeEditor.document && snakeOptions.enabled) {
event.contentChanges.forEach((contentChange) => {
try {
// Ensure that the range covers the change
if (0 === contentChange.rangeLength) {
contentChange = {
rangeLength: contentChange.text.length,
range: new vscode.Range(contentChange.range.start, new vscode.Position(contentChange.range.end.line, contentChange.range.end.character + contentChange.text.length)),
text: contentChange.text
};
}
var snakeRange: SnakeRange = {
opacityIndex: opacities.length - 1,
range: contentChange.range,
rangeLength: contentChange.rangeLength,
text: contentChange.text
};
snakeRanges.push(snakeRange);
// Create our animation logic
var fn = function () {
snakeRange.opacityIndex -= 1;
triggerUpdateDecorations();
if (0 <= snakeRange.opacityIndex) {
setTimeout(fn, snakeOptions.fadeMS + (Math.min(snakeRange.rangeLength, 10)));
}
};
setTimeout(fn, snakeOptions.fadeMS);
} catch (err) {
console.log(err);
}
});
triggerUpdateDecorations();
}
}, null, context.subscriptions);
// Use a timer to avoid overloading the system
var timeout = null;
function triggerUpdateDecorations() {
if (!timeout) {
queuedTimeout = false;
timeout = setTimeout(updateDecorations, snakeOptions.redrawFrequency);
} else {
queuedTimeout = true;
}
}
// Update the decorations
function updateDecorations() {
try {
if (!activeEditor) {
return;
}
// Create placeholder arrays (for each opacity level)
var prunedSnakeRanges: Array<Array<SnakeRange>> = [];
for (var i = 0; i < opacities.length; ++i) {
prunedSnakeRanges.push([]);
}
// Sort the snakeRanges into the correct array for their opacity level
snakeRanges.forEach((snakeRange) => {
if (0 <= snakeRange.opacityIndex) {
prunedSnakeRanges[snakeRange.opacityIndex].push(snakeRange);
}
});
// Add the ranges to the decorator
prunedSnakeRanges.forEach((snakeRanges, index) => {
var decorators: vscode.DecorationOptions[] = [];
snakeRanges.forEach((snakeRange) => {
var decoration = { range: snakeRange.range, hoverMessage: snakeRange.text };
decorators.push(decoration);
});
if (null !== snakeDecorations && index < snakeDecorations.length) {
activeEditor.setDecorations(snakeDecorations[index], decorators);
}
});
// Signal that we are done
timeout = null;
if (queuedTimeout) {
triggerUpdateDecorations();
}
} catch (err) {
console.log(err);
}
}
}