@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,319 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The editor: a document, a selection, a history, and the one way to change them.
|
|
3
|
+
*
|
|
4
|
+
* Nothing in here touches the DOM, and nothing in here knows about stx. That
|
|
5
|
+
* is what makes it testable without a browser, and it is also what lets the
|
|
6
|
+
* view be replaced: today the view is a textarea laid over a highlighted layer,
|
|
7
|
+
* and the model does not know. When the editor grows its own input surface, for
|
|
8
|
+
* several cursors at once, this file is the part that stays.
|
|
9
|
+
*
|
|
10
|
+
* Everything goes through `dispatch`. A keystroke the textarea handled, a
|
|
11
|
+
* command, a value set from outside, an undo: each is a transaction, and each
|
|
12
|
+
* tells every listener the same things in the same order.
|
|
13
|
+
*/
|
|
14
|
+
import type { EditorSelection, SelectionRange } from './selection'
|
|
15
|
+
import type { AppliedChange, ChangeSpec, Position } from './text'
|
|
16
|
+
import { History } from './history'
|
|
17
|
+
import { clampSelection, mainRange, mapSelection, rangeFrom, rangeTo, selectionsEqual, singleSelection } from './selection'
|
|
18
|
+
import { normalizeNewlines, TextDocument } from './text'
|
|
19
|
+
|
|
20
|
+
export interface LanguageConfig {
|
|
21
|
+
/** Starts a comment that runs to the end of the line. Empty for none. */
|
|
22
|
+
lineComment: string
|
|
23
|
+
/** Opening characters and what closes each. */
|
|
24
|
+
brackets: Record<string, string>
|
|
25
|
+
/** Characters that open and close with themselves. */
|
|
26
|
+
quotes: string[]
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export const DEFAULT_LANGUAGE: LanguageConfig = {
|
|
30
|
+
lineComment: '//',
|
|
31
|
+
brackets: { '(': ')', '[': ']', '{': '}' },
|
|
32
|
+
quotes: ['"', '\'', '`'],
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/** Per-language overrides, by the id the highlighter knows the language as. */
|
|
36
|
+
export const LANGUAGE_CONFIGS: Readonly<Record<string, Partial<LanguageConfig>>> = {
|
|
37
|
+
python: { lineComment: '#', quotes: ['"', '\''] },
|
|
38
|
+
ruby: { lineComment: '#' },
|
|
39
|
+
bash: { lineComment: '#' },
|
|
40
|
+
yaml: { lineComment: '#', quotes: ['"', '\''] },
|
|
41
|
+
toml: { lineComment: '#', quotes: ['"', '\''] },
|
|
42
|
+
dockerfile: { lineComment: '#', quotes: ['"', '\''] },
|
|
43
|
+
sql: { lineComment: '--', quotes: ['"', '\''] },
|
|
44
|
+
lua: { lineComment: '--', quotes: ['"', '\''] },
|
|
45
|
+
json: { lineComment: '', quotes: ['"'] },
|
|
46
|
+
html: { lineComment: '', quotes: ['"', '\''] },
|
|
47
|
+
css: { lineComment: '', quotes: ['"', '\''] },
|
|
48
|
+
markdown: { lineComment: '', brackets: { '(': ')', '[': ']' } },
|
|
49
|
+
text: { lineComment: '', brackets: {}, quotes: [] },
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
export function languageConfig(language: string | undefined): LanguageConfig {
|
|
53
|
+
return { ...DEFAULT_LANGUAGE, ...(language ? LANGUAGE_CONFIGS[language.toLowerCase()] : undefined) }
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
export interface EditorOptions {
|
|
57
|
+
value?: string
|
|
58
|
+
/** Columns a tab stop is apart. Default 2. */
|
|
59
|
+
tabSize?: number
|
|
60
|
+
/** Indent with spaces rather than tab characters. Default true. */
|
|
61
|
+
insertSpaces?: boolean
|
|
62
|
+
readOnly?: boolean
|
|
63
|
+
language?: Partial<LanguageConfig>
|
|
64
|
+
historyDepth?: number
|
|
65
|
+
/** Supplies the time for history grouping. A test passes its own. */
|
|
66
|
+
now?: () => number
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/** Where a transaction came from, which decides who still has to be told. */
|
|
70
|
+
export type TransactionOrigin = 'input' | 'command' | 'api' | 'history'
|
|
71
|
+
|
|
72
|
+
export interface TransactionSpec {
|
|
73
|
+
/** Sorted, not overlapping, in the coordinates of the document as it is now. */
|
|
74
|
+
changes?: ChangeSpec[]
|
|
75
|
+
/** The selection afterwards. Mapped through the changes when left out. */
|
|
76
|
+
selection?: EditorSelection | SelectionRange
|
|
77
|
+
/** `type` and `delete` join into one undo step; anything else stands alone. */
|
|
78
|
+
kind?: string
|
|
79
|
+
origin?: TransactionOrigin
|
|
80
|
+
addToHistory?: boolean
|
|
81
|
+
scrollIntoView?: boolean
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
export interface Transaction {
|
|
85
|
+
/** In before-the-change coordinates, ascending. Empty for a pure selection move. */
|
|
86
|
+
changes: AppliedChange[]
|
|
87
|
+
before: EditorSelection
|
|
88
|
+
selection: EditorSelection
|
|
89
|
+
docChanged: boolean
|
|
90
|
+
selectionChanged: boolean
|
|
91
|
+
kind: string
|
|
92
|
+
origin: TransactionOrigin
|
|
93
|
+
scrollIntoView: boolean
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
export type TransactionListener = (transaction: Transaction, editor: EditorCore) => void
|
|
97
|
+
|
|
98
|
+
/** A command answers whether it did anything, so a key can fall through. */
|
|
99
|
+
export type Command = (editor: EditorCore) => boolean
|
|
100
|
+
|
|
101
|
+
function isSelection(value: EditorSelection | SelectionRange): value is EditorSelection {
|
|
102
|
+
return Array.isArray((value as EditorSelection).ranges)
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
export class EditorCore {
|
|
106
|
+
readonly doc: TextDocument
|
|
107
|
+
readonly history: History
|
|
108
|
+
readonly commands: Map<string, Command> = new Map()
|
|
109
|
+
tabSize: number
|
|
110
|
+
insertSpaces: boolean
|
|
111
|
+
readOnly: boolean
|
|
112
|
+
language: LanguageConfig
|
|
113
|
+
private current: EditorSelection = singleSelection(0)
|
|
114
|
+
private listeners: Set<TransactionListener> = new Set()
|
|
115
|
+
private readonly now: () => number
|
|
116
|
+
|
|
117
|
+
constructor(options: EditorOptions = {}) {
|
|
118
|
+
this.doc = new TextDocument(options.value ?? '')
|
|
119
|
+
this.history = new History({ depth: options.historyDepth })
|
|
120
|
+
this.tabSize = Math.max(1, options.tabSize ?? 2)
|
|
121
|
+
this.insertSpaces = options.insertSpaces ?? true
|
|
122
|
+
this.readOnly = options.readOnly ?? false
|
|
123
|
+
this.language = { ...DEFAULT_LANGUAGE, ...options.language }
|
|
124
|
+
this.now = options.now ?? (() => Date.now())
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
get selection(): EditorSelection {
|
|
128
|
+
return this.current
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
/** The range the caret belongs to. */
|
|
132
|
+
get main(): SelectionRange {
|
|
133
|
+
return mainRange(this.current)
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
get value(): string {
|
|
137
|
+
return this.doc.toString()
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
/** One level of indentation, as this editor writes it. */
|
|
141
|
+
get indentUnit(): string {
|
|
142
|
+
return this.insertSpaces ? ' '.repeat(this.tabSize) : '\t'
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
/** The caret as a line and a column. */
|
|
146
|
+
get cursor(): Position {
|
|
147
|
+
return this.doc.positionAt(this.main.head)
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
/** The selected text of the main range. */
|
|
151
|
+
get selectedText(): string {
|
|
152
|
+
return this.doc.slice(rangeFrom(this.main), rangeTo(this.main))
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
/** Listen to every transaction. Returns the function that stops listening. */
|
|
156
|
+
subscribe(listener: TransactionListener): () => void {
|
|
157
|
+
this.listeners.add(listener)
|
|
158
|
+
|
|
159
|
+
return () => {
|
|
160
|
+
this.listeners.delete(listener)
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
dispatch(spec: TransactionSpec): Transaction {
|
|
165
|
+
const origin = spec.origin ?? 'command'
|
|
166
|
+
const kind = spec.kind ?? 'edit'
|
|
167
|
+
const before = this.current
|
|
168
|
+
const specs = this.readOnly && origin !== 'api' ? [] : (spec.changes ?? []).filter(change => change.from !== change.to || change.insert !== '')
|
|
169
|
+
|
|
170
|
+
// Last first, so the offsets of the earlier ones are still true when their
|
|
171
|
+
// turn comes. The record is kept in ascending order for everything else.
|
|
172
|
+
const applied: AppliedChange[] = Array.from({ length: specs.length })
|
|
173
|
+
for (let index = specs.length - 1; index >= 0; index--) {
|
|
174
|
+
const change = specs[index]!
|
|
175
|
+
applied[index] = this.doc.replace(change.from, change.to, change.insert)
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
let selection: EditorSelection
|
|
179
|
+
if (spec.selection)
|
|
180
|
+
selection = isSelection(spec.selection) ? spec.selection : { ranges: [spec.selection], main: 0 }
|
|
181
|
+
else
|
|
182
|
+
selection = applied.length > 0 ? mapSelection(before, applied) : before
|
|
183
|
+
|
|
184
|
+
selection = clampSelection(selection, this.doc.length)
|
|
185
|
+
this.current = selection
|
|
186
|
+
|
|
187
|
+
const docChanged = applied.length > 0
|
|
188
|
+
const selectionChanged = !selectionsEqual(before, selection)
|
|
189
|
+
|
|
190
|
+
if (docChanged && origin !== 'history' && spec.addToHistory !== false)
|
|
191
|
+
this.history.record(applied, before, selection, kind, this.now())
|
|
192
|
+
else if (!docChanged && selectionChanged)
|
|
193
|
+
this.history.breakGroup()
|
|
194
|
+
|
|
195
|
+
const transaction: Transaction = {
|
|
196
|
+
changes: applied,
|
|
197
|
+
before,
|
|
198
|
+
selection,
|
|
199
|
+
docChanged,
|
|
200
|
+
selectionChanged,
|
|
201
|
+
kind,
|
|
202
|
+
origin,
|
|
203
|
+
scrollIntoView: spec.scrollIntoView ?? (docChanged || selectionChanged),
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
if (docChanged || selectionChanged) {
|
|
207
|
+
for (const listener of this.listeners)
|
|
208
|
+
listener(transaction, this)
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
return transaction
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
/** Move the selection without changing the text. */
|
|
215
|
+
setSelection(anchor: number, head: number = anchor, origin: TransactionOrigin = 'api'): Transaction {
|
|
216
|
+
return this.dispatch({ selection: { anchor, head }, origin, kind: 'select' })
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
/**
|
|
220
|
+
* Replace the whole text from outside.
|
|
221
|
+
*
|
|
222
|
+
* The history is cleared: an undo that reached back across a value the host
|
|
223
|
+
* swapped in would restore text belonging to a different document.
|
|
224
|
+
*/
|
|
225
|
+
setValue(text: string, options: { keepHistory?: boolean } = {}): Transaction {
|
|
226
|
+
const next = normalizeNewlines(text)
|
|
227
|
+
if (next === this.doc.toString())
|
|
228
|
+
return this.dispatch({ origin: 'api' })
|
|
229
|
+
|
|
230
|
+
const head = Math.min(this.main.head, next.length)
|
|
231
|
+
const transaction = this.dispatch({
|
|
232
|
+
changes: [{ from: 0, to: this.doc.length, insert: next }],
|
|
233
|
+
selection: { anchor: head, head },
|
|
234
|
+
origin: 'api',
|
|
235
|
+
kind: 'set',
|
|
236
|
+
addToHistory: options.keepHistory === true,
|
|
237
|
+
scrollIntoView: false,
|
|
238
|
+
})
|
|
239
|
+
|
|
240
|
+
if (!options.keepHistory)
|
|
241
|
+
this.history.clear()
|
|
242
|
+
|
|
243
|
+
return transaction
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
/**
|
|
247
|
+
* Take in what a text field now holds.
|
|
248
|
+
*
|
|
249
|
+
* The textarea did the editing: it knows about input methods, dictation,
|
|
250
|
+
* spellcheck replacements, drag and drop and whatever the platform invents
|
|
251
|
+
* next, and the editor is told afterwards. What changed is found by trimming
|
|
252
|
+
* what the two texts share at each end, which names one replaced range. That
|
|
253
|
+
* is exactly what a text field does on any single input event.
|
|
254
|
+
*/
|
|
255
|
+
syncInput(value: string, selectionStart: number, selectionEnd: number, backward = false): Transaction {
|
|
256
|
+
const next = normalizeNewlines(value)
|
|
257
|
+
const previous = this.doc.toString()
|
|
258
|
+
const selection = backward
|
|
259
|
+
? { anchor: selectionEnd, head: selectionStart }
|
|
260
|
+
: { anchor: selectionStart, head: selectionEnd }
|
|
261
|
+
|
|
262
|
+
if (next === previous)
|
|
263
|
+
return this.dispatch({ selection, origin: 'input', kind: 'select' })
|
|
264
|
+
|
|
265
|
+
const limit = Math.min(previous.length, next.length)
|
|
266
|
+
let prefix = 0
|
|
267
|
+
while (prefix < limit && previous.charCodeAt(prefix) === next.charCodeAt(prefix))
|
|
268
|
+
prefix++
|
|
269
|
+
|
|
270
|
+
let suffix = 0
|
|
271
|
+
while (
|
|
272
|
+
suffix < limit - prefix
|
|
273
|
+
&& previous.charCodeAt(previous.length - 1 - suffix) === next.charCodeAt(next.length - 1 - suffix)
|
|
274
|
+
) {
|
|
275
|
+
suffix++
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
const insert = next.slice(prefix, next.length - suffix)
|
|
279
|
+
const removed = previous.length - suffix - prefix
|
|
280
|
+
const kind = removed === 0 ? 'type' : insert === '' ? 'delete' : 'replace'
|
|
281
|
+
|
|
282
|
+
const transaction = this.dispatch({
|
|
283
|
+
changes: [{ from: prefix, to: previous.length - suffix, insert }],
|
|
284
|
+
selection,
|
|
285
|
+
origin: 'input',
|
|
286
|
+
kind,
|
|
287
|
+
})
|
|
288
|
+
this.doc.adoptText(next)
|
|
289
|
+
|
|
290
|
+
return transaction
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
undo(): boolean {
|
|
294
|
+
const entry = this.history.popUndo()
|
|
295
|
+
if (!entry)
|
|
296
|
+
return false
|
|
297
|
+
|
|
298
|
+
this.dispatch({ changes: entry.inverse, selection: entry.before, origin: 'history', kind: 'undo' })
|
|
299
|
+
|
|
300
|
+
return true
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
redo(): boolean {
|
|
304
|
+
const entry = this.history.popRedo()
|
|
305
|
+
if (!entry)
|
|
306
|
+
return false
|
|
307
|
+
|
|
308
|
+
this.dispatch({ changes: entry.changes, selection: entry.after, origin: 'history', kind: 'redo' })
|
|
309
|
+
|
|
310
|
+
return true
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
/** Run a command by name. False when there is none, or it had nothing to do. */
|
|
314
|
+
exec(name: string): boolean {
|
|
315
|
+
const command = this.commands.get(name)
|
|
316
|
+
|
|
317
|
+
return command ? command(this) : false
|
|
318
|
+
}
|
|
319
|
+
}
|
|
@@ -0,0 +1,203 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Highlighting that only redoes what a change could have affected.
|
|
3
|
+
*
|
|
4
|
+
* A tokenizer reads a line given the state the previous line left it in:
|
|
5
|
+
* inside a block comment, inside a template string, or nowhere in particular.
|
|
6
|
+
* So each line is remembered with the state it started in, and after an edit the
|
|
7
|
+
* tokenizer starts at the first changed line and stops at the first unchanged
|
|
8
|
+
* line it would enter in the same state as last time. That line, and everything
|
|
9
|
+
* below it, would tokenize exactly as it already has.
|
|
10
|
+
*
|
|
11
|
+
* For an ordinary keystroke that is one line. For one that opens a block
|
|
12
|
+
* comment it is every line down to wherever the comment ends, which is the
|
|
13
|
+
* honest answer, and it is why the work is given a budget: a pass stops when
|
|
14
|
+
* the budget is spent and says where it got to, and the caller continues when
|
|
15
|
+
* it is idle. The lines on screen are done first either way.
|
|
16
|
+
*
|
|
17
|
+
* The tokenizer is passed in. This file does not import one, which keeps the
|
|
18
|
+
* editor core free of the highlighter and lets a test use a tokenizer small
|
|
19
|
+
* enough to reason about.
|
|
20
|
+
*/
|
|
21
|
+
|
|
22
|
+
export interface HighlightToken {
|
|
23
|
+
/** The class to colour it with. Empty for plain text. */
|
|
24
|
+
type: string
|
|
25
|
+
content: string
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/** A tokenizer, one line at a time, with its state made explicit. */
|
|
29
|
+
export interface LineTokenizer<State = unknown> {
|
|
30
|
+
initialState: () => State
|
|
31
|
+
tokenizeLine: (text: string, state: State) => { tokens: HighlightToken[], endState: State }
|
|
32
|
+
/** A string that is equal exactly when two states are. */
|
|
33
|
+
stateKey: (state: State) => string
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export interface HighlightedLine<State = unknown> {
|
|
37
|
+
text: string
|
|
38
|
+
tokens: HighlightToken[]
|
|
39
|
+
/** The state the line was entered in, as a key. What settling compares. */
|
|
40
|
+
startKey: string
|
|
41
|
+
endState: State
|
|
42
|
+
endKey: string
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export interface LineChange {
|
|
46
|
+
fromLine: number
|
|
47
|
+
removedLines: number
|
|
48
|
+
insertedLines: number
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
export interface HighlightPass {
|
|
52
|
+
/** First line retokenized in this pass, or -1 when there was nothing to do. */
|
|
53
|
+
from: number
|
|
54
|
+
/** One past the last line retokenized. */
|
|
55
|
+
to: number
|
|
56
|
+
/** Whether lines are still waiting, for a later pass. */
|
|
57
|
+
pending: boolean
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/** A source of line text: the document, or anything shaped like it. */
|
|
61
|
+
export interface LineSource {
|
|
62
|
+
readonly lineCount: number
|
|
63
|
+
line: (index: number) => string
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
export class LineHighlighter<State = unknown> {
|
|
67
|
+
/** One entry per document line. `null` is a line with no current tokens. */
|
|
68
|
+
private lines: Array<HighlightedLine<State> | null> = []
|
|
69
|
+
/** Whether anything is waiting to be tokenized. */
|
|
70
|
+
private stale = true
|
|
71
|
+
/** Every line before this one is current. */
|
|
72
|
+
private dirtyFrom = 0
|
|
73
|
+
/** Lines from `dirtyFrom` up to here have to be redone whatever state they are entered in. */
|
|
74
|
+
private dirtyTo = 0
|
|
75
|
+
private tokenizer: LineTokenizer<State>
|
|
76
|
+
private source: LineSource
|
|
77
|
+
|
|
78
|
+
constructor(tokenizer: LineTokenizer<State>, source: LineSource) {
|
|
79
|
+
this.tokenizer = tokenizer
|
|
80
|
+
this.source = source
|
|
81
|
+
this.reset()
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/** Forget everything, for a new tokenizer or a new document. */
|
|
85
|
+
reset(tokenizer?: LineTokenizer<State>, source?: LineSource): void {
|
|
86
|
+
if (tokenizer)
|
|
87
|
+
this.tokenizer = tokenizer
|
|
88
|
+
if (source)
|
|
89
|
+
this.source = source
|
|
90
|
+
|
|
91
|
+
this.lines = Array.from({ length: this.source.lineCount }, () => null)
|
|
92
|
+
this.stale = true
|
|
93
|
+
this.dirtyFrom = 0
|
|
94
|
+
this.dirtyTo = this.lines.length
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/** The tokens of a line, or null when it has not been reached yet. */
|
|
98
|
+
get(index: number): HighlightedLine<State> | null {
|
|
99
|
+
return this.lines[index] ?? null
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
get pending(): boolean {
|
|
103
|
+
return this.stale
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/** First line that may be stale, or the line count when none is. */
|
|
107
|
+
get firstDirtyLine(): number {
|
|
108
|
+
return this.stale ? this.dirtyFrom : this.lines.length
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
/**
|
|
112
|
+
* Record that lines were replaced. Tokenizes nothing.
|
|
113
|
+
*
|
|
114
|
+
* Changes arrive in the order they were applied, which for a transaction is
|
|
115
|
+
* last first, so each one's line numbers are true at the moment it is read.
|
|
116
|
+
*/
|
|
117
|
+
applyChange(change: LineChange): void {
|
|
118
|
+
const fresh = Array.from<HighlightedLine<State> | null>({ length: change.insertedLines }).fill(null)
|
|
119
|
+
this.lines.splice(change.fromLine, change.removedLines, ...fresh)
|
|
120
|
+
|
|
121
|
+
const end = change.fromLine + change.insertedLines
|
|
122
|
+
|
|
123
|
+
if (!this.stale) {
|
|
124
|
+
this.stale = true
|
|
125
|
+
this.dirtyFrom = change.fromLine
|
|
126
|
+
this.dirtyTo = end
|
|
127
|
+
return
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
// Something is already waiting. Its far end moves with the lines below the
|
|
131
|
+
// change, or is swallowed by it.
|
|
132
|
+
const removedEnd = change.fromLine + change.removedLines
|
|
133
|
+
let to = this.dirtyTo
|
|
134
|
+
if (to >= removedEnd)
|
|
135
|
+
to += change.insertedLines - change.removedLines
|
|
136
|
+
else if (to > change.fromLine)
|
|
137
|
+
to = end
|
|
138
|
+
|
|
139
|
+
this.dirtyFrom = Math.min(this.dirtyFrom, change.fromLine)
|
|
140
|
+
this.dirtyTo = Math.min(this.lines.length, Math.max(to, end))
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
/**
|
|
144
|
+
* Bring lines up to date, starting at the first stale one.
|
|
145
|
+
*
|
|
146
|
+
* Runs until the changed lines are done and the state has settled, or until
|
|
147
|
+
* `maxLines` have been tokenized past `through` (the last line that has to be
|
|
148
|
+
* right now, which is the bottom of the screen). Whatever is left is reported
|
|
149
|
+
* as pending.
|
|
150
|
+
*/
|
|
151
|
+
update(options: { through?: number, maxLines?: number } = {}): HighlightPass {
|
|
152
|
+
if (!this.stale)
|
|
153
|
+
return { from: -1, to: -1, pending: false }
|
|
154
|
+
|
|
155
|
+
const count = this.source.lineCount
|
|
156
|
+
const through = Math.min(count - 1, options.through ?? count - 1)
|
|
157
|
+
const maxLines = options.maxLines ?? Number.POSITIVE_INFINITY
|
|
158
|
+
const start = Math.min(this.dirtyFrom, count)
|
|
159
|
+
const before = start > 0 ? this.lines[start - 1] : null
|
|
160
|
+
|
|
161
|
+
let index = start
|
|
162
|
+
let state: State = before ? before.endState : this.tokenizer.initialState()
|
|
163
|
+
let key = before ? before.endKey : this.tokenizer.stateKey(state)
|
|
164
|
+
let beyond = 0
|
|
165
|
+
let settled = false
|
|
166
|
+
|
|
167
|
+
while (index < count) {
|
|
168
|
+
if (index >= this.dirtyTo) {
|
|
169
|
+
// Past everything that changed. If this line was last entered in the
|
|
170
|
+
// state it is about to be entered in, nothing below can differ.
|
|
171
|
+
const existing = this.lines[index]
|
|
172
|
+
if (existing && existing.startKey === key) {
|
|
173
|
+
settled = true
|
|
174
|
+
break
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
if (index > through && ++beyond > maxLines)
|
|
179
|
+
break
|
|
180
|
+
|
|
181
|
+
const text = this.source.line(index)
|
|
182
|
+
const result = this.tokenizer.tokenizeLine(text, state)
|
|
183
|
+
const endKey = this.tokenizer.stateKey(result.endState)
|
|
184
|
+
|
|
185
|
+
this.lines[index] = { text, tokens: result.tokens, startKey: key, endState: result.endState, endKey }
|
|
186
|
+
state = result.endState
|
|
187
|
+
key = endKey
|
|
188
|
+
index++
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
if (settled || index >= count) {
|
|
192
|
+
this.stale = false
|
|
193
|
+
this.dirtyFrom = count
|
|
194
|
+
this.dirtyTo = count
|
|
195
|
+
}
|
|
196
|
+
else {
|
|
197
|
+
this.dirtyFrom = index
|
|
198
|
+
this.dirtyTo = Math.max(this.dirtyTo, index)
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
return { from: start, to: index, pending: this.stale }
|
|
202
|
+
}
|
|
203
|
+
}
|