@nibling/editor 0.0.0-stage → 0.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +53 -2
- package/dist/commands/autoclose.d.ts +13 -0
- package/dist/commands/autoclose.js +78 -0
- package/dist/commands/commands.d.ts +104 -0
- package/dist/commands/commands.js +442 -0
- package/dist/commands/comments.d.ts +3 -0
- package/dist/commands/comments.js +9 -0
- package/dist/commands/fold.d.ts +18 -0
- package/dist/commands/fold.js +75 -0
- package/dist/commands/keymap.d.ts +40 -0
- package/dist/commands/keymap.js +118 -0
- package/dist/complete/autocomplete.d.ts +35 -0
- package/dist/complete/autocomplete.js +386 -0
- package/dist/complete/source.d.ts +72 -0
- package/dist/complete/source.js +52 -0
- package/dist/complete/words.d.ts +53 -0
- package/dist/complete/words.js +203 -0
- package/dist/element.d.ts +119 -0
- package/dist/element.js +1011 -0
- package/dist/expand/expansions.d.ts +68 -0
- package/dist/expand/expansions.js +143 -0
- package/dist/expand/projection.d.ts +73 -0
- package/dist/expand/projection.js +189 -0
- package/dist/expand/resolver.d.ts +42 -0
- package/dist/expand/resolver.js +585 -0
- package/dist/expand/slice-tokens.d.ts +25 -0
- package/dist/expand/slice-tokens.js +79 -0
- package/dist/extension.d.ts +58 -0
- package/dist/extension.js +1 -0
- package/dist/find/find.d.ts +12 -0
- package/dist/find/find.js +469 -0
- package/dist/find/search.d.ts +81 -0
- package/dist/find/search.js +308 -0
- package/dist/headless.d.ts +16 -0
- package/dist/headless.js +19 -0
- package/dist/index.d.ts +6 -0
- package/dist/index.js +6 -0
- package/dist/inline-expansion.d.ts +89 -0
- package/dist/inline-expansion.js +324 -0
- package/dist/input/edit-context.d.ts +23 -0
- package/dist/input/edit-context.js +102 -0
- package/dist/input/surface.d.ts +101 -0
- package/dist/input/surface.js +84 -0
- package/dist/input/textarea.d.ts +26 -0
- package/dist/input/textarea.js +125 -0
- package/dist/sheet.d.ts +6 -0
- package/dist/sheet.js +12 -0
- package/dist/state/core.d.ts +73 -0
- package/dist/state/core.js +86 -0
- package/dist/state/fold-ranges.d.ts +41 -0
- package/dist/state/fold-ranges.js +167 -0
- package/dist/state/folds.d.ts +45 -0
- package/dist/state/folds.js +240 -0
- package/dist/state/history.d.ts +25 -0
- package/dist/state/history.js +127 -0
- package/dist/state/indent.d.ts +33 -0
- package/dist/state/indent.js +121 -0
- package/dist/state/selection.d.ts +45 -0
- package/dist/state/selection.js +106 -0
- package/dist/state/whitespace.d.ts +16 -0
- package/dist/state/whitespace.js +54 -0
- package/dist/text/changeset.d.ts +56 -0
- package/dist/text/changeset.js +321 -0
- package/dist/text/text.d.ts +30 -0
- package/dist/text/text.js +321 -0
- package/dist/tokens/bridge.d.ts +107 -0
- package/dist/tokens/bridge.js +461 -0
- package/dist/tokens/engine.d.ts +39 -0
- package/dist/tokens/engine.js +360 -0
- package/dist/tokens/protocol.d.ts +113 -0
- package/dist/tokens/protocol.js +38 -0
- package/dist/tokens/textmate/ascii-regex.d.ts +5 -0
- package/dist/tokens/textmate/ascii-regex.js +156 -0
- package/dist/tokens/textmate/index.d.ts +7 -0
- package/dist/tokens/textmate/index.js +19 -0
- package/dist/tokens/textmate/linker.d.ts +67 -0
- package/dist/tokens/textmate/linker.js +238 -0
- package/dist/tokens/textmate/scopes.d.ts +14 -0
- package/dist/tokens/textmate/scopes.js +54 -0
- package/dist/tokens/textmate/theme.d.ts +54 -0
- package/dist/tokens/textmate/theme.js +138 -0
- package/dist/tokens/textmate/tokenizer.d.ts +29 -0
- package/dist/tokens/textmate/tokenizer.js +684 -0
- package/dist/tokens/textmate.d.ts +49 -0
- package/dist/tokens/textmate.js +4 -0
- package/dist/view/decorations.d.ts +183 -0
- package/dist/view/decorations.js +51 -0
- package/dist/view/guides.d.ts +58 -0
- package/dist/view/guides.js +174 -0
- package/dist/view/heightmap.d.ts +97 -0
- package/dist/view/heightmap.js +229 -0
- package/dist/view/line-dom.d.ts +54 -0
- package/dist/view/line-dom.js +168 -0
- package/dist/view/measure.d.ts +37 -0
- package/dist/view/measure.js +136 -0
- package/dist/view/overlays.d.ts +32 -0
- package/dist/view/overlays.js +135 -0
- package/dist/view/rail.d.ts +27 -0
- package/dist/view/rail.js +232 -0
- package/dist/view/remote.d.ts +24 -0
- package/dist/view/remote.js +143 -0
- package/dist/view/scope-focus.d.ts +27 -0
- package/dist/view/scope-focus.js +255 -0
- package/dist/view/view.d.ts +114 -0
- package/dist/view/view.js +1231 -0
- package/dist/vite.d.ts +12 -0
- package/dist/vite.js +66 -0
- package/dist/worker.d.ts +1 -0
- package/dist/worker.js +64 -0
- package/grammars/astro.mjs +2 -0
- package/grammars/chunks/r-90cb351dba.mjs +1 -0
- package/grammars/chunks/r-b92a500aab.mjs +1 -0
- package/grammars/chunks/r-ced5311992.mjs +1 -0
- package/grammars/chunks/r-ebae91d4ec.mjs +1 -0
- package/grammars/chunks/s-1821e66170.mjs +1 -0
- package/grammars/css.mjs +1 -0
- package/grammars/diff.mjs +1 -0
- package/grammars/docker.mjs +1 -0
- package/grammars/dotenv.mjs +1 -0
- package/grammars/graphql.mjs +1 -0
- package/grammars/html.mjs +2 -0
- package/grammars/ini.mjs +1 -0
- package/grammars/javascript.mjs +5 -0
- package/grammars/json.mjs +1 -0
- package/grammars/json5.mjs +1 -0
- package/grammars/jsonc.mjs +1 -0
- package/grammars/less.mjs +1 -0
- package/grammars/manifest.json +58 -0
- package/grammars/markdown.mjs +1 -0
- package/grammars/mdx.mjs +1 -0
- package/grammars/pug.mjs +1 -0
- package/grammars/scss.mjs +1 -0
- package/grammars/shellscript.mjs +1 -0
- package/grammars/sql.mjs +1 -0
- package/grammars/stylus.mjs +1 -0
- package/grammars/svelte.mjs +1 -0
- package/grammars/toml.mjs +1 -0
- package/grammars/tsx.mjs +5 -0
- package/grammars/typescript.mjs +2 -0
- package/grammars/vue.mjs +1 -0
- package/grammars/yaml.mjs +1 -0
- package/package.json +71 -3
- package/themes/dark-plus.json +1 -0
- package/themes/light-plus.json +1 -0
- package/themes/monokai.json +1 -0
|
@@ -0,0 +1,1231 @@
|
|
|
1
|
+
import { isCursor, rangeFrom, rangeTo } from "../state/selection.js";
|
|
2
|
+
import { selectionMatches } from "../commands/commands.js";
|
|
3
|
+
import { RemoteCursors, remoteCss } from "./remote.js";
|
|
4
|
+
import { overlayKinds } from "./decorations.js";
|
|
5
|
+
import { BlockHeightMap, FoldedHeightMap } from "./heightmap.js";
|
|
6
|
+
import { NO_RUNS, renderLine, sameRuns, styleRule } from "./line-dom.js";
|
|
7
|
+
import { IndentGuides, guidesCss } from "./guides.js";
|
|
8
|
+
import { markedWhitespace } from "../state/whitespace.js";
|
|
9
|
+
import { Measurer } from "./measure.js";
|
|
10
|
+
import { OverlayPainter } from "./overlays.js";
|
|
11
|
+
import { sheetFor } from "../sheet.js";
|
|
12
|
+
const cleanDirt = () => ({ viewport: false, lines: new Set(), overlays: false, carets: false, reveal: false, marks: false });
|
|
13
|
+
/**
|
|
14
|
+
* How long a far jump may hold uncolored lines before painting them plain anyway. Three frames at
|
|
15
|
+
* 60 Hz: long enough for the worker's viewport-first slice (4 ms each, per the tokenizer bridge
|
|
16
|
+
* doc) to answer for the jumped-to window on an idle CPU, short enough that a worker that is stuck
|
|
17
|
+
* or far behind never stalls input past a few frames. Unlike the document-open hold
|
|
18
|
+
* (`highlightHold`, hundreds of ms for a one-time cold load), this runs on every jump, so it stays
|
|
19
|
+
* small.
|
|
20
|
+
*/
|
|
21
|
+
const JUMP_HOLD_MS = 50;
|
|
22
|
+
const EMPTY_STARTS = new Uint32Array(0);
|
|
23
|
+
/** New line number of old line `n`, or null when an edit removed it. */
|
|
24
|
+
function mapLine(edits, n) {
|
|
25
|
+
let shifted = n;
|
|
26
|
+
for (const e of edits) {
|
|
27
|
+
if (n < e.oldLo)
|
|
28
|
+
break;
|
|
29
|
+
if (n <= e.oldHi) {
|
|
30
|
+
const k = e.newLo + (n - e.oldLo);
|
|
31
|
+
return k <= e.newHi ? k : null;
|
|
32
|
+
}
|
|
33
|
+
shifted = n + (e.newHi - e.oldHi);
|
|
34
|
+
}
|
|
35
|
+
return shifted;
|
|
36
|
+
}
|
|
37
|
+
const overlayColors = {
|
|
38
|
+
selection: 'var(--nib-selection, var(--nib-theme-selection, #264f78))',
|
|
39
|
+
composition: 'var(--nib-composition, currentColor)',
|
|
40
|
+
// Theme `editor.findMatchHighlightBackground` and `editor.findMatchBackground` (element.ts).
|
|
41
|
+
match: 'var(--nib-editor-match, var(--nib-match, var(--nib-theme-match, rgba(234, 92, 0, 0.33))))',
|
|
42
|
+
'selection-match': 'var(--nib-editor-selection-match)',
|
|
43
|
+
'match-current': 'var(--nib-editor-match-current, var(--nib-theme-match-current, #515c6a))',
|
|
44
|
+
'diagnostic-error': 'var(--nib-editor-error)',
|
|
45
|
+
'diagnostic-warning': 'var(--nib-editor-warning)',
|
|
46
|
+
'diagnostic-info': 'var(--nib-editor-info)',
|
|
47
|
+
bracket: 'var(--nib-bracket, rgba(0, 100, 0, 0.3))',
|
|
48
|
+
...Object.fromEntries([1, 2, 3, 4, 5, 6, 7, 8].map((n) => [`remote-${n}`, `color-mix(in srgb, var(--nib-editor-presence-${n}) 30%, transparent)`])),
|
|
49
|
+
};
|
|
50
|
+
const baseCss = `
|
|
51
|
+
/* Status and presence knobs (docs/design/theming.md): nibling's tokens, then the TextMate theme's
|
|
52
|
+
color or VS Code's default for the theme's kind, which the element writes as --nib-theme-*. */
|
|
53
|
+
:host{
|
|
54
|
+
--nib-editor-error:var(--nib-color-danger,var(--nib-theme-error,#f14c4c));
|
|
55
|
+
--nib-editor-warning:var(--nib-color-warning,var(--nib-theme-warning,#cca700));
|
|
56
|
+
--nib-editor-info:var(--nib-color-info,var(--nib-theme-info,#3794ff));
|
|
57
|
+
--nib-editor-selection-match:var(--nib-theme-selection-match,color-mix(in srgb, var(--nib-color-accent, currentColor) 20%, transparent));
|
|
58
|
+
${[1, 2, 3, 4, 5, 6, 7, 8].map((n) => `--nib-editor-presence-${n}:var(--nib-color-presence-${n},var(--nib-theme-presence-${n},#a78bfa));`).join('\n ')}
|
|
59
|
+
--nib-editor-on-presence:var(--nib-color-on-presence,var(--nib-theme-on-presence,#1e1e1e));
|
|
60
|
+
}
|
|
61
|
+
:host{
|
|
62
|
+
--nib-editor-motion-duration-fast:var(--nib-motion-duration-fast,150ms);
|
|
63
|
+
--nib-editor-motion-ease-out:var(--nib-motion-ease-out,cubic-bezier(0.2,0,0,1));
|
|
64
|
+
}
|
|
65
|
+
.nib-scroller{position:relative;overflow:auto;overflow-anchor:none;height:100%;width:100%;box-sizing:border-box;
|
|
66
|
+
background:var(--nib-bg,#1e1e1e);color:var(--nib-fg,#d4d4d4);
|
|
67
|
+
font:var(--nib-font,13px/18px ui-monospace,Menlo,Consolas,monospace);tab-size:4;-moz-tab-size:4;
|
|
68
|
+
--nib-gutter:calc((var(--nib-digits,1) + 3) * 1ch)}
|
|
69
|
+
.nib-sizer{position:relative;min-width:100%}
|
|
70
|
+
.nib-gutter{position:sticky;left:0;z-index:2;width:var(--nib-gutter);height:100%;
|
|
71
|
+
background:var(--nib-bg,#1e1e1e);color:var(--nib-gutter-fg,#858585)}
|
|
72
|
+
.nib-gutter-line{position:absolute;right:1.5ch;height:var(--nib-lh,18px);white-space:pre}
|
|
73
|
+
.nib-content{position:absolute;top:0;left:var(--nib-gutter);right:0}
|
|
74
|
+
.nib-line{position:absolute;left:0;height:var(--nib-lh,18px);white-space:pre}
|
|
75
|
+
.nib-awaiting .nib-line,.nib-awaiting .nib-line *{color:transparent!important}
|
|
76
|
+
.nib-carets{position:absolute;top:0;left:var(--nib-gutter);z-index:1;pointer-events:none}
|
|
77
|
+
.nib-caret{position:absolute;width:2px;height:var(--nib-lh,18px);background:var(--nib-caret,var(--nib-theme-caret,#aeafad))}
|
|
78
|
+
.nib-eol{position:absolute;height:var(--nib-lh,18px);background:var(--nib-selection,#264f78)}
|
|
79
|
+
.nib-probe{position:absolute;top:0;left:0;visibility:hidden;white-space:pre;pointer-events:none}
|
|
80
|
+
.nib-blocks{position:absolute;top:0;left:0;right:0;z-index:3}
|
|
81
|
+
.nib-block{position:absolute;left:0;right:0}
|
|
82
|
+
.nib-blurred .nib-caret{visibility:hidden}
|
|
83
|
+
.nib-focused .nib-caret{animation:nib-blink-a 1.2s steps(1) infinite}
|
|
84
|
+
.nib-focused .nib-carets.nib-alt .nib-caret{animation-name:nib-blink-b}
|
|
85
|
+
@keyframes nib-blink-a{50%{opacity:0}}
|
|
86
|
+
@keyframes nib-blink-b{50%{opacity:0}}
|
|
87
|
+
@media (prefers-reduced-motion:reduce){.nib-focused .nib-caret{animation:none}}
|
|
88
|
+
`;
|
|
89
|
+
/** Selection backgrounds: the built-in OverlaySource. The view re-queries it on every Update. */
|
|
90
|
+
const selectionSource = {
|
|
91
|
+
*query(state, from, to) {
|
|
92
|
+
for (const r of state.selection.ranges) {
|
|
93
|
+
const a = rangeFrom(r);
|
|
94
|
+
const b = rangeTo(r);
|
|
95
|
+
if (b > a && b >= from && a <= to)
|
|
96
|
+
yield { kind: 'selection', from: a, to: b };
|
|
97
|
+
}
|
|
98
|
+
},
|
|
99
|
+
subscribe: () => () => { },
|
|
100
|
+
};
|
|
101
|
+
/**
|
|
102
|
+
* Other occurrences of the primary selection (docs/TODO.md): stateless like `selectionSource`,
|
|
103
|
+
* since the Snapshot already carries everything `commands.selectionMatches` needs. The view
|
|
104
|
+
* re-queries every overlay source on every Update (`dirt.overlays = true` in `#onUpdate`), so this
|
|
105
|
+
* needs no invalidation hook of its own to react to the selection changing.
|
|
106
|
+
*/
|
|
107
|
+
const selectionMatchSource = {
|
|
108
|
+
*query(state, from, to) {
|
|
109
|
+
for (const r of selectionMatches(state, from, to))
|
|
110
|
+
yield { kind: 'selection-match', ...r };
|
|
111
|
+
},
|
|
112
|
+
subscribe: () => () => { },
|
|
113
|
+
};
|
|
114
|
+
/**
|
|
115
|
+
* The selected columns of the line spanning [lineFrom, lineTo], as flat `[from, to)` pairs. Binary
|
|
116
|
+
* searches the sorted ranges, so a thousand cursors cost a few steps per line.
|
|
117
|
+
*/
|
|
118
|
+
function selectedColumns(state, lineFrom, lineTo) {
|
|
119
|
+
const ranges = state.selection.ranges;
|
|
120
|
+
let lo = 0;
|
|
121
|
+
let hi = ranges.length;
|
|
122
|
+
while (lo < hi) {
|
|
123
|
+
const mid = (lo + hi) >> 1;
|
|
124
|
+
if (rangeTo(ranges[mid]) <= lineFrom)
|
|
125
|
+
lo = mid + 1;
|
|
126
|
+
else
|
|
127
|
+
hi = mid;
|
|
128
|
+
}
|
|
129
|
+
const out = [];
|
|
130
|
+
for (let i = lo; i < ranges.length; i++) {
|
|
131
|
+
const r = ranges[i];
|
|
132
|
+
const a = rangeFrom(r);
|
|
133
|
+
if (a >= lineTo)
|
|
134
|
+
break;
|
|
135
|
+
const b = rangeTo(r);
|
|
136
|
+
if (b > a)
|
|
137
|
+
out.push(Math.max(a, lineFrom) - lineFrom, Math.min(b, lineTo) - lineFrom);
|
|
138
|
+
}
|
|
139
|
+
return out;
|
|
140
|
+
}
|
|
141
|
+
/**
|
|
142
|
+
* The virtualized DOM view (D1), one per element. A follower of EditorCore and of the TokenBridge.
|
|
143
|
+
*
|
|
144
|
+
* DOM it owns, inside the element's shadow root:
|
|
145
|
+
* scroller (overflow: auto)
|
|
146
|
+
* sizer (height = heightMap.totalHeight)
|
|
147
|
+
* gutter: one absolutely positioned number per mounted line
|
|
148
|
+
* content: one absolutely positioned div per mounted line (LineBox), top = heightMap.topOf
|
|
149
|
+
* carets: one absolutely positioned element per cursor, plus past-EOL selection boxes
|
|
150
|
+
*
|
|
151
|
+
* Per scroll frame: viewport = heightMap.lineAt(scrollTop - overscan..scrollTop + height + overscan);
|
|
152
|
+
* reuse LineBoxes still mounted, recycle the rest into the newly mounted lines (renderLine);
|
|
153
|
+
* bridge.setViewport; re-query overlay sources for the mounted range; position carets.
|
|
154
|
+
* No reads of layout except scrollTop/clientHeight (read first, then write only).
|
|
155
|
+
* Per keystroke: core Update (sync) -> heightMap.update, mark changed lines + overlays + carets
|
|
156
|
+
* + reveal; bridge has already re-keyed its window, so touched lines rerender with provisional
|
|
157
|
+
* runs in the same frame. Line-count changes shift `top` of boxes below; no rerender for them.
|
|
158
|
+
* Per token batch: mark the batch's lines; the frame rerenders only lines whose (text, tokens)
|
|
159
|
+
* identity changed, then invalidates their overlay ranges.
|
|
160
|
+
*
|
|
161
|
+
* Coordinates: offsets -> (x, y) via heightMap.topOf(line) + measurer.xAt(box, col). Positions
|
|
162
|
+
* on unmounted lines return null; nothing asks for them in the first slice.
|
|
163
|
+
*/
|
|
164
|
+
export class EditorView {
|
|
165
|
+
#core;
|
|
166
|
+
#bridge;
|
|
167
|
+
#measurer = new Measurer();
|
|
168
|
+
#painter;
|
|
169
|
+
#overscan;
|
|
170
|
+
#folding;
|
|
171
|
+
#blocks;
|
|
172
|
+
#lineNumbers;
|
|
173
|
+
/** Measured height of each block widget, kept while it is unmounted so geometry holds. */
|
|
174
|
+
#blockHeights = new Map();
|
|
175
|
+
#mountedBlocks = new Map();
|
|
176
|
+
#blockKey = new WeakMap();
|
|
177
|
+
#blockLayer = null;
|
|
178
|
+
#blockObserver = null;
|
|
179
|
+
/** Block geometry changed: rebuild the HeightMap at the top of the frame, keeping the line at
|
|
180
|
+
* the top of the viewport where it is. */
|
|
181
|
+
#reanchor = false;
|
|
182
|
+
/** The line at the top of the viewport after the last frame, and how far into it (or its block
|
|
183
|
+
* widgets) the top was. With block widgets, an edit wholly above it keeps it where it is. */
|
|
184
|
+
#topLine = 0;
|
|
185
|
+
#topInto = 0;
|
|
186
|
+
#pinTop = false;
|
|
187
|
+
#disposed = false;
|
|
188
|
+
#viewWidth = -1;
|
|
189
|
+
#heights;
|
|
190
|
+
#lineHeight = 18;
|
|
191
|
+
#boxes = new Map();
|
|
192
|
+
/** Boxes an Update unbound from their line; the next frame reuses or removes them. */
|
|
193
|
+
#pool = [];
|
|
194
|
+
/** Mounted lines [from, to), 1-based. */
|
|
195
|
+
#mounted = { from: 1, to: 1 };
|
|
196
|
+
#overlaySources = [];
|
|
197
|
+
#lineClassSources = [];
|
|
198
|
+
#dirt = { viewport: true, lines: 'all', overlays: true, carets: true, reveal: false, marks: false };
|
|
199
|
+
#guides;
|
|
200
|
+
#whitespace;
|
|
201
|
+
/** Whether the last marks pass saw a non-empty selection, so a run of cursor-only updates
|
|
202
|
+
* (typing) skips it. */
|
|
203
|
+
#selected = false;
|
|
204
|
+
#frameRequested = false;
|
|
205
|
+
#rafId = 0;
|
|
206
|
+
#needsCalibrate = false;
|
|
207
|
+
#frameListeners = [];
|
|
208
|
+
#scroller;
|
|
209
|
+
#sizer;
|
|
210
|
+
#gutter;
|
|
211
|
+
#content;
|
|
212
|
+
#caretLayer;
|
|
213
|
+
#remote;
|
|
214
|
+
#revealAt = null;
|
|
215
|
+
#probe;
|
|
216
|
+
#tokenSheet;
|
|
217
|
+
#carets = [];
|
|
218
|
+
#eols = [];
|
|
219
|
+
#viewportHeight = 0;
|
|
220
|
+
#sizerHeight = -1;
|
|
221
|
+
#contentWidth = 0;
|
|
222
|
+
/**
|
|
223
|
+
* Size the scroll width from the text's longest line on the next frame, so horizontal scroll
|
|
224
|
+
* reaches it before that line has ever been mounted. 'reset' also forgets widths measured for a
|
|
225
|
+
* previous document. Rendered lines still grow it when they measure wider (tabs, wide glyphs).
|
|
226
|
+
*/
|
|
227
|
+
#widthFromText = 'grow';
|
|
228
|
+
#digits = 0;
|
|
229
|
+
/** `lineNumbers.offset()` for the current frame. */
|
|
230
|
+
#numberOffset = 0;
|
|
231
|
+
#layout;
|
|
232
|
+
constructor(root, core, bridge, options, signal) {
|
|
233
|
+
this.#core = core;
|
|
234
|
+
this.#bridge = bridge;
|
|
235
|
+
this.#painter = new OverlayPainter(options.instance);
|
|
236
|
+
this.#overscan = options.overscan ?? 20;
|
|
237
|
+
this.#folding = options.folding;
|
|
238
|
+
this.#blocks = options.blocks;
|
|
239
|
+
this.#lineNumbers = options.lineNumbers;
|
|
240
|
+
this.#heights = this.#makeHeights();
|
|
241
|
+
const doc = root.ownerDocument;
|
|
242
|
+
const div = (cls) => {
|
|
243
|
+
const el = doc.createElement('div');
|
|
244
|
+
el.className = cls;
|
|
245
|
+
return el;
|
|
246
|
+
};
|
|
247
|
+
this.#scroller = div('nib-scroller');
|
|
248
|
+
this.#sizer = div('nib-sizer');
|
|
249
|
+
this.#gutter = div('nib-gutter');
|
|
250
|
+
this.#gutter.part.add('rail');
|
|
251
|
+
this.#content = div('nib-content');
|
|
252
|
+
this.#caretLayer = div('nib-carets');
|
|
253
|
+
const remoteLayer = div('nib-remote');
|
|
254
|
+
this.#remote = new RemoteCursors(remoteLayer, () => {
|
|
255
|
+
this.#dirt.overlays = this.#dirt.carets = true;
|
|
256
|
+
this.#schedule();
|
|
257
|
+
});
|
|
258
|
+
signal.addEventListener('abort', () => this.#remote.dispose(), { once: true });
|
|
259
|
+
this.#probe = div('nib-probe');
|
|
260
|
+
this.#probe.appendChild(doc.createElement('span'));
|
|
261
|
+
this.#sizer.append(this.#gutter, this.#content, remoteLayer, this.#caretLayer, this.#probe);
|
|
262
|
+
if (options.blocks) {
|
|
263
|
+
this.#blockLayer = div('nib-blocks');
|
|
264
|
+
this.#sizer.appendChild(this.#blockLayer);
|
|
265
|
+
}
|
|
266
|
+
this.#scroller.appendChild(this.#sizer);
|
|
267
|
+
this.#guides = new IndentGuides(this.#scroller, options.indentGuides ?? true);
|
|
268
|
+
this.#whitespace = options.whitespace ?? 'selection';
|
|
269
|
+
const base = sheetFor(root);
|
|
270
|
+
base.replaceSync(baseCss + guidesCss + remoteCss);
|
|
271
|
+
this.#tokenSheet = sheetFor(root);
|
|
272
|
+
const overlaySheet = sheetFor(root);
|
|
273
|
+
overlaySheet.replaceSync(this.#painter.css(overlayColors));
|
|
274
|
+
const ours = [base, this.#tokenSheet, overlaySheet];
|
|
275
|
+
root.adoptedStyleSheets = [...root.adoptedStyleSheets, ...ours];
|
|
276
|
+
root.appendChild(this.#scroller);
|
|
277
|
+
this.#calibrate();
|
|
278
|
+
const view = this;
|
|
279
|
+
this.#layout = {
|
|
280
|
+
moveVertically: (pos, dir, goalX) => this.#moveVertically(pos, dir, goalX),
|
|
281
|
+
get pageLines() {
|
|
282
|
+
return Math.max(1, Math.floor(view.#viewportHeight / view.#lineHeight));
|
|
283
|
+
},
|
|
284
|
+
};
|
|
285
|
+
core.listen((u) => this.#onUpdate(u), signal);
|
|
286
|
+
const offTokens = bridge.onChange((c) => this.#onTokens(c));
|
|
287
|
+
this.#scroller.addEventListener('scroll', () => {
|
|
288
|
+
this.#topLine = 0; // unknown until the next frame reads it, so nothing anchors to the old one
|
|
289
|
+
this.#dirt.viewport = true;
|
|
290
|
+
this.#schedule();
|
|
291
|
+
}, { passive: true, signal });
|
|
292
|
+
const ro = new ResizeObserver(() => {
|
|
293
|
+
this.#scrollbar = -1;
|
|
294
|
+
this.#needsCalibrate = true;
|
|
295
|
+
this.#dirt.viewport = true;
|
|
296
|
+
this.#schedule();
|
|
297
|
+
});
|
|
298
|
+
ro.observe(this.#scroller);
|
|
299
|
+
ro.observe(this.#probe.firstElementChild);
|
|
300
|
+
this.addOverlaySource(selectionSource, signal);
|
|
301
|
+
this.addOverlaySource(selectionMatchSource, signal);
|
|
302
|
+
const blocks = options.blocks;
|
|
303
|
+
if (blocks) {
|
|
304
|
+
this.#blockObserver = new ResizeObserver((entries) => this.#onBlockResize(entries));
|
|
305
|
+
const off = blocks.subscribe(() => {
|
|
306
|
+
const live = new Set(blocks.widgets().map((w) => w.key));
|
|
307
|
+
for (const key of this.#blockHeights.keys())
|
|
308
|
+
if (!live.has(key))
|
|
309
|
+
this.#blockHeights.delete(key);
|
|
310
|
+
this.#reanchor = true;
|
|
311
|
+
this.#dirt.viewport = this.#dirt.carets = this.#dirt.overlays = true;
|
|
312
|
+
this.#schedule();
|
|
313
|
+
});
|
|
314
|
+
signal.addEventListener('abort', off, { once: true });
|
|
315
|
+
}
|
|
316
|
+
const folding = options.folding;
|
|
317
|
+
if (folding) {
|
|
318
|
+
this.addLineClassSource(folding.lineClasses, signal);
|
|
319
|
+
const off = folding.subscribe(() => {
|
|
320
|
+
this.#heights = this.#makeHeights();
|
|
321
|
+
this.#dirt.viewport = this.#dirt.carets = this.#dirt.overlays = true;
|
|
322
|
+
this.#schedule();
|
|
323
|
+
});
|
|
324
|
+
signal.addEventListener('abort', off, { once: true });
|
|
325
|
+
}
|
|
326
|
+
signal.addEventListener('abort', () => {
|
|
327
|
+
cancelAnimationFrame(this.#rafId);
|
|
328
|
+
this.#frameRequested = true; // nothing schedules again after teardown
|
|
329
|
+
this.#disposed = true;
|
|
330
|
+
ro.disconnect();
|
|
331
|
+
this.#blockObserver?.disconnect();
|
|
332
|
+
for (const m of this.#mountedBlocks.values())
|
|
333
|
+
m.ac.abort();
|
|
334
|
+
this.#mountedBlocks.clear();
|
|
335
|
+
offTokens();
|
|
336
|
+
this.#painter.dispose();
|
|
337
|
+
this.#scroller.remove();
|
|
338
|
+
root.adoptedStyleSheets = root.adoptedStyleSheets.filter((s) => !ours.includes(s));
|
|
339
|
+
}, { once: true });
|
|
340
|
+
this.#schedule();
|
|
341
|
+
}
|
|
342
|
+
/** For vertical motion commands. */
|
|
343
|
+
get layout() {
|
|
344
|
+
return this.#layout;
|
|
345
|
+
}
|
|
346
|
+
/** Segments the overlay budget dropped in the last frame. */
|
|
347
|
+
get droppedOverlays() {
|
|
348
|
+
return this.#painter.dropped;
|
|
349
|
+
}
|
|
350
|
+
/** Current vertical geometry, for a follower that positions its own element in the content
|
|
351
|
+
* layer (ScopeFocus) instead of asking the view to draw it. */
|
|
352
|
+
get heights() {
|
|
353
|
+
return this.#heights;
|
|
354
|
+
}
|
|
355
|
+
/** Turn indent guides on or off. */
|
|
356
|
+
setIndentGuides(on) {
|
|
357
|
+
this.#guides.enabled = on;
|
|
358
|
+
this.#schedule();
|
|
359
|
+
}
|
|
360
|
+
/** Change which whitespace is marked. Every mounted line is checked again. */
|
|
361
|
+
setWhitespace(mode) {
|
|
362
|
+
if (mode === this.#whitespace)
|
|
363
|
+
return;
|
|
364
|
+
this.#whitespace = mode;
|
|
365
|
+
this.#dirt.lines = 'all';
|
|
366
|
+
this.#dirt.marks = true;
|
|
367
|
+
this.#schedule();
|
|
368
|
+
}
|
|
369
|
+
/** The indent unit guides are drawn with. For tests and tools. */
|
|
370
|
+
get guides() {
|
|
371
|
+
return this.#guides;
|
|
372
|
+
}
|
|
373
|
+
/** Carets show only while focused, and blink then. */
|
|
374
|
+
setFocused(focused) {
|
|
375
|
+
this.#scroller.classList.toggle('nib-focused', focused);
|
|
376
|
+
this.#scroller.classList.toggle('nib-blurred', !focused);
|
|
377
|
+
}
|
|
378
|
+
/**
|
|
379
|
+
* Document offset for a pointer. A press (`drag` false) returns null on the scrollbars or
|
|
380
|
+
* outside the scroller; inside, it clamps to the content, so the gutter and the space below the
|
|
381
|
+
* last line still hit text. A drag clamps from anywhere to the visible content.
|
|
382
|
+
*/
|
|
383
|
+
pointerPos(clientX, clientY, drag) {
|
|
384
|
+
const s = this.#scroller;
|
|
385
|
+
const outer = s.getBoundingClientRect();
|
|
386
|
+
const left = outer.left + s.clientLeft;
|
|
387
|
+
const top = outer.top + s.clientTop;
|
|
388
|
+
const right = left + s.clientWidth;
|
|
389
|
+
const bottom = top + s.clientHeight;
|
|
390
|
+
if (!drag && (clientX < left || clientX >= right || clientY < top || clientY >= bottom))
|
|
391
|
+
return null;
|
|
392
|
+
const rect = this.#content.getBoundingClientRect();
|
|
393
|
+
const visibleY = Math.max(top, Math.min(clientY, bottom - 1));
|
|
394
|
+
const n = this.#heights.lineAt(Math.max(0, Math.min(visibleY - rect.top, this.#heights.totalHeight - 1)));
|
|
395
|
+
const box = this.#boxes.get(n);
|
|
396
|
+
if (!box)
|
|
397
|
+
return null;
|
|
398
|
+
const line = this.#core.state.text.line(n);
|
|
399
|
+
const x = Math.max(0, Math.min(clientX, right) - rect.left);
|
|
400
|
+
return line.from + Math.min(this.#measurer.offsetAt(box, x), line.to - line.from);
|
|
401
|
+
}
|
|
402
|
+
/** Document offset under a client point, or null outside the content. Used for clicks and
|
|
403
|
+
* Alt-click. Hit-tests lines via heightMap.lineAt, columns via measurer.offsetAt. */
|
|
404
|
+
posAtCoords(clientX, clientY) {
|
|
405
|
+
const outer = this.#scroller.getBoundingClientRect();
|
|
406
|
+
if (clientX < outer.left || clientX >= outer.right || clientY < outer.top || clientY >= outer.bottom)
|
|
407
|
+
return null;
|
|
408
|
+
const rect = this.#content.getBoundingClientRect();
|
|
409
|
+
const y = clientY - rect.top;
|
|
410
|
+
if (clientX < rect.left || y < 0 || y >= this.#heights.totalHeight)
|
|
411
|
+
return null;
|
|
412
|
+
const n = this.#heights.lineAt(y);
|
|
413
|
+
const box = this.#boxes.get(n);
|
|
414
|
+
if (!box)
|
|
415
|
+
return null;
|
|
416
|
+
const line = this.#core.state.text.line(n);
|
|
417
|
+
return line.from + Math.min(this.#measurer.offsetAt(box, clientX - rect.left), line.to - line.from);
|
|
418
|
+
}
|
|
419
|
+
/** Client rect of a zero-width caret at pos, or null when its line is not mounted. */
|
|
420
|
+
coordsAtPos(pos) {
|
|
421
|
+
const line = this.#core.state.text.lineAt(pos);
|
|
422
|
+
const box = this.#boxes.get(line.number);
|
|
423
|
+
if (!box)
|
|
424
|
+
return null;
|
|
425
|
+
const rect = this.#content.getBoundingClientRect();
|
|
426
|
+
const x = this.#measurer.xAt(box, pos - line.from);
|
|
427
|
+
return new DOMRect(rect.left + x, rect.top + this.#heights.topOf(line.number), 0, this.#heights.heightOf(line.number));
|
|
428
|
+
}
|
|
429
|
+
addOverlaySource(source, signal) {
|
|
430
|
+
if (signal?.aborted)
|
|
431
|
+
return;
|
|
432
|
+
this.#overlaySources.push(source);
|
|
433
|
+
const off = source.subscribe(() => {
|
|
434
|
+
this.#dirt.overlays = true;
|
|
435
|
+
this.#schedule();
|
|
436
|
+
});
|
|
437
|
+
signal?.addEventListener('abort', () => {
|
|
438
|
+
off();
|
|
439
|
+
this.#overlaySources = this.#overlaySources.filter((s) => s !== source);
|
|
440
|
+
this.#dirt.overlays = true;
|
|
441
|
+
this.#schedule();
|
|
442
|
+
}, { once: true });
|
|
443
|
+
this.#dirt.overlays = true;
|
|
444
|
+
this.#schedule();
|
|
445
|
+
}
|
|
446
|
+
/** Scrolls `pos` into view on the next frame without moving the selection, as following someone does. */
|
|
447
|
+
revealPos(pos) {
|
|
448
|
+
this.#revealAt = pos;
|
|
449
|
+
this.#dirt.reveal = true;
|
|
450
|
+
this.#schedule();
|
|
451
|
+
}
|
|
452
|
+
/** Collaborators' carets, labels, and selections. */
|
|
453
|
+
addRemoteCursorSource(source, signal) {
|
|
454
|
+
this.#remote.add(source, signal);
|
|
455
|
+
}
|
|
456
|
+
addLineClassSource(source, signal) {
|
|
457
|
+
if (signal?.aborted)
|
|
458
|
+
return;
|
|
459
|
+
this.#lineClassSources.push(source);
|
|
460
|
+
const invalidate = () => {
|
|
461
|
+
this.#dirt.lines = 'all';
|
|
462
|
+
this.#schedule();
|
|
463
|
+
};
|
|
464
|
+
const off = source.subscribe(invalidate);
|
|
465
|
+
signal?.addEventListener('abort', () => {
|
|
466
|
+
off();
|
|
467
|
+
this.#lineClassSources = this.#lineClassSources.filter((s) => s !== source);
|
|
468
|
+
invalidate();
|
|
469
|
+
}, { once: true });
|
|
470
|
+
invalidate();
|
|
471
|
+
}
|
|
472
|
+
/** Height of the whole document's content, block widgets included. */
|
|
473
|
+
get contentHeight() {
|
|
474
|
+
return this.#heights.totalHeight;
|
|
475
|
+
}
|
|
476
|
+
/**
|
|
477
|
+
* Space the scroller's horizontal scrollbar takes, 0 with overlay scrollbars. It reads layout,
|
|
478
|
+
* so it is read again only after the content width or the scroller's size changed; reading it
|
|
479
|
+
* on every frame forced a layout per keystroke in an inline expansion.
|
|
480
|
+
*/
|
|
481
|
+
get scrollbarHeight() {
|
|
482
|
+
if (this.#scrollbar < 0)
|
|
483
|
+
this.#scrollbar = this.#scroller.offsetHeight - this.#scroller.clientHeight;
|
|
484
|
+
return this.#scrollbar;
|
|
485
|
+
}
|
|
486
|
+
#scrollbar = -1;
|
|
487
|
+
/** Line height in px, as last calibrated. */
|
|
488
|
+
get lineHeight() {
|
|
489
|
+
return this.#lineHeight;
|
|
490
|
+
}
|
|
491
|
+
/** `lineNumbers` changed: write every gutter number again on the next frame. */
|
|
492
|
+
invalidateGutter() {
|
|
493
|
+
this.#dirt.viewport = true;
|
|
494
|
+
this.#schedule();
|
|
495
|
+
}
|
|
496
|
+
/** Called after each frame with fresh geometry; the element forwards it to the InputSurface. */
|
|
497
|
+
onFrame(listener) {
|
|
498
|
+
this.#frameListeners.push(listener);
|
|
499
|
+
return () => {
|
|
500
|
+
this.#frameListeners = this.#frameListeners.filter((l) => l !== listener);
|
|
501
|
+
};
|
|
502
|
+
}
|
|
503
|
+
/** Called from core.listen. O(changed lines); no DOM access. */
|
|
504
|
+
#onUpdate(u) {
|
|
505
|
+
if (u.origin === 'host.set') {
|
|
506
|
+
this.#skipJumpHold = true;
|
|
507
|
+
this.#widthFromText = 'reset';
|
|
508
|
+
this.#guides.resetUnit();
|
|
509
|
+
}
|
|
510
|
+
else if (u.docChanged && this.#widthFromText === false)
|
|
511
|
+
this.#widthFromText = 'grow';
|
|
512
|
+
const dirt = this.#dirt;
|
|
513
|
+
if (u.docChanged) {
|
|
514
|
+
const before = u.before.text;
|
|
515
|
+
const after = u.after.text;
|
|
516
|
+
this.#heights = this.#heights.update(u.changes, before, after);
|
|
517
|
+
const edits = [];
|
|
518
|
+
// A remote burst is hundreds of scattered single-character inserts in one ChangeSet, so this
|
|
519
|
+
// runs hundreds of times per Update; `lineAt` is a tree descent, so the two calls this skips
|
|
520
|
+
// when they're redundant (a point edit can't straddle a line break on that side, and no
|
|
521
|
+
// inserted text means the new side can't gain one either) are worth skipping.
|
|
522
|
+
u.changes.iterChanges((fromA, toA, fromB, toB, inserted) => {
|
|
523
|
+
const oldLo = before.lineAt(fromA).number;
|
|
524
|
+
const newLo = after.lineAt(fromB).number;
|
|
525
|
+
edits.push({
|
|
526
|
+
oldLo,
|
|
527
|
+
oldHi: toA === fromA ? oldLo : before.lineAt(toA).number,
|
|
528
|
+
newLo,
|
|
529
|
+
newHi: toB === fromB || !inserted.includes('\n') ? newLo : after.lineAt(toB).number,
|
|
530
|
+
});
|
|
531
|
+
});
|
|
532
|
+
this.#guides.edited(edits, u.after.version);
|
|
533
|
+
// An expansion edits its definition wherever it is, often far above the viewport, so the
|
|
534
|
+
// lines on screen would jump with every Enter typed inside it. Keep the top line in place
|
|
535
|
+
// when the edit is wholly above it, as overflow-anchor would. Only with block widgets, so
|
|
536
|
+
// the view without them behaves as before.
|
|
537
|
+
if (this.#blocks && this.#topLine > 0 && edits[0] && edits[0].oldHi < this.#topLine) {
|
|
538
|
+
const k = mapLine(edits, this.#topLine);
|
|
539
|
+
if (k !== null) {
|
|
540
|
+
this.#topLine = k;
|
|
541
|
+
this.#pinTop = true;
|
|
542
|
+
}
|
|
543
|
+
}
|
|
544
|
+
if (edits.some((e) => e.oldHi - e.oldLo !== e.newHi - e.newLo)) {
|
|
545
|
+
this.#rekey(edits);
|
|
546
|
+
dirt.viewport = true;
|
|
547
|
+
}
|
|
548
|
+
if (dirt.lines !== 'all') {
|
|
549
|
+
// Lines outside the mounted range get a fresh box (and a render) when they mount.
|
|
550
|
+
const { from, to } = this.#mounted;
|
|
551
|
+
for (const e of edits)
|
|
552
|
+
for (let n = Math.max(e.newLo, from); n <= Math.min(e.newHi, to - 1); n++)
|
|
553
|
+
dirt.lines.add(n);
|
|
554
|
+
}
|
|
555
|
+
}
|
|
556
|
+
dirt.overlays = true;
|
|
557
|
+
dirt.carets = true;
|
|
558
|
+
dirt.reveal ||= u.scrollIntoView;
|
|
559
|
+
if (this.#whitespace === 'selection' && (u.docChanged || u.before.selection !== u.after.selection))
|
|
560
|
+
dirt.marks = true;
|
|
561
|
+
this.#schedule();
|
|
562
|
+
}
|
|
563
|
+
/** Line structure changed: move every box (and pending dirty line) to its new line number. */
|
|
564
|
+
#rekey(edits) {
|
|
565
|
+
const next = new Map();
|
|
566
|
+
for (const [n, box] of this.#boxes) {
|
|
567
|
+
const k = mapLine(edits, n);
|
|
568
|
+
if (k === null || next.has(k))
|
|
569
|
+
this.#pool.push(box);
|
|
570
|
+
else {
|
|
571
|
+
box.line = k;
|
|
572
|
+
next.set(k, box);
|
|
573
|
+
}
|
|
574
|
+
}
|
|
575
|
+
this.#boxes = next;
|
|
576
|
+
let lo = Infinity;
|
|
577
|
+
let hi = 0;
|
|
578
|
+
for (const n of next.keys()) {
|
|
579
|
+
lo = Math.min(lo, n);
|
|
580
|
+
hi = Math.max(hi, n + 1);
|
|
581
|
+
}
|
|
582
|
+
if (hi > 0)
|
|
583
|
+
this.#mounted = { from: lo, to: hi };
|
|
584
|
+
const dirt = this.#dirt;
|
|
585
|
+
if (dirt.lines !== 'all') {
|
|
586
|
+
const moved = new Set();
|
|
587
|
+
for (const n of dirt.lines) {
|
|
588
|
+
const k = mapLine(edits, n);
|
|
589
|
+
if (k !== null)
|
|
590
|
+
moved.add(k);
|
|
591
|
+
}
|
|
592
|
+
dirt.lines = moved;
|
|
593
|
+
}
|
|
594
|
+
}
|
|
595
|
+
#awaitTimer = null;
|
|
596
|
+
#releaseAwait = false;
|
|
597
|
+
/** True while the current hold is a jump's (see `awaitTokens`'s `blank` parameter). */
|
|
598
|
+
#holdBlank = false;
|
|
599
|
+
/** Lines blanked instead of plain-painted during the current hold; forced to render on release. */
|
|
600
|
+
#heldLines = new Set();
|
|
601
|
+
/**
|
|
602
|
+
* The jump-hold check in `#frame` treats "the mounted range just moved" as a scroll, but the
|
|
603
|
+
* same condition is also true on construction (before anything has ever mounted) and on the
|
|
604
|
+
* frame right after a document reset (`EditorCore.reset`, origin `'host.set'`), neither of which
|
|
605
|
+
* is a jump: both are `element.ts`'s call to hold (or, with `highlightHold` 0, its call to not
|
|
606
|
+
* hold), and a document just opening behaves nothing like scrolling into a cold region of one
|
|
607
|
+
* already open. Set on construction and on every `'host.set'` Update; cleared after one frame.
|
|
608
|
+
*/
|
|
609
|
+
#skipJumpHold = true;
|
|
610
|
+
/**
|
|
611
|
+
* Hide text until every visible line has tokens, for at most `ms`, so a document that opens with
|
|
612
|
+
* a grammar first paints colored instead of flashing plain or coloring in batch by batch (the
|
|
613
|
+
* worker flushes every slice). Layout, gutter, carets and selection stay visible. If the
|
|
614
|
+
* tokenizer is slow or fails, plain text shows when the time runs out.
|
|
615
|
+
*
|
|
616
|
+
* A far jump (`#frame`) calls this too, for `JUMP_HOLD_MS`, with `blank: true`: its freshly
|
|
617
|
+
* mounted lines that have no cached tokens are cleared instead of rendered plain (`#blankLine`),
|
|
618
|
+
* so a jump costs one render per line instead of a plain one immediately followed by a colored
|
|
619
|
+
* one. It marks the content `nib-jump-hold`, which hides nothing: lines that already have
|
|
620
|
+
* tokens stay painted. The document-open hold (`blank: false`, the default) keeps rendering plain text under the
|
|
621
|
+
* CSS hide, because `element.ts`'s hold spans a one-time cold load rather than a per-jump cost.
|
|
622
|
+
*/
|
|
623
|
+
awaitTokens(ms, blank = false) {
|
|
624
|
+
if (ms <= 0)
|
|
625
|
+
return;
|
|
626
|
+
if (this.#awaitTimer)
|
|
627
|
+
clearTimeout(this.#awaitTimer);
|
|
628
|
+
// A jump's hold blanks only its uncolored lines and leaves colored ones visible, so a fast
|
|
629
|
+
// scroll that outruns the prefetch never hides text that is already right.
|
|
630
|
+
this.#content.classList.add(blank ? 'nib-jump-hold' : 'nib-awaiting');
|
|
631
|
+
this.#releaseAwait = false;
|
|
632
|
+
this.#holdBlank = blank;
|
|
633
|
+
this.#awaitTimer = setTimeout(() => {
|
|
634
|
+
this.#releaseAwait = true;
|
|
635
|
+
this.#schedule();
|
|
636
|
+
}, ms);
|
|
637
|
+
}
|
|
638
|
+
#onTokens(c) {
|
|
639
|
+
if (c.reset)
|
|
640
|
+
this.#tokenSheet.replaceSync(c.styles.map(styleRule).join('\n'));
|
|
641
|
+
else
|
|
642
|
+
for (const s of c.styles)
|
|
643
|
+
this.#tokenSheet.insertRule(styleRule(s), this.#tokenSheet.cssRules.length);
|
|
644
|
+
const dirt = this.#dirt;
|
|
645
|
+
if (dirt.lines !== 'all')
|
|
646
|
+
for (const n of c.lines)
|
|
647
|
+
dirt.lines.add(n);
|
|
648
|
+
this.#schedule();
|
|
649
|
+
}
|
|
650
|
+
/** One rAF per burst of updates. Idempotent. */
|
|
651
|
+
#schedule() {
|
|
652
|
+
if (this.#frameRequested)
|
|
653
|
+
return;
|
|
654
|
+
this.#frameRequested = true;
|
|
655
|
+
this.#rafId = requestAnimationFrame(() => {
|
|
656
|
+
this.#frameRequested = false;
|
|
657
|
+
this.#frame();
|
|
658
|
+
});
|
|
659
|
+
}
|
|
660
|
+
#makeHeights() {
|
|
661
|
+
const folding = this.#folding;
|
|
662
|
+
const base = FoldedHeightMap.create(this.#core.state.text.lines, this.#lineHeight, folding ? () => folding.hidden() : () => []);
|
|
663
|
+
return this.#blocks ? BlockHeightMap.create(base, () => this.#slots()) : base;
|
|
664
|
+
}
|
|
665
|
+
#slots() {
|
|
666
|
+
return this.#blocks.widgets().map((w) => ({ line: w.line, height: this.#blockHeights.get(w.key) ?? w.estimate(this.#lineHeight) }));
|
|
667
|
+
}
|
|
668
|
+
/**
|
|
669
|
+
* A mounted widget changed size. Runs the frame now, inside the ResizeObserver callback, so the
|
|
670
|
+
* lines below move in the same frame the widget grew: a rAF later would paint one frame with
|
|
671
|
+
* the widget overlapping them. Removed hosts report 0; only mounted ones count.
|
|
672
|
+
*/
|
|
673
|
+
#onBlockResize(entries) {
|
|
674
|
+
let changed = false;
|
|
675
|
+
for (const e of entries) {
|
|
676
|
+
const key = this.#blockKey.get(e.target);
|
|
677
|
+
if (key === undefined || !this.#mountedBlocks.has(key))
|
|
678
|
+
continue;
|
|
679
|
+
const h = e.borderBoxSize?.[0]?.blockSize ?? e.contentRect.height;
|
|
680
|
+
if (this.#blockHeights.get(key) === h)
|
|
681
|
+
continue;
|
|
682
|
+
this.#blockHeights.set(key, h);
|
|
683
|
+
changed = true;
|
|
684
|
+
}
|
|
685
|
+
if (!changed || this.#disposed)
|
|
686
|
+
return;
|
|
687
|
+
this.#reanchor = true;
|
|
688
|
+
this.#dirt.viewport = true;
|
|
689
|
+
cancelAnimationFrame(this.#rafId);
|
|
690
|
+
this.#frameRequested = false;
|
|
691
|
+
this.#inObserver = true;
|
|
692
|
+
try {
|
|
693
|
+
this.#frame();
|
|
694
|
+
}
|
|
695
|
+
finally {
|
|
696
|
+
this.#inObserver = false;
|
|
697
|
+
}
|
|
698
|
+
}
|
|
699
|
+
#inObserver = false;
|
|
700
|
+
/** True while a frame runs inside a ResizeObserver callback. A frame listener that resizes an
|
|
701
|
+
* observed element then should wait for the next frame, or the browser reports a loop. */
|
|
702
|
+
get inResizeObserver() {
|
|
703
|
+
return this.#inObserver;
|
|
704
|
+
}
|
|
705
|
+
/**
|
|
706
|
+
* Mount, place, and unmount block widgets with their anchor lines: a widget is in the DOM exactly
|
|
707
|
+
* when its line is mounted and not folded away. Widgets on one line stack in list order.
|
|
708
|
+
*/
|
|
709
|
+
#placeBlocks(from, to, heights) {
|
|
710
|
+
const layer = this.#blockLayer;
|
|
711
|
+
const seen = new Set();
|
|
712
|
+
let line = -1;
|
|
713
|
+
let offset = 0;
|
|
714
|
+
for (const w of this.#blocks.widgets()) {
|
|
715
|
+
if (w.line < from || w.line >= to || heights.heightOf(w.line) === 0)
|
|
716
|
+
continue;
|
|
717
|
+
if (w.line !== line) {
|
|
718
|
+
line = w.line;
|
|
719
|
+
offset = 0;
|
|
720
|
+
}
|
|
721
|
+
const top = heights.topOf(line) + heights.heightOf(line) + offset;
|
|
722
|
+
offset += this.#blockHeights.get(w.key) ?? w.estimate(this.#lineHeight);
|
|
723
|
+
seen.add(w.key);
|
|
724
|
+
let m = this.#mountedBlocks.get(w.key);
|
|
725
|
+
if (!m) {
|
|
726
|
+
const host = layer.ownerDocument.createElement('div');
|
|
727
|
+
host.className = 'nib-block';
|
|
728
|
+
m = { host, ac: new AbortController(), top: -1 };
|
|
729
|
+
this.#mountedBlocks.set(w.key, m);
|
|
730
|
+
this.#blockKey.set(host, w.key);
|
|
731
|
+
layer.appendChild(host);
|
|
732
|
+
w.mount(host, m.ac.signal);
|
|
733
|
+
this.#blockObserver.observe(host);
|
|
734
|
+
}
|
|
735
|
+
if (m.top !== top) {
|
|
736
|
+
m.host.style.top = `${top}px`;
|
|
737
|
+
m.top = top;
|
|
738
|
+
}
|
|
739
|
+
}
|
|
740
|
+
for (const [key, m] of this.#mountedBlocks) {
|
|
741
|
+
if (seen.has(key))
|
|
742
|
+
continue;
|
|
743
|
+
this.#blockObserver.unobserve(m.host);
|
|
744
|
+
m.ac.abort();
|
|
745
|
+
m.host.remove();
|
|
746
|
+
this.#mountedBlocks.delete(key);
|
|
747
|
+
}
|
|
748
|
+
}
|
|
749
|
+
/** Read the font metrics; on a change, every vertical position and slow-path cache is stale. */
|
|
750
|
+
#calibrate() {
|
|
751
|
+
this.#measurer.calibrate(this.#probe);
|
|
752
|
+
const lh = this.#measurer.lineHeight;
|
|
753
|
+
if (lh > 0 && lh !== this.#lineHeight) {
|
|
754
|
+
this.#lineHeight = lh;
|
|
755
|
+
this.#heights = this.#makeHeights();
|
|
756
|
+
this.#scroller.style.setProperty('--nib-lh', `${lh}px`);
|
|
757
|
+
this.#dirt.lines = 'all';
|
|
758
|
+
this.#dirt.viewport = true;
|
|
759
|
+
for (const b of this.#boxes.values())
|
|
760
|
+
b.top = -1;
|
|
761
|
+
}
|
|
762
|
+
this.#measurer.invalidate(this.#boxes.values());
|
|
763
|
+
this.#contentWidth = 0;
|
|
764
|
+
this.#widthFromText ||= 'grow';
|
|
765
|
+
this.#dirt.carets = true;
|
|
766
|
+
this.#dirt.overlays = true;
|
|
767
|
+
}
|
|
768
|
+
/**
|
|
769
|
+
* The frame. Order matters for layout thrash: read (scrollTop, clientHeight), compute, write.
|
|
770
|
+
* 1. reveal: if the primary head's line is outside the visible band, set scrollTop.
|
|
771
|
+
* 2. viewport: compute mounted range; recycle boxes; bridge.setViewport.
|
|
772
|
+
* 3. lines: for each dirty mounted line, renderLine if (text, tokens, classes) changed;
|
|
773
|
+
* collect rerendered lines.
|
|
774
|
+
* 4. overlays: if dirt.overlays or anything rerendered: painter.invalidateLines(rerendered);
|
|
775
|
+
* gather sources' query(state, mountedFrom, mountedTo); painter.paint.
|
|
776
|
+
* 5. carets: position one element per range head; past-EOL selection boxes.
|
|
777
|
+
* 6. input geometry: element syncs the InputSurface after the frame (caret rect is fresh).
|
|
778
|
+
*/
|
|
779
|
+
#frame() {
|
|
780
|
+
const state = this.#core.state;
|
|
781
|
+
const text = state.text;
|
|
782
|
+
if (this.#needsCalibrate) {
|
|
783
|
+
this.#needsCalibrate = false;
|
|
784
|
+
this.#calibrate();
|
|
785
|
+
}
|
|
786
|
+
let scrollTop = this.#scroller.scrollTop;
|
|
787
|
+
const height = this.#scroller.clientHeight;
|
|
788
|
+
const scrollLeft = this.#scroller.scrollLeft;
|
|
789
|
+
const width = this.#scroller.clientWidth;
|
|
790
|
+
this.#viewportHeight = height;
|
|
791
|
+
const dirt = this.#dirt;
|
|
792
|
+
if (this.#widthFromText) {
|
|
793
|
+
if (this.#widthFromText === 'reset')
|
|
794
|
+
this.#setContentWidth(0);
|
|
795
|
+
this.#widthFromText = false;
|
|
796
|
+
this.#growWidth(text.maxLineLength * this.#measurer.charWidth);
|
|
797
|
+
}
|
|
798
|
+
this.#dirt = cleanDirt();
|
|
799
|
+
// Scroll anchoring for block widgets (the scroller has overflow-anchor: none): keep the line at
|
|
800
|
+
// the top of the viewport, and how far into it or its widgets the top is, where it was.
|
|
801
|
+
let anchoredTop = -1;
|
|
802
|
+
if (this.#reanchor) {
|
|
803
|
+
this.#reanchor = false;
|
|
804
|
+
const old = this.#heights;
|
|
805
|
+
const n = old.lineAt(scrollTop);
|
|
806
|
+
const into = scrollTop - old.topOf(n);
|
|
807
|
+
this.#heights = this.#makeHeights();
|
|
808
|
+
anchoredTop = this.#heights.topOf(n) + Math.min(into, this.#heights.heightOf(n) + this.#heights.blockSpace(n));
|
|
809
|
+
}
|
|
810
|
+
const heights = this.#heights;
|
|
811
|
+
if (this.#pinTop) {
|
|
812
|
+
this.#pinTop = false;
|
|
813
|
+
const n = Math.min(this.#topLine, text.lines);
|
|
814
|
+
anchoredTop = heights.topOf(n) + Math.min(this.#topInto, heights.heightOf(n) + heights.blockSpace(n));
|
|
815
|
+
}
|
|
816
|
+
const total = heights.totalHeight;
|
|
817
|
+
if (total !== this.#sizerHeight) {
|
|
818
|
+
this.#sizer.style.height = `${total}px`;
|
|
819
|
+
this.#sizerHeight = total;
|
|
820
|
+
}
|
|
821
|
+
if (anchoredTop >= 0) {
|
|
822
|
+
const st = Math.max(0, Math.min(anchoredTop, total - height));
|
|
823
|
+
if (Math.abs(st - scrollTop) >= 0.5) {
|
|
824
|
+
this.#scroller.scrollTop = st;
|
|
825
|
+
scrollTop = st;
|
|
826
|
+
}
|
|
827
|
+
}
|
|
828
|
+
if (this.#blocks && width !== this.#viewWidth) {
|
|
829
|
+
this.#viewWidth = width;
|
|
830
|
+
this.#scroller.style.setProperty('--nib-view-w', `${width}px`);
|
|
831
|
+
}
|
|
832
|
+
this.#numberOffset = this.#lineNumbers?.offset() ?? 0;
|
|
833
|
+
const digits = Math.max(String(text.lines + this.#numberOffset).length, this.#lineNumbers?.digits?.() ?? 0);
|
|
834
|
+
if (digits !== this.#digits) {
|
|
835
|
+
this.#digits = digits;
|
|
836
|
+
this.#scroller.style.setProperty('--nib-digits', String(digits));
|
|
837
|
+
}
|
|
838
|
+
// A position someone asked to see (revealPos), else the primary head.
|
|
839
|
+
const revealing = Math.min(this.#revealAt ?? state.selection.primary.head, text.length);
|
|
840
|
+
if (dirt.reveal) {
|
|
841
|
+
const n = text.lineAt(revealing).number;
|
|
842
|
+
const top = heights.topOf(n);
|
|
843
|
+
const bottom = top + heights.heightOf(n);
|
|
844
|
+
let st = scrollTop;
|
|
845
|
+
if (top < st)
|
|
846
|
+
st = top;
|
|
847
|
+
else if (bottom > st + height)
|
|
848
|
+
st = bottom - height;
|
|
849
|
+
st = Math.max(0, Math.min(st, total - height));
|
|
850
|
+
if (st !== scrollTop) {
|
|
851
|
+
this.#scroller.scrollTop = st;
|
|
852
|
+
scrollTop = st;
|
|
853
|
+
}
|
|
854
|
+
}
|
|
855
|
+
const first = heights.lineAt(scrollTop);
|
|
856
|
+
const last = heights.lineAt(scrollTop + Math.max(height, 1) - 1);
|
|
857
|
+
this.#topLine = first;
|
|
858
|
+
this.#topInto = scrollTop - heights.topOf(first);
|
|
859
|
+
const from = Math.max(1, first - this.#overscan);
|
|
860
|
+
const to = Math.min(text.lines, last + this.#overscan) + 1;
|
|
861
|
+
const rangeMoved = from !== this.#mounted.from || to !== this.#mounted.to;
|
|
862
|
+
const rerendered = [];
|
|
863
|
+
/** Lines now shown by a different box, whose old StaticRanges point into the wrong DOM. */
|
|
864
|
+
const rebound = [];
|
|
865
|
+
const all = dirt.lines === 'all';
|
|
866
|
+
const dirtyLines = dirt.lines === 'all' ? null : dirt.lines;
|
|
867
|
+
if (dirt.marks)
|
|
868
|
+
this.#markSelection(state, from, to, dirtyLines);
|
|
869
|
+
// A far jump: the range just moved and the visible band (not the overscan margin) has no
|
|
870
|
+
// cached tokens at all. A small scroll stays inside the bridge's prefetch/cache margin, so
|
|
871
|
+
// this is false for it and nothing here runs. Starting a hold that is already running would
|
|
872
|
+
// reset its cap on every one of the random jumps this workload makes, so only the first jump
|
|
873
|
+
// of a run starts the timer; later jumps just add their lines to the same hold. Skipped for
|
|
874
|
+
// one frame after construction and after a document reset (see `#skipJumpHold`), both of
|
|
875
|
+
// which are `element.ts`'s call, not a scroll.
|
|
876
|
+
if (rangeMoved && !this.#skipJumpHold && !this.#awaitTimer) {
|
|
877
|
+
let allColored = true;
|
|
878
|
+
for (let n = first, end = Math.min(last, text.lines); n <= end; n++) {
|
|
879
|
+
if (this.#bridge.lineTokens(n) === undefined) {
|
|
880
|
+
allColored = false;
|
|
881
|
+
break;
|
|
882
|
+
}
|
|
883
|
+
}
|
|
884
|
+
if (!allColored)
|
|
885
|
+
this.awaitTokens(JUMP_HOLD_MS, true);
|
|
886
|
+
}
|
|
887
|
+
this.#skipJumpHold = false;
|
|
888
|
+
const holdingBlank = this.#awaitTimer !== null && this.#holdBlank;
|
|
889
|
+
const remount = rangeMoved || dirt.viewport || all || this.#pool.length > 0;
|
|
890
|
+
if (remount) {
|
|
891
|
+
if (rangeMoved)
|
|
892
|
+
this.#bridge.setViewport(from, to);
|
|
893
|
+
const free = this.#pool;
|
|
894
|
+
this.#pool = [];
|
|
895
|
+
const next = new Map();
|
|
896
|
+
for (const [n, b] of this.#boxes) {
|
|
897
|
+
if (n >= from && n < to && heights.heightOf(n) > 0)
|
|
898
|
+
next.set(n, b);
|
|
899
|
+
else
|
|
900
|
+
free.push(b);
|
|
901
|
+
}
|
|
902
|
+
for (let n = heights.heightOf(from) > 0 ? from : heights.next(from, 1); n < to; n = heights.next(n, 1)) {
|
|
903
|
+
let b = next.get(n);
|
|
904
|
+
let fresh = false;
|
|
905
|
+
if (!b) {
|
|
906
|
+
b = free.pop() ?? this.#createLine();
|
|
907
|
+
b.line = n;
|
|
908
|
+
next.set(n, b);
|
|
909
|
+
fresh = true;
|
|
910
|
+
}
|
|
911
|
+
if (fresh || all || dirtyLines.has(n)) {
|
|
912
|
+
if (fresh && holdingBlank && this.#bridge.lineTokens(n) === undefined) {
|
|
913
|
+
this.#blankLine(b, n);
|
|
914
|
+
this.#heldLines.add(n);
|
|
915
|
+
}
|
|
916
|
+
else if (this.#renderIfChanged(b, n, state))
|
|
917
|
+
rerendered.push(n);
|
|
918
|
+
else if (fresh)
|
|
919
|
+
rebound.push(n);
|
|
920
|
+
}
|
|
921
|
+
this.#place(b, n);
|
|
922
|
+
}
|
|
923
|
+
for (const b of free) {
|
|
924
|
+
b.el.remove();
|
|
925
|
+
b.gutter.remove();
|
|
926
|
+
}
|
|
927
|
+
this.#boxes = next;
|
|
928
|
+
this.#mounted = { from, to };
|
|
929
|
+
if (this.#blocks)
|
|
930
|
+
this.#placeBlocks(from, to, heights);
|
|
931
|
+
}
|
|
932
|
+
else {
|
|
933
|
+
for (const n of dirtyLines) {
|
|
934
|
+
const b = this.#boxes.get(n);
|
|
935
|
+
if (b && this.#renderIfChanged(b, n, state))
|
|
936
|
+
rerendered.push(n);
|
|
937
|
+
}
|
|
938
|
+
}
|
|
939
|
+
// Released in the frame that rendered the last visible line's tokens, so all of it appears
|
|
940
|
+
// already colored. A held line still blank when the hold ends (the cap ran out, or this jump's
|
|
941
|
+
// lines weren't the ones checked above) renders now, plain if the worker still hasn't answered.
|
|
942
|
+
if (this.#awaitTimer && !this.#releaseAwait) {
|
|
943
|
+
this.#releaseAwait = true;
|
|
944
|
+
for (let n = first, end = Math.min(last, text.lines); n <= end; n++) {
|
|
945
|
+
if (this.#bridge.lineTokens(n) === undefined) {
|
|
946
|
+
this.#releaseAwait = false;
|
|
947
|
+
break;
|
|
948
|
+
}
|
|
949
|
+
}
|
|
950
|
+
}
|
|
951
|
+
if (this.#releaseAwait) {
|
|
952
|
+
this.#releaseAwait = false;
|
|
953
|
+
if (this.#awaitTimer)
|
|
954
|
+
clearTimeout(this.#awaitTimer);
|
|
955
|
+
this.#awaitTimer = null;
|
|
956
|
+
this.#content.classList.remove('nib-awaiting', 'nib-jump-hold');
|
|
957
|
+
if (this.#heldLines.size > 0) {
|
|
958
|
+
for (const n of this.#heldLines) {
|
|
959
|
+
const b = this.#boxes.get(n);
|
|
960
|
+
if (b && this.#renderIfChanged(b, n, state))
|
|
961
|
+
rerendered.push(n);
|
|
962
|
+
}
|
|
963
|
+
this.#heldLines.clear();
|
|
964
|
+
}
|
|
965
|
+
}
|
|
966
|
+
this.#guides.paint(state, this.#measurer.tabSize, this.#boxes, remount);
|
|
967
|
+
if (dirt.overlays || rangeMoved || rerendered.length > 0 || rebound.length > 0) {
|
|
968
|
+
this.#painter.invalidateLines(rerendered);
|
|
969
|
+
this.#painter.invalidateLines(rebound);
|
|
970
|
+
const mFrom = text.line(from).from;
|
|
971
|
+
const mTo = text.line(to - 1).to;
|
|
972
|
+
const overlays = [];
|
|
973
|
+
for (const src of this.#overlaySources)
|
|
974
|
+
for (const o of src.query(state, mFrom, mTo))
|
|
975
|
+
overlays.push(o);
|
|
976
|
+
for (const o of this.#remote.overlays(state, mFrom, mTo))
|
|
977
|
+
overlays.push(o);
|
|
978
|
+
overlays.sort((a, b) => overlayKinds[b.kind].priority - overlayKinds[a.kind].priority || a.from - b.from);
|
|
979
|
+
this.#painter.paint(overlays, text, this.#boxes);
|
|
980
|
+
}
|
|
981
|
+
if (dirt.carets || rangeMoved || rerendered.length > 0 || all)
|
|
982
|
+
this.#drawCarets(state);
|
|
983
|
+
if (dirt.reveal) {
|
|
984
|
+
const head = revealing;
|
|
985
|
+
this.#revealAt = null;
|
|
986
|
+
const line = text.lineAt(head);
|
|
987
|
+
const box = this.#boxes.get(line.number);
|
|
988
|
+
if (box) {
|
|
989
|
+
const cw = this.#measurer.charWidth;
|
|
990
|
+
const x = this.#measurer.xAt(box, head - line.from);
|
|
991
|
+
// The sticky gutter covers the scroller's left edge, so the visible text band starts after it.
|
|
992
|
+
const band = width - cw * (this.#digits + 3);
|
|
993
|
+
let sl = scrollLeft;
|
|
994
|
+
if (x < sl + cw)
|
|
995
|
+
sl = Math.max(0, x - 4 * cw);
|
|
996
|
+
else if (x > sl + band - 2 * cw)
|
|
997
|
+
sl = x - band + 4 * cw;
|
|
998
|
+
if (sl !== scrollLeft)
|
|
999
|
+
this.#scroller.scrollLeft = sl;
|
|
1000
|
+
}
|
|
1001
|
+
}
|
|
1002
|
+
for (const l of this.#frameListeners)
|
|
1003
|
+
l();
|
|
1004
|
+
}
|
|
1005
|
+
#createLine() {
|
|
1006
|
+
const doc = this.#content.ownerDocument;
|
|
1007
|
+
const el = doc.createElement('div');
|
|
1008
|
+
el.className = 'nib-line';
|
|
1009
|
+
const gutter = doc.createElement('div');
|
|
1010
|
+
gutter.className = 'nib-gutter-line';
|
|
1011
|
+
this.#content.appendChild(el);
|
|
1012
|
+
this.#gutter.appendChild(gutter);
|
|
1013
|
+
return {
|
|
1014
|
+
el,
|
|
1015
|
+
gutter,
|
|
1016
|
+
line: 0,
|
|
1017
|
+
text: '',
|
|
1018
|
+
tokens: undefined,
|
|
1019
|
+
classes: undefined,
|
|
1020
|
+
ws: NO_RUNS,
|
|
1021
|
+
wsKey: '',
|
|
1022
|
+
starts: new Uint32Array(0),
|
|
1023
|
+
simple: true,
|
|
1024
|
+
rects: null,
|
|
1025
|
+
top: -1,
|
|
1026
|
+
shown: 0,
|
|
1027
|
+
guides: 0,
|
|
1028
|
+
};
|
|
1029
|
+
}
|
|
1030
|
+
#renderIfChanged(b, n, state) {
|
|
1031
|
+
const line = state.text.line(n);
|
|
1032
|
+
const s = state.text.slice(line.from, line.to);
|
|
1033
|
+
const tokens = this.#bridge.lineTokens(n);
|
|
1034
|
+
const classes = this.#classesFor(state, n);
|
|
1035
|
+
const key = this.#marksKey(state, line);
|
|
1036
|
+
const ws = key === b.wsKey && s === b.text ? b.ws : this.#marks(s, key);
|
|
1037
|
+
b.wsKey = key;
|
|
1038
|
+
if (b.text === s && b.tokens === tokens && b.classes === classes && sameRuns(b.ws, ws))
|
|
1039
|
+
return false;
|
|
1040
|
+
renderLine(b, s, tokens, classes, ws, this.#whitespace === 'selection');
|
|
1041
|
+
this.#folding?.line(b.el, n);
|
|
1042
|
+
const cw = this.#measurer.charWidth;
|
|
1043
|
+
this.#growWidth(b.simple ? this.#measurer.xAt(b, s.length) : s.length * 2 * cw);
|
|
1044
|
+
return true;
|
|
1045
|
+
}
|
|
1046
|
+
/**
|
|
1047
|
+
* What a line's marks depend on besides its text: the mode, or in 'selection' mode the line's
|
|
1048
|
+
* selected columns, so a line the selection did not move across keeps its key and its marks.
|
|
1049
|
+
*/
|
|
1050
|
+
#marksKey(state, line) {
|
|
1051
|
+
if (this.#whitespace !== 'selection')
|
|
1052
|
+
return this.#whitespace;
|
|
1053
|
+
const cols = selectedColumns(state, line.from, line.to);
|
|
1054
|
+
return cols.length === 0 ? '' : cols.join(',');
|
|
1055
|
+
}
|
|
1056
|
+
#marks(text, key) {
|
|
1057
|
+
const mode = this.#whitespace;
|
|
1058
|
+
if (mode === 'none' || key === '')
|
|
1059
|
+
return NO_RUNS;
|
|
1060
|
+
if (mode !== 'selection')
|
|
1061
|
+
return markedWhitespace(text, mode);
|
|
1062
|
+
const runs = markedWhitespace(text, mode, key.split(',').map(Number));
|
|
1063
|
+
return runs.length === 0 ? NO_RUNS : runs;
|
|
1064
|
+
}
|
|
1065
|
+
/**
|
|
1066
|
+
* 'selection' mode, after the selection or document changed: mark dirty only the mounted lines
|
|
1067
|
+
* whose selected columns changed, the ones the selection now touches and the ones that had marks.
|
|
1068
|
+
* A cursor-only selection that follows another one does nothing here, so typing costs nothing.
|
|
1069
|
+
*/
|
|
1070
|
+
#markSelection(state, from, to, dirty) {
|
|
1071
|
+
const text = state.text;
|
|
1072
|
+
const selected = state.selection.ranges.some((r) => !isCursor(r));
|
|
1073
|
+
const skip = !selected && !this.#selected;
|
|
1074
|
+
this.#selected = selected;
|
|
1075
|
+
// With every line dirty, rendering checks each line's marks anyway.
|
|
1076
|
+
if (skip || !dirty)
|
|
1077
|
+
return;
|
|
1078
|
+
const check = (n, b) => {
|
|
1079
|
+
if (this.#marksKey(state, text.line(n)) !== b.wsKey)
|
|
1080
|
+
dirty.add(n);
|
|
1081
|
+
};
|
|
1082
|
+
for (const [n, b] of this.#boxes)
|
|
1083
|
+
if (b.wsKey !== '')
|
|
1084
|
+
check(n, b);
|
|
1085
|
+
if (!selected)
|
|
1086
|
+
return;
|
|
1087
|
+
for (const r of state.selection.ranges) {
|
|
1088
|
+
if (isCursor(r))
|
|
1089
|
+
continue;
|
|
1090
|
+
const lo = Math.max(from, text.lineAt(rangeFrom(r)).number);
|
|
1091
|
+
const hi = Math.min(to - 1, text.lineAt(rangeTo(r)).number);
|
|
1092
|
+
for (let n = lo; n <= hi; n++) {
|
|
1093
|
+
const b = this.#boxes.get(n);
|
|
1094
|
+
if (b && b.wsKey === '')
|
|
1095
|
+
check(n, b);
|
|
1096
|
+
}
|
|
1097
|
+
}
|
|
1098
|
+
}
|
|
1099
|
+
#growWidth(width) {
|
|
1100
|
+
if (width > this.#contentWidth)
|
|
1101
|
+
this.#setContentWidth(width);
|
|
1102
|
+
}
|
|
1103
|
+
#setContentWidth(width) {
|
|
1104
|
+
this.#contentWidth = width;
|
|
1105
|
+
this.#scrollbar = -1;
|
|
1106
|
+
const pad = width > 0 ? 2 * this.#measurer.charWidth : 0;
|
|
1107
|
+
this.#sizer.style.width = width > 0 ? `calc(var(--nib-gutter) + ${Math.ceil(width + pad)}px)` : '';
|
|
1108
|
+
}
|
|
1109
|
+
/**
|
|
1110
|
+
* A fresh box in a jump's mounted range whose line has no cached tokens: clear it instead of
|
|
1111
|
+
* calling `renderLine` with plain text. Cheaper than a plain render (no text-node/slice work for
|
|
1112
|
+
* the line's content), and the box stays invisible under `nib-awaiting` until a real render
|
|
1113
|
+
* replaces it, so the line still shows only once, colored (or plain, if the hold's cap runs out
|
|
1114
|
+
* first). `b.text` left at `''` means the next dirty pass for `n` (the worker's batch, or the
|
|
1115
|
+
* hold's forced release) always calls `renderLine` for real, because an actual line of that text
|
|
1116
|
+
* would already be mounted as `''` only when it truly is empty.
|
|
1117
|
+
*/
|
|
1118
|
+
#blankLine(b, n) {
|
|
1119
|
+
if (b.el.childNodes.length > 0)
|
|
1120
|
+
b.el.replaceChildren();
|
|
1121
|
+
if (b.el.className !== 'nib-line')
|
|
1122
|
+
b.el.className = 'nib-line';
|
|
1123
|
+
b.line = n;
|
|
1124
|
+
b.text = '';
|
|
1125
|
+
b.tokens = undefined;
|
|
1126
|
+
b.classes = undefined;
|
|
1127
|
+
b.ws = NO_RUNS;
|
|
1128
|
+
b.wsKey = '';
|
|
1129
|
+
b.starts = EMPTY_STARTS;
|
|
1130
|
+
b.simple = true;
|
|
1131
|
+
b.rects = null;
|
|
1132
|
+
}
|
|
1133
|
+
#place(b, n) {
|
|
1134
|
+
const top = this.#heights.topOf(n);
|
|
1135
|
+
if (b.top !== top) {
|
|
1136
|
+
b.el.style.top = b.gutter.style.top = `${top}px`;
|
|
1137
|
+
b.top = top;
|
|
1138
|
+
}
|
|
1139
|
+
const label = n + this.#numberOffset;
|
|
1140
|
+
if (b.shown !== label) {
|
|
1141
|
+
b.gutter.textContent = String(label);
|
|
1142
|
+
b.shown = label;
|
|
1143
|
+
}
|
|
1144
|
+
this.#folding?.gutter(b.gutter, n);
|
|
1145
|
+
}
|
|
1146
|
+
#classesFor(state, n) {
|
|
1147
|
+
if (this.#lineClassSources.length === 0)
|
|
1148
|
+
return undefined;
|
|
1149
|
+
const parts = [];
|
|
1150
|
+
for (const s of this.#lineClassSources) {
|
|
1151
|
+
const c = s.classesFor(state, n);
|
|
1152
|
+
if (c)
|
|
1153
|
+
parts.push(c);
|
|
1154
|
+
}
|
|
1155
|
+
return parts.length > 0 ? parts.join(' ') : undefined;
|
|
1156
|
+
}
|
|
1157
|
+
/** Reads every position first (slow lines may measure), then writes. */
|
|
1158
|
+
#drawCarets(state) {
|
|
1159
|
+
const text = state.text;
|
|
1160
|
+
const { from, to } = this.#mounted;
|
|
1161
|
+
const heads = [];
|
|
1162
|
+
const eols = [];
|
|
1163
|
+
for (const r of state.selection.ranges) {
|
|
1164
|
+
const line = text.lineAt(r.head);
|
|
1165
|
+
const box = this.#boxes.get(line.number);
|
|
1166
|
+
if (box)
|
|
1167
|
+
heads.push({ x: this.#measurer.xAt(box, r.head - line.from), y: this.#heights.topOf(line.number) });
|
|
1168
|
+
if (isCursor(r))
|
|
1169
|
+
continue;
|
|
1170
|
+
const a = rangeFrom(r);
|
|
1171
|
+
const b = rangeTo(r);
|
|
1172
|
+
if (b < text.line(from).from || a > text.line(to - 1).to)
|
|
1173
|
+
continue;
|
|
1174
|
+
const lo = Math.max(text.lineAt(a).number, from);
|
|
1175
|
+
const hi = Math.min(text.lineAt(b).number, to - 1);
|
|
1176
|
+
for (let n = lo; n <= hi; n = this.#heights.next(n, 1)) {
|
|
1177
|
+
const l = text.line(n);
|
|
1178
|
+
const lb = this.#boxes.get(n);
|
|
1179
|
+
if (lb && l.to >= a && l.to < b)
|
|
1180
|
+
eols.push({ x: this.#measurer.xAt(lb, l.to - l.from), y: this.#heights.topOf(n) });
|
|
1181
|
+
}
|
|
1182
|
+
}
|
|
1183
|
+
this.#carets = this.#writeMarks(this.#carets, heads, 'nib-caret', 0);
|
|
1184
|
+
// Swapping between two identical keyframe names restarts the blink, so the caret stays solid
|
|
1185
|
+
// while it moves.
|
|
1186
|
+
this.#caretLayer.classList.toggle('nib-alt');
|
|
1187
|
+
this.#eols = this.#writeMarks(this.#eols, eols, 'nib-eol', Math.max(1, Math.round(this.#measurer.charWidth)));
|
|
1188
|
+
this.#remote.draw(state, (pos) => {
|
|
1189
|
+
const line = text.lineAt(Math.min(pos, text.length));
|
|
1190
|
+
const box = this.#boxes.get(line.number);
|
|
1191
|
+
return box ? { x: this.#measurer.xAt(box, Math.min(pos, text.length) - line.from), y: this.#heights.topOf(line.number) } : null;
|
|
1192
|
+
});
|
|
1193
|
+
}
|
|
1194
|
+
#writeMarks(els, marks, cls, width) {
|
|
1195
|
+
const doc = this.#caretLayer.ownerDocument;
|
|
1196
|
+
for (let i = 0; i < marks.length; i++) {
|
|
1197
|
+
let el = els[i];
|
|
1198
|
+
if (!el) {
|
|
1199
|
+
el = doc.createElement('div');
|
|
1200
|
+
el.className = cls;
|
|
1201
|
+
if (width)
|
|
1202
|
+
el.style.width = `${width}px`;
|
|
1203
|
+
this.#caretLayer.appendChild(el);
|
|
1204
|
+
els.push(el);
|
|
1205
|
+
}
|
|
1206
|
+
const m = marks[i];
|
|
1207
|
+
el.style.left = `${m.x}px`;
|
|
1208
|
+
el.style.top = `${m.y}px`;
|
|
1209
|
+
}
|
|
1210
|
+
for (let i = marks.length; i < els.length; i++)
|
|
1211
|
+
els[i].remove();
|
|
1212
|
+
return els.slice(0, marks.length);
|
|
1213
|
+
}
|
|
1214
|
+
#moveVertically(pos, dir, goalX) {
|
|
1215
|
+
const text = this.#core.state.text;
|
|
1216
|
+
const line = text.lineAt(pos);
|
|
1217
|
+
const box = this.#boxes.get(line.number);
|
|
1218
|
+
const col = pos - line.from;
|
|
1219
|
+
const gx = goalX ?? (box ? this.#measurer.xAt(box, col) : col * this.#measurer.charWidth);
|
|
1220
|
+
const t = this.#heights.next(line.number, dir);
|
|
1221
|
+
if (t < 1)
|
|
1222
|
+
return { pos: 0, goalX: gx };
|
|
1223
|
+
if (t > text.lines)
|
|
1224
|
+
return { pos: text.length, goalX: gx };
|
|
1225
|
+
const target = text.line(t);
|
|
1226
|
+
const s = text.slice(target.from, target.to);
|
|
1227
|
+
const tb = this.#boxes.get(t);
|
|
1228
|
+
const off = tb && tb.text === s ? this.#measurer.offsetAt(tb, gx) : this.#measurer.offsetAtText(s, gx);
|
|
1229
|
+
return { pos: target.from + off, goalX: gx };
|
|
1230
|
+
}
|
|
1231
|
+
}
|