Skip to content

Commit 9f5d117

Browse files
authored
fix(editor): apply external updates to the code editor as the span that changed (#10259)
A streamed write, a Code tab refresh or a reload reached Pierre as a whole new document on every update. The agent's paced reveal updates every frame, so a 40,000-line file streamed at 28 fps with 42 main-thread stalls. The editor now applies only the span that differs (a single native comparison for an append), through Pierre's incremental edits: every size up to 40,000 lines streams at 60 fps with no stalls. Unchanged lines keep their scroll position, selection and tokens, and the update stays out of undo.
1 parent 74eca43 commit 9f5d117

1 file changed

Lines changed: 68 additions & 6 deletions

File tree

‎apps/sim/components/code-editor/code-editor.tsx‎

Lines changed: 68 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
useCallback,
77
useEffect,
88
useImperativeHandle,
9+
useLayoutEffect,
910
useMemo,
1011
useRef,
1112
useState,
@@ -162,6 +163,42 @@ function offsetAt(text: string, { line, character }: Position): number {
162163
return Math.min(offset + character, text.length)
163164
}
164165

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+
165202
function endOf(text: string): Position {
166203
const lines = text.split('\n')
167204
return { line: lines.length - 1, character: lines.at(-1)?.length ?? 0 }
@@ -202,13 +239,38 @@ export function CodeEditor({
202239
valueRef.current = value
203240
const followingRef = useRef(true)
204241

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. */
206243
const [editorText, setEditorText] = useState(value)
207244
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])
212274

213275
const items = useMemo(
214276
(): CodeViewFileItem[] => [
@@ -346,7 +408,7 @@ export function CodeEditor({
346408
useEffect(() => {
347409
if (!followOutput || !followingRef.current) return
348410
viewRef.current?.scrollTo({ type: 'item', id: ITEM_ID, align: 'end' })
349-
}, [followOutput, published])
411+
}, [followOutput, value])
350412

351413
/* CodeView rebuilds on any new option or callback identity, so each one is stable. */
352414
const options = useMemo(

0 commit comments

Comments
 (0)