Skip to content

Commit

Permalink
Fix(VIM-3260): Processing the offsets at the file end
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexPl292 committed Jan 26, 2024
1 parent e04a15b commit 808533b
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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)
}

0 comments on commit 808533b

Please sign in to comment.