mirror of
https://github.com/chylex/IntelliJ-IdeaVim.git
synced 2025-08-04 15:59:04 +02:00
Fix last selection type
This commit is contained in:
parent
8370248390
commit
11b2096dba
src/com/maddyhome/idea/vim
@ -86,6 +86,10 @@ public class CommandState {
|
||||
return state.getMode() == Mode.VISUAL && state.getSubMode() == SubMode.VISUAL_CHARACTER;
|
||||
}
|
||||
|
||||
public static boolean inVisualMode(@Nullable Editor editor) {
|
||||
return getInstance(editor).getMode() == Mode.VISUAL;
|
||||
}
|
||||
|
||||
public static boolean inVisualLineMode(@Nullable Editor editor) {
|
||||
final CommandState state = getInstance(editor);
|
||||
return state.getMode() == Mode.VISUAL && state.getSubMode() == SubMode.VISUAL_LINE;
|
||||
|
@ -358,10 +358,10 @@ public class MotionGroup {
|
||||
if (offset >= 0 && offset <= editor.getDocument().getTextLength()) {
|
||||
|
||||
if (CommandState.inVisualBlockMode(editor)) {
|
||||
scrollCaretIntoView(editor);
|
||||
UtilsKt.vimMoveBlockSelectionToOffset(editor, offset);
|
||||
Caret blockMainCaret = CaretDataKt.getVimBlockMainCaret(editor);
|
||||
CaretData.setLastColumn(editor, blockMainCaret, blockMainCaret.getVisualPosition().column);
|
||||
Caret primaryCaret = editor.getCaretModel().getPrimaryCaret();
|
||||
CaretData.setLastColumn(editor, primaryCaret, primaryCaret.getVisualPosition().column);
|
||||
scrollCaretIntoView(editor);
|
||||
return;
|
||||
}
|
||||
|
||||
|
@ -34,7 +34,6 @@ import com.maddyhome.idea.vim.group.MotionGroup
|
||||
import com.maddyhome.idea.vim.helper.CaretData
|
||||
import com.maddyhome.idea.vim.helper.EditorData
|
||||
import com.maddyhome.idea.vim.helper.EditorHelper
|
||||
import com.maddyhome.idea.vim.helper.vimBlockMainCaretSetToNull
|
||||
import com.maddyhome.idea.vim.helper.vimSelectionStart
|
||||
import com.maddyhome.idea.vim.helper.vimSelectionStartSetToNull
|
||||
import com.maddyhome.idea.vim.helper.vimStartSelectionAtPoint
|
||||
@ -267,7 +266,6 @@ object VisualMotionGroup {
|
||||
fun resetVisual(editor: Editor) {
|
||||
val wasVisualBlock = CommandState.inVisualBlockMode(editor)
|
||||
val selectionType = SelectionType.fromSubMode(CommandState.getInstance(editor).subMode)
|
||||
EditorData.setLastSelectionType(editor, selectionType)
|
||||
|
||||
if (!EditorData.isKeepingVisualOperatorAction(editor)) {
|
||||
for (caret in editor.caretModel.allCarets) {
|
||||
@ -278,13 +276,13 @@ object VisualMotionGroup {
|
||||
editor.caretModel.removeSecondaryCarets()
|
||||
}
|
||||
|
||||
if (CommandState.getInstance(editor).mode == CommandState.Mode.VISUAL) {
|
||||
if (CommandState.inVisualMode(editor)) {
|
||||
EditorData.setLastSelectionType(editor, selectionType)
|
||||
// FIXME: 2019-03-05 Make it multicaret
|
||||
val primaryCaret = editor.caretModel.primaryCaret
|
||||
val vimSelectionStart = primaryCaret.vimSelectionStart
|
||||
VimPlugin.getMark().setVisualSelectionMarks(editor, TextRange(vimSelectionStart, primaryCaret.offset))
|
||||
editor.caretModel.allCarets.forEach { it.vimSelectionStartSetToNull() }
|
||||
editor.vimBlockMainCaretSetToNull()
|
||||
|
||||
CommandState.getInstance(editor).subMode = CommandState.SubMode.NONE
|
||||
}
|
||||
|
@ -30,7 +30,6 @@ import com.maddyhome.idea.vim.command.CommandFlags
|
||||
import com.maddyhome.idea.vim.command.CommandState
|
||||
import com.maddyhome.idea.vim.group.MotionGroup
|
||||
import com.maddyhome.idea.vim.helper.EditorHelper
|
||||
import com.maddyhome.idea.vim.helper.vimBlockMainCaret
|
||||
import com.maddyhome.idea.vim.helper.vimSelectionStart
|
||||
|
||||
/**
|
||||
@ -48,10 +47,7 @@ abstract class MotionEditorActionHandler : EditorActionHandlerBase(false) {
|
||||
final override fun execute(editor: Editor, context: DataContext, cmd: Command): Boolean {
|
||||
val visualBlockActive = CommandState.inVisualBlockMode(editor)
|
||||
|
||||
if (visualBlockActive) {
|
||||
val primaryCaret = editor.vimBlockMainCaret
|
||||
doExecute(editor, primaryCaret, context, cmd)
|
||||
} else if (editor.caretModel.caretCount == 1 || alwaysBatchExecution) {
|
||||
if (visualBlockActive || editor.caretModel.caretCount == 1 || alwaysBatchExecution) {
|
||||
val primaryCaret = editor.caretModel.primaryCaret
|
||||
doExecute(editor, primaryCaret, context, cmd)
|
||||
} else {
|
||||
|
@ -34,7 +34,6 @@ import com.maddyhome.idea.vim.group.motion.VisualMotionGroup
|
||||
import com.maddyhome.idea.vim.helper.CaretData
|
||||
import com.maddyhome.idea.vim.helper.EditorData
|
||||
import com.maddyhome.idea.vim.helper.VimSelection
|
||||
import com.maddyhome.idea.vim.helper.vimBlockMainCaret
|
||||
import com.maddyhome.idea.vim.helper.vimSelectionStart
|
||||
import com.maddyhome.idea.vim.helper.visualBlockRange
|
||||
|
||||
@ -96,8 +95,8 @@ abstract class VisualOperatorActionHandler : EditorActionHandlerBase(false) {
|
||||
|
||||
if (CommandState.inVisualBlockMode(this)) {
|
||||
val adj = if (VisualMotionGroup.exclusiveSelection) 0 else 1
|
||||
val (start, end) = vimBlockMainCaret.run { if (vimSelectionStart > offset) vimSelectionStart + adj to offset else vimSelectionStart to offset + adj }
|
||||
return mapOf(vimBlockMainCaret to VimSelection(start, end, SelectionType.BLOCK_WISE, this))
|
||||
val (start, end) = caretModel.primaryCaret.run { if (vimSelectionStart > offset) vimSelectionStart + adj to offset else vimSelectionStart to offset + adj }
|
||||
return mapOf(caretModel.primaryCaret to VimSelection(start, end, SelectionType.BLOCK_WISE, this))
|
||||
}
|
||||
|
||||
return this.caretModel.allCarets.associateWith { caret ->
|
||||
|
@ -59,8 +59,7 @@ public class CaretData {
|
||||
caret.putUserData(LAST_COLUMN, col);
|
||||
}
|
||||
else {
|
||||
Caret vimBlockMainCaret = CaretDataKt.getVimBlockMainCaret(editor);
|
||||
vimBlockMainCaret.putUserData(LAST_COLUMN, col);
|
||||
editor.getCaretModel().getPrimaryCaret().putUserData(LAST_COLUMN, col);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -47,17 +47,3 @@ fun Caret.vimSelectionStartSetToNull() {
|
||||
|
||||
private var Caret._vimSelectionStart: Int? by userData()
|
||||
private var Editor._vimBlockSelectinoStart: Int? by userData()
|
||||
|
||||
|
||||
var Editor.vimBlockMainCaret: Caret
|
||||
get() = _vimBlockMainCaret
|
||||
?: throw AssertionError("Trying to access block main caret, but it's not set")
|
||||
set(value) {
|
||||
_vimBlockMainCaret = value
|
||||
}
|
||||
|
||||
fun Editor.vimBlockMainCaretSetToNull() {
|
||||
this._vimBlockMainCaret = null
|
||||
}
|
||||
|
||||
private var Editor._vimBlockMainCaret: Caret? by userData()
|
||||
|
@ -57,10 +57,10 @@ fun vimMoveBlockSelectionToOffset(editor: Editor, offset: Int) {
|
||||
if (!CommandState.inVisualBlockMode(editor))
|
||||
throw RuntimeException("Move caret with [vimMoveSelectionToCaret]")
|
||||
|
||||
val vimBlockMainCaret = editor.vimBlockMainCaret
|
||||
val startOffsetMark = vimBlockMainCaret.vimSelectionStart
|
||||
val primaryCaret = editor.caretModel.primaryCaret
|
||||
val startOffsetMark = primaryCaret.vimSelectionStart
|
||||
|
||||
setVisualSelection(startOffsetMark, offset, vimBlockMainCaret)
|
||||
setVisualSelection(startOffsetMark, offset, primaryCaret)
|
||||
}
|
||||
|
||||
fun Caret.vimUpdateEditorSelection() {
|
||||
@ -68,8 +68,8 @@ fun Caret.vimUpdateEditorSelection() {
|
||||
setVisualSelection(startOffsetMark, offset, this)
|
||||
}
|
||||
|
||||
private fun setVisualSelection(firstOffset: Int, secondOffset: Int, caret: Caret) {
|
||||
val (start, end) = if (firstOffset > secondOffset) secondOffset to firstOffset else firstOffset to secondOffset
|
||||
private fun setVisualSelection(selectionStart: Int, selectionEnd: Int, caret: Caret) {
|
||||
val (start, end) = if (selectionStart > selectionEnd) selectionEnd to selectionStart else selectionStart to selectionEnd
|
||||
val editor = caret.editor
|
||||
val subMode = CommandState.getInstance(editor).subMode
|
||||
when (subMode) {
|
||||
@ -87,14 +87,14 @@ private fun setVisualSelection(firstOffset: Int, secondOffset: Int, caret: Caret
|
||||
CommandState.SubMode.VISUAL_BLOCK -> {
|
||||
editor.caretModel.removeSecondaryCarets()
|
||||
|
||||
var blockStart = editor.offsetToLogicalPosition(start)
|
||||
var blockEnd = editor.offsetToLogicalPosition(end)
|
||||
if (blockStart.column > blockEnd.column) {
|
||||
// swap variables
|
||||
blockStart = blockEnd.also { blockEnd = blockStart }
|
||||
}
|
||||
var blockStart = editor.offsetToLogicalPosition(selectionStart)
|
||||
var blockEnd = editor.offsetToLogicalPosition(selectionEnd)
|
||||
if (!VisualMotionGroup.exclusiveSelection) {
|
||||
blockEnd = LogicalPosition(blockEnd.line, blockEnd.column + 1)
|
||||
if (blockStart.column > blockEnd.column) {
|
||||
blockStart = LogicalPosition(blockStart.line, blockStart.column + 1)
|
||||
} else {
|
||||
blockEnd = LogicalPosition(blockEnd.line, blockEnd.column + 1)
|
||||
}
|
||||
}
|
||||
editor.selectionModel.setBlockSelection(blockStart, blockEnd)
|
||||
|
||||
@ -110,16 +110,7 @@ private fun setVisualSelection(firstOffset: Int, secondOffset: Int, caret: Caret
|
||||
}
|
||||
}
|
||||
|
||||
val startLine = editor.offsetToLogicalPosition(caret.vimSelectionStart).line
|
||||
val startColumn = editor.offsetToLogicalPosition(caret.vimSelectionStart).column
|
||||
if (editor.caretModel.allCarets.first().logicalPosition.line == startLine) {
|
||||
editor.vimBlockMainCaret = editor.caretModel.allCarets.last()
|
||||
} else {
|
||||
editor.vimBlockMainCaret = editor.caretModel.allCarets.first()
|
||||
}
|
||||
if (editor.vimBlockMainCaret.logicalPosition.column <= startColumn) {
|
||||
editor.vimBlockMainCaret.moveToOffset(editor.vimBlockMainCaret.selectionStart)
|
||||
}
|
||||
editor.caretModel.primaryCaret.moveToOffset(selectionEnd)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user