-
Notifications
You must be signed in to change notification settings - Fork 0
/
extension.ts
executable file
·241 lines (211 loc) · 7.29 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
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
// 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 PhosphorRange {
opacityIndex: number,
range: vscode.Range,
rangeLength: number,
text: string
};
let activeEditor : vscode.TextEditor;
let phosphorOptions;
let queuedTimeout = false;
let phosphorRanges : Array<PhosphorRange> = [];
let opacities : Array<string> = [];
let phosphorDecorations;
export function enable() {
initializeEditPhosphor();
phosphorOptions.enabled = true;
}
export function disable() {
phosphorOptions.enabled = false;
phosphorRanges = [];
phosphorDecorations.forEach((phosphorDecoration) => {
if (activeEditor) {
activeEditor.setDecorations(phosphorDecoration, []);
}
});
}
function dpRounder(number) {
return Math.round( number * 100 ) / 100;
}
function smoothstep(x: number, xStart: number, yStart: number, xEnd: number, yEnd:number, yMax: number, yMin: number): number {
function clamp(x: number, xMin: number, xMax: number): number {
return x<xMin ? xMin : (x>xMax ? xMax : x)
}
x = clamp((x - xStart) / (xEnd - xStart), 0.0, 1.0)
return clamp(yStart + (x * x * (3 - 2 * x) * (yEnd - yStart)), yMin, yMax)
}
function initializeEditPhosphor() {
try {
let phosphorOptionsOverrides = vscode.workspace.getConfiguration('editPhosphor');
phosphorOptions = _.clone(phosphorOptionsOverrides);
// TODO: If disabled, we should drop out after making sure settings are cleared.
if (!phosphorOptions.enabled) {return;}
opacities = [];
let phosphorSmoothstep = phosphorOptions.smoothstep;
if (phosphorSmoothstep) {
let xStart = phosphorOptions.smoothstepXStart;
let yStart = phosphorOptions.smoothstepYStart;
let xEnd = phosphorOptions.smoothstepXEnd;
let yEnd = phosphorOptions.smoothstepYEnd;
let yMax = phosphorOptions.smoothstepYMax;
let yMin = phosphorOptions.smoothstepYMin;
let fadeStart = phosphorOptions.fadeStart;
let fadeEnd = phosphorOptions.fadeEnd;
let stepSize = 1/phosphorOptions.smoothSteps
for (let step = 0; step < 1; step += stepSize) {
let smoothstepOpacity = smoothstep(step, xStart, yStart, xEnd, yEnd, yMax, yMin);
// y = mx + b
// y = (fadeStart-fadeEnd)*x + fadeEnd
// let opacity = dpRounder((fadeStart-fadeEnd) * smoothstepOpacity + fadeEnd);
let opacity = (fadeStart-fadeEnd) * smoothstepOpacity + fadeEnd;
//if (Math.abs(opacity - fadeEnd) < 1/256) {break;} // If there isn't any significant difference from the final value, this can end early
opacities.push(opacity.toString());
}
} else {
let phosphorFade = phosphorOptions.fadeStart;
while (phosphorFade > phosphorOptions.fadeEnd) {
opacities.push(phosphorFade.toString());
phosphorFade = dpRounder(phosphorFade -= phosphorOptions.fadeStep);
}
}
opacities.reverse();
// Create the decorators
let newPhosphorDecorations = [];
for (let i = 0; i < opacities.length; ++i) {
newPhosphorDecorations.push(
vscode.window.createTextEditorDecorationType({
light: {
backgroundColor: 'rgba('+ (phosphorOptions.colorLight || phosphorOptions.color) + ',' + opacities[i] + ')'
},
dark: {
backgroundColor: 'rgba('+ (phosphorOptions.colorDark || phosphorOptions.color) + ',' + opacities[i] + ')'
}
})
);
}
phosphorDecorations = newPhosphorDecorations;
} catch(err) {
console.log(err);
}
}
// this method is called when vs code is activated
export function activate(context: vscode.ExtensionContext) {
console.log('edit-phosphor is activated');
initializeEditPhosphor();
var commands = [
vscode.commands.registerCommand('editPhosphor.enable', enable),
vscode.commands.registerCommand('editPhosphor.disable', disable)
];
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) {
phosphorRanges = [];
}
activeEditor = editor;
if (editor) {
triggerUpdateDecorations();
}
}, null, context.subscriptions);
// Register for Text Editor changes
vscode.workspace.onDidChangeTextDocument(event => {
if (activeEditor && event.document === activeEditor.document && phosphorOptions.enabled) {
event.contentChanges.forEach((contentChange) => {
try {
// Ensure that the range covers the change
if (0 === contentChange.rangeLength) {
contentChange = {
range: new vscode.Range(contentChange.range.start, new vscode.Position(contentChange.range.end.line, contentChange.range.end.character + contentChange.text.length)),
rangeOffset: contentChange.rangeOffset,
rangeLength: contentChange.text.length,
text: contentChange.text
};
}
var phosphorRange: PhosphorRange = {
opacityIndex: opacities.length - 1,
range: contentChange.range,
rangeLength: contentChange.rangeLength,
text: contentChange.text
};
phosphorRanges.push(phosphorRange);
// Create our animation logic
var fn = function () {
//phosphorRange.opacityIndex -= 1;
triggerUpdateDecorations();
if (0 <= phosphorRange.opacityIndex--) {
setTimeout(fn, phosphorOptions.fadeMS + (Math.min(phosphorRange.rangeLength, 10)));
}
};
setTimeout(fn, phosphorOptions.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, phosphorOptions.redrawFrequency);
} else {
queuedTimeout = true;
}
}
// Update the decorations
function updateDecorations() {
try {
if (!activeEditor) {
return;
}
// Create placeholder arrays (for each opacity level)
var prunedPhosphorRanges: Array<Array<PhosphorRange>> = [];
for (var i = 0; i < opacities.length; ++i) {
prunedPhosphorRanges.push([]);
}
// Sort the phosphorRanges into the correct array for their opacity level
phosphorRanges.forEach((phosphorRange) => {
if (0 <= phosphorRange.opacityIndex) {
prunedPhosphorRanges[phosphorRange.opacityIndex].push(phosphorRange);
}
});
// Add the ranges to the decorator
prunedPhosphorRanges.forEach((phosphorRanges, index) => {
var decorators: vscode.DecorationOptions[] = [];
phosphorRanges.forEach((phosphorRange) => {
var decoration = { range: phosphorRange.range, hoverMessage: phosphorRange.text };
decorators.push(decoration);
});
if (null !== phosphorDecorations && index < phosphorDecorations.length) {
activeEditor.setDecorations(phosphorDecorations[index], decorators);
}
});
// Signal that we are done
timeout = null;
if (queuedTimeout) {
triggerUpdateDecorations();
}
} catch (err) {
console.log(err);
}
}
context.subscriptions.push(vscode.workspace.onDidChangeConfiguration(e => {
if (e.affectsConfiguration('editPhosphor')) {
timeout = null;
queuedTimeout = false;
console.log('Refreshing edit-phosphor configuration')
initializeEditPhosphor();
}
}))
}