-
-
Notifications
You must be signed in to change notification settings - Fork 93
Expand file tree
/
Copy pathVscodeScopeRenderer.ts
More file actions
72 lines (64 loc) · 2.42 KB
/
VscodeScopeRenderer.ts
File metadata and controls
72 lines (64 loc) · 2.42 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
import type { Disposable, GeneralizedRange } from "@cursorless/lib-common";
import { isGeneralizedRangeEqual } from "@cursorless/lib-common";
import type { VscodeTextEditor } from "../VscodeTextEditor";
import { blendRangeTypeColors } from "./blendRangeTypeColors";
import type { RangeTypeColors } from "./RangeTypeColors";
import { VscodeFancyRangeHighlighter } from "./VscodeFancyRangeHighlighter";
export interface RendererScope {
domain: GeneralizedRange;
nestedRanges: GeneralizedRange[];
}
/**
* Responsible for rendering scopes, as used by {@link VscodeScopeVisualizer}.
* Includes a hack where we color blend domain and nested ranges that are
* identical, to reduce load on VSCode renderer and to work around some
* glitchiness.
*/
export class VscodeScopeRenderer implements Disposable {
private domainHighlighter: VscodeFancyRangeHighlighter;
private nestedRangeHighlighter: VscodeFancyRangeHighlighter;
/**
* A highlighter that blends domain and nested range colors when they have
* identical ranges
*/
private domainEqualsNestedHighlighter: VscodeFancyRangeHighlighter;
constructor(
domainColors: RangeTypeColors,
nestedRangeColors: RangeTypeColors,
) {
this.domainHighlighter = new VscodeFancyRangeHighlighter(domainColors);
this.nestedRangeHighlighter = new VscodeFancyRangeHighlighter(
nestedRangeColors,
);
this.domainEqualsNestedHighlighter = new VscodeFancyRangeHighlighter(
blendRangeTypeColors(domainColors, nestedRangeColors),
);
}
setScopes(editor: VscodeTextEditor, scopes: RendererScope[]) {
const domainRanges: GeneralizedRange[] = [];
const allNestedRanges: GeneralizedRange[] = [];
const domainEqualsNestedRanges: GeneralizedRange[] = [];
for (const { domain, nestedRanges } of scopes) {
if (
nestedRanges.length === 1 &&
isGeneralizedRangeEqual(nestedRanges[0], domain)
) {
domainEqualsNestedRanges.push(domain);
continue;
}
domainRanges.push(domain);
allNestedRanges.push(...nestedRanges);
}
this.domainHighlighter.setRanges(editor, domainRanges);
this.nestedRangeHighlighter.setRanges(editor, allNestedRanges);
this.domainEqualsNestedHighlighter.setRanges(
editor,
domainEqualsNestedRanges,
);
}
dispose(): void {
this.domainHighlighter.dispose();
this.nestedRangeHighlighter.dispose();
this.domainEqualsNestedHighlighter.dispose();
}
}