@dou-ui/code 0.0.0-stage → 0.1.0-alpha.0
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 +10 -2
- package/dist/_virtual/_rolldown/runtime.js +12 -0
- package/dist/assets/default-BZBMZKf_.js.map +1 -0
- package/dist/code-diff.js +6 -0
- package/dist/code-diff.js.map +1 -0
- package/dist/code-diff.vue_vue_type_script_setup_true_lang.js +792 -0
- package/dist/code-diff.vue_vue_type_script_setup_true_lang.js.map +1 -0
- package/dist/code-editor.js +6 -0
- package/dist/code-editor.js.map +1 -0
- package/dist/code-editor.vue_vue_type_script_setup_true_lang.js +4917 -0
- package/dist/code-editor.vue_vue_type_script_setup_true_lang.js.map +1 -0
- package/dist/code-input.js +6 -0
- package/dist/code-input.js.map +1 -0
- package/dist/code-input.vue_vue_type_script_setup_true_lang.js +237 -0
- package/dist/code-input.vue_vue_type_script_setup_true_lang.js.map +1 -0
- package/dist/code-keys.js +42 -0
- package/dist/code-keys.js.map +1 -0
- package/dist/code-keys.vue_vue_type_script_setup_true_lang.js +168 -0
- package/dist/code-keys.vue_vue_type_script_setup_true_lang.js.map +1 -0
- package/dist/code-keys2.js +6 -0
- package/dist/code-keys2.js.map +1 -0
- package/dist/collaboration/index.js +255 -0
- package/dist/collaboration/index.js.map +1 -0
- package/dist/completion/match.js +55 -0
- package/dist/completion/match.js.map +1 -0
- package/dist/completion/snippet.js +71 -0
- package/dist/completion/snippet.js.map +1 -0
- package/dist/core/brackets.js +55 -0
- package/dist/core/brackets.js.map +1 -0
- package/dist/core/changes.js +185 -0
- package/dist/core/changes.js.map +1 -0
- package/dist/core/chars.js +130 -0
- package/dist/core/chars.js.map +1 -0
- package/dist/core/commands.js +910 -0
- package/dist/core/commands.js.map +1 -0
- package/dist/core/diff.js +215 -0
- package/dist/core/diff.js.map +1 -0
- package/dist/core/folding.js +87 -0
- package/dist/core/folding.js.map +1 -0
- package/dist/core/history.js +74 -0
- package/dist/core/history.js.map +1 -0
- package/dist/core/keymap.js +326 -0
- package/dist/core/keymap.js.map +1 -0
- package/dist/core/search.js +91 -0
- package/dist/core/search.js.map +1 -0
- package/dist/core/selection.js +88 -0
- package/dist/core/selection.js.map +1 -0
- package/dist/core/text.js +320 -0
- package/dist/core/text.js.map +1 -0
- package/dist/core/transaction.js +98 -0
- package/dist/core/transaction.js.map +1 -0
- package/dist/extension.js +16 -0
- package/dist/extension.js.map +1 -0
- package/dist/index.js +38 -0
- package/dist/index.js.map +1 -0
- package/dist/language/highlighter.js +336 -0
- package/dist/language/highlighter.js.map +1 -0
- package/dist/language/language.js +138 -0
- package/dist/language/language.js.map +1 -0
- package/dist/language/stream.js +112 -0
- package/dist/language/stream.js.map +1 -0
- package/dist/language/to-html.js +36 -0
- package/dist/language/to-html.js.map +1 -0
- package/dist/language/worker-client.js +198 -0
- package/dist/language/worker-client.js.map +1 -0
- package/dist/language/worker-protocol.js +48 -0
- package/dist/language/worker-protocol.js.map +1 -0
- package/dist/languages/css.js +128 -0
- package/dist/languages/css.js.map +1 -0
- package/dist/languages/html.js +175 -0
- package/dist/languages/html.js.map +1 -0
- package/dist/languages/javascript.js +300 -0
- package/dist/languages/javascript.js.map +1 -0
- package/dist/languages/json.js +45 -0
- package/dist/languages/json.js.map +1 -0
- package/dist/languages/markdown.js +82 -0
- package/dist/languages/markdown.js.map +1 -0
- package/dist/languages/python.js +88 -0
- package/dist/languages/python.js.map +1 -0
- package/dist/languages/shell.js +153 -0
- package/dist/languages/shell.js.map +1 -0
- package/dist/languages/sql.js +91 -0
- package/dist/languages/sql.js.map +1 -0
- package/dist/languages/yaml.js +85 -0
- package/dist/languages/yaml.js.map +1 -0
- package/dist/locale.js +164 -0
- package/dist/locale.js.map +1 -0
- package/dist/lsp/index.js +715 -0
- package/dist/lsp/index.js.map +1 -0
- package/dist/lsp/markup.js +139 -0
- package/dist/lsp/markup.js.map +1 -0
- package/dist/render-part.js +14 -0
- package/dist/render-part.js.map +1 -0
- package/dist/shiki/index.js +140 -0
- package/dist/shiki/index.js.map +1 -0
- package/dist/style.css +1 -0
- package/dist/style.d.ts +1 -0
- package/dist/style.js +4 -0
- package/dist/types/code-diff.vue.d.ts +45 -0
- package/dist/types/code-editor.vue.d.ts +43 -0
- package/dist/types/code-input.vue.d.ts +32 -0
- package/dist/types/code-keys.d.ts +9 -0
- package/dist/types/code-keys.vue.d.ts +8 -0
- package/dist/types/collaboration/index.d.ts +36 -0
- package/dist/types/completion/match.d.ts +13 -0
- package/dist/types/completion/snippet.d.ts +17 -0
- package/dist/types/core/brackets.d.ts +15 -0
- package/dist/types/core/changes.d.ts +52 -0
- package/dist/types/core/chars.d.ts +24 -0
- package/dist/types/core/commands.d.ts +199 -0
- package/dist/types/core/diff.d.ts +52 -0
- package/dist/types/core/folding.d.ts +23 -0
- package/dist/types/core/history.d.ts +24 -0
- package/dist/types/core/keymap.d.ts +29 -0
- package/dist/types/core/search.d.ts +37 -0
- package/dist/types/core/selection.d.ts +28 -0
- package/dist/types/core/text.d.ts +79 -0
- package/dist/types/core/transaction.d.ts +65 -0
- package/dist/types/extension.d.ts +321 -0
- package/dist/types/index.d.ts +159 -0
- package/dist/types/language/highlighter.d.ts +99 -0
- package/dist/types/language/language.d.ts +100 -0
- package/dist/types/language/stream.d.ts +56 -0
- package/dist/types/language/to-html.d.ts +17 -0
- package/dist/types/language/worker-client.d.ts +52 -0
- package/dist/types/language/worker-protocol.d.ts +50 -0
- package/dist/types/languages/css.d.ts +17 -0
- package/dist/types/languages/html.d.ts +34 -0
- package/dist/types/languages/javascript.d.ts +29 -0
- package/dist/types/languages/json.d.ts +7 -0
- package/dist/types/languages/markdown.d.ts +17 -0
- package/dist/types/languages/python.d.ts +9 -0
- package/dist/types/languages/shell.d.ts +11 -0
- package/dist/types/languages/sql.d.ts +9 -0
- package/dist/types/languages/yaml.d.ts +14 -0
- package/dist/types/locale.d.ts +79 -0
- package/dist/types/lsp/index.d.ts +119 -0
- package/dist/types/lsp/markup.d.ts +14 -0
- package/dist/types/render-part.d.ts +14 -0
- package/dist/types/shiki/index.d.ts +63 -0
- package/dist/types/types.d.ts +267 -0
- package/dist/types/view/input.d.ts +12 -0
- package/dist/types/view/layout.d.ts +55 -0
- package/dist/types/view/measure.d.ts +22 -0
- package/dist/types/worker/default.d.ts +1 -0
- package/dist/types/worker/index.d.ts +34 -0
- package/dist/view/input.js +27 -0
- package/dist/view/input.js.map +1 -0
- package/dist/view/layout.js +194 -0
- package/dist/view/layout.js.map +1 -0
- package/dist/view/measure.js +104 -0
- package/dist/view/measure.js.map +1 -0
- package/dist/worker/default.js +20 -0
- package/dist/worker/default.js.map +1 -0
- package/dist/worker/index.js +139 -0
- package/dist/worker/index.js.map +1 -0
- package/package.json +88 -4
|
@@ -0,0 +1,326 @@
|
|
|
1
|
+
import { addCursorAbove, addCursorBelow, closeSearch, copyLineDown, copyLineUp, cursorCharLeft, cursorCharRight, cursorDocEnd, cursorDocStart, cursorLineDown, cursorLineEnd, cursorLineStart, cursorLineUp, cursorMatchingBracket, cursorPageDown, cursorPageUp, cursorWordLeft, cursorWordRight, deleteCharBackward, deleteCharForward, deleteLine, deleteLineEnd, deleteLineStart, deleteWordBackward, deleteWordForward, findNext, findPrevious, foldCode, indentLess, indentMore, insertNewline, insertTab, moveLineDown, moveLineUp, nextDiagnostic, nextSnippetField, openReplace, openSearch, previousDiagnostic, previousSnippetField, redo, selectAll, selectAllOccurrences, selectCharLeft, selectCharRight, selectDocEnd, selectDocStart, selectLineDown, selectLineEnd, selectLineStart, selectLineUp, selectNextOccurrence, selectPageDown, selectPageUp, selectWordLeft, selectWordRight, showFixes, showHover, simplifySelection, startCompletion, toggleComment, undo, unfoldCode } from "./commands.js";
|
|
2
|
+
// src/core/keymap.ts
|
|
3
|
+
/** A keymap as bindings, in its own order. */
|
|
4
|
+
var keymapBindings = (keymap) => keymap ? Object.entries(keymap).map(([key, run]) => ({
|
|
5
|
+
key,
|
|
6
|
+
run
|
|
7
|
+
})) : [];
|
|
8
|
+
var ORDER = [
|
|
9
|
+
"mod",
|
|
10
|
+
"ctrl",
|
|
11
|
+
"meta",
|
|
12
|
+
"alt",
|
|
13
|
+
"shift"
|
|
14
|
+
];
|
|
15
|
+
/** A key written as a binding, in one spelling for both platforms. */
|
|
16
|
+
function normalizeKey(key, apple) {
|
|
17
|
+
const parts = key.toLowerCase().split("+");
|
|
18
|
+
const name = parts.pop();
|
|
19
|
+
const mods = new Set(parts.map((part) => {
|
|
20
|
+
if (part === "cmd" || part === "command") part = "meta";
|
|
21
|
+
if (part === "control") part = "ctrl";
|
|
22
|
+
if (part === "option") part = "alt";
|
|
23
|
+
if (apple ? part === "meta" : part === "ctrl") return "mod";
|
|
24
|
+
return part;
|
|
25
|
+
}));
|
|
26
|
+
return [...ORDER.filter((mod) => mods.has(mod)), name === "space" ? " " : name].join("+");
|
|
27
|
+
}
|
|
28
|
+
/** A key press, spelled like `normalizeKey`. Letters and digits are read from the physical key, so ⌥ and layouts don't change them. */
|
|
29
|
+
function eventKey(event, apple) {
|
|
30
|
+
let name = event.key.toLowerCase();
|
|
31
|
+
if (/^Key[A-Z]$/.test(event.code)) name = event.code.slice(3).toLowerCase();
|
|
32
|
+
else if (/^Digit\d$/.test(event.code)) name = event.code.slice(5);
|
|
33
|
+
else if (event.code === "BracketLeft") name = "[";
|
|
34
|
+
else if (event.code === "BracketRight") name = "]";
|
|
35
|
+
const mods = [];
|
|
36
|
+
if (apple ? event.metaKey : event.ctrlKey) mods.push("mod");
|
|
37
|
+
if (apple && event.ctrlKey) mods.push("ctrl");
|
|
38
|
+
if (!apple && event.metaKey) mods.push("meta");
|
|
39
|
+
if (event.altKey) mods.push("alt");
|
|
40
|
+
if (event.shiftKey) mods.push("shift");
|
|
41
|
+
return [...mods, name].join("+");
|
|
42
|
+
}
|
|
43
|
+
/** Looks up bindings by key; later bindings win over earlier ones. */
|
|
44
|
+
function buildKeymap(bindings, apple) {
|
|
45
|
+
const map = /* @__PURE__ */ new Map();
|
|
46
|
+
const add = (key, run) => {
|
|
47
|
+
const name = normalizeKey(key, apple);
|
|
48
|
+
map.set(name, [run, ...map.get(name) ?? []]);
|
|
49
|
+
};
|
|
50
|
+
for (const binding of bindings) {
|
|
51
|
+
const key = apple ? binding.mac ?? binding.key : binding.key;
|
|
52
|
+
if (!key) continue;
|
|
53
|
+
add(key, binding.run);
|
|
54
|
+
if (binding.shift) add(`shift+${key}`, binding.shift);
|
|
55
|
+
}
|
|
56
|
+
return map;
|
|
57
|
+
}
|
|
58
|
+
/** The editing and moving keys every editor has. */
|
|
59
|
+
var defaultKeymap = [
|
|
60
|
+
{
|
|
61
|
+
key: "arrowleft",
|
|
62
|
+
run: cursorCharLeft,
|
|
63
|
+
shift: selectCharLeft
|
|
64
|
+
},
|
|
65
|
+
{
|
|
66
|
+
key: "arrowright",
|
|
67
|
+
run: cursorCharRight,
|
|
68
|
+
shift: selectCharRight
|
|
69
|
+
},
|
|
70
|
+
{
|
|
71
|
+
key: "mod+arrowleft",
|
|
72
|
+
mac: "alt+arrowleft",
|
|
73
|
+
run: cursorWordLeft,
|
|
74
|
+
shift: selectWordLeft
|
|
75
|
+
},
|
|
76
|
+
{
|
|
77
|
+
key: "mod+arrowright",
|
|
78
|
+
mac: "alt+arrowright",
|
|
79
|
+
run: cursorWordRight,
|
|
80
|
+
shift: selectWordRight
|
|
81
|
+
},
|
|
82
|
+
{
|
|
83
|
+
mac: "mod+arrowleft",
|
|
84
|
+
run: cursorLineStart,
|
|
85
|
+
shift: selectLineStart
|
|
86
|
+
},
|
|
87
|
+
{
|
|
88
|
+
mac: "mod+arrowright",
|
|
89
|
+
run: cursorLineEnd,
|
|
90
|
+
shift: selectLineEnd
|
|
91
|
+
},
|
|
92
|
+
{
|
|
93
|
+
key: "home",
|
|
94
|
+
run: cursorLineStart,
|
|
95
|
+
shift: selectLineStart
|
|
96
|
+
},
|
|
97
|
+
{
|
|
98
|
+
key: "end",
|
|
99
|
+
run: cursorLineEnd,
|
|
100
|
+
shift: selectLineEnd
|
|
101
|
+
},
|
|
102
|
+
{
|
|
103
|
+
key: "arrowup",
|
|
104
|
+
run: cursorLineUp,
|
|
105
|
+
shift: selectLineUp
|
|
106
|
+
},
|
|
107
|
+
{
|
|
108
|
+
key: "arrowdown",
|
|
109
|
+
run: cursorLineDown,
|
|
110
|
+
shift: selectLineDown
|
|
111
|
+
},
|
|
112
|
+
{
|
|
113
|
+
key: "mod+home",
|
|
114
|
+
mac: "mod+arrowup",
|
|
115
|
+
run: cursorDocStart,
|
|
116
|
+
shift: selectDocStart
|
|
117
|
+
},
|
|
118
|
+
{
|
|
119
|
+
key: "mod+end",
|
|
120
|
+
mac: "mod+arrowdown",
|
|
121
|
+
run: cursorDocEnd,
|
|
122
|
+
shift: selectDocEnd
|
|
123
|
+
},
|
|
124
|
+
{
|
|
125
|
+
mac: "mod+home",
|
|
126
|
+
run: cursorDocStart,
|
|
127
|
+
shift: selectDocStart
|
|
128
|
+
},
|
|
129
|
+
{
|
|
130
|
+
mac: "mod+end",
|
|
131
|
+
run: cursorDocEnd,
|
|
132
|
+
shift: selectDocEnd
|
|
133
|
+
},
|
|
134
|
+
{
|
|
135
|
+
key: "pageup",
|
|
136
|
+
run: cursorPageUp,
|
|
137
|
+
shift: selectPageUp
|
|
138
|
+
},
|
|
139
|
+
{
|
|
140
|
+
key: "pagedown",
|
|
141
|
+
run: cursorPageDown,
|
|
142
|
+
shift: selectPageDown
|
|
143
|
+
},
|
|
144
|
+
{
|
|
145
|
+
key: "backspace",
|
|
146
|
+
run: deleteCharBackward,
|
|
147
|
+
shift: deleteCharBackward
|
|
148
|
+
},
|
|
149
|
+
{
|
|
150
|
+
key: "delete",
|
|
151
|
+
run: deleteCharForward
|
|
152
|
+
},
|
|
153
|
+
{
|
|
154
|
+
key: "mod+backspace",
|
|
155
|
+
mac: "alt+backspace",
|
|
156
|
+
run: deleteWordBackward
|
|
157
|
+
},
|
|
158
|
+
{
|
|
159
|
+
key: "mod+delete",
|
|
160
|
+
mac: "alt+delete",
|
|
161
|
+
run: deleteWordForward
|
|
162
|
+
},
|
|
163
|
+
{
|
|
164
|
+
mac: "mod+backspace",
|
|
165
|
+
run: deleteLineStart
|
|
166
|
+
},
|
|
167
|
+
{
|
|
168
|
+
mac: "mod+delete",
|
|
169
|
+
run: deleteLineEnd
|
|
170
|
+
},
|
|
171
|
+
{
|
|
172
|
+
mac: "ctrl+k",
|
|
173
|
+
run: deleteLineEnd
|
|
174
|
+
},
|
|
175
|
+
{
|
|
176
|
+
key: "enter",
|
|
177
|
+
run: insertNewline,
|
|
178
|
+
shift: insertNewline
|
|
179
|
+
},
|
|
180
|
+
{
|
|
181
|
+
key: "tab",
|
|
182
|
+
run: insertTab,
|
|
183
|
+
shift: indentLess
|
|
184
|
+
},
|
|
185
|
+
{
|
|
186
|
+
key: "mod+]",
|
|
187
|
+
run: indentMore
|
|
188
|
+
},
|
|
189
|
+
{
|
|
190
|
+
key: "mod+[",
|
|
191
|
+
run: indentLess
|
|
192
|
+
},
|
|
193
|
+
{
|
|
194
|
+
key: "mod+a",
|
|
195
|
+
run: selectAll
|
|
196
|
+
},
|
|
197
|
+
{
|
|
198
|
+
key: "mod+z",
|
|
199
|
+
run: undo
|
|
200
|
+
},
|
|
201
|
+
{
|
|
202
|
+
key: "mod+shift+z",
|
|
203
|
+
run: redo
|
|
204
|
+
},
|
|
205
|
+
{
|
|
206
|
+
key: "mod+y",
|
|
207
|
+
mac: "mod+shift+z",
|
|
208
|
+
run: redo
|
|
209
|
+
},
|
|
210
|
+
{
|
|
211
|
+
key: "escape",
|
|
212
|
+
run: simplifySelection
|
|
213
|
+
},
|
|
214
|
+
{
|
|
215
|
+
key: "escape",
|
|
216
|
+
run: closeSearch
|
|
217
|
+
},
|
|
218
|
+
{
|
|
219
|
+
key: "mod+/",
|
|
220
|
+
run: toggleComment
|
|
221
|
+
},
|
|
222
|
+
{
|
|
223
|
+
key: "mod+d",
|
|
224
|
+
run: selectNextOccurrence
|
|
225
|
+
},
|
|
226
|
+
{
|
|
227
|
+
key: "mod+f",
|
|
228
|
+
run: openSearch
|
|
229
|
+
},
|
|
230
|
+
{
|
|
231
|
+
key: "mod+h",
|
|
232
|
+
mac: "mod+alt+f",
|
|
233
|
+
run: openReplace
|
|
234
|
+
},
|
|
235
|
+
{
|
|
236
|
+
key: "f3",
|
|
237
|
+
run: findNext,
|
|
238
|
+
shift: findPrevious
|
|
239
|
+
},
|
|
240
|
+
{
|
|
241
|
+
key: "mod+g",
|
|
242
|
+
run: findNext,
|
|
243
|
+
shift: findPrevious
|
|
244
|
+
},
|
|
245
|
+
{
|
|
246
|
+
key: "ctrl+shift+[",
|
|
247
|
+
mac: "mod+alt+[",
|
|
248
|
+
run: foldCode
|
|
249
|
+
},
|
|
250
|
+
{
|
|
251
|
+
key: "ctrl+shift+]",
|
|
252
|
+
mac: "mod+alt+]",
|
|
253
|
+
run: unfoldCode
|
|
254
|
+
},
|
|
255
|
+
{
|
|
256
|
+
key: "f8",
|
|
257
|
+
run: nextDiagnostic,
|
|
258
|
+
shift: previousDiagnostic
|
|
259
|
+
},
|
|
260
|
+
{
|
|
261
|
+
key: "mod+.",
|
|
262
|
+
run: showFixes
|
|
263
|
+
},
|
|
264
|
+
{
|
|
265
|
+
key: "ctrl+space",
|
|
266
|
+
run: startCompletion
|
|
267
|
+
},
|
|
268
|
+
{
|
|
269
|
+
key: "alt+escape",
|
|
270
|
+
run: startCompletion
|
|
271
|
+
},
|
|
272
|
+
{
|
|
273
|
+
key: "mod+i",
|
|
274
|
+
run: showHover
|
|
275
|
+
},
|
|
276
|
+
{
|
|
277
|
+
key: "tab",
|
|
278
|
+
run: nextSnippetField,
|
|
279
|
+
shift: previousSnippetField
|
|
280
|
+
},
|
|
281
|
+
{
|
|
282
|
+
key: "mod+shift+l",
|
|
283
|
+
run: selectAllOccurrences
|
|
284
|
+
},
|
|
285
|
+
{
|
|
286
|
+
key: "ctrl+alt+arrowup",
|
|
287
|
+
mac: "mod+alt+arrowup",
|
|
288
|
+
run: addCursorAbove
|
|
289
|
+
},
|
|
290
|
+
{
|
|
291
|
+
key: "ctrl+alt+arrowdown",
|
|
292
|
+
mac: "mod+alt+arrowdown",
|
|
293
|
+
run: addCursorBelow
|
|
294
|
+
},
|
|
295
|
+
{
|
|
296
|
+
key: "alt+arrowup",
|
|
297
|
+
run: moveLineUp
|
|
298
|
+
},
|
|
299
|
+
{
|
|
300
|
+
key: "alt+arrowdown",
|
|
301
|
+
run: moveLineDown
|
|
302
|
+
},
|
|
303
|
+
{
|
|
304
|
+
key: "shift+alt+arrowup",
|
|
305
|
+
run: copyLineUp
|
|
306
|
+
},
|
|
307
|
+
{
|
|
308
|
+
key: "shift+alt+arrowdown",
|
|
309
|
+
run: copyLineDown
|
|
310
|
+
},
|
|
311
|
+
{
|
|
312
|
+
key: "mod+shift+k",
|
|
313
|
+
run: deleteLine
|
|
314
|
+
},
|
|
315
|
+
{
|
|
316
|
+
key: "mod+shift+\\",
|
|
317
|
+
run: cursorMatchingBracket
|
|
318
|
+
},
|
|
319
|
+
{
|
|
320
|
+
key: "mod+shift+|",
|
|
321
|
+
run: cursorMatchingBracket
|
|
322
|
+
}
|
|
323
|
+
];
|
|
324
|
+
export { buildKeymap, defaultKeymap, eventKey, keymapBindings, normalizeKey };
|
|
325
|
+
|
|
326
|
+
//# sourceMappingURL=keymap.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"keymap.js","names":["cmd.cursorCharLeft","cmd.selectCharLeft","cmd.cursorCharRight","cmd.selectCharRight","cmd.cursorWordLeft","cmd.selectWordLeft","cmd.cursorWordRight","cmd.selectWordRight","cmd.cursorLineStart","cmd.selectLineStart","cmd.cursorLineEnd","cmd.selectLineEnd","cmd.cursorLineUp","cmd.selectLineUp","cmd.cursorLineDown","cmd.selectLineDown","cmd.cursorDocStart","cmd.selectDocStart","cmd.cursorDocEnd","cmd.selectDocEnd","cmd.cursorPageUp","cmd.selectPageUp","cmd.cursorPageDown","cmd.selectPageDown","cmd.deleteCharBackward","cmd.deleteCharForward","cmd.deleteWordBackward","cmd.deleteWordForward","cmd.deleteLineStart","cmd.deleteLineEnd","cmd.insertNewline","cmd.insertTab","cmd.indentLess","cmd.indentMore","cmd.selectAll","cmd.undo","cmd.redo","cmd.simplifySelection","cmd.closeSearch","cmd.toggleComment","cmd.selectNextOccurrence","cmd.openSearch","cmd.openReplace","cmd.findNext","cmd.findPrevious","cmd.foldCode","cmd.unfoldCode","cmd.nextDiagnostic","cmd.previousDiagnostic","cmd.showFixes","cmd.startCompletion","cmd.showHover","cmd.nextSnippetField","cmd.previousSnippetField","cmd.selectAllOccurrences","cmd.addCursorAbove","cmd.addCursorBelow","cmd.moveLineUp","cmd.moveLineDown","cmd.copyLineUp","cmd.copyLineDown","cmd.deleteLine","cmd.cursorMatchingBracket"],"sources":["../../src/core/keymap.ts"],"sourcesContent":["import * as cmd from './commands'\nimport type { CodeCommand } from './commands'\n\n/**\n * A key and what it runs. Keys are written like `mod+shift+z`: `mod` is ⌘\n * on Apple devices and Ctrl elsewhere. `mac` replaces `key` on Apple\n * devices; leave `key` out for a binding that only exists there.\n */\nexport interface CodeKeyBinding {\n key?: string\n mac?: string\n run: CodeCommand\n /** Runs this with Shift held too, as the selecting version of a move. */\n shift?: CodeCommand\n}\n\n/**\n * Keys and the commands they run, as `{ 'mod+s': save }`. A command that\n * returns `false` lets the next binding of the same key try.\n */\nexport type CodeKeymap = Record<string, CodeCommand>\n\n/** A keymap as bindings, in its own order. */\nexport const keymapBindings = (keymap: CodeKeymap | null | undefined): CodeKeyBinding[] =>\n keymap ? Object.entries(keymap).map(([key, run]) => ({ key, run })) : []\n\nconst ORDER = ['mod', 'ctrl', 'meta', 'alt', 'shift']\n\n/** A key written as a binding, in one spelling for both platforms. */\nexport function normalizeKey(key: string, apple: boolean): string {\n const parts = key.toLowerCase().split('+')\n const name = parts.pop()!\n const mods = new Set(\n parts.map((part) => {\n if (part === 'cmd' || part === 'command') part = 'meta'\n if (part === 'control') part = 'ctrl'\n if (part === 'option') part = 'alt'\n // `mod` and the key it stands for on this platform are the same.\n if (apple ? part === 'meta' : part === 'ctrl') return 'mod'\n return part\n }),\n )\n return [...ORDER.filter((mod) => mods.has(mod)), name === 'space' ? ' ' : name].join('+')\n}\n\n/** A key press, spelled like `normalizeKey`. Letters and digits are read from the physical key, so ⌥ and layouts don't change them. */\nexport function eventKey(event: KeyboardEvent, apple: boolean): string {\n let name = event.key.toLowerCase()\n if (/^Key[A-Z]$/.test(event.code)) name = event.code.slice(3).toLowerCase()\n else if (/^Digit\\d$/.test(event.code)) name = event.code.slice(5)\n // Shift and ⌥ change what the bracket keys type (`{`, `“`); the binding means the key.\n else if (event.code === 'BracketLeft') name = '['\n else if (event.code === 'BracketRight') name = ']'\n const mods: string[] = []\n if (apple ? event.metaKey : event.ctrlKey) mods.push('mod')\n if (apple && event.ctrlKey) mods.push('ctrl')\n if (!apple && event.metaKey) mods.push('meta')\n if (event.altKey) mods.push('alt')\n if (event.shiftKey) mods.push('shift')\n return [...mods, name].join('+')\n}\n\n/** Looks up bindings by key; later bindings win over earlier ones. */\nexport function buildKeymap(bindings: readonly CodeKeyBinding[], apple: boolean) {\n const map = new Map<string, CodeCommand[]>()\n const add = (key: string, run: CodeCommand) => {\n const name = normalizeKey(key, apple)\n map.set(name, [run, ...(map.get(name) ?? [])])\n }\n for (const binding of bindings) {\n const key = apple ? (binding.mac ?? binding.key) : binding.key\n if (!key) continue\n add(key, binding.run)\n if (binding.shift) add(`shift+${key}`, binding.shift)\n }\n return map\n}\n\n/** The editing and moving keys every editor has. */\nexport const defaultKeymap: readonly CodeKeyBinding[] = [\n { key: 'arrowleft', run: cmd.cursorCharLeft, shift: cmd.selectCharLeft },\n { key: 'arrowright', run: cmd.cursorCharRight, shift: cmd.selectCharRight },\n {\n key: 'mod+arrowleft',\n mac: 'alt+arrowleft',\n run: cmd.cursorWordLeft,\n shift: cmd.selectWordLeft,\n },\n {\n key: 'mod+arrowright',\n mac: 'alt+arrowright',\n run: cmd.cursorWordRight,\n shift: cmd.selectWordRight,\n },\n { mac: 'mod+arrowleft', run: cmd.cursorLineStart, shift: cmd.selectLineStart },\n { mac: 'mod+arrowright', run: cmd.cursorLineEnd, shift: cmd.selectLineEnd },\n { key: 'home', run: cmd.cursorLineStart, shift: cmd.selectLineStart },\n { key: 'end', run: cmd.cursorLineEnd, shift: cmd.selectLineEnd },\n { key: 'arrowup', run: cmd.cursorLineUp, shift: cmd.selectLineUp },\n { key: 'arrowdown', run: cmd.cursorLineDown, shift: cmd.selectLineDown },\n { key: 'mod+home', mac: 'mod+arrowup', run: cmd.cursorDocStart, shift: cmd.selectDocStart },\n { key: 'mod+end', mac: 'mod+arrowdown', run: cmd.cursorDocEnd, shift: cmd.selectDocEnd },\n { mac: 'mod+home', run: cmd.cursorDocStart, shift: cmd.selectDocStart },\n { mac: 'mod+end', run: cmd.cursorDocEnd, shift: cmd.selectDocEnd },\n { key: 'pageup', run: cmd.cursorPageUp, shift: cmd.selectPageUp },\n { key: 'pagedown', run: cmd.cursorPageDown, shift: cmd.selectPageDown },\n { key: 'backspace', run: cmd.deleteCharBackward, shift: cmd.deleteCharBackward },\n { key: 'delete', run: cmd.deleteCharForward },\n { key: 'mod+backspace', mac: 'alt+backspace', run: cmd.deleteWordBackward },\n { key: 'mod+delete', mac: 'alt+delete', run: cmd.deleteWordForward },\n { mac: 'mod+backspace', run: cmd.deleteLineStart },\n { mac: 'mod+delete', run: cmd.deleteLineEnd },\n { mac: 'ctrl+k', run: cmd.deleteLineEnd },\n { key: 'enter', run: cmd.insertNewline, shift: cmd.insertNewline },\n { key: 'tab', run: cmd.insertTab, shift: cmd.indentLess },\n { key: 'mod+]', run: cmd.indentMore },\n { key: 'mod+[', run: cmd.indentLess },\n { key: 'mod+a', run: cmd.selectAll },\n { key: 'mod+z', run: cmd.undo },\n { key: 'mod+shift+z', run: cmd.redo },\n { key: 'mod+y', mac: 'mod+shift+z', run: cmd.redo },\n { key: 'escape', run: cmd.simplifySelection },\n { key: 'escape', run: cmd.closeSearch },\n { key: 'mod+/', run: cmd.toggleComment },\n { key: 'mod+d', run: cmd.selectNextOccurrence },\n { key: 'mod+f', run: cmd.openSearch },\n { key: 'mod+h', mac: 'mod+alt+f', run: cmd.openReplace },\n { key: 'f3', run: cmd.findNext, shift: cmd.findPrevious },\n { key: 'mod+g', run: cmd.findNext, shift: cmd.findPrevious },\n { key: 'ctrl+shift+[', mac: 'mod+alt+[', run: cmd.foldCode },\n { key: 'ctrl+shift+]', mac: 'mod+alt+]', run: cmd.unfoldCode },\n { key: 'f8', run: cmd.nextDiagnostic, shift: cmd.previousDiagnostic },\n { key: 'mod+.', run: cmd.showFixes },\n // Ctrl + Space switches input methods on Apple devices: Alt + Esc as well.\n { key: 'ctrl+space', run: cmd.startCompletion },\n { key: 'alt+escape', run: cmd.startCompletion },\n { key: 'mod+i', run: cmd.showHover },\n // Tried before indenting: inside a snippet, Tab goes to its next place.\n { key: 'tab', run: cmd.nextSnippetField, shift: cmd.previousSnippetField },\n { key: 'mod+shift+l', run: cmd.selectAllOccurrences },\n { key: 'ctrl+alt+arrowup', mac: 'mod+alt+arrowup', run: cmd.addCursorAbove },\n { key: 'ctrl+alt+arrowdown', mac: 'mod+alt+arrowdown', run: cmd.addCursorBelow },\n { key: 'alt+arrowup', run: cmd.moveLineUp },\n { key: 'alt+arrowdown', run: cmd.moveLineDown },\n { key: 'shift+alt+arrowup', run: cmd.copyLineUp },\n { key: 'shift+alt+arrowdown', run: cmd.copyLineDown },\n { key: 'mod+shift+k', run: cmd.deleteLine },\n { key: 'mod+shift+\\\\', run: cmd.cursorMatchingBracket },\n { key: 'mod+shift+|', run: cmd.cursorMatchingBracket },\n]\n"],"mappings":";;;AAuBA,IAAa,kBAAkB,WAC7B,SAAS,OAAO,QAAQ,MAAM,CAAC,CAAC,KAAK,CAAC,KAAK,UAAU;CAAE;CAAK;AAAI,EAAE,IAAI,CAAC;AA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,OAAOC;CAAuB;CACpE;EAAE,KAAK;EAAS,KAAKC;CAAc;CAEnC;EAAE,KAAK;EAAc,KAAKC;CAAoB;CAC9C;EAAE,KAAK;EAAc,KAAKA;CAAoB;CAC9C;EAAE,KAAK;EAAS,KAAKC;CAAc;CAEnC;EAAE,KAAK;EAAO,KAAKC;EAAsB,OAAOC;CAAyB;CACzE;EAAE,KAAK;EAAe,KAAKC;CAAyB;CACpD;EAAE,KAAK;EAAoB,KAAK;EAAmB,KAAKC;CAAmB;CAC3E;EAAE,KAAK;EAAsB,KAAK;EAAqB,KAAKC;CAAmB;CAC/E;EAAE,KAAK;EAAe,KAAKC;CAAe;CAC1C;EAAE,KAAK;EAAiB,KAAKC;CAAiB;CAC9C;EAAE,KAAK;EAAqB,KAAKC;CAAe;CAChD;EAAE,KAAK;EAAuB,KAAKC;CAAiB;CACpD;EAAE,KAAK;EAAe,KAAKC;CAAe;CAC1C;EAAE,KAAK;EAAgB,KAAKC;CAA0B;CACtD;EAAE,KAAK;EAAe,KAAKA;CAA0B;AACvD"}
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
// src/core/search.ts
|
|
2
|
+
var WORD = /[\p{L}\p{N}_$]/u;
|
|
3
|
+
var escapeRegExp = (text) => text.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
4
|
+
/** The query as one global regular expression, or the reason it can't be one. */
|
|
5
|
+
function queryRegExp(query) {
|
|
6
|
+
const flags = "gmu" + (query.caseSensitive ? "" : "i");
|
|
7
|
+
try {
|
|
8
|
+
return new RegExp(query.regexp ? query.search : escapeRegExp(query.search), flags);
|
|
9
|
+
} catch (error) {
|
|
10
|
+
return error.message;
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* Every match of `query` in `doc`, in order, at most `limit` of them. An
|
|
15
|
+
* empty match (a regular expression like `^`) is skipped, since it would
|
|
16
|
+
* select nothing.
|
|
17
|
+
*/
|
|
18
|
+
function searchText(doc, query, limit = 1e4) {
|
|
19
|
+
if (!query.search) return {
|
|
20
|
+
matches: [],
|
|
21
|
+
capped: false
|
|
22
|
+
};
|
|
23
|
+
const re = queryRegExp(query);
|
|
24
|
+
if (typeof re === "string") return {
|
|
25
|
+
matches: [],
|
|
26
|
+
capped: false,
|
|
27
|
+
error: re
|
|
28
|
+
};
|
|
29
|
+
const text = doc.toString();
|
|
30
|
+
const matches = [];
|
|
31
|
+
re.lastIndex = 0;
|
|
32
|
+
let found;
|
|
33
|
+
while (found = re.exec(text)) {
|
|
34
|
+
const from = found.index;
|
|
35
|
+
const to = from + found[0].length;
|
|
36
|
+
if (to === from) {
|
|
37
|
+
re.lastIndex = from + 1;
|
|
38
|
+
continue;
|
|
39
|
+
}
|
|
40
|
+
if (query.wholeWord && (WORD.test(text[from - 1] ?? "") || WORD.test(text[to] ?? ""))) continue;
|
|
41
|
+
if (matches.length >= limit) return {
|
|
42
|
+
matches,
|
|
43
|
+
capped: true
|
|
44
|
+
};
|
|
45
|
+
matches.push({
|
|
46
|
+
from,
|
|
47
|
+
to
|
|
48
|
+
});
|
|
49
|
+
}
|
|
50
|
+
return {
|
|
51
|
+
matches,
|
|
52
|
+
capped: false
|
|
53
|
+
};
|
|
54
|
+
}
|
|
55
|
+
/** The text that replaces one match: `query.replace`, with groups filled in for a regular expression. */
|
|
56
|
+
function replacementFor(doc, match, query) {
|
|
57
|
+
const replace = query.replace ?? "";
|
|
58
|
+
if (!query.regexp) return replace;
|
|
59
|
+
const re = queryRegExp(query);
|
|
60
|
+
if (typeof re === "string") return replace;
|
|
61
|
+
const text = doc.toString();
|
|
62
|
+
const sticky = new RegExp(re.source, re.flags.replace("g", "") + "y");
|
|
63
|
+
sticky.lastIndex = match.from;
|
|
64
|
+
const found = sticky.exec(text);
|
|
65
|
+
return found ? expand(replace, found) : replace;
|
|
66
|
+
}
|
|
67
|
+
/** Fills `$&`, `$1`…`$99`, `$<name>` and `$$` in a replacement from a match. */
|
|
68
|
+
function expand(replace, found) {
|
|
69
|
+
return replace.replace(/\$(\$|&|\d{1,2}|<([^>]*)>)/g, (all, what, name) => {
|
|
70
|
+
if (what === "$") return "$";
|
|
71
|
+
if (what === "&") return found[0];
|
|
72
|
+
if (name !== void 0) return found.groups?.[name] ?? "";
|
|
73
|
+
const group = Number(what);
|
|
74
|
+
return group < found.length ? found[group] ?? "" : all;
|
|
75
|
+
});
|
|
76
|
+
}
|
|
77
|
+
/** The index of the first match at or after `pos` (wrapping to the first), or `-1` without matches. */
|
|
78
|
+
function matchAfter(matches, pos) {
|
|
79
|
+
if (!matches.length) return -1;
|
|
80
|
+
let lo = 0;
|
|
81
|
+
let hi = matches.length;
|
|
82
|
+
while (lo < hi) {
|
|
83
|
+
const mid = lo + hi >> 1;
|
|
84
|
+
if (matches[mid].from < pos) lo = mid + 1;
|
|
85
|
+
else hi = mid;
|
|
86
|
+
}
|
|
87
|
+
return lo === matches.length ? 0 : lo;
|
|
88
|
+
}
|
|
89
|
+
export { matchAfter, queryRegExp, replacementFor, searchText };
|
|
90
|
+
|
|
91
|
+
//# sourceMappingURL=search.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"search.js","names":[],"sources":["../../src/core/search.ts"],"sourcesContent":["import type { CodeText } from './text'\n\n/** What to look for. */\nexport interface CodeSearchQuery {\n /** The text, or a regular expression with `regexp`. */\n search: string\n /** Tells upper and lower case apart. */\n caseSensitive?: boolean\n /** Only matches that are whole words. */\n wholeWord?: boolean\n /** `search` is a regular expression (JavaScript syntax). */\n regexp?: boolean\n /** What `replace` puts in; with `regexp`, `$1` and `$<name>` refer to groups. */\n replace?: string\n}\n\nexport interface CodeMatch {\n from: number\n to: number\n}\n\nexport interface CodeSearchResult {\n matches: CodeMatch[]\n /** More matches exist beyond `limit`. */\n capped: boolean\n /** Why the regular expression is not valid, when it isn't. */\n error?: string\n}\n\nconst WORD = /[\\p{L}\\p{N}_$]/u\nconst escapeRegExp = (text: string) => text.replace(/[.*+?^${}()|[\\]\\\\]/g, '\\\\$&')\n\n/** The query as one global regular expression, or the reason it can't be one. */\nexport function queryRegExp(query: CodeSearchQuery): RegExp | string {\n const flags = 'gmu' + (query.caseSensitive ? '' : 'i')\n try {\n return new RegExp(query.regexp ? query.search : escapeRegExp(query.search), flags)\n } catch (error) {\n return (error as Error).message\n }\n}\n\n/**\n * Every match of `query` in `doc`, in order, at most `limit` of them. An\n * empty match (a regular expression like `^`) is skipped, since it would\n * select nothing.\n */\nexport function searchText(\n doc: CodeText,\n query: CodeSearchQuery,\n limit = 10_000,\n): CodeSearchResult {\n if (!query.search) return { matches: [], capped: false }\n const re = queryRegExp(query)\n if (typeof re === 'string') return { matches: [], capped: false, error: re }\n const text = doc.toString()\n const matches: CodeMatch[] = []\n re.lastIndex = 0\n let found: RegExpExecArray | null\n while ((found = re.exec(text))) {\n const from = found.index\n const to = from + found[0].length\n if (to === from) {\n re.lastIndex = from + 1\n continue\n }\n if (query.wholeWord && (WORD.test(text[from - 1] ?? '') || WORD.test(text[to] ?? ''))) continue\n if (matches.length >= limit) return { matches, capped: true }\n matches.push({ from, to })\n }\n return { matches, capped: false }\n}\n\n/** The text that replaces one match: `query.replace`, with groups filled in for a regular expression. */\nexport function replacementFor(doc: CodeText, match: CodeMatch, query: CodeSearchQuery): string {\n const replace = query.replace ?? ''\n if (!query.regexp) return replace\n const re = queryRegExp(query)\n if (typeof re === 'string') return replace\n // Match again at the same place, so `$1` refers to this match's groups (lookbehind sees the text before).\n const text = doc.toString()\n const sticky = new RegExp(re.source, re.flags.replace('g', '') + 'y')\n sticky.lastIndex = match.from\n const found = sticky.exec(text)\n return found ? expand(replace, found) : replace\n}\n\n/** Fills `$&`, `$1`…`$99`, `$<name>` and `$$` in a replacement from a match. */\nfunction expand(replace: string, found: RegExpExecArray) {\n return replace.replace(/\\$(\\$|&|\\d{1,2}|<([^>]*)>)/g, (all, what: string, name?: string) => {\n if (what === '$') return '$'\n if (what === '&') return found[0]\n if (name !== undefined) return found.groups?.[name] ?? ''\n const group = Number(what)\n return group < found.length ? (found[group] ?? '') : all\n })\n}\n\n/** The index of the first match at or after `pos` (wrapping to the first), or `-1` without matches. */\nexport function matchAfter(matches: readonly CodeMatch[], pos: number): number {\n if (!matches.length) return -1\n let lo = 0\n let hi = matches.length\n while (lo < hi) {\n const mid = (lo + hi) >> 1\n if (matches[mid]!.from < pos) lo = mid + 1\n else hi = mid\n }\n return lo === matches.length ? 0 : lo\n}\n"],"mappings":";AA6BA,IAAM,OAAO;AACb,IAAM,gBAAgB,SAAiB,KAAK,QAAQ,uBAAuB,MAAM;;AAGjF,SAAgB,YAAY,OAAyC;CACnE,MAAM,QAAQ,SAAS,MAAM,gBAAgB,KAAK;CAClD,IAAI;EACF,OAAO,IAAI,OAAO,MAAM,SAAS,MAAM,SAAS,aAAa,MAAM,MAAM,GAAG,KAAK;CACnF,SAAS,OAAO;EACd,OAAQ,MAAgB;CAC1B;AACF;;;;;;AAOA,SAAgB,WACd,KACA,OACA,QAAQ,KACU;CAClB,IAAI,CAAC,MAAM,QAAQ,OAAO;EAAE,SAAS,CAAC;EAAG,QAAQ;CAAM;CACvD,MAAM,KAAK,YAAY,KAAK;CAC5B,IAAI,OAAO,OAAO,UAAU,OAAO;EAAE,SAAS,CAAC;EAAG,QAAQ;EAAO,OAAO;CAAG;CAC3E,MAAM,OAAO,IAAI,SAAS;CAC1B,MAAM,UAAuB,CAAC;CAC9B,GAAG,YAAY;CACf,IAAI;CACJ,OAAQ,QAAQ,GAAG,KAAK,IAAI,GAAI;EAC9B,MAAM,OAAO,MAAM;EACnB,MAAM,KAAK,OAAO,MAAM,EAAE,CAAC;EAC3B,IAAI,OAAO,MAAM;GACf,GAAG,YAAY,OAAO;GACtB;EACF;EACA,IAAI,MAAM,cAAc,KAAK,KAAK,KAAK,OAAO,MAAM,EAAE,KAAK,KAAK,KAAK,KAAK,OAAO,EAAE,IAAI;EACvF,IAAI,QAAQ,UAAU,OAAO,OAAO;GAAE;GAAS,QAAQ;EAAK;EAC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, range.head);
|
|
4
|
+
var isEmptyRange = (range) => range.anchor === range.head;
|
|
5
|
+
/** A selection of one range; just a cursor when `head` is left out. */
|
|
6
|
+
function selectionOf(anchor, head = anchor) {
|
|
7
|
+
return {
|
|
8
|
+
ranges: [{
|
|
9
|
+
anchor,
|
|
10
|
+
head
|
|
11
|
+
}],
|
|
12
|
+
main: 0
|
|
13
|
+
};
|
|
14
|
+
}
|
|
15
|
+
/** The main range. */
|
|
16
|
+
var mainRange = (selection) => selection.ranges[selection.main] ?? selection.ranges[0];
|
|
17
|
+
/**
|
|
18
|
+
* Clamps every range into a text of `length`, sorts them and joins the ones
|
|
19
|
+
* that overlap (or are cursors at the same spot). The main range keeps its
|
|
20
|
+
* place in whichever range it ended up in.
|
|
21
|
+
*/
|
|
22
|
+
function normalizeSelection(selection, length) {
|
|
23
|
+
const clamp = (pos) => Math.max(0, Math.min(length, Math.round(pos) || 0));
|
|
24
|
+
const main = mainRange(selection);
|
|
25
|
+
const ranges = selection.ranges.map((range) => ({
|
|
26
|
+
anchor: clamp(range.anchor),
|
|
27
|
+
head: clamp(range.head),
|
|
28
|
+
main: range === main
|
|
29
|
+
}));
|
|
30
|
+
if (!ranges.length) return selectionOf(0);
|
|
31
|
+
ranges.sort((a, b) => Math.min(a.anchor, a.head) - Math.min(b.anchor, b.head));
|
|
32
|
+
const out = [];
|
|
33
|
+
for (const range of ranges) {
|
|
34
|
+
const last = out[out.length - 1];
|
|
35
|
+
const from = rangeFrom(range);
|
|
36
|
+
if (last) {
|
|
37
|
+
const lastTo = rangeTo(last);
|
|
38
|
+
if (from < lastTo || from === lastTo && (isEmptyRange(range) || isEmptyRange(last))) {
|
|
39
|
+
const start = Math.min(rangeFrom(last), from);
|
|
40
|
+
const end = Math.max(lastTo, rangeTo(range));
|
|
41
|
+
const backward = (range.main ? range : last).head < (range.main ? range : last).anchor;
|
|
42
|
+
last.anchor = backward ? end : start;
|
|
43
|
+
last.head = backward ? start : end;
|
|
44
|
+
last.main ||= range.main;
|
|
45
|
+
continue;
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
out.push(range);
|
|
49
|
+
}
|
|
50
|
+
const index = Math.max(0, out.findIndex((range) => range.main));
|
|
51
|
+
return {
|
|
52
|
+
ranges: out.map(({ anchor, head }) => ({
|
|
53
|
+
anchor,
|
|
54
|
+
head
|
|
55
|
+
})),
|
|
56
|
+
main: index
|
|
57
|
+
};
|
|
58
|
+
}
|
|
59
|
+
/** Moves every range through `changes`. Cursors stay after text typed at them. */
|
|
60
|
+
function mapSelection(selection, changes) {
|
|
61
|
+
if (changes.empty) return selection;
|
|
62
|
+
return {
|
|
63
|
+
ranges: selection.ranges.map((range) => {
|
|
64
|
+
if (isEmptyRange(range)) {
|
|
65
|
+
const pos = changes.mapPos(range.head, 1);
|
|
66
|
+
return {
|
|
67
|
+
anchor: pos,
|
|
68
|
+
head: pos
|
|
69
|
+
};
|
|
70
|
+
}
|
|
71
|
+
const forward = range.head >= range.anchor;
|
|
72
|
+
return {
|
|
73
|
+
anchor: changes.mapPos(range.anchor, forward ? 1 : -1),
|
|
74
|
+
head: changes.mapPos(range.head, forward ? -1 : 1)
|
|
75
|
+
};
|
|
76
|
+
}),
|
|
77
|
+
main: selection.main
|
|
78
|
+
};
|
|
79
|
+
}
|
|
80
|
+
/** Whether two selections are the same. */
|
|
81
|
+
function sameSelection(a, b) {
|
|
82
|
+
if (a === b) return true;
|
|
83
|
+
if (a.main !== b.main || a.ranges.length !== b.ranges.length) return false;
|
|
84
|
+
return a.ranges.every((range, i) => range.anchor === b.ranges[i].anchor && range.head === b.ranges[i].head);
|
|
85
|
+
}
|
|
86
|
+
export { isEmptyRange, mainRange, mapSelection, normalizeSelection, rangeFrom, rangeTo, sameSelection, selectionOf };
|
|
87
|
+
|
|
88
|
+
//# sourceMappingURL=selection.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"selection.js","names":[],"sources":["../../src/core/selection.ts"],"sourcesContent":["import type { ChangeSet } from './changes'\n\n/** One selected range. `anchor` stays put while `head` (the cursor) moves; equal means a bare cursor. */\nexport interface CodeRange {\n anchor: number\n head: number\n}\n\n/** The selection: one or more ranges, sorted and apart, and which one is the main one. */\nexport interface CodeSelection {\n ranges: CodeRange[]\n main: number\n}\n\nexport const rangeFrom = (range: CodeRange) => Math.min(range.anchor, range.head)\nexport const rangeTo = (range: CodeRange) => Math.max(range.anchor, range.head)\nexport const isEmptyRange = (range: CodeRange) => range.anchor === range.head\n\n/** A selection of one range; just a cursor when `head` is left out. */\nexport function selectionOf(anchor: number, head = anchor): CodeSelection {\n return { ranges: [{ anchor, head }], main: 0 }\n}\n\n/** The main range. */\nexport const mainRange = (selection: CodeSelection) =>\n selection.ranges[selection.main] ?? selection.ranges[0]!\n\n/**\n * Clamps every range into a text of `length`, sorts them and joins the ones\n * that overlap (or are cursors at the same spot). The main range keeps its\n * place in whichever range it ended up in.\n */\nexport function normalizeSelection(selection: CodeSelection, length: number): CodeSelection {\n const clamp = (pos: number) => Math.max(0, Math.min(length, Math.round(pos) || 0))\n const main = mainRange(selection)\n const ranges = selection.ranges.map((range) => ({\n anchor: clamp(range.anchor),\n head: clamp(range.head),\n main: range === main,\n }))\n if (!ranges.length) return selectionOf(0)\n ranges.sort((a, b) => Math.min(a.anchor, a.head) - Math.min(b.anchor, b.head))\n const out: (CodeRange & { main: boolean })[] = []\n for (const range of ranges) {\n const last = out[out.length - 1]\n const from = rangeFrom(range)\n if (last) {\n const lastTo = rangeTo(last)\n const overlaps =\n from < lastTo || (from === lastTo && (isEmptyRange(range) || isEmptyRange(last)))\n if (overlaps) {\n const start = Math.min(rangeFrom(last), from)\n const end = Math.max(lastTo, rangeTo(range))\n // Keep the direction of the range that was reaching outward.\n const backward = (range.main ? range : last).head < (range.main ? range : last).anchor\n last.anchor = backward ? end : start\n last.head = backward ? start : end\n last.main ||= range.main\n continue\n }\n }\n out.push(range)\n }\n const index = Math.max(\n 0,\n out.findIndex((range) => range.main),\n )\n return { ranges: out.map(({ anchor, head }) => ({ anchor, head })), main: index }\n}\n\n/** Moves every range through `changes`. Cursors stay after text typed at them. */\nexport function mapSelection(selection: CodeSelection, changes: ChangeSet): CodeSelection {\n if (changes.empty) return selection\n return {\n ranges: selection.ranges.map((range) => {\n if (isEmptyRange(range)) {\n const pos = changes.mapPos(range.head, 1)\n return { anchor: pos, head: pos }\n }\n // A range grows to take in text typed at its edges only from the inside.\n const forward = range.head >= range.anchor\n return {\n anchor: changes.mapPos(range.anchor, forward ? 1 : -1),\n head: changes.mapPos(range.head, forward ? -1 : 1),\n }\n }),\n main: selection.main,\n }\n}\n\n/** Whether two selections are the same. */\nexport function sameSelection(a: CodeSelection, b: CodeSelection): boolean {\n if (a === b) return true\n if (a.main !== b.main || a.ranges.length !== b.ranges.length) return false\n return a.ranges.every(\n (range, i) => range.anchor === b.ranges[i]!.anchor && range.head === b.ranges[i]!.head,\n )\n}\n"],"mappings":";AAcA,IAAa,aAAa,UAAqB,KAAK,IAAI,MAAM,QAAQ,MAAM,IAAI;AAChF,IAAa,WAAW,UAAqB,KAAK,IAAI,MAAM,QAAQ,MAAM,IAAI;AAC9E,IAAa,gBAAgB,UAAqB,MAAM,WAAW,MAAM;;AAGzE,SAAgB,YAAY,QAAgB,OAAO,QAAuB;CACxE,OAAO;EAAE,QAAQ,CAAC;GAAE;GAAQ;EAAK,CAAC;EAAG,MAAM;CAAE;AAC/C;;AAGA,IAAa,aAAa,cACxB,UAAU,OAAO,UAAU,SAAS,UAAU,OAAO;;;;;;AAOvD,SAAgB,mBAAmB,WAA0B,QAA+B;CAC1F,MAAM,SAAS,QAAgB,KAAK,IAAI,GAAG,KAAK,IAAI,QAAQ,KAAK,MAAM,GAAG,KAAK,CAAC,CAAC;CACjF,MAAM,OAAO,UAAU,SAAS;CAChC,MAAM,SAAS,UAAU,OAAO,KAAK,WAAW;EAC9C,QAAQ,MAAM,MAAM,MAAM;EAC1B,MAAM,MAAM,MAAM,IAAI;EACtB,MAAM,UAA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