@stacksjs/components 0.2.853 → 0.2.855
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/dist/CodeEditor-jg4w7e15.stx +497 -0
- package/dist/code-editor/editor.js +2166 -0
- package/dist/components/CodeBlock.d.ts +12 -5
- package/dist/index.d.ts +2 -0
- package/dist/index.js +29 -12
- package/dist/ui/code-editor/CodeEditor.d.ts +46 -0
- package/dist/ui/code-editor/controller.d.ts +103 -0
- package/dist/ui/code-editor/core/commands.d.ts +55 -0
- package/dist/ui/code-editor/core/completion.d.ts +40 -0
- package/dist/ui/code-editor/core/editor.d.ts +69 -0
- package/dist/ui/code-editor/core/highlight.d.ts +61 -0
- package/dist/ui/code-editor/core/history.d.ts +27 -0
- package/dist/ui/code-editor/core/index.d.ts +13 -0
- package/dist/ui/code-editor/core/keymap.d.ts +38 -0
- package/dist/ui/code-editor/core/selection.d.ts +30 -0
- package/dist/ui/code-editor/core/text.d.ts +64 -0
- package/dist/ui/code-editor/editor.d.ts +15 -0
- package/dist/ui/code-editor/index.d.ts +29 -0
- package/dist/ui/code-editor/tokenizer.d.ts +18 -0
- package/dist/ui/code-editor/view.d.ts +59 -0
- package/dist/utils/highlighter.d.ts +19 -1
- package/dist/utils/highlighter.js +24 -7
- package/dist/utils/syntax-theme.d.ts +44 -0
- package/package.json +9 -4
- package/src/components/CodeBlock.stx +195 -17
- package/src/index.ts +2 -0
- package/src/ui/code-editor/CodeEditor.stx +497 -0
- package/src/ui/code-editor/README.md +134 -0
- package/src/ui/code-editor/controller.ts +517 -0
- package/src/ui/code-editor/core/commands.ts +381 -0
- package/src/ui/code-editor/core/completion.ts +212 -0
- package/src/ui/code-editor/core/editor.ts +319 -0
- package/src/ui/code-editor/core/highlight.ts +203 -0
- package/src/ui/code-editor/core/history.ts +190 -0
- package/src/ui/code-editor/core/index.ts +14 -0
- package/src/ui/code-editor/core/keymap.ts +125 -0
- package/src/ui/code-editor/core/selection.ts +104 -0
- package/src/ui/code-editor/core/text.ts +252 -0
- package/src/ui/code-editor/editor.ts +16 -0
- package/src/ui/code-editor/index.ts +46 -0
- package/src/ui/code-editor/tokenizer.ts +74 -0
- package/src/ui/code-editor/view.ts +1066 -0
- package/src/utils/highlighter.ts +121 -218
- package/src/utils/syntax-theme.ts +144 -0
|
@@ -0,0 +1,381 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* What the keys do.
|
|
3
|
+
*
|
|
4
|
+
* Each command reads the editor and dispatches at most one transaction, so
|
|
5
|
+
* each is one undo step. They answer false when they have nothing to do, which
|
|
6
|
+
* lets the key fall through to the text field: Backspace with nothing special
|
|
7
|
+
* to delete is just Backspace, and the field already knows how to do that
|
|
8
|
+
* across grapheme clusters and input methods.
|
|
9
|
+
*/
|
|
10
|
+
import type { Command, EditorCore } from './editor'
|
|
11
|
+
import type { SelectionRange } from './selection'
|
|
12
|
+
import type { ChangeSpec } from './text'
|
|
13
|
+
import { mapOffset, rangeEmpty, rangeFrom, rangeTo } from './selection'
|
|
14
|
+
import { indentString, indentWidth, leadingWhitespace } from './text'
|
|
15
|
+
|
|
16
|
+
const WORD = /[\p{L}\p{N}_$]/u
|
|
17
|
+
|
|
18
|
+
function isWordCharacter(character: string): boolean {
|
|
19
|
+
return character !== '' && WORD.test(character)
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/** First and last line the main range touches. */
|
|
23
|
+
function selectedLines(editor: EditorCore): { first: number, last: number } {
|
|
24
|
+
const from = rangeFrom(editor.main)
|
|
25
|
+
const to = rangeTo(editor.main)
|
|
26
|
+
const first = editor.doc.lineIndexAt(from)
|
|
27
|
+
let last = editor.doc.lineIndexAt(to)
|
|
28
|
+
|
|
29
|
+
// A selection that ends at the very start of a line has not selected any of
|
|
30
|
+
// that line, and indenting or commenting it would surprise.
|
|
31
|
+
if (last > first && editor.doc.lineInfo(last).from === to)
|
|
32
|
+
last--
|
|
33
|
+
|
|
34
|
+
return { first, last }
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* Where the selection goes when whole lines are changed under it.
|
|
39
|
+
*
|
|
40
|
+
* The end that sits at the very start of a line stays there. Left to the
|
|
41
|
+
* default, indenting would push it in with the text, and a selection that
|
|
42
|
+
* covered three whole lines would cover two and most of a third.
|
|
43
|
+
*/
|
|
44
|
+
function linewiseSelection(editor: EditorCore, changes: readonly ChangeSpec[]): SelectionRange {
|
|
45
|
+
const { anchor, head } = editor.main
|
|
46
|
+
if (anchor === head)
|
|
47
|
+
return { anchor: mapOffset(anchor, changes, 1), head: mapOffset(head, changes, 1) }
|
|
48
|
+
|
|
49
|
+
const start = Math.min(anchor, head)
|
|
50
|
+
const atLineStart = editor.doc.lineAt(start).from === start
|
|
51
|
+
const map = (offset: number): number => mapOffset(offset, changes, offset === start && atLineStart ? -1 : 1)
|
|
52
|
+
|
|
53
|
+
return { anchor: map(anchor), head: map(head) }
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* Enter: a new line that starts where the old one did.
|
|
58
|
+
*
|
|
59
|
+
* One level deeper after an opening bracket. Between a pair, the closer goes
|
|
60
|
+
* to a line of its own and the caret is left on the indented line between.
|
|
61
|
+
*/
|
|
62
|
+
export const insertNewlineAndIndent: Command = (editor) => {
|
|
63
|
+
if (editor.readOnly)
|
|
64
|
+
return false
|
|
65
|
+
|
|
66
|
+
const from = rangeFrom(editor.main)
|
|
67
|
+
const to = rangeTo(editor.main)
|
|
68
|
+
const line = editor.doc.lineAt(from)
|
|
69
|
+
const before = line.text.slice(0, from - line.from)
|
|
70
|
+
const after = editor.doc.slice(to, editor.doc.lineAt(to).to)
|
|
71
|
+
const base = leadingWhitespace(before)
|
|
72
|
+
const trimmed = before.trimEnd()
|
|
73
|
+
const opener = trimmed.charAt(trimmed.length - 1)
|
|
74
|
+
const closer = editor.language.brackets[opener]
|
|
75
|
+
|
|
76
|
+
if (closer === undefined) {
|
|
77
|
+
const insert = `\n${base}`
|
|
78
|
+
editor.dispatch({
|
|
79
|
+
changes: [{ from, to, insert }],
|
|
80
|
+
selection: { anchor: from + insert.length, head: from + insert.length },
|
|
81
|
+
kind: 'newline',
|
|
82
|
+
})
|
|
83
|
+
|
|
84
|
+
return true
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
const deeper = indentString(indentWidth(base, editor.tabSize) + editor.tabSize, editor.tabSize, editor.insertSpaces)
|
|
88
|
+
const head = `\n${deeper}`
|
|
89
|
+
const between = after.trimStart().startsWith(closer)
|
|
90
|
+
// Whitespace between the caret and the closer would be stranded on the
|
|
91
|
+
// indented line, so it goes with the range being replaced.
|
|
92
|
+
const end = between ? to + (after.length - after.trimStart().length) : to
|
|
93
|
+
const insert = between ? `${head}\n${base}` : head
|
|
94
|
+
|
|
95
|
+
editor.dispatch({
|
|
96
|
+
changes: [{ from, to: end, insert }],
|
|
97
|
+
selection: { anchor: from + head.length, head: from + head.length },
|
|
98
|
+
kind: 'newline',
|
|
99
|
+
})
|
|
100
|
+
|
|
101
|
+
return true
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/** Tab: indent the selected lines, or move the caret to the next tab stop. */
|
|
105
|
+
export const indentMore: Command = (editor) => {
|
|
106
|
+
if (editor.readOnly)
|
|
107
|
+
return false
|
|
108
|
+
|
|
109
|
+
const from = rangeFrom(editor.main)
|
|
110
|
+
const to = rangeTo(editor.main)
|
|
111
|
+
const { first, last } = selectedLines(editor)
|
|
112
|
+
|
|
113
|
+
if (first === last && editor.doc.lineIndexAt(to) === first) {
|
|
114
|
+
const column = from - editor.doc.lineInfo(first).from
|
|
115
|
+
const width = indentWidth(editor.doc.line(first).slice(0, column), editor.tabSize)
|
|
116
|
+
const insert = editor.insertSpaces ? ' '.repeat(editor.tabSize - (width % editor.tabSize)) : '\t'
|
|
117
|
+
|
|
118
|
+
editor.dispatch({
|
|
119
|
+
changes: [{ from, to, insert }],
|
|
120
|
+
selection: { anchor: from + insert.length, head: from + insert.length },
|
|
121
|
+
kind: 'indent',
|
|
122
|
+
})
|
|
123
|
+
|
|
124
|
+
return true
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
const changes: ChangeSpec[] = []
|
|
128
|
+
for (let index = first; index <= last; index++) {
|
|
129
|
+
const info = editor.doc.lineInfo(index)
|
|
130
|
+
// An empty line stays empty: indenting it only leaves trailing whitespace.
|
|
131
|
+
if (info.text.length > 0)
|
|
132
|
+
changes.push({ from: info.from, to: info.from, insert: editor.indentUnit })
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
if (changes.length === 0)
|
|
136
|
+
return true
|
|
137
|
+
|
|
138
|
+
editor.dispatch({ changes, selection: linewiseSelection(editor, changes), kind: 'indent' })
|
|
139
|
+
|
|
140
|
+
return true
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
/** Shift-Tab: take one level of indentation off every selected line. */
|
|
144
|
+
export const indentLess: Command = (editor) => {
|
|
145
|
+
if (editor.readOnly)
|
|
146
|
+
return false
|
|
147
|
+
|
|
148
|
+
const { first, last } = selectedLines(editor)
|
|
149
|
+
const changes: ChangeSpec[] = []
|
|
150
|
+
|
|
151
|
+
for (let index = first; index <= last; index++) {
|
|
152
|
+
const info = editor.doc.lineInfo(index)
|
|
153
|
+
const whitespace = leadingWhitespace(info.text)
|
|
154
|
+
if (whitespace === '')
|
|
155
|
+
continue
|
|
156
|
+
|
|
157
|
+
// Back to the previous tab stop, however the indentation is written.
|
|
158
|
+
const width = indentWidth(whitespace, editor.tabSize)
|
|
159
|
+
const target = Math.max(0, width - (width % editor.tabSize || editor.tabSize))
|
|
160
|
+
let cut = whitespace.length
|
|
161
|
+
while (cut > 0 && indentWidth(whitespace.slice(0, cut - 1), editor.tabSize) >= target)
|
|
162
|
+
cut--
|
|
163
|
+
|
|
164
|
+
changes.push({ from: info.from + cut, to: info.from + whitespace.length, insert: '' })
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
if (changes.length === 0)
|
|
168
|
+
return true
|
|
169
|
+
|
|
170
|
+
editor.dispatch({ changes, selection: linewiseSelection(editor, changes), kind: 'indent' })
|
|
171
|
+
|
|
172
|
+
return true
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
/**
|
|
176
|
+
* Comment the selected lines, or uncomment them if every one already is.
|
|
177
|
+
*
|
|
178
|
+
* The markers line up at the shallowest indentation among the lines rather
|
|
179
|
+
* than at column zero or at each line's own, so a commented block still reads
|
|
180
|
+
* as a block and uncommenting gives back exactly what was there.
|
|
181
|
+
*/
|
|
182
|
+
export const toggleLineComment: Command = (editor) => {
|
|
183
|
+
const token = editor.language.lineComment
|
|
184
|
+
if (editor.readOnly || token === '')
|
|
185
|
+
return false
|
|
186
|
+
|
|
187
|
+
const { first, last } = selectedLines(editor)
|
|
188
|
+
const lines = []
|
|
189
|
+
for (let index = first; index <= last; index++)
|
|
190
|
+
lines.push(editor.doc.lineInfo(index))
|
|
191
|
+
|
|
192
|
+
const content = lines.filter(line => line.text.trim() !== '')
|
|
193
|
+
if (content.length === 0)
|
|
194
|
+
return true
|
|
195
|
+
|
|
196
|
+
const commented = content.every(line => line.text.trimStart().startsWith(token))
|
|
197
|
+
const changes: ChangeSpec[] = []
|
|
198
|
+
|
|
199
|
+
if (commented) {
|
|
200
|
+
for (const line of content) {
|
|
201
|
+
const start = line.from + leadingWhitespace(line.text).length
|
|
202
|
+
const spaced = line.text.charAt(start - line.from + token.length) === ' '
|
|
203
|
+
changes.push({ from: start, to: start + token.length + (spaced ? 1 : 0), insert: '' })
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
else {
|
|
207
|
+
const column = Math.min(...content.map(line => leadingWhitespace(line.text).length))
|
|
208
|
+
for (const line of content)
|
|
209
|
+
changes.push({ from: line.from + column, to: line.from + column, insert: `${token} ` })
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
editor.dispatch({ changes, selection: linewiseSelection(editor, changes), kind: 'comment' })
|
|
213
|
+
|
|
214
|
+
return true
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
/**
|
|
218
|
+
* Backspace, where it means more than one character.
|
|
219
|
+
*
|
|
220
|
+
* Between an empty pair it takes both halves, since the closer was never
|
|
221
|
+
* typed. In leading spaces it goes back a whole tab stop. Anything else is
|
|
222
|
+
* left to the text field.
|
|
223
|
+
*/
|
|
224
|
+
export const deleteBackward: Command = (editor) => {
|
|
225
|
+
if (editor.readOnly || !rangeEmpty(editor.main))
|
|
226
|
+
return false
|
|
227
|
+
|
|
228
|
+
const head = editor.main.head
|
|
229
|
+
if (head === 0)
|
|
230
|
+
return false
|
|
231
|
+
|
|
232
|
+
const line = editor.doc.lineAt(head)
|
|
233
|
+
const column = head - line.from
|
|
234
|
+
const previous = line.text.charAt(column - 1)
|
|
235
|
+
const next = line.text.charAt(column)
|
|
236
|
+
|
|
237
|
+
const pairs = editor.language.brackets[previous] === next && next !== ''
|
|
238
|
+
const quotes = previous === next && editor.language.quotes.includes(previous)
|
|
239
|
+
if (pairs || quotes) {
|
|
240
|
+
editor.dispatch({
|
|
241
|
+
changes: [{ from: head - 1, to: head + 1, insert: '' }],
|
|
242
|
+
selection: { anchor: head - 1, head: head - 1 },
|
|
243
|
+
kind: 'delete-pair',
|
|
244
|
+
})
|
|
245
|
+
|
|
246
|
+
return true
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
const before = line.text.slice(0, column)
|
|
250
|
+
if (editor.insertSpaces && column > 1 && /^ +$/.test(before)) {
|
|
251
|
+
const remove = column % editor.tabSize || editor.tabSize
|
|
252
|
+
if (remove > 1) {
|
|
253
|
+
editor.dispatch({
|
|
254
|
+
changes: [{ from: head - remove, to: head, insert: '' }],
|
|
255
|
+
selection: { anchor: head - remove, head: head - remove },
|
|
256
|
+
kind: 'delete-indent',
|
|
257
|
+
})
|
|
258
|
+
|
|
259
|
+
return true
|
|
260
|
+
}
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
return false
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
/**
|
|
267
|
+
* A typed character, where the editor has an opinion about it.
|
|
268
|
+
*
|
|
269
|
+
* Opening a bracket closes it. Typing a closer that is already there steps
|
|
270
|
+
* over it instead of doubling it. A closing brace on a line of nothing but
|
|
271
|
+
* indentation drops back a level. With text selected, an opener wraps it.
|
|
272
|
+
*
|
|
273
|
+
* Returns false for everything else, and the text field inserts the character
|
|
274
|
+
* itself - which is what keeps input methods and dead keys working, since the
|
|
275
|
+
* editor never sees a composition as individual keys.
|
|
276
|
+
*/
|
|
277
|
+
export function handleTextInput(editor: EditorCore, text: string): boolean {
|
|
278
|
+
if (editor.readOnly || text.length !== 1)
|
|
279
|
+
return false
|
|
280
|
+
|
|
281
|
+
const from = rangeFrom(editor.main)
|
|
282
|
+
const to = rangeTo(editor.main)
|
|
283
|
+
const line = editor.doc.lineAt(from)
|
|
284
|
+
const column = from - line.from
|
|
285
|
+
const previous = line.text.charAt(column - 1)
|
|
286
|
+
const next = editor.doc.slice(to, to + 1)
|
|
287
|
+
const { brackets, quotes } = editor.language
|
|
288
|
+
const closers = Object.values(brackets)
|
|
289
|
+
const isQuote = quotes.includes(text)
|
|
290
|
+
const closer = brackets[text]
|
|
291
|
+
|
|
292
|
+
if (from !== to) {
|
|
293
|
+
const close = closer ?? (isQuote ? text : undefined)
|
|
294
|
+
if (close === undefined)
|
|
295
|
+
return false
|
|
296
|
+
|
|
297
|
+
// Wrap, and keep what was selected selected.
|
|
298
|
+
const selected = editor.doc.slice(from, to)
|
|
299
|
+
editor.dispatch({
|
|
300
|
+
changes: [{ from, to, insert: `${text}${selected}${close}` }],
|
|
301
|
+
selection: { anchor: from + 1, head: to + 1 },
|
|
302
|
+
kind: 'wrap',
|
|
303
|
+
})
|
|
304
|
+
|
|
305
|
+
return true
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
// Step over a closer the editor (or anyone) already put there.
|
|
309
|
+
if ((closers.includes(text) || isQuote) && next === text) {
|
|
310
|
+
if (!isQuote || previous !== '\\') {
|
|
311
|
+
editor.dispatch({ selection: { anchor: from + 1, head: from + 1 }, kind: 'type' })
|
|
312
|
+
|
|
313
|
+
return true
|
|
314
|
+
}
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
if (closer !== undefined) {
|
|
318
|
+
// Only where a closer cannot land in front of something it would capture.
|
|
319
|
+
if (next !== '' && next !== '\n' && !/\s/.test(next) && !closers.includes(next) && next !== ',' && next !== ';')
|
|
320
|
+
return false
|
|
321
|
+
|
|
322
|
+
editor.dispatch({
|
|
323
|
+
changes: [{ from, to, insert: `${text}${closer}` }],
|
|
324
|
+
selection: { anchor: from + 1, head: from + 1 },
|
|
325
|
+
kind: 'pair',
|
|
326
|
+
})
|
|
327
|
+
|
|
328
|
+
return true
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
if (isQuote) {
|
|
332
|
+
// An apostrophe inside a word, an escaped quote, or a quote typed against
|
|
333
|
+
// the start of a word is a quote on its own, not half of a pair.
|
|
334
|
+
if (isWordCharacter(previous) || previous === '\\' || previous === text || isWordCharacter(next))
|
|
335
|
+
return false
|
|
336
|
+
|
|
337
|
+
editor.dispatch({
|
|
338
|
+
changes: [{ from, to, insert: `${text}${text}` }],
|
|
339
|
+
selection: { anchor: from + 1, head: from + 1 },
|
|
340
|
+
kind: 'pair',
|
|
341
|
+
})
|
|
342
|
+
|
|
343
|
+
return true
|
|
344
|
+
}
|
|
345
|
+
|
|
346
|
+
if (closers.includes(text) && column > 0 && line.text.slice(0, column).trim() === '') {
|
|
347
|
+
// A closer on a line of only indentation belongs one level out.
|
|
348
|
+
const width = indentWidth(line.text.slice(0, column), editor.tabSize)
|
|
349
|
+
const outdented = indentString(Math.max(0, width - editor.tabSize), editor.tabSize, editor.insertSpaces)
|
|
350
|
+
const insert = `${outdented}${text}`
|
|
351
|
+
|
|
352
|
+
editor.dispatch({
|
|
353
|
+
changes: [{ from: line.from, to: from, insert }],
|
|
354
|
+
selection: { anchor: line.from + insert.length, head: line.from + insert.length },
|
|
355
|
+
kind: 'outdent',
|
|
356
|
+
})
|
|
357
|
+
|
|
358
|
+
return true
|
|
359
|
+
}
|
|
360
|
+
|
|
361
|
+
return false
|
|
362
|
+
}
|
|
363
|
+
|
|
364
|
+
export const undo: Command = editor => editor.undo()
|
|
365
|
+
export const redo: Command = editor => editor.redo()
|
|
366
|
+
|
|
367
|
+
/** The commands every editor starts with, by the name a keymap refers to. */
|
|
368
|
+
export const defaultCommands: Readonly<Record<string, Command>> = {
|
|
369
|
+
insertNewlineAndIndent,
|
|
370
|
+
indentMore,
|
|
371
|
+
indentLess,
|
|
372
|
+
toggleLineComment,
|
|
373
|
+
deleteBackward,
|
|
374
|
+
undo,
|
|
375
|
+
redo,
|
|
376
|
+
}
|
|
377
|
+
|
|
378
|
+
export function registerDefaultCommands(editor: EditorCore): void {
|
|
379
|
+
for (const [name, command] of Object.entries(defaultCommands))
|
|
380
|
+
editor.commands.set(name, command)
|
|
381
|
+
}
|
|
@@ -0,0 +1,212 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The completion list, as state.
|
|
3
|
+
*
|
|
4
|
+
* What is open, what is left after filtering by what has been typed, which
|
|
5
|
+
* item is selected, and the edit that accepting it makes. Where the list is
|
|
6
|
+
* drawn and what asked for it are the view's business.
|
|
7
|
+
*
|
|
8
|
+
* The host supplies the items - from a language server, from a table of names,
|
|
9
|
+
* from anywhere - along with the offset the completed word starts at. From
|
|
10
|
+
* then on, typing narrows the list here without another round trip, which is
|
|
11
|
+
* what keeps it from flickering on every key.
|
|
12
|
+
*/
|
|
13
|
+
import type { ChangeSpec } from './text'
|
|
14
|
+
|
|
15
|
+
export interface CompletionItem {
|
|
16
|
+
/** What the list shows, and what is matched against. */
|
|
17
|
+
label: string
|
|
18
|
+
/** What is inserted. Defaults to the label. */
|
|
19
|
+
insert?: string
|
|
20
|
+
/** A short note beside the label, such as a type. */
|
|
21
|
+
detail?: string
|
|
22
|
+
/** What sort of thing it is (`function`, `property`), for an icon or a tint. */
|
|
23
|
+
kind?: string
|
|
24
|
+
/** Matched instead of the label when set. */
|
|
25
|
+
filterText?: string
|
|
26
|
+
/** Raises or lowers an item among equal matches. */
|
|
27
|
+
boost?: number
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export interface RankedCompletion {
|
|
31
|
+
item: CompletionItem
|
|
32
|
+
score: number
|
|
33
|
+
/** Indices into the label of the characters that matched, for emphasis. */
|
|
34
|
+
matches: number[]
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
const WORD_CHARACTER = /[\p{L}\p{N}_$]/u
|
|
38
|
+
|
|
39
|
+
/** Where the word ending at `offset` begins. */
|
|
40
|
+
export function wordStart(text: string, offset: number): number {
|
|
41
|
+
let start = offset
|
|
42
|
+
while (start > 0 && WORD_CHARACTER.test(text.charAt(start - 1)))
|
|
43
|
+
start--
|
|
44
|
+
|
|
45
|
+
return start
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* How well `query` matches `text`, or null when it does not.
|
|
50
|
+
*
|
|
51
|
+
* Every character of the query has to appear in order. A match that starts at
|
|
52
|
+
* the beginning beats one that does not, a run of adjacent characters beats
|
|
53
|
+
* the same characters scattered, a match at the start of a word (`gU` for
|
|
54
|
+
* `getUser`) beats one in the middle of it, and an exact case match breaks ties.
|
|
55
|
+
*/
|
|
56
|
+
export function matchScore(query: string, text: string): { score: number, matches: number[] } | null {
|
|
57
|
+
if (query === '')
|
|
58
|
+
return { score: 0, matches: [] }
|
|
59
|
+
|
|
60
|
+
const needle = query.toLowerCase()
|
|
61
|
+
const haystack = text.toLowerCase()
|
|
62
|
+
const matches: number[] = []
|
|
63
|
+
let score = 0
|
|
64
|
+
let position = 0
|
|
65
|
+
let previous = -2
|
|
66
|
+
|
|
67
|
+
for (let index = 0; index < needle.length; index++) {
|
|
68
|
+
const found = haystack.indexOf(needle.charAt(index), position)
|
|
69
|
+
if (found === -1)
|
|
70
|
+
return null
|
|
71
|
+
|
|
72
|
+
matches.push(found)
|
|
73
|
+
score += 10
|
|
74
|
+
|
|
75
|
+
if (found === previous + 1)
|
|
76
|
+
score += 15
|
|
77
|
+
if (found === 0)
|
|
78
|
+
score += 30
|
|
79
|
+
else if (!WORD_CHARACTER.test(text.charAt(found - 1)) || isHump(text, found))
|
|
80
|
+
score += 12
|
|
81
|
+
if (text.charAt(found) === query.charAt(index))
|
|
82
|
+
score += 2
|
|
83
|
+
|
|
84
|
+
previous = found
|
|
85
|
+
position = found + 1
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
if (haystack.startsWith(needle))
|
|
89
|
+
score += 40
|
|
90
|
+
if (haystack === needle)
|
|
91
|
+
score += 20
|
|
92
|
+
|
|
93
|
+
// Shorter is closer: `get` should offer `get` before `getUserByEmail`.
|
|
94
|
+
score -= Math.min(20, text.length - needle.length) * 0.5
|
|
95
|
+
|
|
96
|
+
return { score, matches }
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
function isHump(text: string, index: number): boolean {
|
|
100
|
+
const character = text.charAt(index)
|
|
101
|
+
const before = text.charAt(index - 1)
|
|
102
|
+
|
|
103
|
+
return character !== character.toLowerCase() && before === before.toLowerCase()
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/** The items a query leaves, best first. Order among equals is the host's. */
|
|
107
|
+
export function rankCompletions(items: readonly CompletionItem[], query: string): RankedCompletion[] {
|
|
108
|
+
const ranked: Array<RankedCompletion & { order: number }> = []
|
|
109
|
+
|
|
110
|
+
items.forEach((item, order) => {
|
|
111
|
+
const match = matchScore(query, item.filterText ?? item.label)
|
|
112
|
+
if (match)
|
|
113
|
+
ranked.push({ item, score: match.score + (item.boost ?? 0), matches: item.filterText ? [] : match.matches, order })
|
|
114
|
+
})
|
|
115
|
+
|
|
116
|
+
ranked.sort((a, b) => b.score - a.score || a.order - b.order)
|
|
117
|
+
|
|
118
|
+
return ranked.map(({ item, score, matches }) => ({ item, score, matches }))
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
export class CompletionList {
|
|
122
|
+
private items: readonly CompletionItem[] = []
|
|
123
|
+
private ranked: RankedCompletion[] = []
|
|
124
|
+
private index = 0
|
|
125
|
+
private start = 0
|
|
126
|
+
private opened = false
|
|
127
|
+
|
|
128
|
+
get isOpen(): boolean {
|
|
129
|
+
return this.opened && this.ranked.length > 0
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
/** What is left after filtering, best first. */
|
|
133
|
+
get visible(): readonly RankedCompletion[] {
|
|
134
|
+
return this.opened ? this.ranked : []
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
get selectedIndex(): number {
|
|
138
|
+
return this.index
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
get selected(): CompletionItem | undefined {
|
|
142
|
+
return this.visible[this.index]?.item
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
/** Offset the completed word starts at. */
|
|
146
|
+
get from(): number {
|
|
147
|
+
return this.start
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
/**
|
|
151
|
+
* Open with a set of items for the word starting at `from`.
|
|
152
|
+
*
|
|
153
|
+
* `query` is what has been typed of that word so far.
|
|
154
|
+
*/
|
|
155
|
+
open(items: readonly CompletionItem[], from: number, query: string): void {
|
|
156
|
+
this.items = items
|
|
157
|
+
this.start = from
|
|
158
|
+
this.opened = true
|
|
159
|
+
this.filter(query)
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
/** Narrow to what still matches. Closes when nothing does. */
|
|
163
|
+
filter(query: string): void {
|
|
164
|
+
const previous = this.selected
|
|
165
|
+
this.ranked = rankCompletions(this.items, query)
|
|
166
|
+
|
|
167
|
+
// The best match takes the selection, unless the reader had moved off it
|
|
168
|
+
// and what they chose is still there.
|
|
169
|
+
const kept = previous && this.index > 0 ? this.ranked.findIndex(entry => entry.item === previous) : -1
|
|
170
|
+
this.index = kept > 0 ? kept : 0
|
|
171
|
+
|
|
172
|
+
if (this.ranked.length === 0)
|
|
173
|
+
this.opened = false
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
/** Move the selection, wrapping at either end. */
|
|
177
|
+
move(delta: number): void {
|
|
178
|
+
const count = this.ranked.length
|
|
179
|
+
if (count === 0)
|
|
180
|
+
return
|
|
181
|
+
|
|
182
|
+
this.index = ((this.index + delta) % count + count) % count
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
select(index: number): void {
|
|
186
|
+
if (index >= 0 && index < this.ranked.length)
|
|
187
|
+
this.index = index
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
close(): void {
|
|
191
|
+
this.opened = false
|
|
192
|
+
this.ranked = []
|
|
193
|
+
this.items = []
|
|
194
|
+
this.index = 0
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
/**
|
|
198
|
+
* The edit that accepts the selected item, with the caret at `head`.
|
|
199
|
+
*
|
|
200
|
+
* Replaces the word from where it started up to the caret, so accepting
|
|
201
|
+
* `getUser` after typing `getU` does not leave `getUgetUser`.
|
|
202
|
+
*/
|
|
203
|
+
accept(head: number): { change: ChangeSpec, item: CompletionItem } | null {
|
|
204
|
+
const item = this.selected
|
|
205
|
+
if (!this.isOpen || !item)
|
|
206
|
+
return null
|
|
207
|
+
|
|
208
|
+
const insert = item.insert ?? item.label
|
|
209
|
+
|
|
210
|
+
return { change: { from: Math.min(this.start, head), to: head, insert }, item }
|
|
211
|
+
}
|
|
212
|
+
}
|