@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,240 @@
|
|
|
1
|
+
function stepsOf(changes, before) {
|
|
2
|
+
const out = [];
|
|
3
|
+
let dOff = 0;
|
|
4
|
+
let dLine = 0;
|
|
5
|
+
changes.iterChanges((fromA, toA, fromB, toB, inserted) => {
|
|
6
|
+
let added = 0;
|
|
7
|
+
for (let i = inserted.indexOf('\n'); i >= 0; i = inserted.indexOf('\n', i + 1))
|
|
8
|
+
added++;
|
|
9
|
+
const removed = toA > fromA ? before.lineAt(toA).number - before.lineAt(fromA).number : 0;
|
|
10
|
+
dOff += toB - fromB - (toA - fromA);
|
|
11
|
+
dLine += added - removed;
|
|
12
|
+
out.push({ fromA, toA, dOff, dLine });
|
|
13
|
+
});
|
|
14
|
+
return out;
|
|
15
|
+
}
|
|
16
|
+
/** Index of the last step with fromA <= pos, or -1. */
|
|
17
|
+
function stepAt(steps, pos) {
|
|
18
|
+
let lo = 0;
|
|
19
|
+
let hi = steps.length - 1;
|
|
20
|
+
let found = -1;
|
|
21
|
+
while (lo <= hi) {
|
|
22
|
+
const mid = (lo + hi) >> 1;
|
|
23
|
+
if (steps[mid].fromA <= pos) {
|
|
24
|
+
found = mid;
|
|
25
|
+
lo = mid + 1;
|
|
26
|
+
}
|
|
27
|
+
else
|
|
28
|
+
hi = mid - 1;
|
|
29
|
+
}
|
|
30
|
+
return found;
|
|
31
|
+
}
|
|
32
|
+
/** Hidden spans of a list of folds sorted by header, merged where they overlap or touch. */
|
|
33
|
+
function spansOf(folds) {
|
|
34
|
+
const out = [];
|
|
35
|
+
for (const f of folds) {
|
|
36
|
+
const last = out.at(-1);
|
|
37
|
+
if (last && f.line + 1 <= last[1] + 1)
|
|
38
|
+
last[1] = Math.max(last[1], f.end);
|
|
39
|
+
else
|
|
40
|
+
out.push([f.line + 1, f.end]);
|
|
41
|
+
}
|
|
42
|
+
return out;
|
|
43
|
+
}
|
|
44
|
+
/** Index of the span containing `line`, or -1. Spans are sorted and disjoint. */
|
|
45
|
+
export function spanIndex(spans, line) {
|
|
46
|
+
let lo = 0;
|
|
47
|
+
let hi = spans.length - 1;
|
|
48
|
+
while (lo <= hi) {
|
|
49
|
+
const mid = (lo + hi) >> 1;
|
|
50
|
+
const s = spans[mid];
|
|
51
|
+
if (line < s[0])
|
|
52
|
+
hi = mid - 1;
|
|
53
|
+
else if (line > s[1])
|
|
54
|
+
lo = mid + 1;
|
|
55
|
+
else
|
|
56
|
+
return mid;
|
|
57
|
+
}
|
|
58
|
+
return -1;
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* The nearest line from `line` in `dir` that is not hidden: 0 above the first line, `lines + 1`
|
|
62
|
+
* below the last. Merged spans never touch, so one jump is enough.
|
|
63
|
+
*/
|
|
64
|
+
export function nextVisibleLine(spans, line, dir, lines) {
|
|
65
|
+
const t = line + dir;
|
|
66
|
+
if (t < 1)
|
|
67
|
+
return 0;
|
|
68
|
+
if (t > lines)
|
|
69
|
+
return lines + 1;
|
|
70
|
+
const i = spanIndex(spans, t);
|
|
71
|
+
if (i < 0)
|
|
72
|
+
return t;
|
|
73
|
+
const s = spans[i];
|
|
74
|
+
return dir > 0 ? (s[1] + 1 > lines ? lines + 1 : s[1] + 1) : s[0] - 1;
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* Which regions are folded. A follower of EditorCore with no DOM, so a terminal view can use it
|
|
78
|
+
* as the element does. Discovering what *can* fold is a separate FoldRangeSource.
|
|
79
|
+
*
|
|
80
|
+
* Every Update maps each fold through its ChangeSet in one sweep: O(folds + changes x log lines).
|
|
81
|
+
* A fold is dropped when an edit deletes its header's line break (the header line is gone or
|
|
82
|
+
* merged into the next), or when it ends up hiding no lines. Edits inside a fold keep it.
|
|
83
|
+
*
|
|
84
|
+
* When a selection head lands on a hidden line, every fold hiding that line opens. Vertical motion
|
|
85
|
+
* skips hidden lines, so only jumps put a head there: find, undo, a host or remote selection, or
|
|
86
|
+
* a horizontal step across the fold's edge. Opening is the one answer that is right for all of
|
|
87
|
+
* them, because the caret is never invisible.
|
|
88
|
+
*/
|
|
89
|
+
export class FoldState {
|
|
90
|
+
#core;
|
|
91
|
+
/** Sorted by header line. Nested folds are kept: opening an outer one leaves an inner one shut. */
|
|
92
|
+
#folds = [];
|
|
93
|
+
#spans = [];
|
|
94
|
+
#listeners = [];
|
|
95
|
+
constructor(core, signal) {
|
|
96
|
+
this.#core = core;
|
|
97
|
+
core.listen((u) => this.#onUpdate(u), signal);
|
|
98
|
+
}
|
|
99
|
+
/** Folded regions, sorted by header line, current as of `core.state`. */
|
|
100
|
+
get ranges() {
|
|
101
|
+
return this.#folds;
|
|
102
|
+
}
|
|
103
|
+
/** Hidden lines as merged, sorted spans. */
|
|
104
|
+
get hidden() {
|
|
105
|
+
return this.#spans;
|
|
106
|
+
}
|
|
107
|
+
isFolded(line) {
|
|
108
|
+
return this.#indexOf(line) >= 0;
|
|
109
|
+
}
|
|
110
|
+
isHidden(line) {
|
|
111
|
+
return spanIndex(this.#spans, line) >= 0;
|
|
112
|
+
}
|
|
113
|
+
/** Fold `range`. False when it hides nothing or is already folded. */
|
|
114
|
+
fold(range) {
|
|
115
|
+
const text = this.#core.state.text;
|
|
116
|
+
const { line, end } = range;
|
|
117
|
+
if (!(line >= 1 && end > line && end <= text.lines))
|
|
118
|
+
return false;
|
|
119
|
+
const at = this.#indexOf(line);
|
|
120
|
+
if (at >= 0 && this.#folds[at].end === end)
|
|
121
|
+
return false;
|
|
122
|
+
const f = { eol: text.line(line).to, to: text.line(end).to, line, end };
|
|
123
|
+
const folds = this.#folds.filter((g) => g.line !== line);
|
|
124
|
+
let i = folds.findIndex((g) => g.line > line);
|
|
125
|
+
if (i < 0)
|
|
126
|
+
i = folds.length;
|
|
127
|
+
folds.splice(i, 0, f);
|
|
128
|
+
this.#set(folds);
|
|
129
|
+
return true;
|
|
130
|
+
}
|
|
131
|
+
/** Open the fold whose header is `line`. False when there is none. */
|
|
132
|
+
unfold(line) {
|
|
133
|
+
const i = this.#indexOf(line);
|
|
134
|
+
if (i < 0)
|
|
135
|
+
return false;
|
|
136
|
+
this.#set(this.#folds.filter((_, j) => j !== i));
|
|
137
|
+
return true;
|
|
138
|
+
}
|
|
139
|
+
unfoldAll() {
|
|
140
|
+
if (this.#folds.length === 0)
|
|
141
|
+
return false;
|
|
142
|
+
this.#set([]);
|
|
143
|
+
return true;
|
|
144
|
+
}
|
|
145
|
+
/** Called after any fold opens, closes, or is dropped. Not called when an edit only moves them. */
|
|
146
|
+
subscribe(listener) {
|
|
147
|
+
this.#listeners.push(listener);
|
|
148
|
+
return () => {
|
|
149
|
+
this.#listeners = this.#listeners.filter((l) => l !== listener);
|
|
150
|
+
};
|
|
151
|
+
}
|
|
152
|
+
#indexOf(line) {
|
|
153
|
+
const folds = this.#folds;
|
|
154
|
+
let lo = 0;
|
|
155
|
+
let hi = folds.length - 1;
|
|
156
|
+
while (lo <= hi) {
|
|
157
|
+
const mid = (lo + hi) >> 1;
|
|
158
|
+
const l = folds[mid].line;
|
|
159
|
+
if (l < line)
|
|
160
|
+
lo = mid + 1;
|
|
161
|
+
else if (l > line)
|
|
162
|
+
hi = mid - 1;
|
|
163
|
+
else
|
|
164
|
+
return mid;
|
|
165
|
+
}
|
|
166
|
+
return -1;
|
|
167
|
+
}
|
|
168
|
+
#set(folds) {
|
|
169
|
+
this.#folds = folds;
|
|
170
|
+
this.#spans = spansOf(folds);
|
|
171
|
+
for (const l of this.#listeners)
|
|
172
|
+
l();
|
|
173
|
+
}
|
|
174
|
+
#onUpdate(u) {
|
|
175
|
+
if (this.#folds.length === 0)
|
|
176
|
+
return;
|
|
177
|
+
let folds = this.#folds;
|
|
178
|
+
let changed = false;
|
|
179
|
+
if (u.docChanged) {
|
|
180
|
+
const mapped = this.#map(u);
|
|
181
|
+
changed = mapped.length !== folds.length;
|
|
182
|
+
folds = mapped;
|
|
183
|
+
this.#folds = folds;
|
|
184
|
+
this.#spans = spansOf(folds);
|
|
185
|
+
}
|
|
186
|
+
const text = u.after.text;
|
|
187
|
+
const spans = this.#spans;
|
|
188
|
+
let open = null;
|
|
189
|
+
for (const r of u.after.selection.ranges) {
|
|
190
|
+
const line = text.lineAt(r.head).number;
|
|
191
|
+
if (spanIndex(spans, line) < 0)
|
|
192
|
+
continue;
|
|
193
|
+
open ??= new Set();
|
|
194
|
+
for (const f of folds)
|
|
195
|
+
if (f.line < line && line <= f.end)
|
|
196
|
+
open.add(f);
|
|
197
|
+
}
|
|
198
|
+
if (open)
|
|
199
|
+
this.#set(folds.filter((f) => !open.has(f)));
|
|
200
|
+
else if (changed)
|
|
201
|
+
this.#set(folds);
|
|
202
|
+
}
|
|
203
|
+
#map(u) {
|
|
204
|
+
const steps = stepsOf(u.changes, u.before.text);
|
|
205
|
+
const after = u.after.text;
|
|
206
|
+
const out = [];
|
|
207
|
+
// Offsets inside or touching a change map exactly; the rest shift by the running deltas.
|
|
208
|
+
const point = (pos, line, assoc) => {
|
|
209
|
+
const i = stepAt(steps, pos);
|
|
210
|
+
if (i >= 0 && pos <= steps[i].toA) {
|
|
211
|
+
let p = u.changes.mapPos(pos, assoc);
|
|
212
|
+
// The last hidden line's break was deleted with it: the region now ends a line earlier.
|
|
213
|
+
if (assoc < 0 && pos < steps[i].toA && p > 0 && after.lineAt(p).from === p)
|
|
214
|
+
p--;
|
|
215
|
+
return [p, after.lineAt(p).number];
|
|
216
|
+
}
|
|
217
|
+
const s = i >= 0 ? steps[i] : null;
|
|
218
|
+
return [pos + (s?.dOff ?? 0), line + (s?.dLine ?? 0)];
|
|
219
|
+
};
|
|
220
|
+
for (const f of this.#folds) {
|
|
221
|
+
const i = stepAt(steps, f.eol);
|
|
222
|
+
if (i >= 0 && f.eol < steps[i].toA)
|
|
223
|
+
continue; // the header's line break was deleted
|
|
224
|
+
// Text typed at the end of the header stays on the header; text after the last hidden
|
|
225
|
+
// line's end stays visible.
|
|
226
|
+
const [eol, line] = point(f.eol, f.line, 1);
|
|
227
|
+
const [to, end] = point(f.to, f.end, -1);
|
|
228
|
+
if (end <= line)
|
|
229
|
+
continue;
|
|
230
|
+
const prev = out.at(-1);
|
|
231
|
+
if (prev && prev.line === line) {
|
|
232
|
+
if (end > prev.end)
|
|
233
|
+
out[out.length - 1] = { eol, to, line, end };
|
|
234
|
+
continue;
|
|
235
|
+
}
|
|
236
|
+
out.push({ eol, to, line, end });
|
|
237
|
+
}
|
|
238
|
+
return out;
|
|
239
|
+
}
|
|
240
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import type { Command } from '../commands/commands.ts';
|
|
2
|
+
import type { EditorCore, Origin } from './core.ts';
|
|
3
|
+
/** How each origin participates in undo. The switch over Origin in `policy` is exhaustive. */
|
|
4
|
+
type Policy = 'group-typing' | 'own-step' | 'ignore' | 'reset' | 'self';
|
|
5
|
+
export declare function policy(origin: Origin): Policy;
|
|
6
|
+
export interface HistoryOptions {
|
|
7
|
+
/** Consecutive typing within this many ms, at the previous step's end, joins one entry. */
|
|
8
|
+
readonly groupDelay?: number;
|
|
9
|
+
readonly now?: () => number;
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* Local undo/redo as a follower of EditorCore. It owns both stacks privately and derives them
|
|
13
|
+
* from Updates; it never reads another module's state. Per-user by construction: it records only
|
|
14
|
+
* local origins and maps its stacks over 'remote' updates.
|
|
15
|
+
*/
|
|
16
|
+
export declare class History {
|
|
17
|
+
#private;
|
|
18
|
+
constructor(core: EditorCore, options?: HistoryOptions, signal?: AbortSignal);
|
|
19
|
+
/** Commands for the keymap. Each returns null when its stack is empty. */
|
|
20
|
+
readonly undo: Command;
|
|
21
|
+
readonly redo: Command;
|
|
22
|
+
get canUndo(): boolean;
|
|
23
|
+
get canRedo(): boolean;
|
|
24
|
+
}
|
|
25
|
+
export {};
|
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
export function policy(origin) {
|
|
2
|
+
switch (origin) {
|
|
3
|
+
case 'input.type':
|
|
4
|
+
case 'input.delete':
|
|
5
|
+
case 'input.compose':
|
|
6
|
+
return 'group-typing';
|
|
7
|
+
case 'input.paste':
|
|
8
|
+
case 'input.newline':
|
|
9
|
+
case 'command':
|
|
10
|
+
case 'host.apply':
|
|
11
|
+
return 'own-step';
|
|
12
|
+
case 'select':
|
|
13
|
+
return 'ignore';
|
|
14
|
+
case 'remote':
|
|
15
|
+
return 'ignore'; // still mapped into both stacks, never recorded
|
|
16
|
+
case 'host.set':
|
|
17
|
+
return 'reset';
|
|
18
|
+
case 'history.undo':
|
|
19
|
+
case 'history.redo':
|
|
20
|
+
return 'self';
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Local undo/redo as a follower of EditorCore. It owns both stacks privately and derives them
|
|
25
|
+
* from Updates; it never reads another module's state. Per-user by construction: it records only
|
|
26
|
+
* local origins and maps its stacks over 'remote' updates.
|
|
27
|
+
*/
|
|
28
|
+
export class History {
|
|
29
|
+
#done = [];
|
|
30
|
+
#undone = [];
|
|
31
|
+
#core;
|
|
32
|
+
#groupDelay;
|
|
33
|
+
#now;
|
|
34
|
+
constructor(core, options = {}, signal) {
|
|
35
|
+
this.#core = core;
|
|
36
|
+
this.#groupDelay = options.groupDelay ?? 500;
|
|
37
|
+
this.#now = options.now ?? (() => performance.now());
|
|
38
|
+
core.listen((u) => this.#record(u), signal);
|
|
39
|
+
}
|
|
40
|
+
/** Commands for the keymap. Each returns null when its stack is empty. */
|
|
41
|
+
undo = () => this.#spec(this.#done, 'history.undo');
|
|
42
|
+
redo = () => this.#spec(this.#undone, 'history.redo');
|
|
43
|
+
get canUndo() {
|
|
44
|
+
return this.#done.length > 0;
|
|
45
|
+
}
|
|
46
|
+
get canRedo() {
|
|
47
|
+
return this.#undone.length > 0;
|
|
48
|
+
}
|
|
49
|
+
#spec(stack, origin) {
|
|
50
|
+
const e = stack.at(-1);
|
|
51
|
+
if (!e)
|
|
52
|
+
return null;
|
|
53
|
+
return { changes: e.undo, selection: e.selectionBefore, origin, scrollIntoView: true };
|
|
54
|
+
}
|
|
55
|
+
#record(u) {
|
|
56
|
+
const p = policy(u.origin);
|
|
57
|
+
switch (p) {
|
|
58
|
+
case 'ignore': {
|
|
59
|
+
if (u.docChanged) {
|
|
60
|
+
// Remote updates need to rebase both stacks over the incoming change. That is
|
|
61
|
+
// ChangeSet.map, which is later; collaboration is not in the first slice.
|
|
62
|
+
throw new Error('remote updates need ChangeSet.map; collaboration is not in the first slice');
|
|
63
|
+
}
|
|
64
|
+
return;
|
|
65
|
+
}
|
|
66
|
+
case 'reset': {
|
|
67
|
+
this.#done = [];
|
|
68
|
+
this.#undone = [];
|
|
69
|
+
return;
|
|
70
|
+
}
|
|
71
|
+
case 'self': {
|
|
72
|
+
const fromStack = u.origin === 'history.undo' ? this.#done : this.#undone;
|
|
73
|
+
const toStack = u.origin === 'history.undo' ? this.#undone : this.#done;
|
|
74
|
+
const e = fromStack.pop();
|
|
75
|
+
if (!e)
|
|
76
|
+
return;
|
|
77
|
+
const entry = {
|
|
78
|
+
undo: u.changes.invert(u.before.text),
|
|
79
|
+
selectionBefore: e.selectionAfter,
|
|
80
|
+
selectionAfter: e.selectionBefore,
|
|
81
|
+
origin: u.origin,
|
|
82
|
+
time: this.#now(),
|
|
83
|
+
end: u.after.selection.primary.head,
|
|
84
|
+
};
|
|
85
|
+
toStack.push(entry);
|
|
86
|
+
return;
|
|
87
|
+
}
|
|
88
|
+
case 'group-typing': {
|
|
89
|
+
const top = this.#done.at(-1);
|
|
90
|
+
if (top &&
|
|
91
|
+
policy(top.origin) === 'group-typing' &&
|
|
92
|
+
this.#now() - top.time < this.#groupDelay &&
|
|
93
|
+
top.end === u.before.selection.primary.head) {
|
|
94
|
+
const merged = {
|
|
95
|
+
undo: u.changes.invert(u.before.text).compose(top.undo),
|
|
96
|
+
selectionBefore: top.selectionBefore,
|
|
97
|
+
selectionAfter: u.after.selection,
|
|
98
|
+
origin: u.origin,
|
|
99
|
+
time: this.#now(),
|
|
100
|
+
end: u.after.selection.primary.head,
|
|
101
|
+
};
|
|
102
|
+
this.#done[this.#done.length - 1] = merged;
|
|
103
|
+
this.#undone = [];
|
|
104
|
+
return;
|
|
105
|
+
}
|
|
106
|
+
this.#pushOwnStep(u);
|
|
107
|
+
return;
|
|
108
|
+
}
|
|
109
|
+
case 'own-step': {
|
|
110
|
+
this.#pushOwnStep(u);
|
|
111
|
+
return;
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
#pushOwnStep(u) {
|
|
116
|
+
const entry = {
|
|
117
|
+
undo: u.changes.invert(u.before.text),
|
|
118
|
+
selectionBefore: u.before.selection,
|
|
119
|
+
selectionAfter: u.after.selection,
|
|
120
|
+
origin: u.origin,
|
|
121
|
+
time: this.#now(),
|
|
122
|
+
end: u.after.selection.primary.head,
|
|
123
|
+
};
|
|
124
|
+
this.#done.push(entry);
|
|
125
|
+
this.#undone = [];
|
|
126
|
+
}
|
|
127
|
+
}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import type { Text } from '../text/text.ts';
|
|
2
|
+
/**
|
|
3
|
+
* Indentation as the editor reads it: what one level is, how deep a line sits, and which block
|
|
4
|
+
* the cursor is in. Pure and DOM-free, so the view's indent guides and a terminal view can share
|
|
5
|
+
* it. Nothing here reads more than a bounded number of lines per call.
|
|
6
|
+
*/
|
|
7
|
+
/** One indent level: a tab, or `width` spaces. With tabs, `width` is the tab size in columns. */
|
|
8
|
+
export interface IndentUnit {
|
|
9
|
+
readonly tabs: boolean;
|
|
10
|
+
readonly width: number;
|
|
11
|
+
}
|
|
12
|
+
/** Reads a line's indent in columns, or -1 when it is blank. Lets a caller cache. */
|
|
13
|
+
export type IndentReader = (line: number) => number;
|
|
14
|
+
/** Leading columns of a line, with tabs to the next stop, or -1 when it is blank or only whitespace. */
|
|
15
|
+
export declare function indentColumns(s: string, tabSize: number): number;
|
|
16
|
+
/** `indentColumns` of line `n`, reading at most PREFIX characters of it. */
|
|
17
|
+
export declare function lineIndent(text: Text, n: number, tabSize: number): number;
|
|
18
|
+
/**
|
|
19
|
+
* A simplified VS Code `guessIndentation` over the first `sample` non-blank lines. Tabs win when
|
|
20
|
+
* more lines start with a tab than with a space. Otherwise the space step between neighbouring
|
|
21
|
+
* lines that occurs most often is the unit, preferring 2 when 2-steps are at least half as common
|
|
22
|
+
* as 4-steps (VS Code's rule). With no evidence, `tabSize` spaces.
|
|
23
|
+
*/
|
|
24
|
+
export declare function detectIndentUnit(text: Text, tabSize?: number, sample?: number): IndentUnit;
|
|
25
|
+
/**
|
|
26
|
+
* How many guide levels line `line` shows: one per unit of indent, a partial unit rounding up, so
|
|
27
|
+
* every guide sits left of the text. A blank line takes the shallower of its nearest content
|
|
28
|
+
* lines above and below, so guides run through blank lines inside a block and stop at its last
|
|
29
|
+
* content line. A blank line right after a header (content below is deeper) is inside the block
|
|
30
|
+
* and takes one level more than the header, as VS Code does. Neighbours further than BLANK_SCAN
|
|
31
|
+
* lines away don't count: such a line shows no guides. Reads at most 2 * BLANK_SCAN + 1 lines.
|
|
32
|
+
*/
|
|
33
|
+
export declare function guideDepth(text: Text, line: number, unit: IndentUnit, tabSize: number, read?: IndentReader): number;
|
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
/** Blank lines a guide looks across for content above and below. Past this a blank line shows none. */
|
|
2
|
+
const BLANK_SCAN = 200;
|
|
3
|
+
/** Leading characters read to measure indentation, so a minified line costs no more than a short one. */
|
|
4
|
+
const PREFIX = 512;
|
|
5
|
+
/** Space steps `detectIndentUnit` tries, in VS Code's order of preference on a tie. */
|
|
6
|
+
const CANDIDATES = [2, 4, 6, 8, 3, 5, 7];
|
|
7
|
+
/** Leading columns of a line, with tabs to the next stop, or -1 when it is blank or only whitespace. */
|
|
8
|
+
export function indentColumns(s, tabSize) {
|
|
9
|
+
let w = 0;
|
|
10
|
+
for (let i = 0; i < s.length; i++) {
|
|
11
|
+
const c = s.charCodeAt(i);
|
|
12
|
+
if (c === 32)
|
|
13
|
+
w++;
|
|
14
|
+
else if (c === 9)
|
|
15
|
+
w += tabSize - (w % tabSize);
|
|
16
|
+
else if (c !== 13)
|
|
17
|
+
return w;
|
|
18
|
+
}
|
|
19
|
+
return -1;
|
|
20
|
+
}
|
|
21
|
+
/** `indentColumns` of line `n`, reading at most PREFIX characters of it. */
|
|
22
|
+
export function lineIndent(text, n, tabSize) {
|
|
23
|
+
const l = text.line(n);
|
|
24
|
+
return indentColumns(text.slice(l.from, Math.min(l.to, l.from + PREFIX)), tabSize);
|
|
25
|
+
}
|
|
26
|
+
const levels = (columns, unit) => Math.ceil(columns / unit.width);
|
|
27
|
+
/**
|
|
28
|
+
* A simplified VS Code `guessIndentation` over the first `sample` non-blank lines. Tabs win when
|
|
29
|
+
* more lines start with a tab than with a space. Otherwise the space step between neighbouring
|
|
30
|
+
* lines that occurs most often is the unit, preferring 2 when 2-steps are at least half as common
|
|
31
|
+
* as 4-steps (VS Code's rule). With no evidence, `tabSize` spaces.
|
|
32
|
+
*/
|
|
33
|
+
export function detectIndentUnit(text, tabSize = 4, sample = 1000) {
|
|
34
|
+
let tabLines = 0;
|
|
35
|
+
let spaceLines = 0;
|
|
36
|
+
const steps = new Array(9).fill(0);
|
|
37
|
+
let prev = -1;
|
|
38
|
+
let seen = 0;
|
|
39
|
+
// Blank lines don't count toward the sample, so cap the lines read as well.
|
|
40
|
+
const last = Math.min(text.lines, sample * 8);
|
|
41
|
+
for (let n = 1; n <= last && seen < sample; n++) {
|
|
42
|
+
const l = text.line(n);
|
|
43
|
+
const s = text.slice(l.from, Math.min(l.to, l.from + PREFIX));
|
|
44
|
+
let spaces = 0;
|
|
45
|
+
let hasTab = false;
|
|
46
|
+
let i = 0;
|
|
47
|
+
for (; i < s.length; i++) {
|
|
48
|
+
const c = s.charCodeAt(i);
|
|
49
|
+
if (c === 32)
|
|
50
|
+
spaces++;
|
|
51
|
+
else if (c === 9)
|
|
52
|
+
hasTab = true;
|
|
53
|
+
else
|
|
54
|
+
break;
|
|
55
|
+
}
|
|
56
|
+
if (i === s.length || (i === s.length - 1 && s.charCodeAt(i) === 13))
|
|
57
|
+
continue;
|
|
58
|
+
seen++;
|
|
59
|
+
if (i > 0) {
|
|
60
|
+
if (s.charCodeAt(0) === 9)
|
|
61
|
+
tabLines++;
|
|
62
|
+
else
|
|
63
|
+
spaceLines++;
|
|
64
|
+
}
|
|
65
|
+
if (hasTab) {
|
|
66
|
+
prev = -1;
|
|
67
|
+
continue;
|
|
68
|
+
}
|
|
69
|
+
if (prev >= 0) {
|
|
70
|
+
const d = Math.abs(spaces - prev);
|
|
71
|
+
if (d >= 2 && d <= 8)
|
|
72
|
+
steps[d]++;
|
|
73
|
+
}
|
|
74
|
+
prev = spaces;
|
|
75
|
+
}
|
|
76
|
+
if (tabLines > spaceLines)
|
|
77
|
+
return { tabs: true, width: tabSize };
|
|
78
|
+
let best = 0;
|
|
79
|
+
let bestCount = 0;
|
|
80
|
+
for (const c of CANDIDATES) {
|
|
81
|
+
if (steps[c] > bestCount) {
|
|
82
|
+
best = c;
|
|
83
|
+
bestCount = steps[c];
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
if (best === 4 && steps[2] > 0 && steps[2] >= steps[4] / 2)
|
|
87
|
+
best = 2;
|
|
88
|
+
return { tabs: false, width: best || tabSize };
|
|
89
|
+
}
|
|
90
|
+
/** The nearest non-blank line from `line` in direction `dir`, within BLANK_SCAN lines, or 0. */
|
|
91
|
+
function nearest(text, line, dir, read) {
|
|
92
|
+
for (let k = 1, n = line + dir; k <= BLANK_SCAN && n >= 1 && n <= text.lines; k++, n += dir) {
|
|
93
|
+
if (read(n) >= 0)
|
|
94
|
+
return n;
|
|
95
|
+
}
|
|
96
|
+
return 0;
|
|
97
|
+
}
|
|
98
|
+
/**
|
|
99
|
+
* How many guide levels line `line` shows: one per unit of indent, a partial unit rounding up, so
|
|
100
|
+
* every guide sits left of the text. A blank line takes the shallower of its nearest content
|
|
101
|
+
* lines above and below, so guides run through blank lines inside a block and stop at its last
|
|
102
|
+
* content line. A blank line right after a header (content below is deeper) is inside the block
|
|
103
|
+
* and takes one level more than the header, as VS Code does. Neighbours further than BLANK_SCAN
|
|
104
|
+
* lines away don't count: such a line shows no guides. Reads at most 2 * BLANK_SCAN + 1 lines.
|
|
105
|
+
*/
|
|
106
|
+
export function guideDepth(text, line, unit, tabSize, read = (n) => lineIndent(text, n, tabSize)) {
|
|
107
|
+
const own = read(line);
|
|
108
|
+
if (own >= 0)
|
|
109
|
+
return levels(own, unit);
|
|
110
|
+
const up = nearest(text, line, -1, read);
|
|
111
|
+
if (!up)
|
|
112
|
+
return 0;
|
|
113
|
+
const down = nearest(text, line, 1, read);
|
|
114
|
+
if (!down)
|
|
115
|
+
return 0;
|
|
116
|
+
const above = read(up);
|
|
117
|
+
const below = read(down);
|
|
118
|
+
if (above < below)
|
|
119
|
+
return Math.min(levels(above, unit) + 1, levels(below, unit));
|
|
120
|
+
return levels(below, unit);
|
|
121
|
+
}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import type { ChangeSet } from '../text/changeset.ts';
|
|
2
|
+
/**
|
|
3
|
+
* One selection range. `anchor === head` is a cursor. `goalX` is the remembered horizontal
|
|
4
|
+
* position (px from the line's content start) for vertical motion; any non-vertical command
|
|
5
|
+
* drops it by building a range without it.
|
|
6
|
+
*/
|
|
7
|
+
export interface SelRange {
|
|
8
|
+
readonly anchor: number;
|
|
9
|
+
readonly head: number;
|
|
10
|
+
readonly goalX?: number;
|
|
11
|
+
}
|
|
12
|
+
export declare const rangeFrom: (r: SelRange) => number;
|
|
13
|
+
export declare const rangeTo: (r: SelRange) => number;
|
|
14
|
+
export declare const isCursor: (r: SelRange) => boolean;
|
|
15
|
+
export declare const cursor: (pos: number, goalX?: number) => SelRange;
|
|
16
|
+
/**
|
|
17
|
+
* A normalized multi-range selection (D4).
|
|
18
|
+
*
|
|
19
|
+
* Invariants, established by `create` and nothing else (the constructor is private):
|
|
20
|
+
* - `ranges` is non-empty, sorted by `rangeFrom`.
|
|
21
|
+
* - No two ranges overlap. Touching non-empty ranges merge; two cursors at the same offset merge.
|
|
22
|
+
* A cursor touching a non-empty range does not merge with it.
|
|
23
|
+
* - `primaryIndex` indexes `ranges`. When ranges merge, the merged range is primary if any input
|
|
24
|
+
* to it was.
|
|
25
|
+
*/
|
|
26
|
+
export declare class Selection {
|
|
27
|
+
readonly ranges: readonly [SelRange, ...SelRange[]];
|
|
28
|
+
readonly primaryIndex: number;
|
|
29
|
+
private constructor();
|
|
30
|
+
static single(anchor: number, head?: number): Selection;
|
|
31
|
+
/** Sorts, merges, and picks the primary. Total: any non-empty input yields a valid Selection. */
|
|
32
|
+
static create(ranges: readonly [SelRange, ...SelRange[]], primaryIndex: number): Selection;
|
|
33
|
+
get primary(): SelRange;
|
|
34
|
+
/**
|
|
35
|
+
* Map every range through `changes`, then normalize. Text inserted at a range's edge, by another
|
|
36
|
+
* cursor or a remote user, stays outside it, and a cursor stays before text inserted at it.
|
|
37
|
+
* Commands that type at a cursor set the selection explicitly.
|
|
38
|
+
*/
|
|
39
|
+
map(changes: ChangeSet): Selection;
|
|
40
|
+
/** Alt-click. Adds a cursor and makes it primary. */
|
|
41
|
+
addCursor(pos: number): Selection;
|
|
42
|
+
/** Escape. Keeps only the primary range, as a cursor at its head when `collapse` is set. */
|
|
43
|
+
toPrimary(collapse: boolean): Selection;
|
|
44
|
+
eq(other: Selection): boolean;
|
|
45
|
+
}
|