forked from Acode-Foundation/Acode
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathlineNumberSelection.ts
More file actions
115 lines (101 loc) · 3.11 KB
/
Copy pathlineNumberSelection.ts
File metadata and controls
115 lines (101 loc) · 3.11 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
import { EditorSelection } from "@codemirror/state";
import type { BlockInfo, EditorView } from "@codemirror/view";
type LineInfo = Pick<BlockInfo, "from" | "to"> | null | undefined;
type LineNumberClickEvent = Pick<
MouseEvent,
| "button"
| "shiftKey"
| "altKey"
| "ctrlKey"
| "metaKey"
| "preventDefault"
| "defaultPrevented"
>;
function toDocumentOffset(
value: number | null | undefined,
fallback = 0,
): number {
const resolved = value != null ? Number(value) : fallback;
return Number.isFinite(resolved) ? resolved : fallback;
}
/**
* Resolve the selection range for a clicked document line.
* Includes the trailing line break when one exists to mirror Ace's
* full-line selection behavior.
*/
export function getLineSelectionRange(
state: EditorView["state"],
line: LineInfo,
): { from: number; to: number } | null {
if (!line) return null;
const from = Math.max(0, toDocumentOffset(line.from));
const to = Math.max(from, toDocumentOffset(line.to, from));
return {
from,
to: Math.min(to + 1, state.doc.length),
};
}
function getCurrentSelectionLineRange(state: EditorView["state"]): {
from: number;
to: number;
} {
const selection = state.selection.main;
const startLine = state.doc.lineAt(selection.from);
const endPos = selection.empty
? selection.head
: Math.max(selection.to - 1, selection.from);
const endLine = state.doc.lineAt(endPos);
const startRange = getLineSelectionRange(state, startLine);
const endRange = getLineSelectionRange(state, endLine);
return {
from: startRange?.from ?? selection.from,
to: endRange?.to ?? selection.to,
};
}
function createLineSelection(range: {
from: number;
to: number;
}): EditorSelection {
return EditorSelection.single(range.to, range.from);
}
function createExtendedLineSelection(
state: EditorView["state"],
clickedRange: { from: number; to: number },
): EditorSelection {
const currentRange = getCurrentSelectionLineRange(state);
const from = Math.min(currentRange.from, clickedRange.from);
const to = Math.max(currentRange.to, clickedRange.to);
if (clickedRange.from <= currentRange.from) {
return EditorSelection.single(to, from);
}
return EditorSelection.single(from, to);
}
/**
* Select the clicked line from the line-number gutter.
* Shift-click extends the current selection by whole lines.
* Other modified or non-primary clicks are ignored so they don't interfere
* with context menus or alternate selection gestures.
*/
export function handleLineNumberClick(
view: EditorView | null | undefined,
line: LineInfo,
event: LineNumberClickEvent | null | undefined,
): boolean {
if (!view || !event || event.defaultPrevented) return false;
if ((event.button ?? 0) !== 0) return false;
if (event.altKey || event.ctrlKey || event.metaKey) {
return false;
}
const range = getLineSelectionRange(view.state, line);
if (!range) return false;
event.preventDefault();
view.dispatch({
selection: event.shiftKey
? createExtendedLineSelection(view.state, range)
: createLineSelection(range),
userEvent: event.shiftKey ? "select.extend.pointer" : "select.pointer",
});
view.focus();
return true;
}
export default handleLineNumberClick;