diff --git a/CHANGELOG.md b/CHANGELOG.md index 5468a775..59756480 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,7 @@ ### Editor - Both platforms now highlight through one syntax engine written in C++, in place of the separate Kotlin and Swift engines. The shipped languages highlight exactly as before: the tests compare the colors of every sample file against a recording of the old engine. On the Android emulator, the example app used 10-14 MB less memory with the 10,000-line benchmark file open, most of it Java heap. +- On Android, pressing Enter in the middle of a line no longer crashes the app when a colored token follows the cursor. This happened in every 0.1 release. On both platforms, the text moved to the new line now keeps its colors until the new highlighting arrives, instead of showing plain for a frame. ### Languages diff --git a/android/src/main/java/me/sectiontn/codepad/view/CodeEditorView.kt b/android/src/main/java/me/sectiontn/codepad/view/CodeEditorView.kt index c6ddcfaa..6e2d65a7 100644 --- a/android/src/main/java/me/sectiontn/codepad/view/CodeEditorView.kt +++ b/android/src/main/java/me/sectiontn/codepad/view/CodeEditorView.kt @@ -97,6 +97,7 @@ class CodeEditorView(context: Context) : View(context) { private var appliedRevision = -1 // The dropped entry's spans moved along a one line change, painted until the snapshot lands. private var pendingSpans: IntArray? = null + private var pendingTail: IntArray? = null private var pendingLine = -1 private var pendingRevision = -1 private var highlighter: Highlighter? = null @@ -665,8 +666,9 @@ class CodeEditorView(context: Context) : View(context) { /** Lays out one line plain, for hit tests and reveals on lines outside the drawn range. */ private fun layoutLine(line: Int): Entry { - val layout = LineLayout(core.document.line(line), null, paint, theme, viewport.wrapWidth, core.settings.tabSize, rowMetrics()) - val entry = Entry(layout, null, -1) + val spans = keptSpans(line) + val layout = LineLayout(core.document.line(line), spans, paint, theme, viewport.wrapWidth, core.settings.tabSize, rowMetrics()) + val entry = Entry(layout, spans, -1) cache.put(line, entry) viewport.measured(line, layout.rowCount) if (!viewport.wrap) viewport.widestLine = maxOf(viewport.widestLine, Math.round(layout.width())) @@ -1298,8 +1300,8 @@ class CodeEditorView(context: Context) : View(context) { entry.revision = revision continue } - } else if (line == pendingLine && pendingRevision == doc.revision) { - fresh = pendingSpans + } else { + fresh = keptSpans(line) } val layout = LineLayout(doc.line(line), fresh, paint, theme, viewport.wrapWidth, core.settings.tabSize, m) cache.put(line, Entry(layout, fresh, if (inRun) revision else -1)) @@ -1309,19 +1311,33 @@ class CodeEditorView(context: Context) : View(context) { if (revision >= 0) { appliedRevision = revision pendingSpans = null + pendingTail = null } } - /** The edited line's previous spans moved along a change that stays inside it, or a newline typed at its end. */ + /** The spans [keepSpans] kept for a line of the current revision, or null. */ + private fun keptSpans(line: Int): IntArray? = when { + pendingRevision != core.document.revision -> null + line == pendingLine -> pendingSpans + line == pendingLine + 1 -> pendingTail + else -> null + } + + /** The edited line's previous spans moved along a change that stays inside it, or split by a typed newline. */ private fun keepSpans(first: Int, oldCount: Int, newCount: Int, change: Change) { pendingSpans = null + pendingTail = null if (oldCount != 1) return val spans = cache[first]?.spans ?: return val doc = core.document val column = change.start - doc.lineStart(first) pendingSpans = when { newCount == 1 -> shiftSpans(spans, column, change.removed.length, change.inserted.length) - newCount == 2 && change.removed.isEmpty() && change.inserted.startsWith("\n") && doc.lineLength(first) == column -> spans + newCount == 2 && change.removed.isEmpty() && change.inserted.startsWith("\n") -> { + val added = change.inserted.length - 1 + pendingTail = shiftSpans(spans, 0, column, added) + shiftSpans(spans, column, doc.lineLength(first + 1) - added, 0) + } else -> return } pendingLine = first diff --git a/example/android/app/src/androidTest/java/codepad/example/KeyFrameHighlightTest.kt b/example/android/app/src/androidTest/java/codepad/example/KeyFrameHighlightTest.kt index 0d319938..899bcb01 100644 --- a/example/android/app/src/androidTest/java/codepad/example/KeyFrameHighlightTest.kt +++ b/example/android/app/src/androidTest/java/codepad/example/KeyFrameHighlightTest.kt @@ -60,4 +60,30 @@ class KeyFrameHighlightTest { assertTrue("the number kept its color in the frame before the snapshot", redPixels(before, 80) > 0) } } + + // Enter before a colored token used to keep the whole line's spans on the shortened line and crash in setSpan. + @Test + fun aNewlineBeforeAColoredTokenMovesItsColorToTheNewLine() { + launch().use { s -> + val editor = s.editor() + val number = TokenStyleColors(0xFFFF0000.toInt(), false, false) + onMain { + editor.setTheme(ThemeColors.default.withTokens(mapOf("number" to number))) + editor.setLanguage("javascript") + editor.setInitialText("const f = 1;\n") + layout(editor) + draw(editor) + } + Thread.sleep(300) + assertTrue("the number painted", redPixels(onMain { draw(editor) }, 80) > 0) + val before = onMain { + editor.core.setSelection(6, 6) + editor.typeText("\n") + draw(editor) + } + Thread.sleep(300) + assertTrue("the number painted after the snapshot", redPixels(onMain { draw(editor) }, 80) > 0) + assertTrue("the number kept its color in the frame before the snapshot", redPixels(before, 80) > 0) + } + } } diff --git a/example/ios/CodepadTests/KeyFrameHighlightTests.swift b/example/ios/CodepadTests/KeyFrameHighlightTests.swift index bc87e320..34741f1f 100644 --- a/example/ios/CodepadTests/KeyFrameHighlightTests.swift +++ b/example/ios/CodepadTests/KeyFrameHighlightTests.swift @@ -63,4 +63,24 @@ final class KeyFrameHighlightTests: XCTestCase { XCTAssertGreaterThan(redPixels(draw(editor), rows: 22), 0, "the number painted after the snapshot") XCTAssertGreaterThan(before, 0, "the number kept its color in the frame before the snapshot") } + + // Enter before a colored token used to leave the moved text plain until the snapshot. + func testANewlineBeforeAColoredTokenMovesItsColorToTheNewLine() { + let editor = launch() + let number = TokenStyle(color: ThemeColors.color(255, 0, 0), bold: false, italic: false) + editor.setTheme(ThemeColors.default.withTokens(["number": number])) + editor.setLanguage("javascript") + editor.setInitialText("const f = 1;\n") + layout(editor) + draw(editor) + waitForSnapshot(editor) + XCTAssertGreaterThan(redPixels(draw(editor), rows: 60), 0, "the number painted") + editor.core.setSelection(anchor: 6, head: 6) + editor.typeText("\n") + XCTAssertNotEqual(editor.snapshotRevision, editor.core.document.revision, "the parse is still in flight") + let before = redPixels(draw(editor), rows: 60) + waitForSnapshot(editor) + XCTAssertGreaterThan(redPixels(draw(editor), rows: 60), 0, "the number painted after the snapshot") + XCTAssertGreaterThan(before, 0, "the number kept its color in the frame before the snapshot") + } } diff --git a/ios/Codepad/view/CodeEditorView.swift b/ios/Codepad/view/CodeEditorView.swift index 60a44e86..3cd00afa 100644 --- a/ios/Codepad/view/CodeEditorView.swift +++ b/ios/Codepad/view/CodeEditorView.swift @@ -100,6 +100,7 @@ final class CodeEditorView: UIScrollView { private var appliedRevision = -1 // The dropped entry's spans moved along a one line change, painted until the snapshot lands. private var pendingSpans: [Int]? + private var pendingTail: [Int]? private var pendingLine = -1 private var pendingRevision = -1 private var highlighter: Highlighter? @@ -898,7 +899,8 @@ final class CodeEditorView: UIScrollView { /// Lays out one line plain, for hit tests and reveals on lines outside the drawn range. @discardableResult private func layoutLine(_ line: Int) -> Entry { - let entry = Entry(id: takeEntryId(), layout: makeLayout(line, spans: nil, rowMetrics()), spans: nil, revision: -1) + let spans = keptSpans(line) + let entry = Entry(id: takeEntryId(), layout: makeLayout(line, spans: spans, rowMetrics()), spans: spans, revision: -1) cache.put(line, entry) let scrollBefore = viewport.scrollY measured(line, entry.layout) @@ -999,8 +1001,8 @@ final class CodeEditorView: UIScrollView { entry.revision = revision continue } - } else if line == pendingLine, pendingRevision == doc.revision { - fresh = pendingSpans + } else { + fresh = keptSpans(line) } let layout = makeLayout(line, spans: fresh, m) cache.put(line, Entry(id: takeEntryId(), layout: layout, spans: fresh, revision: inRun ? revision : -1)) @@ -1009,20 +1011,32 @@ final class CodeEditorView: UIScrollView { if revision >= 0 { appliedRevision = revision pendingSpans = nil + pendingTail = nil } } - /// The edited line's previous spans moved along a change that stays inside it, or a newline typed at its end. + /// The spans keepSpans kept for a line of the current revision, or nil. + private func keptSpans(_ line: Int) -> [Int]? { + guard pendingRevision == core.document.revision else { return nil } + if line == pendingLine { return pendingSpans } + if line == pendingLine + 1 { return pendingTail } + return nil + } + + /// The edited line's previous spans moved along a change that stays inside it, or split by a typed newline. private func keepSpans(_ first: Int, _ oldCount: Int, _ newCount: Int, _ change: Change) { pendingSpans = nil + pendingTail = nil guard oldCount == 1, let spans = cache[first]?.spans else { return } let doc = core.document let column = change.start - doc.lineStart(first) let removed = change.removed.utf16.count if newCount == 1 { pendingSpans = shiftSpans(spans, start: column, removed: removed, inserted: change.inserted.utf16.count) - } else if newCount == 2, removed == 0, change.inserted.hasPrefix("\n"), doc.lineLength(first) == column { - pendingSpans = spans + } else if newCount == 2, removed == 0, change.inserted.hasPrefix("\n") { + let added = change.inserted.utf16.count - 1 + pendingTail = shiftSpans(spans, start: 0, removed: column, inserted: added) + pendingSpans = shiftSpans(spans, start: column, removed: doc.lineLength(first + 1) - added, inserted: 0) } else { return }