1
0
mirror of https://github.com/chylex/IntelliJ-IdeaVim.git synced 2025-04-16 19:15:46 +02:00
IntelliJ-IdeaVim/src/main/java/com/maddyhome/idea/vim/common/CharacterPosition.kt
2022-11-01 20:00:07 +02:00

31 lines
1.0 KiB
Kotlin

/*
* Copyright 2022 The IdeaVim authors
*
* Use of this source code is governed by an MIT-style
* license that can be found in the LICENSE.txt file or at
* https://opensource.org/licenses/MIT.
*/
package com.maddyhome.idea.vim.common
import com.intellij.openapi.editor.Editor
import com.intellij.openapi.editor.LogicalPosition
import com.maddyhome.idea.vim.helper.EditorHelper.getLineStartOffset
class CharacterPosition(line: Int, col: Int) : LogicalPosition(line, col) {
fun toOffset(editor: Editor) = getLineStartOffset(editor, line) + column
companion object {
fun fromOffset(editor: Editor, offset: Int): CharacterPosition {
// logical position "expands" tabs
val logicalPosition = editor.offsetToLogicalPosition(offset)
val lineStartOffset = getLineStartOffset(editor, logicalPosition.line)
return CharacterPosition(logicalPosition.line, offset - lineStartOffset)
}
fun atCaret(editor: Editor): CharacterPosition {
return fromOffset(editor, editor.caretModel.offset)
}
}
}