code: whole-line operations respect the exclusive selection end
- Commit
1f5536e53b7fa3797a1942ab2fe0d5030bd758b7- Author
- Vayun Mathur <[email protected]>
- When
- 2026-12-01 09:34 UTC (5 days ago)
- Parents
afa1c34
lineBlock computed the block end from the line containing selection.max, but a selection is half-open: in 'foo\nbar' with selection (0,4) covering only 'foo\n', duplicateLine duplicated both lines and deleteLine deleted both. Every whole-line operation (duplicate/delete/move/comment/indent/ dedent) inherited the extra line. The shared helper now steps the end back one character when a non-collapsed selection ends exactly at a line start; a collapsed caret at a line start still belongs to that line, so caret line-ops are unchanged. Two regression tests pin both (the exclusive-end case failed pre-fix). Verified: :code:testDevUnitTest BUILD SUCCESSFUL; lint errors are pre-existing on untouched files.
Diff
code/src/main/java/com/vayunmathur/code/platform/EditorInput.kt
diff --git a/code/src/main/java/com/vayunmathur/code/platform/EditorInput.kt b/code/src/main/java/com/vayunmathur/code/platform/EditorInput.ktindex 51fce88749..0b2da9aaed 100644--- a/code/src/main/java/com/vayunmathur/code/platform/EditorInput.kt+++ b/code/src/main/java/com/vayunmathur/code/platform/EditorInput.kt@@ -136,11 +136,16 @@ private fun handleBracket( /** * The `[start, end)` character range of the block of whole lines the selection touches: from * the start of the line containing the selection start to the end (before the newline) of the- * line containing the selection end.+ * last line the selection covers. A selection ending exactly at a line start covers no+ * character of that line, so the block ends on the previous line. */ private fun lineBlock(text: String, selMin: Int, selMax: Int): Pair<Int, Int> { val start = text.lastIndexOf('\n', selMin - 1) + 1- var end = text.indexOf('\n', selMax)+ // A non-collapsed selection is half-open: when it ends exactly at the start of a line,+ // no character of that line is selected, so the block ends on the previous line. (A+ // collapsed caret at a line start does belong to that line and keeps it.)+ val endPos = if (selMax > selMin && selMax > 0 && text[selMax - 1] == '\n') selMax - 1 else selMax+ var end = text.indexOf('\n', endPos) if (end < 0) end = text.length return start to end }
code/src/test/java/com/vayunmathur/code/util/EditorInputTest.kt
diff --git a/code/src/test/java/com/vayunmathur/code/util/EditorInputTest.kt b/code/src/test/java/com/vayunmathur/code/util/EditorInputTest.ktindex 47cfaa1a95..e027332648 100644--- a/code/src/test/java/com/vayunmathur/code/util/EditorInputTest.kt+++ b/code/src/test/java/com/vayunmathur/code/util/EditorInputTest.kt@@ -191,6 +191,25 @@ class EditorInputTest { assertEquals(5, result.selection.start) } + @Test+ fun lineOpsIgnoreLineAtExclusiveSelectionEnd() {+ // Selection [0, 4) covers "foo\n" only: no character of "bar" is selected, so+ // whole-line operations must act on "foo" alone, not drag "bar" along.+ val selected = TextFieldValue("foo\nbar", TextRange(0, 4))+ assertEquals("foo\nfoo\nbar", duplicateLine(selected).text)+ assertEquals("bar", deleteLine(selected).text)+ assertEquals("// foo\nbar", toggleLineComment(selected, "//").text)+ val moved = moveLineDown(TextFieldValue("foo\nbar\nbaz", TextRange(0, 4)))+ assertEquals("bar\nfoo\nbaz", moved.text)+ }++ @Test+ fun lineOpsCollapsedCaretAtLineStartStillActsOnThatLine() {+ // A collapsed caret at the start of "bar" does belong to "bar".+ val caret = TextFieldValue("foo\nbar", TextRange(4))+ assertEquals("foo\nbar\nbar", duplicateLine(caret).text)+ }+ @Test fun deleteLineRemovesCurrentLineAndNewline() { val result = deleteLine(TextFieldValue("foo\nbar\nbaz", TextRange(5)))