Skip to content

Commit

Permalink
Respect emojis and don't assume that every character is a single offset
Browse files Browse the repository at this point in the history
  • Loading branch information
lippfi committed Aug 23, 2024
1 parent 71be802 commit 0a15d0a
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import com.maddyhome.idea.vim.api.VimEditor
import com.maddyhome.idea.vim.api.injector
import com.maddyhome.idea.vim.command.Command
import com.maddyhome.idea.vim.command.OperatorArguments
import com.maddyhome.idea.vim.common.Graphemes
import com.maddyhome.idea.vim.handler.VimActionHandler

@CommandOrMotion(keys = ["<DEL>"], modes = [Mode.CMD_LINE])
Expand All @@ -32,9 +33,11 @@ class DeleteNextCharAction : VimActionHandler.SingleExecution() {
}

val newText = if (caretOffset == oldText.length) {
oldText.substring(0, oldText.lastIndex)
val preEndOffset = Graphemes.prev(oldText, oldText.length) ?: return true
oldText.substring(0, preEndOffset)
} else {
oldText.substring(0, caretOffset) + oldText.substring(caretOffset + 1)
val nextOffset = Graphemes.next(oldText, caretOffset) ?: return true
oldText.substring(0, caretOffset) + oldText.substring(nextOffset)
}
commandLine.setText(newText)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import com.maddyhome.idea.vim.api.VimEditor
import com.maddyhome.idea.vim.api.injector
import com.maddyhome.idea.vim.command.Command
import com.maddyhome.idea.vim.command.OperatorArguments
import com.maddyhome.idea.vim.common.Graphemes
import com.maddyhome.idea.vim.handler.VimActionHandler

@CommandOrMotion(keys = ["<BS>", "<C-H>"], modes = [Mode.CMD_LINE])
Expand All @@ -33,8 +34,9 @@ class DeletePreviousCharAction : VimActionHandler.SingleExecution() {
val caretOffset = commandLine.caret.offset
if (caretOffset == 0) return true

commandLine.caret.offset -= 1
val newText = oldText.substring(0, caretOffset - 1) + oldText.substring(caretOffset)
val prevOffset = Graphemes.prev(oldText, caretOffset) ?: 0
commandLine.caret.offset = prevOffset
val newText = oldText.substring(0, prevOffset) + oldText.substring(caretOffset)
commandLine.setText(newText)

return true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import com.maddyhome.idea.vim.api.VimEditor
import com.maddyhome.idea.vim.api.injector
import com.maddyhome.idea.vim.command.Command
import com.maddyhome.idea.vim.command.OperatorArguments
import com.maddyhome.idea.vim.common.Graphemes
import com.maddyhome.idea.vim.handler.VimActionHandler

@CommandOrMotion(keys = ["<Left>"], modes = [Mode.CMD_LINE])
Expand All @@ -25,8 +26,8 @@ class MoveCaretLeftAction : VimActionHandler.SingleExecution() {
val commandLine = injector.commandLine.getActiveCommandLine() ?: return false
val caret = commandLine.caret

if (caret.offset == 0) return true
caret.offset -= 1
val prevOffset = Graphemes.prev(commandLine.actualText, caret.offset) ?: return true
caret.offset = prevOffset

return true
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import com.maddyhome.idea.vim.api.VimEditor
import com.maddyhome.idea.vim.api.injector
import com.maddyhome.idea.vim.command.Command
import com.maddyhome.idea.vim.command.OperatorArguments
import com.maddyhome.idea.vim.common.Graphemes
import com.maddyhome.idea.vim.handler.VimActionHandler

@CommandOrMotion(keys = ["<Right>"], modes = [Mode.CMD_LINE])
Expand All @@ -25,8 +26,8 @@ class MoveCaretRightAction : VimActionHandler.SingleExecution() {
val commandLine = injector.commandLine.getActiveCommandLine() ?: return false
val caret = commandLine.caret

if (caret.offset == commandLine.actualText.length) return true
caret.offset += 1
val nextOffset = Graphemes.next(commandLine.actualText, caret.offset) ?: return true
caret.offset = nextOffset

return true
}
Expand Down

0 comments on commit 0a15d0a

Please sign in to comment.