|
6 | 6 | useCallback, |
7 | 7 | useEffect, |
8 | 8 | useImperativeHandle, |
| 9 | + useLayoutEffect, |
9 | 10 | useMemo, |
10 | 11 | useRef, |
11 | 12 | useState, |
@@ -162,6 +163,42 @@ function offsetAt(text: string, { line, character }: Position): number { |
162 | 163 | return Math.min(offset + character, text.length) |
163 | 164 | } |
164 | 165 |
|
| 166 | +const isHighSurrogate = (code: number) => code >= 0xd800 && code <= 0xdbff |
| 167 | +const isLowSurrogate = (code: number) => code >= 0xdc00 && code <= 0xdfff |
| 168 | + |
| 169 | +/** |
| 170 | + * The one span that differs between `before` and `after`: everything between their common |
| 171 | + * prefix and suffix, as `[start, end)` offsets into `before` and its replacement. Null when they |
| 172 | + * are equal. Never splits a surrogate pair, so both ends map to character positions. |
| 173 | + */ |
| 174 | +function differingSpan( |
| 175 | + before: string, |
| 176 | + after: string |
| 177 | +): { start: number; end: number; text: string } | null { |
| 178 | + if (before === after) return null |
| 179 | + /* A stream appends: settle it with one native comparison instead of a scan. */ |
| 180 | + if (after.startsWith(before) && !isHighSurrogate(before.charCodeAt(before.length - 1))) { |
| 181 | + return { start: before.length, end: before.length, text: after.slice(before.length) } |
| 182 | + } |
| 183 | + const shorter = Math.min(before.length, after.length) |
| 184 | + let prefix = 0 |
| 185 | + while (prefix < shorter && before.charCodeAt(prefix) === after.charCodeAt(prefix)) prefix++ |
| 186 | + if (prefix > 0 && isHighSurrogate(before.charCodeAt(prefix - 1))) prefix-- |
| 187 | + let suffix = 0 |
| 188 | + while ( |
| 189 | + suffix < shorter - prefix && |
| 190 | + before.charCodeAt(before.length - 1 - suffix) === after.charCodeAt(after.length - 1 - suffix) |
| 191 | + ) { |
| 192 | + suffix++ |
| 193 | + } |
| 194 | + if (suffix > 0 && isLowSurrogate(before.charCodeAt(before.length - suffix))) suffix-- |
| 195 | + return { |
| 196 | + start: prefix, |
| 197 | + end: before.length - suffix, |
| 198 | + text: after.slice(prefix, after.length - suffix), |
| 199 | + } |
| 200 | +} |
| 201 | + |
165 | 202 | function endOf(text: string): Position { |
166 | 203 | const lines = text.split('\n') |
167 | 204 | return { line: lines.length - 1, character: lines.at(-1)?.length ?? 0 } |
@@ -202,13 +239,38 @@ export function CodeEditor({ |
202 | 239 | valueRef.current = value |
203 | 240 | const followingRef = useRef(true) |
204 | 241 |
|
205 | | - /* What the editor holds, and the external document last handed to it. */ |
| 242 | + /* What the editor holds, and the document handed to Pierre when it has no editor to edit. */ |
206 | 243 | const [editorText, setEditorText] = useState(value) |
207 | 244 | const [published, setPublished] = useState({ contents: value, version: 0 }) |
208 | | - if (value !== editorText) { |
209 | | - setEditorText(value) |
210 | | - setPublished({ contents: value, version: published.version + 1 }) |
211 | | - } |
| 245 | + |
| 246 | + /* |
| 247 | + * An external value (a stream, a refresh, a reload) reaches the editor as the one span that |
| 248 | + * changed, so a streamed append costs the size of the append rather than of the document, |
| 249 | + * and the scroll position, selection and tokens of every unchanged line stay put. The edit |
| 250 | + * reports back through `handleEditChange`, which keeps it out of the undo history. Before the |
| 251 | + * editor attaches, the item itself carries the value. |
| 252 | + */ |
| 253 | + useLayoutEffect(() => { |
| 254 | + if (value === editorText) return |
| 255 | + const editor = editorRef.current |
| 256 | + const document = editor?.getEditState()?.document |
| 257 | + if (!editor || !document) { |
| 258 | + setPublished((current) => ({ contents: value, version: current.version + 1 })) |
| 259 | + setEditorText(value) |
| 260 | + return |
| 261 | + } |
| 262 | + const span = differingSpan(editorText, value) |
| 263 | + if (!span) return |
| 264 | + editor.applyEdits( |
| 265 | + [ |
| 266 | + { |
| 267 | + range: { start: document.positionAt(span.start), end: document.positionAt(span.end) }, |
| 268 | + newText: span.text, |
| 269 | + }, |
| 270 | + ], |
| 271 | + false |
| 272 | + ) |
| 273 | + }, [value, editorText]) |
212 | 274 |
|
213 | 275 | const items = useMemo( |
214 | 276 | (): CodeViewFileItem[] => [ |
@@ -346,7 +408,7 @@ export function CodeEditor({ |
346 | 408 | useEffect(() => { |
347 | 409 | if (!followOutput || !followingRef.current) return |
348 | 410 | viewRef.current?.scrollTo({ type: 'item', id: ITEM_ID, align: 'end' }) |
349 | | - }, [followOutput, published]) |
| 411 | + }, [followOutput, value]) |
350 | 412 |
|
351 | 413 | /* CodeView rebuilds on any new option or callback identity, so each one is stable. */ |
352 | 414 | const options = useMemo( |
|
0 commit comments