mirror of
https://github.com/chylex/IntelliJ-IdeaVim.git
synced 2025-08-10 06:40:37 +02:00
Move CaretData to kotlin
This commit is contained in:
parent
f1a667ae2e
commit
5200ff8e84
src/com/maddyhome/idea/vim
@ -65,7 +65,6 @@ import com.maddyhome.idea.vim.common.Register;
|
||||
import com.maddyhome.idea.vim.common.TextRange;
|
||||
import com.maddyhome.idea.vim.ex.LineRange;
|
||||
import com.maddyhome.idea.vim.handler.CaretOrder;
|
||||
import com.maddyhome.idea.vim.helper.CaretData;
|
||||
import com.maddyhome.idea.vim.helper.CaretDataKt;
|
||||
import com.maddyhome.idea.vim.helper.CharacterHelper;
|
||||
import com.maddyhome.idea.vim.helper.EditorData;
|
||||
@ -174,7 +173,7 @@ public class ChangeGroup {
|
||||
for (Caret caret : editor.getCaretModel().getAllCarets()) {
|
||||
if (caret.getVisualPosition().line == 0) {
|
||||
MotionGroup.moveCaret(editor, caret, VimPlugin.getMotion().moveCaretToLineStart(editor, caret));
|
||||
CaretData.setWasInFirstLine(caret, true);
|
||||
CaretDataKt.setVimWasIsFirstLine(caret, true);
|
||||
}
|
||||
else {
|
||||
MotionGroup.moveCaret(editor, caret, VimPlugin.getMotion().moveCaretVertical(editor, caret, -1));
|
||||
@ -186,8 +185,8 @@ public class ChangeGroup {
|
||||
runEnterAction(editor, context);
|
||||
|
||||
for (Caret caret : editor.getCaretModel().getAllCarets()) {
|
||||
if (CaretData.wasInFirstLine(caret)) {
|
||||
CaretData.setWasInFirstLine(caret, false);
|
||||
if (CaretDataKt.getVimWasIsFirstLine(caret)) {
|
||||
CaretDataKt.setVimWasIsFirstLine(caret, false);
|
||||
MotionGroup.moveCaret(editor, caret, VimPlugin.getMotion().moveCaretVertical(editor, caret, -1));
|
||||
}
|
||||
}
|
||||
@ -205,7 +204,7 @@ public class ChangeGroup {
|
||||
|
||||
if (caret.getVisualPosition().line == 0) {
|
||||
MotionGroup.moveCaret(editor, caret, VimPlugin.getMotion().moveCaretToLineStart(editor, caret));
|
||||
CaretData.setWasInFirstLine(caret, true);
|
||||
CaretDataKt.setVimWasIsFirstLine(caret, true);
|
||||
}
|
||||
else {
|
||||
MotionGroup.moveCaret(editor, caret, VimPlugin.getMotion().moveCaretVertical(editor, caret, -1));
|
||||
@ -215,9 +214,9 @@ public class ChangeGroup {
|
||||
EditorData.setChangeSwitchMode(editor, CommandState.Mode.INSERT);
|
||||
insertText(editor, caret, "\n" + IndentConfig.create(editor).createIndentBySize(col));
|
||||
|
||||
if (CaretData.wasInFirstLine(caret)) {
|
||||
if (CaretDataKt.getVimWasIsFirstLine(caret)) {
|
||||
MotionGroup.moveCaret(editor, caret, VimPlugin.getMotion().moveCaretVertical(editor, caret, -1));
|
||||
CaretData.setWasInFirstLine(caret, false);
|
||||
CaretDataKt.setVimWasIsFirstLine(caret, false);
|
||||
}
|
||||
}
|
||||
|
||||
@ -332,7 +331,7 @@ public class ChangeGroup {
|
||||
* @return true if able to delete the text, false if not
|
||||
*/
|
||||
public boolean insertDeleteInsertedText(@NotNull Editor editor, @NotNull Caret caret) {
|
||||
int deleteTo = CaretData.getInsertStart(caret);
|
||||
int deleteTo = CaretDataKt.getVimInsertStart(caret);
|
||||
int offset = caret.getOffset();
|
||||
if (offset == deleteTo) {
|
||||
deleteTo = VimPlugin.getMotion().moveCaretToLineStartSkipLeading(editor, caret);
|
||||
@ -375,7 +374,7 @@ public class ChangeGroup {
|
||||
|
||||
final CaretModel caretModel = editor.getCaretModel();
|
||||
for (Caret caret : caretModel.getAllCarets()) {
|
||||
CaretData.setInsertStart(caret, caret.getOffset());
|
||||
CaretDataKt.setVimInsertStart(caret, caret.getOffset());
|
||||
if (caret == caretModel.getPrimaryCaret()) {
|
||||
VimPlugin.getMark().setMark(editor, MarkGroup.MARK_CHANGE_START, caret.getOffset());
|
||||
}
|
||||
@ -716,7 +715,7 @@ public class ChangeGroup {
|
||||
strokes.clear();
|
||||
repeatCharsCount = 0;
|
||||
for (Caret caret : editor.getCaretModel().getAllCarets()) {
|
||||
CaretData.setInsertStart(caret, caret.getOffset());
|
||||
CaretDataKt.setVimInsertStart(caret, caret.getOffset());
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -31,10 +31,10 @@ import com.maddyhome.idea.vim.command.SelectionType
|
||||
import com.maddyhome.idea.vim.command.VisualChange
|
||||
import com.maddyhome.idea.vim.common.TextRange
|
||||
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.vimLastColumn
|
||||
import com.maddyhome.idea.vim.helper.vimLastVisualOperatorRange
|
||||
import com.maddyhome.idea.vim.helper.vimSelectionStart
|
||||
import com.maddyhome.idea.vim.helper.vimSelectionStartSetToNull
|
||||
import com.maddyhome.idea.vim.helper.vimStartSelectionAtPoint
|
||||
@ -143,7 +143,7 @@ object VisualMotionGroup {
|
||||
return false
|
||||
}
|
||||
// FIXME: 2019-03-05 When there was no previous Visual operation [count] characters are selected.
|
||||
val range = CaretData.getLastVisualOperatorRange(editor.caretModel.primaryCaret) ?: return false
|
||||
val range = editor.caretModel.primaryCaret.vimLastVisualOperatorRange ?: return false
|
||||
val newSubMode = range.type.toSubMode()
|
||||
val start = editor.caretModel.offset
|
||||
val end = calculateVisualRange(editor, range, count)
|
||||
|
@ -31,11 +31,13 @@ import com.maddyhome.idea.vim.command.SelectionType
|
||||
import com.maddyhome.idea.vim.command.VisualChange
|
||||
import com.maddyhome.idea.vim.group.MotionGroup
|
||||
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.vimLastColumn
|
||||
import com.maddyhome.idea.vim.helper.vimLastVisualOperatorRange
|
||||
import com.maddyhome.idea.vim.helper.vimPreviousLastColumn
|
||||
import com.maddyhome.idea.vim.helper.vimSelectionStart
|
||||
import com.maddyhome.idea.vim.helper.vimVisualChange
|
||||
import com.maddyhome.idea.vim.helper.visualBlockRange
|
||||
|
||||
/**
|
||||
@ -121,8 +123,8 @@ abstract class VisualOperatorActionHandler : EditorActionHandlerBase(false) {
|
||||
|
||||
private fun startForCaret(caret: Caret) {
|
||||
if (CommandState.getInstance(editor).mode == CommandState.Mode.REPEAT) {
|
||||
CaretData.setPreviousLastColumn(caret, caret.vimLastColumn)
|
||||
val range = CaretData.getLastVisualOperatorRange(caret)
|
||||
caret.vimPreviousLastColumn = caret.vimLastColumn
|
||||
val range = caret.vimLastVisualOperatorRange
|
||||
VisualMotionGroup.toggleVisual(editor, 1, 1, CommandState.SubMode.NONE)
|
||||
if (range != null && range.columns == MotionGroup.LAST_COLUMN) {
|
||||
caret.vimLastColumn = MotionGroup.LAST_COLUMN
|
||||
@ -137,7 +139,7 @@ abstract class VisualOperatorActionHandler : EditorActionHandlerBase(false) {
|
||||
}
|
||||
logger.debug("change=$change")
|
||||
}
|
||||
CaretData.setVisualChange(caret, change)
|
||||
caret.vimVisualChange = change
|
||||
}
|
||||
|
||||
fun start() {
|
||||
@ -168,13 +170,13 @@ abstract class VisualOperatorActionHandler : EditorActionHandlerBase(false) {
|
||||
private fun finishForCaret(caret: Caret, res: Boolean) {
|
||||
if (CommandFlags.FLAG_MULTIKEY_UNDO !in cmd.flags && CommandFlags.FLAG_EXPECT_MORE !in cmd.flags) {
|
||||
if (wasRepeat) {
|
||||
caret.vimLastColumn = CaretData.getPreviousLastColumn(caret)
|
||||
caret.vimLastColumn = caret.vimPreviousLastColumn
|
||||
}
|
||||
}
|
||||
|
||||
if (res) {
|
||||
CaretData.getVisualChange(caret)?.let {
|
||||
CaretData.setLastVisualOperatorRange(caret, it)
|
||||
caret.vimVisualChange?.let {
|
||||
caret.vimLastVisualOperatorRange = it
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,132 +0,0 @@
|
||||
/*
|
||||
* IdeaVim - Vim emulator for IDEs based on the IntelliJ platform
|
||||
* Copyright (C) 2003-2019 The IdeaVim authors
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 2 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package com.maddyhome.idea.vim.helper;
|
||||
|
||||
import com.intellij.openapi.editor.Caret;
|
||||
import com.intellij.openapi.util.Key;
|
||||
import com.maddyhome.idea.vim.command.VisualChange;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
/**
|
||||
* This class is used to manipulate caret specific data. Each caret has a user defined map associated with it.
|
||||
* These methods provide convenient methods for working with that Vim Plugin specific data.
|
||||
*/
|
||||
public class CaretData {
|
||||
|
||||
/**
|
||||
* Gets the previous visual operator range on the caret.
|
||||
*/
|
||||
@Nullable
|
||||
public static VisualChange getLastVisualOperatorRange(@NotNull Caret caret) {
|
||||
return caret.getUserData(VISUAL_OP);
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the previous visual operator range on the caret.
|
||||
*/
|
||||
public static void setLastVisualOperatorRange(@NotNull Caret caret, @NotNull VisualChange range) {
|
||||
caret.putUserData(VISUAL_OP, range);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the previous last column (set by {@link com.maddyhome.idea.vim.handler.VisualOperatorActionHandler.VisualStartFinishWrapper}).
|
||||
*/
|
||||
public static int getPreviousLastColumn(@NotNull Caret caret) {
|
||||
@Nullable Integer ret = caret.getUserData(PREV_LAST_COLUMN);
|
||||
|
||||
|
||||
if (ret == null) {
|
||||
return caret.getLogicalPosition().column;
|
||||
}
|
||||
else {
|
||||
return ret;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the previous last column.
|
||||
*/
|
||||
public static void setPreviousLastColumn(@NotNull Caret caret, int prevLastColumn) {
|
||||
caret.putUserData(PREV_LAST_COLUMN, prevLastColumn);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the visual change for current visual operator action.
|
||||
*/
|
||||
@Nullable
|
||||
public static VisualChange getVisualChange(@NotNull Caret caret) {
|
||||
return caret.getUserData(VISUAL_CHANGE);
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the visual change for current visual operator action.
|
||||
*/
|
||||
public static void setVisualChange(@NotNull Caret caret, @Nullable VisualChange visualChange) {
|
||||
caret.putUserData(VISUAL_CHANGE, visualChange);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the insertion start for the caret
|
||||
*/
|
||||
public static int getInsertStart(@NotNull Caret caret) {
|
||||
Integer ret = caret.getUserData(INSERT_START);
|
||||
|
||||
if (ret == null) {
|
||||
return caret.getOffset();
|
||||
}
|
||||
else {
|
||||
return ret;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the insertion start for the caret
|
||||
*/
|
||||
public static void setInsertStart(@NotNull Caret caret, int insertStart) {
|
||||
caret.putUserData(INSERT_START, insertStart);
|
||||
}
|
||||
|
||||
/**
|
||||
* Determines whether a caret was in the first line before inserting a new line above.
|
||||
*/
|
||||
public static boolean wasInFirstLine(@NotNull Caret caret) {
|
||||
Boolean res = caret.getUserData(WAS_IN_FIRST_LINE);
|
||||
return (res != null) && res;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the flag determining that a caret was in the first line before inserting a new line above.
|
||||
*/
|
||||
public static void setWasInFirstLine(@NotNull Caret caret, boolean value) {
|
||||
caret.putUserData(WAS_IN_FIRST_LINE, value);
|
||||
}
|
||||
|
||||
/**
|
||||
* This class is completely static, no instances needed.
|
||||
*/
|
||||
private CaretData() {
|
||||
}
|
||||
|
||||
private static final Key<Integer> PREV_LAST_COLUMN = new Key<>("previousLastColumn");
|
||||
private static final Key<Integer> INSERT_START = new Key<>("insertStart");
|
||||
private static final Key<Boolean> WAS_IN_FIRST_LINE = new Key<>("wasInFirstLine");
|
||||
private static final Key<VisualChange> VISUAL_CHANGE = new Key<>("visualChange");
|
||||
private static final Key<VisualChange> VISUAL_OP = new Key<>("visualOp");
|
||||
}
|
@ -22,6 +22,7 @@ import com.intellij.openapi.editor.Caret
|
||||
import com.intellij.openapi.editor.Editor
|
||||
import com.intellij.openapi.util.Key
|
||||
import com.maddyhome.idea.vim.command.CommandState
|
||||
import com.maddyhome.idea.vim.command.VisualChange
|
||||
|
||||
/**
|
||||
* @author Alex Plate
|
||||
@ -49,6 +50,7 @@ fun Caret.vimSelectionStartSetToNull() {
|
||||
private var Caret._vimSelectionStart: Int? by userData()
|
||||
private var Editor._vimBlockSelectinoStart: Int? by userData()
|
||||
|
||||
|
||||
private val LAST_COLUMN: Key<Int> = Key.create("lastColumn")
|
||||
var Caret.vimLastColumn: Int
|
||||
get() = getUserData(LAST_COLUMN) ?: visualPosition.column
|
||||
@ -57,3 +59,9 @@ var Caret.vimLastColumn: Int
|
||||
} else {
|
||||
putUserData(LAST_COLUMN, value)
|
||||
}
|
||||
|
||||
var Caret.vimLastVisualOperatorRange: VisualChange? by userData()
|
||||
var Caret.vimVisualChange: VisualChange? by userData()
|
||||
var Caret.vimPreviousLastColumn: Int by userDataOr { (this as Caret).logicalPosition.column }
|
||||
var Caret.vimInsertStart: Int by userDataOr { (this as Caret).offset }
|
||||
var Caret.vimWasIsFirstLine: Boolean by userDataOr { false }
|
||||
|
@ -150,3 +150,23 @@ fun <T> userData(): ReadWriteProperty<UserDataHolder, T?> {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun <T> userDataOr(default: UserDataHolder.() -> T): ReadWriteProperty<UserDataHolder, T> {
|
||||
return object : ReadWriteProperty<UserDataHolder, T> {
|
||||
private var key: Key<T>? = null
|
||||
private fun getKey(property: KProperty<*>): Key<T> {
|
||||
if (key == null) {
|
||||
key = Key.create(property.name + "by userData()")
|
||||
}
|
||||
return key as Key<T>
|
||||
}
|
||||
|
||||
override fun getValue(thisRef: UserDataHolder, property: KProperty<*>): T {
|
||||
return thisRef.getUserData(getKey(property)) ?: thisRef.default()
|
||||
}
|
||||
|
||||
override fun setValue(thisRef: UserDataHolder, property: KProperty<*>, value: T) {
|
||||
thisRef.putUserData(getKey(property), value)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user