@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,442 @@
|
|
|
1
|
+
import { ChangeSet } from "../text/changeset.js";
|
|
2
|
+
import { Selection, cursor, isCursor, rangeFrom, rangeTo } from "../state/selection.js";
|
|
3
|
+
/**
|
|
4
|
+
* D4's "run per range, merge, normalize". Each range is processed against the same snapshot.
|
|
5
|
+
* The changes are combined into one ChangeSet; each result range is shifted by the net length of
|
|
6
|
+
* the changes before it; the selection is re-normalized. Overlapping changes from adjacent ranges
|
|
7
|
+
* cannot happen because ranges do not overlap and each step's change stays within its range plus
|
|
8
|
+
* one grapheme (callers keep to that).
|
|
9
|
+
*/
|
|
10
|
+
function offset(r, shift) {
|
|
11
|
+
return r.goalX === undefined
|
|
12
|
+
? { anchor: r.anchor + shift, head: r.head + shift }
|
|
13
|
+
: { anchor: r.anchor + shift, head: r.head + shift, goalX: r.goalX };
|
|
14
|
+
}
|
|
15
|
+
export function perRange(state, origin, step) {
|
|
16
|
+
const specs = [];
|
|
17
|
+
const ranges = [];
|
|
18
|
+
let shift = 0;
|
|
19
|
+
for (const r of state.selection.ranges) {
|
|
20
|
+
const res = step(r, state.text);
|
|
21
|
+
if (res.change)
|
|
22
|
+
specs.push(res.change);
|
|
23
|
+
ranges.push(offset(res.range, shift));
|
|
24
|
+
if (res.change)
|
|
25
|
+
shift += (res.change.insert?.length ?? 0) - ((res.change.to ?? res.change.from) - res.change.from);
|
|
26
|
+
}
|
|
27
|
+
return {
|
|
28
|
+
changes: ChangeSet.of(specs, state.text.length),
|
|
29
|
+
selection: Selection.create(ranges, state.selection.primaryIndex),
|
|
30
|
+
origin,
|
|
31
|
+
scrollIntoView: true,
|
|
32
|
+
};
|
|
33
|
+
}
|
|
34
|
+
// Grapheme boundaries (D3: computed only for motion and deletion, never stored).
|
|
35
|
+
/** Boundaries of grapheme clusters within `s`, including 0 and `s.length`. */
|
|
36
|
+
function clusterBoundaries(s) {
|
|
37
|
+
const bounds = [0];
|
|
38
|
+
if (s.length === 0)
|
|
39
|
+
return bounds;
|
|
40
|
+
const segmenter = new Intl.Segmenter(undefined, { granularity: 'grapheme' });
|
|
41
|
+
for (const part of segmenter.segment(s))
|
|
42
|
+
bounds.push(part.index + part.segment.length);
|
|
43
|
+
return bounds;
|
|
44
|
+
}
|
|
45
|
+
/** Next grapheme boundary after `pos`, crossing "\n" as one step. Segments only the current line. */
|
|
46
|
+
export function nextGrapheme(text, pos) {
|
|
47
|
+
if (pos >= text.length)
|
|
48
|
+
return pos;
|
|
49
|
+
const line = text.lineAt(pos);
|
|
50
|
+
if (pos >= line.to)
|
|
51
|
+
return pos + 1;
|
|
52
|
+
const rel = pos - line.from;
|
|
53
|
+
const bounds = clusterBoundaries(text.slice(line.from, line.to));
|
|
54
|
+
for (const b of bounds)
|
|
55
|
+
if (b > rel)
|
|
56
|
+
return line.from + b;
|
|
57
|
+
return line.to;
|
|
58
|
+
}
|
|
59
|
+
export function prevGrapheme(text, pos) {
|
|
60
|
+
if (pos <= 0)
|
|
61
|
+
return 0;
|
|
62
|
+
const line = text.lineAt(pos);
|
|
63
|
+
if (pos <= line.from)
|
|
64
|
+
return pos - 1;
|
|
65
|
+
const rel = pos - line.from;
|
|
66
|
+
const bounds = clusterBoundaries(text.slice(line.from, line.to));
|
|
67
|
+
let prev = 0;
|
|
68
|
+
for (const b of bounds) {
|
|
69
|
+
if (b >= rel)
|
|
70
|
+
break;
|
|
71
|
+
prev = b;
|
|
72
|
+
}
|
|
73
|
+
return line.from + prev;
|
|
74
|
+
}
|
|
75
|
+
function columnOf(text, pos) {
|
|
76
|
+
return pos - text.lineAt(pos).from;
|
|
77
|
+
}
|
|
78
|
+
// Editing. Each inserts or deletes at every range (multicursor for free via perRange).
|
|
79
|
+
/** Replace every range with `text`. Origin decides undo grouping. Paste of N lines into N
|
|
80
|
+
* cursors distributes one line per cursor (TODO, CM6 behavior). */
|
|
81
|
+
export const insertText = (text, origin = 'input.type') => (ctx) => perRange(ctx.state, origin, (r) => ({
|
|
82
|
+
change: { from: rangeFrom(r), to: rangeTo(r), insert: text },
|
|
83
|
+
range: cursor(rangeFrom(r) + text.length),
|
|
84
|
+
}));
|
|
85
|
+
export const deleteBackward = (ctx) => perRange(ctx.state, 'input.delete', (r, text) => {
|
|
86
|
+
if (!isCursor(r))
|
|
87
|
+
return { change: { from: rangeFrom(r), to: rangeTo(r) }, range: cursor(rangeFrom(r)) };
|
|
88
|
+
const from = prevGrapheme(text, r.head);
|
|
89
|
+
return { change: { from, to: r.head }, range: cursor(from) };
|
|
90
|
+
});
|
|
91
|
+
export const deleteForward = (ctx) => perRange(ctx.state, 'input.delete', (r, text) => {
|
|
92
|
+
if (!isCursor(r))
|
|
93
|
+
return { change: { from: rangeFrom(r), to: rangeTo(r) }, range: cursor(rangeFrom(r)) };
|
|
94
|
+
const to = nextGrapheme(text, r.head);
|
|
95
|
+
return { change: { from: r.head, to }, range: cursor(r.head) };
|
|
96
|
+
});
|
|
97
|
+
/** Inserts "\n" plus the current line's leading whitespace. */
|
|
98
|
+
export const insertNewline = (ctx) => perRange(ctx.state, 'input.newline', (r, text) => {
|
|
99
|
+
const line = text.lineAt(rangeFrom(r));
|
|
100
|
+
const lineStr = text.slice(line.from, line.to);
|
|
101
|
+
const ws = /^[ \t]*/.exec(lineStr)?.[0] ?? '';
|
|
102
|
+
const insert = '\n' + ws;
|
|
103
|
+
return {
|
|
104
|
+
change: { from: rangeFrom(r), to: rangeTo(r), insert },
|
|
105
|
+
range: cursor(rangeFrom(r) + insert.length),
|
|
106
|
+
};
|
|
107
|
+
});
|
|
108
|
+
// Motion. `extend` keeps anchors (Shift).
|
|
109
|
+
export const moveGrapheme = (dir, extend) => (ctx) => perRange(ctx.state, 'select', (r, text) => {
|
|
110
|
+
if (!extend && !isCursor(r)) {
|
|
111
|
+
const pos = dir < 0 ? rangeFrom(r) : rangeTo(r);
|
|
112
|
+
return { range: cursor(pos) };
|
|
113
|
+
}
|
|
114
|
+
const pos = dir < 0 ? prevGrapheme(text, r.head) : nextGrapheme(text, r.head);
|
|
115
|
+
return { range: extend ? { anchor: r.anchor, head: pos } : cursor(pos) };
|
|
116
|
+
});
|
|
117
|
+
/** Home goes to the first non-whitespace column, then to column 0 on a second press. */
|
|
118
|
+
export const moveLineBoundary = (side, extend) => (ctx) => perRange(ctx.state, 'select', (r, text) => {
|
|
119
|
+
const line = text.lineAt(r.head);
|
|
120
|
+
let pos;
|
|
121
|
+
if (side === 'end') {
|
|
122
|
+
pos = line.to;
|
|
123
|
+
}
|
|
124
|
+
else {
|
|
125
|
+
const ws = /^[ \t]*/.exec(text.slice(line.from, line.to))?.[0] ?? '';
|
|
126
|
+
const firstNonWs = line.from + ws.length;
|
|
127
|
+
pos = r.head === firstNonWs && firstNonWs !== line.from ? line.from : firstNonWs;
|
|
128
|
+
}
|
|
129
|
+
return { range: extend ? { anchor: r.anchor, head: pos } : cursor(pos) };
|
|
130
|
+
});
|
|
131
|
+
export const moveVertical = (dir, extend, page = false) => (ctx) => perRange(ctx.state, 'select', (r, text) => {
|
|
132
|
+
// With a layout, goalX is pixels the layout computes and remembers; headless, it is a column.
|
|
133
|
+
let pos;
|
|
134
|
+
let newGoalX;
|
|
135
|
+
if (ctx.layout) {
|
|
136
|
+
let p = r.head;
|
|
137
|
+
let gx = r.goalX;
|
|
138
|
+
const times = page ? ctx.layout.pageLines : 1;
|
|
139
|
+
for (let i = 0; i < times; i++) {
|
|
140
|
+
const res = ctx.layout.moveVertically(p, dir, gx);
|
|
141
|
+
p = res.pos;
|
|
142
|
+
gx = res.goalX;
|
|
143
|
+
}
|
|
144
|
+
pos = p;
|
|
145
|
+
newGoalX = gx ?? 0;
|
|
146
|
+
}
|
|
147
|
+
else {
|
|
148
|
+
const goalX = r.goalX ?? columnOf(text, r.head);
|
|
149
|
+
const line = text.lineAt(r.head);
|
|
150
|
+
const targetLineNum = dir < 0 ? line.number - 1 : line.number + 1;
|
|
151
|
+
if (targetLineNum < 1 || targetLineNum > text.lines)
|
|
152
|
+
return { range: r };
|
|
153
|
+
const target = text.line(targetLineNum);
|
|
154
|
+
const col = Math.min(goalX, target.to - target.from);
|
|
155
|
+
pos = target.from + col;
|
|
156
|
+
newGoalX = goalX;
|
|
157
|
+
}
|
|
158
|
+
const range = extend ? { anchor: r.anchor, head: pos, goalX: newGoalX } : { anchor: pos, head: pos, goalX: newGoalX };
|
|
159
|
+
return { range };
|
|
160
|
+
});
|
|
161
|
+
export const selectAll = (ctx) => ({
|
|
162
|
+
selection: Selection.single(0, ctx.state.text.length),
|
|
163
|
+
origin: 'select',
|
|
164
|
+
});
|
|
165
|
+
/** Escape: collapse to the primary cursor. Returns null with a single cursor so Escape can bubble. */
|
|
166
|
+
export const collapseSelection = (ctx) => {
|
|
167
|
+
const sel = ctx.state.selection;
|
|
168
|
+
if (sel.ranges.length === 1 && isCursor(sel.primary))
|
|
169
|
+
return null;
|
|
170
|
+
return { selection: sel.toPrimary(true), origin: 'select' };
|
|
171
|
+
};
|
|
172
|
+
const WORD = /[\p{L}\p{N}\p{M}_]/u;
|
|
173
|
+
const charClass = (ch) => (/\s/.test(ch) ? 0 : WORD.test(ch) ? 1 : 2);
|
|
174
|
+
/** Whether `ch` is a word character (letter, number, combining mark, or underscore). Shared with
|
|
175
|
+
* autoclose.ts, which skips pairing next to one. `''` (an edge of the document) is not a word
|
|
176
|
+
* character. */
|
|
177
|
+
export const isWordChar = (ch) => WORD.test(ch);
|
|
178
|
+
/** The word boundary from `pos` in `dir`: skip spaces, then one run of word characters or of
|
|
179
|
+
* punctuation. At a line edge, the line break is one step, as with graphemes. */
|
|
180
|
+
export function wordBoundary(text, pos, dir) {
|
|
181
|
+
const line = text.lineAt(pos);
|
|
182
|
+
if (dir < 0 ? pos <= line.from : pos >= line.to)
|
|
183
|
+
return dir < 0 ? Math.max(0, pos - 1) : Math.min(text.length, pos + 1);
|
|
184
|
+
const s = text.slice(line.from, line.to);
|
|
185
|
+
let i = pos - line.from;
|
|
186
|
+
const at = (j) => s[dir < 0 ? j - 1 : j];
|
|
187
|
+
const inside = (j) => (dir < 0 ? j > 0 : j < s.length);
|
|
188
|
+
while (inside(i) && charClass(at(i)) === 0)
|
|
189
|
+
i += dir;
|
|
190
|
+
if (inside(i)) {
|
|
191
|
+
const cls = charClass(at(i));
|
|
192
|
+
while (inside(i) && charClass(at(i)) === cls)
|
|
193
|
+
i += dir;
|
|
194
|
+
}
|
|
195
|
+
return line.from + i;
|
|
196
|
+
}
|
|
197
|
+
/** Delete one span per range, merged where they overlap, leaving a cursor at each span's start.
|
|
198
|
+
* A non-empty range deletes itself; `span` decides what a cursor deletes. */
|
|
199
|
+
function deleteSpans(ctx, origin, span) {
|
|
200
|
+
const { text, selection } = ctx.state;
|
|
201
|
+
return deleteMerged(ctx, origin, selection.ranges.map((r) => (isCursor(r) ? span(r.head, text) : [rangeFrom(r), rangeTo(r)])));
|
|
202
|
+
}
|
|
203
|
+
function deleteMerged(ctx, origin, raw) {
|
|
204
|
+
const { text, selection } = ctx.state;
|
|
205
|
+
const spans = raw
|
|
206
|
+
.map(([a, b]) => [Math.max(0, Math.min(a, b)), Math.min(text.length, Math.max(a, b))])
|
|
207
|
+
.sort((a, b) => a[0] - b[0]);
|
|
208
|
+
const merged = [];
|
|
209
|
+
for (const s of spans) {
|
|
210
|
+
const last = merged.at(-1);
|
|
211
|
+
if (last && s[0] <= last[1])
|
|
212
|
+
last[1] = Math.max(last[1], s[1]);
|
|
213
|
+
else
|
|
214
|
+
merged.push([s[0], s[1]]);
|
|
215
|
+
}
|
|
216
|
+
const changes = ChangeSet.of(merged.map(([from, to]) => ({ from, to })), text.length);
|
|
217
|
+
const ranges = merged.map(([from]) => cursor(changes.mapPos(from, -1)));
|
|
218
|
+
const primary = selection.primary;
|
|
219
|
+
const primaryAt = changes.mapPos(isCursor(primary) ? primary.head : rangeFrom(primary), -1);
|
|
220
|
+
const p = Math.max(0, ranges.findIndex((r) => r.head >= primaryAt));
|
|
221
|
+
return { changes, selection: Selection.create(ranges, p), origin, scrollIntoView: true };
|
|
222
|
+
}
|
|
223
|
+
export const moveWord = (dir, extend) => (ctx) => perRange(ctx.state, 'select', (r, text) => {
|
|
224
|
+
const head = !extend && !isCursor(r) ? (dir < 0 ? rangeFrom(r) : rangeTo(r)) : wordBoundary(text, r.head, dir);
|
|
225
|
+
return { range: extend ? { anchor: r.anchor, head } : cursor(head) };
|
|
226
|
+
});
|
|
227
|
+
export const deleteWord = (dir) => (ctx) => deleteSpans(ctx, 'input.delete', (head, text) => [head, wordBoundary(text, head, dir)]);
|
|
228
|
+
/** Cmd-Backspace / Cmd-Delete on Apple platforms: to the start or end of the line. At the edge,
|
|
229
|
+
* the line break goes, as with a single Backspace. */
|
|
230
|
+
export const deleteToLineBoundary = (side) => (ctx) => deleteSpans(ctx, 'input.delete', (head, text) => {
|
|
231
|
+
const line = text.lineAt(head);
|
|
232
|
+
if (side === 'start')
|
|
233
|
+
return [head > line.from ? line.from : Math.max(0, head - 1), head];
|
|
234
|
+
return [head, head < line.to ? line.to : Math.min(text.length, head + 1)];
|
|
235
|
+
});
|
|
236
|
+
/** Delete every line any range touches, with its line break. */
|
|
237
|
+
export const deleteLine = (ctx) => {
|
|
238
|
+
const { text, selection } = ctx.state;
|
|
239
|
+
const spans = selection.ranges.map((r) => {
|
|
240
|
+
const first = text.lineAt(rangeFrom(r));
|
|
241
|
+
const last = text.lineAt(rangeTo(r));
|
|
242
|
+
if (last.to < text.length)
|
|
243
|
+
return [first.from, last.to + 1];
|
|
244
|
+
return [Math.max(0, first.from - 1), last.to];
|
|
245
|
+
});
|
|
246
|
+
return deleteMerged(ctx, 'command', spans);
|
|
247
|
+
};
|
|
248
|
+
/**
|
|
249
|
+
* Mod-/. With a line comment, every non-blank line the selection touches gets the token at the
|
|
250
|
+
* smallest indentation among them, or loses it when all of them already have it. With only a
|
|
251
|
+
* block comment, each touched line's content is wrapped or unwrapped.
|
|
252
|
+
*/
|
|
253
|
+
export const toggleComment = (syntax) => (ctx) => {
|
|
254
|
+
if (!syntax?.line && !syntax?.block)
|
|
255
|
+
return null;
|
|
256
|
+
const { text, selection } = ctx.state;
|
|
257
|
+
const lineNumbers = new Set();
|
|
258
|
+
for (const r of selection.ranges) {
|
|
259
|
+
const a = text.lineAt(rangeFrom(r)).number;
|
|
260
|
+
let b = text.lineAt(rangeTo(r)).number;
|
|
261
|
+
if (b > a && !isCursor(r) && text.line(b).from === rangeTo(r))
|
|
262
|
+
b--;
|
|
263
|
+
for (let n = a; n <= b; n++)
|
|
264
|
+
lineNumbers.add(n);
|
|
265
|
+
}
|
|
266
|
+
const lines = [...lineNumbers].sort((x, y) => x - y).map((n) => {
|
|
267
|
+
const l = text.line(n);
|
|
268
|
+
const s = text.slice(l.from, l.to);
|
|
269
|
+
return { from: l.from, s, indent: s.length - s.trimStart().length };
|
|
270
|
+
});
|
|
271
|
+
const content = lines.filter((l) => l.s.trim() !== '');
|
|
272
|
+
if (content.length === 0)
|
|
273
|
+
return null;
|
|
274
|
+
const changes = [];
|
|
275
|
+
if (syntax.line) {
|
|
276
|
+
const token = syntax.line;
|
|
277
|
+
const commented = content.every((l) => l.s.startsWith(token, l.indent));
|
|
278
|
+
if (commented) {
|
|
279
|
+
for (const l of content) {
|
|
280
|
+
const at = l.from + l.indent;
|
|
281
|
+
const len = token.length + (l.s[l.indent + token.length] === ' ' ? 1 : 0);
|
|
282
|
+
changes.push({ from: at, to: at + len });
|
|
283
|
+
}
|
|
284
|
+
}
|
|
285
|
+
else {
|
|
286
|
+
const col = Math.min(...content.map((l) => l.indent));
|
|
287
|
+
for (const l of content)
|
|
288
|
+
changes.push({ from: l.from + col, insert: `${token} ` });
|
|
289
|
+
}
|
|
290
|
+
}
|
|
291
|
+
else {
|
|
292
|
+
const [open, close] = syntax.block;
|
|
293
|
+
for (const l of content) {
|
|
294
|
+
const body = l.s.slice(l.indent).trimEnd();
|
|
295
|
+
const start = l.from + l.indent;
|
|
296
|
+
if (body.startsWith(open) && body.endsWith(close) && body.length >= open.length + close.length) {
|
|
297
|
+
const innerFrom = open.length + (body[open.length] === ' ' ? 1 : 0);
|
|
298
|
+
const innerTo = body.length - close.length - (body[body.length - close.length - 1] === ' ' ? 1 : 0);
|
|
299
|
+
changes.push({ from: start, to: start + innerFrom }, { from: start + innerTo, to: start + body.length });
|
|
300
|
+
}
|
|
301
|
+
else {
|
|
302
|
+
changes.push({ from: start, insert: `${open} ` }, { from: start + body.length, insert: ` ${close}` });
|
|
303
|
+
}
|
|
304
|
+
}
|
|
305
|
+
}
|
|
306
|
+
return { changes: ChangeSet.of(changes, text.length), origin: 'command', scrollIntoView: false };
|
|
307
|
+
};
|
|
308
|
+
// Multi-cursor commands.
|
|
309
|
+
/** Mod-Alt-ArrowUp / ArrowDown: a new primary cursor on the line above the topmost range, or
|
|
310
|
+
* below the bottommost, at the same goal column. */
|
|
311
|
+
export const addCursorVertical = (dir) => (ctx) => {
|
|
312
|
+
const { text, selection } = ctx.state;
|
|
313
|
+
const edge = dir < 0 ? selection.ranges[0] : selection.ranges[selection.ranges.length - 1];
|
|
314
|
+
const line = text.lineAt(edge.head);
|
|
315
|
+
if (dir < 0 ? line.number === 1 : line.number === text.lines)
|
|
316
|
+
return { selection, origin: 'select' };
|
|
317
|
+
let head;
|
|
318
|
+
let goalX;
|
|
319
|
+
if (ctx.layout)
|
|
320
|
+
({ pos: head, goalX } = ctx.layout.moveVertically(edge.head, dir, edge.goalX));
|
|
321
|
+
else {
|
|
322
|
+
goalX = edge.goalX ?? edge.head - line.from;
|
|
323
|
+
const target = text.line(line.number + dir);
|
|
324
|
+
head = target.from + Math.min(goalX, target.to - target.from);
|
|
325
|
+
}
|
|
326
|
+
const ranges = [...selection.ranges, { anchor: head, head, goalX }];
|
|
327
|
+
return { selection: Selection.create(ranges, ranges.length - 1), origin: 'select', scrollIntoView: true };
|
|
328
|
+
};
|
|
329
|
+
/** The word around `pos`, or null when `pos` touches no word character. */
|
|
330
|
+
export function wordAt(text, pos) {
|
|
331
|
+
const line = text.lineAt(pos);
|
|
332
|
+
const s = text.slice(line.from, line.to);
|
|
333
|
+
let from = pos - line.from;
|
|
334
|
+
let to = from;
|
|
335
|
+
while (from > 0 && charClass(s[from - 1]) === 1)
|
|
336
|
+
from--;
|
|
337
|
+
while (to < s.length && charClass(s[to]) === 1)
|
|
338
|
+
to++;
|
|
339
|
+
return from === to ? null : { from: line.from + from, to: line.from + to };
|
|
340
|
+
}
|
|
341
|
+
/** Selections whose occurrences match whole words: those Mod-d grew from a bare cursor, as VS
|
|
342
|
+
* Code does. A selection the user made by hand matches substrings. */
|
|
343
|
+
const wholeWord = new WeakSet();
|
|
344
|
+
function occurrences(doc, query, word) {
|
|
345
|
+
const out = [];
|
|
346
|
+
for (let i = doc.indexOf(query); i >= 0; i = doc.indexOf(query, i + Math.max(1, query.length))) {
|
|
347
|
+
if (word && ((i > 0 && charClass(doc[i - 1]) === 1) || (i + query.length < doc.length && charClass(doc[i + query.length]) === 1)))
|
|
348
|
+
continue;
|
|
349
|
+
out.push(i);
|
|
350
|
+
}
|
|
351
|
+
return out;
|
|
352
|
+
}
|
|
353
|
+
function markWhole(sel, word) {
|
|
354
|
+
if (word)
|
|
355
|
+
wholeWord.add(sel);
|
|
356
|
+
return sel;
|
|
357
|
+
}
|
|
358
|
+
/** Mod-d: select the word at a bare cursor, then add the next occurrence of the primary
|
|
359
|
+
* selection after it, wrapping, as a new primary. */
|
|
360
|
+
export const selectNextOccurrence = (ctx) => {
|
|
361
|
+
const { text, selection } = ctx.state;
|
|
362
|
+
const p = selection.primary;
|
|
363
|
+
if (isCursor(p)) {
|
|
364
|
+
const w = wordAt(text, p.head);
|
|
365
|
+
if (!w)
|
|
366
|
+
return null;
|
|
367
|
+
const ranges = selection.ranges.map((r, i) => (i === selection.primaryIndex ? { anchor: w.from, head: w.to } : r));
|
|
368
|
+
return { selection: markWhole(Selection.create(ranges, selection.primaryIndex), true), origin: 'select' };
|
|
369
|
+
}
|
|
370
|
+
const word = wholeWord.has(selection);
|
|
371
|
+
const query = text.slice(rangeFrom(p), rangeTo(p));
|
|
372
|
+
const taken = new Set(selection.ranges.map((r) => rangeFrom(r)));
|
|
373
|
+
const all = occurrences(text.toString(), query, word);
|
|
374
|
+
const next = all.find((i) => i > rangeFrom(p) && !taken.has(i)) ?? all.find((i) => !taken.has(i));
|
|
375
|
+
if (next === undefined)
|
|
376
|
+
return { selection, origin: 'select' };
|
|
377
|
+
const ranges = [...selection.ranges, { anchor: next, head: next + query.length }];
|
|
378
|
+
return { selection: markWhole(Selection.create(ranges, ranges.length - 1), word), origin: 'select', scrollIntoView: true };
|
|
379
|
+
};
|
|
380
|
+
/** Mod-Shift-l: every occurrence of the primary selection, or of the word at a bare cursor. */
|
|
381
|
+
export const selectAllOccurrences = (ctx) => {
|
|
382
|
+
const { text, selection } = ctx.state;
|
|
383
|
+
const p = selection.primary;
|
|
384
|
+
const w = isCursor(p) ? wordAt(text, p.head) : { from: rangeFrom(p), to: rangeTo(p) };
|
|
385
|
+
if (!w)
|
|
386
|
+
return null;
|
|
387
|
+
const word = isCursor(p) || wholeWord.has(selection);
|
|
388
|
+
const query = text.slice(w.from, w.to);
|
|
389
|
+
const all = occurrences(text.toString(), query, word);
|
|
390
|
+
const ranges = all.map((i) => ({ anchor: i, head: i + query.length }));
|
|
391
|
+
return { selection: markWhole(Selection.create(ranges, Math.max(0, all.indexOf(w.from))), word), origin: 'select' };
|
|
392
|
+
};
|
|
393
|
+
/** Longest selection "highlight other occurrences" will search for (docs/TODO.md). Past this, a
|
|
394
|
+
* frame-budget scan of the mounted range isn't worth it. */
|
|
395
|
+
export const SELECTION_MATCH_MAX_LENGTH = 200;
|
|
396
|
+
/**
|
|
397
|
+
* Other occurrences of the primary selection within `[from, to)`, document offsets, skipping the
|
|
398
|
+
* selection itself. CodeMirror's `highlightSelectionMatches()`: whole-word when the selection
|
|
399
|
+
* spans exactly one word (so selecting "do" inside "dog" doesn't light up "dog"), substring
|
|
400
|
+
* otherwise. Deliberately selection-only — a bare caret matches nothing, so the highlight stays
|
|
401
|
+
* subtle and doesn't fire on every cursor move; this mirrors CodeMirror's default. Matches nothing
|
|
402
|
+
* when the selection is empty, spans more than one line, is whitespace-only, or is longer than
|
|
403
|
+
* `SELECTION_MATCH_MAX_LENGTH`.
|
|
404
|
+
*/
|
|
405
|
+
export function selectionMatches(state, from, to) {
|
|
406
|
+
const { text, selection } = state;
|
|
407
|
+
const p = selection.primary;
|
|
408
|
+
if (isCursor(p))
|
|
409
|
+
return [];
|
|
410
|
+
const a = rangeFrom(p);
|
|
411
|
+
const b = rangeTo(p);
|
|
412
|
+
if (b - a > SELECTION_MATCH_MAX_LENGTH)
|
|
413
|
+
return [];
|
|
414
|
+
const line = text.lineAt(a);
|
|
415
|
+
if (b > line.to)
|
|
416
|
+
return []; // multi-line selection
|
|
417
|
+
const query = text.slice(a, b);
|
|
418
|
+
if (!query.trim())
|
|
419
|
+
return [];
|
|
420
|
+
const w = wordAt(text, a);
|
|
421
|
+
const whole = !!w && w.from === a && w.to === b;
|
|
422
|
+
const lo = Math.max(0, from);
|
|
423
|
+
const hi = Math.min(text.length, to);
|
|
424
|
+
if (hi <= lo)
|
|
425
|
+
return [];
|
|
426
|
+
const hay = text.slice(lo, hi);
|
|
427
|
+
const out = [];
|
|
428
|
+
for (let i = hay.indexOf(query); i >= 0; i = hay.indexOf(query, i + Math.max(1, query.length))) {
|
|
429
|
+
const docFrom = lo + i;
|
|
430
|
+
const docTo = docFrom + query.length;
|
|
431
|
+
if (docFrom === a && docTo === b)
|
|
432
|
+
continue; // the selection itself
|
|
433
|
+
if (whole) {
|
|
434
|
+
const before = docFrom > 0 ? text.slice(docFrom - 1, docFrom) : '';
|
|
435
|
+
const after = docTo < text.length ? text.slice(docTo, docTo + 1) : '';
|
|
436
|
+
if ((before && charClass(before) === 1) || (after && charClass(after) === 1))
|
|
437
|
+
continue;
|
|
438
|
+
}
|
|
439
|
+
out.push({ from: docFrom, to: docTo });
|
|
440
|
+
}
|
|
441
|
+
return out;
|
|
442
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/** Comment syntax per language id. TextMate grammars don't carry it, so it lives here. */
|
|
2
|
+
export const commentSyntax = {
|
|
3
|
+
typescript: { line: '//', block: ['/*', '*/'] },
|
|
4
|
+
tsx: { line: '//', block: ['/*', '*/'] },
|
|
5
|
+
javascript: { line: '//', block: ['/*', '*/'] },
|
|
6
|
+
css: { block: ['/*', '*/'] },
|
|
7
|
+
html: { block: ['<!--', '-->'] },
|
|
8
|
+
markdown: { block: ['<!--', '-->'] },
|
|
9
|
+
};
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import type { Snapshot } from '../state/core.ts';
|
|
2
|
+
import type { FoldRangeSource } from '../state/fold-ranges.ts';
|
|
3
|
+
import { type FoldRange, type FoldState, type HiddenSpan } from '../state/folds.ts';
|
|
4
|
+
import type { Command, Layout } from './commands.ts';
|
|
5
|
+
/** Fold one range, as a click on the rail does. */
|
|
6
|
+
export declare const foldRange: (folds: FoldState, range: FoldRange) => Command;
|
|
7
|
+
/**
|
|
8
|
+
* Mod-Shift-[ (VS Code's editor.fold): at each head, fold the region its line starts, or else the
|
|
9
|
+
* innermost open region around it.
|
|
10
|
+
*/
|
|
11
|
+
export declare const foldCode: (folds: FoldState, source: FoldRangeSource) => Command;
|
|
12
|
+
/** Mod-Shift-] (editor.unfold): open the fold on each head's line. */
|
|
13
|
+
export declare const unfoldCode: (folds: FoldState) => Command;
|
|
14
|
+
/**
|
|
15
|
+
* Vertical motion by columns over folded text, for views with no pixel geometry (a terminal, or
|
|
16
|
+
* tests). Hidden lines are skipped; the goal is a column, as in moveVertical's headless path.
|
|
17
|
+
*/
|
|
18
|
+
export declare function columnLayout(state: () => Snapshot, hidden: () => readonly HiddenSpan[], pageLines?: number): Layout;
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
import { nextVisibleLine } from "../state/folds.js";
|
|
2
|
+
import { Selection } from "../state/selection.js";
|
|
3
|
+
/**
|
|
4
|
+
* Fold commands change FoldState, which is not part of the Snapshot, so they act when the keymap
|
|
5
|
+
* runs them and return the selection-only spec that goes with the change. A head left inside the
|
|
6
|
+
* new fold moves to the end of its header, where the placeholder is; otherwise FoldState would
|
|
7
|
+
* open the fold again on the next Update. Returning a spec, even an unchanged one, tells the
|
|
8
|
+
* keymap the key was taken.
|
|
9
|
+
*/
|
|
10
|
+
function foldAll(state, folds, ranges) {
|
|
11
|
+
let any = false;
|
|
12
|
+
for (const r of ranges)
|
|
13
|
+
any = folds.fold(r) || any;
|
|
14
|
+
if (!any)
|
|
15
|
+
return null;
|
|
16
|
+
const { text, selection } = state;
|
|
17
|
+
const moved = selection.ranges.map((r) => {
|
|
18
|
+
const n = text.lineAt(r.head).number;
|
|
19
|
+
const f = ranges.find((g) => g.line < n && n <= g.end);
|
|
20
|
+
if (!f)
|
|
21
|
+
return r;
|
|
22
|
+
const head = text.line(f.line).to;
|
|
23
|
+
return { anchor: head, head };
|
|
24
|
+
});
|
|
25
|
+
return { selection: Selection.create(moved, selection.primaryIndex), origin: 'select' };
|
|
26
|
+
}
|
|
27
|
+
/** Fold one range, as a click on the rail does. */
|
|
28
|
+
export const foldRange = (folds, range) => (ctx) => foldAll(ctx.state, folds, [range]);
|
|
29
|
+
/**
|
|
30
|
+
* Mod-Shift-[ (VS Code's editor.fold): at each head, fold the region its line starts, or else the
|
|
31
|
+
* innermost open region around it.
|
|
32
|
+
*/
|
|
33
|
+
export const foldCode = (folds, source) => (ctx) => {
|
|
34
|
+
const { text, selection } = ctx.state;
|
|
35
|
+
const targets = [];
|
|
36
|
+
for (const r of selection.ranges) {
|
|
37
|
+
const n = text.lineAt(r.head).number;
|
|
38
|
+
let t = !folds.isFolded(n) ? source.rangeAt(n) : null;
|
|
39
|
+
t ??= source.enclosing(n);
|
|
40
|
+
while (t && folds.isFolded(t.line))
|
|
41
|
+
t = source.enclosing(t.line);
|
|
42
|
+
if (t && !targets.some((g) => g.line === t.line))
|
|
43
|
+
targets.push(t);
|
|
44
|
+
}
|
|
45
|
+
return foldAll(ctx.state, folds, targets);
|
|
46
|
+
};
|
|
47
|
+
/** Mod-Shift-] (editor.unfold): open the fold on each head's line. */
|
|
48
|
+
export const unfoldCode = (folds) => (ctx) => {
|
|
49
|
+
const { text, selection } = ctx.state;
|
|
50
|
+
let any = false;
|
|
51
|
+
for (const r of selection.ranges)
|
|
52
|
+
any = folds.unfold(text.lineAt(r.head).number) || any;
|
|
53
|
+
return any ? { selection, origin: 'select' } : null;
|
|
54
|
+
};
|
|
55
|
+
/**
|
|
56
|
+
* Vertical motion by columns over folded text, for views with no pixel geometry (a terminal, or
|
|
57
|
+
* tests). Hidden lines are skipped; the goal is a column, as in moveVertical's headless path.
|
|
58
|
+
*/
|
|
59
|
+
export function columnLayout(state, hidden, pageLines = 1) {
|
|
60
|
+
return {
|
|
61
|
+
pageLines,
|
|
62
|
+
moveVertically(pos, dir, goalX) {
|
|
63
|
+
const text = state().text;
|
|
64
|
+
const line = text.lineAt(pos);
|
|
65
|
+
const col = goalX ?? pos - line.from;
|
|
66
|
+
const t = nextVisibleLine(hidden(), line.number, dir, text.lines);
|
|
67
|
+
if (t < 1)
|
|
68
|
+
return { pos: 0, goalX: col };
|
|
69
|
+
if (t > text.lines)
|
|
70
|
+
return { pos: text.length, goalX: col };
|
|
71
|
+
const target = text.line(t);
|
|
72
|
+
return { pos: target.from + Math.min(col, target.to - target.from), goalX: col };
|
|
73
|
+
},
|
|
74
|
+
};
|
|
75
|
+
}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import type { Command } from './commands.ts';
|
|
2
|
+
/**
|
|
3
|
+
* Key names are CM6 style: modifiers in the fixed order Mod-Ctrl-Alt-Shift, then the
|
|
4
|
+
* `KeyboardEvent.key` value ("ArrowLeft", "z", "Enter"). "Mod" is Meta on Apple platforms and
|
|
5
|
+
* Ctrl elsewhere; `normalizeKey` folds it so lookups are one Map get.
|
|
6
|
+
*/
|
|
7
|
+
export interface KeyBinding {
|
|
8
|
+
readonly key: string;
|
|
9
|
+
readonly run: Command;
|
|
10
|
+
}
|
|
11
|
+
/** The subset of KeyboardEvent the keymap reads, so it runs headless in tests. */
|
|
12
|
+
export interface KeyLike {
|
|
13
|
+
readonly key: string;
|
|
14
|
+
readonly ctrlKey: boolean;
|
|
15
|
+
readonly metaKey: boolean;
|
|
16
|
+
readonly altKey: boolean;
|
|
17
|
+
readonly shiftKey: boolean;
|
|
18
|
+
readonly isComposing: boolean;
|
|
19
|
+
}
|
|
20
|
+
/** Keys pressed during composition never reach the keymap: `isComposing` folds to a key name
|
|
21
|
+
* that no binding can ever register (it is not a valid `KeyboardEvent.key` value). */
|
|
22
|
+
export declare function normalizeKey(e: KeyLike, apple: boolean): string;
|
|
23
|
+
/**
|
|
24
|
+
* Layers of bindings. Higher priority layers are consulted first; within a layer, later
|
|
25
|
+
* registrations win. `candidates` yields in that order; the caller runs each until one returns a
|
|
26
|
+
* spec. Keys pressed during composition never reach the keymap.
|
|
27
|
+
*/
|
|
28
|
+
export declare class Keymap {
|
|
29
|
+
#private;
|
|
30
|
+
add(bindings: readonly KeyBinding[], priority?: number, signal?: AbortSignal): void;
|
|
31
|
+
candidates(key: string): Iterable<Command>;
|
|
32
|
+
}
|
|
33
|
+
/** The first-slice bindings. Undo/redo are added by the element because they close over History. */
|
|
34
|
+
export declare const defaultKeymap: readonly KeyBinding[];
|
|
35
|
+
/**
|
|
36
|
+
* Word and line bindings differ by platform. Every one of them is bound, because an unbound key
|
|
37
|
+
* falls through to the platform, which acts on the primary cursor only and then collapses the
|
|
38
|
+
* rest of the selection.
|
|
39
|
+
*/
|
|
40
|
+
export declare function platformKeymap(apple: boolean): readonly KeyBinding[];
|