-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathextension.js
More file actions
244 lines (202 loc) · 7.78 KB
/
Copy pathextension.js
File metadata and controls
244 lines (202 loc) · 7.78 KB
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
const vscode = require('vscode');
// ----- //
// Units //
// ----- //
class Config {
alphaMax;
lane;
lineThreshold;
rgbForEditing;
rgbForVisibility;
decayRate;
drawIntervalMs;
decayIntervalMs;
constructor () {
this.load();
}
load () {
const section = vscode.workspace.getConfiguration("heatscroll");
this.alphaMax = parseFloat('' + (section.get("alpha", 0.66) || 0.66));
this.lane = (section.get("lane", vscode.OverviewRulerLane.Center) || vscode.OverviewRulerLane.Center);
this.lineThreshold = parseInt('' + (section.get("lineThreshold", 100) || 100), 10);
this.rgbForEditing = (section.get("rgbEdit", "153,255,238") || "153,255,238").split(",");
this.rgbForVisibility = (section.get("rgbScroll", "255,68,191") || "255,68,191").split(",");
this.decayRate = parseFloat('' + (section.get("decayRate", 0.001) || 0.001));
this.drawIntervalMs = parseInt('' + (section.get("drawIntervalMs", 200) || 200), 10);
this.decayIntervalMs = parseInt('' + (section.get("drawIntervalMs", 1000) || 1000), 10);
}
};
const bucketCountMax = 64;
class Tracker {
bucketsByFileName = {};
bucketsForFile (fileName, bucketCount) {
const buckets = (this.bucketsByFileName[fileName] = this.bucketsByFileName[fileName] || []);
if (buckets.length < bucketCount) {
buckets.push(...new Array(bucketCount - buckets.length).fill(0));
}
return buckets;
}
recordLine (fileName, line, lineMax, rate) {
const bucketCount = Math.min(bucketCountMax, lineMax);
const buckets = this.bucketsForFile(fileName, bucketCount);
const bucket = Math.floor((line / (lineMax - 1)) * (bucketCount - 1));
for (let b = 0; b < bucketCount; ++b) {
const score = b == bucket ? 1 : 0;
buckets[b] = (rate * score) + (1 - rate) * (buckets[b] || 0);
}
}
recordLines (fileName, lineFrom, lineTo, lineMax, rate) {
const bucketCount = Math.min(bucketCountMax, lineMax);
const buckets = this.bucketsForFile(fileName, bucketCount);
for (let b = 0; b < bucketCount; ++b) {
const line = Math.floor((b / (bucketCount - 1)) * (lineMax - 1));
const score = (line >= lineFrom && line <= lineTo) ? 1 : 0;
buckets[b] = (rate * score) + (1 - rate) * (buckets[b] || 0);
}
}
bucketIntensitiesFor (fileName) {
const values = this.bucketsForFile(fileName);
const max = values.reduce((c, i) => i > c ? i : c , 0);
return values.map(x => x / max);
}
};
const alphaLevels = 25;
class Renderer {
/** @type {vscode.TextEditorDecorationType[]} */
decorationTypes = [];
generateDecorationTypes ([r, g, b], alphaMax, lane) {
this.decorationTypes = Array.from({length: alphaLevels}, (_, i) => {
const alpha = (i / (alphaLevels - 1)) * alphaMax;
return vscode.window.createTextEditorDecorationType({
overviewRulerColor: `rgba(${r}, ${g}, ${b}, ${alpha})`,
overviewRulerLane: lane,
});
});
return this;
}
/**
* @param {vscode.TextEditor} editor
* @param {Tracker} tracker
*/
drawFor (editor, tracker, enabled) {
const fileName = editor.document.fileName;
const lineCount = editor.document.lineCount;
const bucketIntensities = tracker.bucketIntensitiesFor(fileName);
const bucketCount = bucketIntensities.length;
const decorationsByAlpha = [];
if (enabled && lineCount >= config.lineThreshold) {
for (let b = 0; b < bucketCount; ++b) {
const alphaLevel = Math.floor((alphaLevels - 1) * bucketIntensities[b]);
const lineNumberFrom = Math.floor((b / (bucketCount - 1)) * (lineCount - 1));
const lineNumberTo = Math.max(Math.floor(((b + 1) / (bucketCount - 1)) * (lineCount - 1)) - 1, 0);
const range = new vscode.Range(new vscode.Position(lineNumberFrom, 0), new vscode.Position(lineNumberTo, 0));
decorationsByAlpha[alphaLevel] = decorationsByAlpha[alphaLevel] || [];
decorationsByAlpha[alphaLevel].push({range});
}
}
for (let alphaLevel = 0; alphaLevel < alphaLevels; ++alphaLevel) {
const decorationType = this.decorationTypes[alphaLevel];
if (decorationsByAlpha[alphaLevel]) {
editor.setDecorations(decorationType, decorationsByAlpha[alphaLevel]);
} else {
editor.setDecorations(decorationType, []);
}
}
}
};
// ----- //
// State //
// ----- //
let enabled = true;
const config = new Config();
const visibilityTracker = new Tracker();
const visibilityRenderer = (new Renderer()).generateDecorationTypes(
config.rgbForVisibility,
config.alphaMax,
config.lane
);
const editingTracker = new Tracker();
const editingRenderer = (new Renderer()).generateDecorationTypes(
config.rgbForEditing,
config.alphaMax,
config.lane
);
let decayInterval;
let drawInterval;
// ----------- //
// Entrypoints //
// ----------- //
/**
* Invoked via the "activationEvents" > "onStartupFinished" in package.json
* @param {vscode.ExtensionContext} context
*/
function activate (context) {
/** @param {vscode.TextEditor} editor */
function updateLineEditing (editor) {
editingTracker.recordLine(
editor.document.fileName,
editor.selection.active.line,
editor.document.lineCount,
config.decayRate
);
}
/** @param {vscode.TextEditor} editor */
function updateLineVisibility (editor) {
if (editor) {
for (const range of editor.visibleRanges) {
visibilityTracker.recordLines(
editor.document.fileName,
range.start.line,
range.end.line,
editor.document.lineCount,
config.decayRate
);
}
}
}
// Track which lines are visible when scrolling:
vscode.window.onDidChangeTextEditorVisibleRanges(function onScroll (e) {
updateLineVisibility(e.textEditor)
});
// Track which lines are visible over time:
decayInterval = setInterval(function tick () {
if (vscode.window.state.active) {
updateLineVisibility(vscode.window.activeTextEditor);
updateLineVisibility(vscode.window.activeTextEditor);
}
}, config.decayIntervalMs);
// Track which lines are being edited:
vscode.workspace.onDidChangeTextDocument(function onEdit (e) {
// Require the changed document to be the one in the active editor:
const editor = vscode.window.activeTextEditor;
if (editor && (editor.document.fileName == e.document.fileName)) {
updateLineEditing(editor);
}
});
/* @todo Respond to changes in configuration:
vscode.workspace.onDidChangeConfiguration(function onConfig (e) {
config.load();
editingRenderer.generateDecorationTypes();
visibilityRenderer.generateDecorationTypes();
});
//*/
// Update the scrollbar view at a fixed rate:
drawInterval = setInterval(function draw () {
const editor = vscode.window.activeTextEditor;
if (editor) {
visibilityRenderer.drawFor(editor, visibilityTracker, enabled);
editingRenderer.drawFor(editor, editingTracker, enabled);
}
}, config.drawIntervalMs);
context.subscriptions.push(vscode.commands.registerCommand('heatscroll.toggle', function onToggle () {
enabled = !enabled;
}));
}
function deactivate () {
clearInterval(decayInterval);
clearInterval(drawInterval);
}
module.exports = {
activate,
deactivate,
};