From 808533b11084b16fb57b0c98a7f978ce82768b3a Mon Sep 17 00:00:00 2001 From: Alex Plate Date: Fri, 26 Jan 2024 17:49:44 +0200 Subject: [PATCH] Fix(VIM-3260): Processing the offsets at the file end --- .../maddyhome/idea/vim/listener/VimListenerManager.kt | 11 +++++++++-- .../com/maddyhome/idea/vim/api/EngineEditorHelper.kt | 7 +++++++ 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/maddyhome/idea/vim/listener/VimListenerManager.kt b/src/main/java/com/maddyhome/idea/vim/listener/VimListenerManager.kt index 2c6ab3943f..ba9189ea11 100644 --- a/src/main/java/com/maddyhome/idea/vim/listener/VimListenerManager.kt +++ b/src/main/java/com/maddyhome/idea/vim/listener/VimListenerManager.kt @@ -55,6 +55,7 @@ import com.maddyhome.idea.vim.VimTypedActionHandler import com.maddyhome.idea.vim.api.LocalOptionInitialisationScenario import com.maddyhome.idea.vim.api.Options import com.maddyhome.idea.vim.api.VimEditor +import com.maddyhome.idea.vim.api.coerceOffset import com.maddyhome.idea.vim.api.getLineEndForOffset import com.maddyhome.idea.vim.api.getLineStartForOffset import com.maddyhome.idea.vim.api.injector @@ -462,11 +463,17 @@ internal object VimListenerManager { if (lineEnd == endOffset - 1) { // When starting on an empty line and dragging vertically upwards onto // another line, the selection should include the entirety of the empty line - caret.setSelection(endOffset + 1, startOffset) + caret.setSelection( + ijVimEditor.coerceOffset(endOffset + 1).point, + ijVimEditor.coerceOffset(startOffset).point, + ) } else if (lineEnd == startOffset + 1 && startOffset == endOffset) { // When dragging left from EOL on a non-empty line, the selection // should include the last character on the line - caret.setSelection(lineEnd, lineEnd - 1) + caret.setSelection( + ijVimEditor.coerceOffset(lineEnd).point, + ijVimEditor.coerceOffset(lineEnd - 1).point, + ) } } //endregion diff --git a/vim-engine/src/main/kotlin/com/maddyhome/idea/vim/api/EngineEditorHelper.kt b/vim-engine/src/main/kotlin/com/maddyhome/idea/vim/api/EngineEditorHelper.kt index fc74be7d4a..6cd2bcc6f2 100644 --- a/vim-engine/src/main/kotlin/com/maddyhome/idea/vim/api/EngineEditorHelper.kt +++ b/vim-engine/src/main/kotlin/com/maddyhome/idea/vim/api/EngineEditorHelper.kt @@ -9,6 +9,7 @@ package com.maddyhome.idea.vim.api import com.maddyhome.idea.vim.common.Graphemes +import com.maddyhome.idea.vim.common.Offset import com.maddyhome.idea.vim.common.TextRange import java.nio.CharBuffer @@ -293,3 +294,9 @@ public fun VimEditor.isLineEmpty(line: Int, allowBlanks: Boolean): Boolean { } return false } + +public fun VimEditor.coerceOffset(offset: Int): Offset { + if (offset < 0) return Offset(0) + if (offset > this.fileSize()) return Offset(this.fileSize().toInt()) + return Offset(offset) +}