-
Notifications
You must be signed in to change notification settings - Fork 307
Expand file tree
/
Copy pathrangeHighlightingExtension.ts
More file actions
47 lines (38 loc) · 1.75 KB
/
Copy pathrangeHighlightingExtension.ts
File metadata and controls
47 lines (38 loc) · 1.75 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
'use client';
import { StateField, Range } from "@codemirror/state";
import { Decoration, DecorationSet, EditorView } from "@codemirror/view";
import { BrowseHighlightRange } from "@/app/(app)/browse/hooks/utils";
const markDecoration = Decoration.mark({
class: "searchMatch-selected",
});
const lineDecoration = Decoration.line({
attributes: { class: "cm-range-border-radius lineHighlight" },
});
export const rangeHighlightingExtension = (range: BrowseHighlightRange) => StateField.define<DecorationSet>({
create(state) {
const { start, end } = range;
if (start.lineNumber > state.doc.lines || end.lineNumber > state.doc.lines) {
console.warn(`Highlight range is out of bounds: start.lineNumber=${start.lineNumber}, end.lineNumber=${end.lineNumber}, doc.lines=${state.doc.lines}`);
return Decoration.none;
}
if ('column' in start && 'column' in end) {
const from = state.doc.line(start.lineNumber).from + start.column - 1;
const to = state.doc.line(end.lineNumber).from + end.column - 1;
const decorations: Range<Decoration>[] = [];
if (from < to) {
decorations.push(markDecoration.range(from, to));
}
return Decoration.set(decorations, /* sort = */ true);
} else {
const decorations: Range<Decoration>[] = [];
for (let line = start.lineNumber; line <= end.lineNumber; line++) {
decorations.push(lineDecoration.range(state.doc.line(line).from));
}
return Decoration.set(decorations, /* sort = */ true);
}
},
update(deco, tr) {
return deco.map(tr.changes);
},
provide: (field) => EditorView.decorations.from(field),
});