mirror of
https://github.com/chylex/IntelliJ-IdeaVim.git
synced 2025-02-26 05:46:00 +01:00
Move some methods of marks group to engine
This commit is contained in:
parent
8ecac1a8cf
commit
56afa6d564
src
main/java/com/maddyhome/idea/vim
action/motion/mark
group
newapi
vimscript/model/commands
test/java/org/jetbrains/plugins/ideavim/ex/implementation/commands
vim-engine/src/main/kotlin/com/maddyhome/idea/vim/mark
@ -24,6 +24,7 @@ import com.maddyhome.idea.vim.command.Argument
|
||||
import com.maddyhome.idea.vim.command.Command
|
||||
import com.maddyhome.idea.vim.command.OperatorArguments
|
||||
import com.maddyhome.idea.vim.handler.VimActionHandler
|
||||
import com.maddyhome.idea.vim.newapi.vim
|
||||
|
||||
class MotionMarkAction : VimActionHandler.SingleExecution() {
|
||||
override val type: Command.Type = Command.Type.OTHER_READONLY
|
||||
@ -32,6 +33,6 @@ class MotionMarkAction : VimActionHandler.SingleExecution() {
|
||||
|
||||
override fun execute(editor: Editor, context: DataContext, cmd: Command, operatorArguments: OperatorArguments): Boolean {
|
||||
val argument = cmd.argument
|
||||
return argument != null && VimPlugin.getMark().setMark(editor, argument.character)
|
||||
return argument != null && VimPlugin.getMark().setMark(editor.vim, argument.character)
|
||||
}
|
||||
}
|
||||
|
@ -437,7 +437,7 @@ public class ChangeGroup implements VimChangeGroup {
|
||||
UserDataManager
|
||||
.setVimInsertStart(caret, editor.getDocument().createRangeMarker(caret.getOffset(), caret.getOffset()));
|
||||
if (caret == caretModel.getPrimaryCaret()) {
|
||||
VimPlugin.getMark().setMark(editor, MARK_CHANGE_START, caret.getOffset());
|
||||
VimPlugin.getMark().setMark(new IjVimEditor(editor), MARK_CHANGE_START, caret.getOffset());
|
||||
}
|
||||
}
|
||||
|
||||
@ -553,8 +553,8 @@ public class ChangeGroup implements VimChangeGroup {
|
||||
// column offset.
|
||||
int offset = editor.getCaretModel().getPrimaryCaret().getOffset();
|
||||
final MarkGroup markGroup = VimPlugin.getMark();
|
||||
markGroup.setMark(editor, '^', offset);
|
||||
markGroup.setMark(editor, MARK_CHANGE_END, offset);
|
||||
markGroup.setMark(new IjVimEditor(editor), '^', offset);
|
||||
markGroup.setMark(new IjVimEditor(editor), MARK_CHANGE_END, offset);
|
||||
|
||||
if (CommandState.getInstance(new IjVimEditor(editor)).getMode() == CommandState.Mode.REPLACE) {
|
||||
setInsertEditorState(editor, true);
|
||||
@ -582,7 +582,7 @@ public class ChangeGroup implements VimChangeGroup {
|
||||
|
||||
// The change pos '.' mark is the offset AFTER processing escape, and after switching to overtype
|
||||
offset = editor.getCaretModel().getPrimaryCaret().getOffset();
|
||||
markGroup.setMark(editor, MARK_CHANGE_POS, offset);
|
||||
markGroup.setMark(new IjVimEditor(editor), MARK_CHANGE_POS, offset);
|
||||
|
||||
CommandState.getInstance(new IjVimEditor(editor)).popModes();
|
||||
exitAllSingleCommandInsertModes(editor);
|
||||
@ -1716,7 +1716,7 @@ public class ChangeGroup implements VimChangeGroup {
|
||||
editor.getDocument().insertString(offset, str);
|
||||
InlayHelperKt.moveToInlayAwareOffset(caret, offset + str.length());
|
||||
|
||||
VimPlugin.getMark().setMark(editor, MARK_CHANGE_POS, offset);
|
||||
VimPlugin.getMark().setMark(new IjVimEditor(editor), MARK_CHANGE_POS, offset);
|
||||
}
|
||||
|
||||
public void insertText(@NotNull Editor editor, @NotNull Caret caret, @NotNull String str) {
|
||||
@ -1756,7 +1756,7 @@ public class ChangeGroup implements VimChangeGroup {
|
||||
|
||||
final int newEnd = start + str.length();
|
||||
VimPlugin.getMark().setChangeMarks(new IjVimEditor(editor), new TextRange(start, newEnd));
|
||||
VimPlugin.getMark().setMark(editor, MARK_CHANGE_POS, newEnd);
|
||||
VimPlugin.getMark().setMark(new IjVimEditor(editor), MARK_CHANGE_POS, newEnd);
|
||||
}
|
||||
|
||||
public void indentRange(@NotNull Editor editor,
|
||||
@ -1890,7 +1890,7 @@ public class ChangeGroup implements VimChangeGroup {
|
||||
|
||||
if (type != null) {
|
||||
final int start = updatedRange.getStartOffset();
|
||||
VimPlugin.getMark().setMark(editor, MARK_CHANGE_POS, start);
|
||||
VimPlugin.getMark().setMark(new IjVimEditor(editor), MARK_CHANGE_POS, start);
|
||||
VimPlugin.getMark().setChangeMarks(new IjVimEditor(editor), new TextRange(start, start + 1));
|
||||
}
|
||||
|
||||
|
@ -69,7 +69,7 @@ public class MarkGroup extends VimMarkGroupBase implements PersistentStateCompon
|
||||
public void editorReleased(@NotNull EditorFactoryEvent event) {
|
||||
// Save off the last caret position of the file before it is closed
|
||||
Editor editor = event.getEditor();
|
||||
setMark(editor, '"', editor.getCaretModel().getOffset());
|
||||
setMark(new IjVimEditor(editor), '"', editor.getCaretModel().getOffset());
|
||||
}
|
||||
|
||||
/**
|
||||
@ -84,7 +84,7 @@ public class MarkGroup extends VimMarkGroupBase implements PersistentStateCompon
|
||||
|
||||
public void saveJumpLocation(@NotNull Editor editor) {
|
||||
addJump(new IjVimEditor(editor), true);
|
||||
setMark(editor, '\'');
|
||||
setMark(new IjVimEditor(editor), '\'');
|
||||
|
||||
Project project = editor.getProject();
|
||||
if (project != null) {
|
||||
@ -114,62 +114,14 @@ public class MarkGroup extends VimMarkGroupBase implements PersistentStateCompon
|
||||
return mark;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the specified mark to the caret position of the editor
|
||||
*
|
||||
* @param editor The editor to get the current position from
|
||||
* @param ch The mark set set
|
||||
* @return True if a valid, writable mark, false if not
|
||||
*/
|
||||
public boolean setMark(@NotNull Editor editor, char ch) {
|
||||
return VALID_SET_MARKS.indexOf(ch) >= 0 && setMark(editor, ch, editor.getCaretModel().getOffset());
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the specified mark to the specified location.
|
||||
*
|
||||
* @param editor The editor the mark is associated with
|
||||
* @param ch The mark to set
|
||||
* @param offset The offset to set the mark to
|
||||
* @return true if able to set the mark, false if not
|
||||
*/
|
||||
public boolean setMark(@NotNull Editor editor, char ch, int offset) {
|
||||
if (ch == '`') ch = '\'';
|
||||
LogicalPosition lp = editor.offsetToLogicalPosition(offset);
|
||||
|
||||
final VirtualFile vf = EditorHelper.getVirtualFile(editor);
|
||||
if (vf == null) {
|
||||
return false;
|
||||
@Override
|
||||
public @Nullable Mark createSystemMark(char ch, int line, int col, @NotNull VimEditor editor) {
|
||||
Editor ijEditor = ((IjVimEditor)editor).getEditor();
|
||||
@Nullable LineBookmark systemMark = SystemMarks.createOrGetSystemMark(ch, line, ijEditor);
|
||||
if (systemMark == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
// File specific marks get added to the file
|
||||
if (FILE_MARKS.indexOf(ch) >= 0) {
|
||||
HashMap<Character, Mark> fmarks = getFileMarks(editor.getDocument());
|
||||
if (fmarks == null) return false;
|
||||
|
||||
Mark mark = new VimMark(ch, lp.line, lp.column, vf.getPath(), extractProtocol(vf));
|
||||
fmarks.put(ch, mark);
|
||||
}
|
||||
// Global marks get set to both the file and the global list of marks
|
||||
else if (GLOBAL_MARKS.indexOf(ch) >= 0) {
|
||||
HashMap<Character, Mark> fmarks = getFileMarks(editor.getDocument());
|
||||
if (fmarks == null) return false;
|
||||
|
||||
@Nullable LineBookmark systemMark = SystemMarks.createOrGetSystemMark(ch, lp.line, editor);
|
||||
Mark mark;
|
||||
if (systemMark != null) {
|
||||
mark = new IntellijMark(systemMark, lp.column, editor.getProject());
|
||||
} else {
|
||||
mark = new VimMark(ch, lp.line, lp.column, vf.getPath(), extractProtocol(vf));
|
||||
}
|
||||
fmarks.put(ch, mark);
|
||||
Mark oldMark = globalMarks.put(ch, mark);
|
||||
if (oldMark instanceof VimMark) {
|
||||
oldMark.clear();
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
return new IntellijMark(systemMark, col, ijEditor.getProject());
|
||||
}
|
||||
|
||||
public static String extractProtocol(@NotNull VirtualFile vf) {
|
||||
@ -177,15 +129,14 @@ public class MarkGroup extends VimMarkGroupBase implements PersistentStateCompon
|
||||
}
|
||||
|
||||
public void setVisualSelectionMarks(@NotNull Editor editor, @NotNull TextRange range) {
|
||||
setMark(editor, MARK_VISUAL_START, range.getStartOffset());
|
||||
setMark(editor, MARK_VISUAL_END, range.getEndOffset());
|
||||
setMark(new IjVimEditor(editor), MARK_VISUAL_START, range.getStartOffset());
|
||||
setMark(new IjVimEditor(editor), MARK_VISUAL_END, range.getEndOffset());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setChangeMarks(@NotNull VimEditor vimEditor, @NotNull TextRange range) {
|
||||
Editor editor = ((IjVimEditor)vimEditor).getEditor();
|
||||
setMark(editor, MARK_CHANGE_START, range.getStartOffset());
|
||||
setMark(editor, MARK_CHANGE_END, range.getEndOffset()-1);
|
||||
setMark(vimEditor, MARK_CHANGE_START, range.getStartOffset());
|
||||
setMark(vimEditor, MARK_CHANGE_END, range.getEndOffset()-1);
|
||||
}
|
||||
|
||||
public @Nullable TextRange getChangeMarks(@NotNull Editor editor) {
|
||||
@ -313,7 +264,7 @@ public class MarkGroup extends VimMarkGroupBase implements PersistentStateCompon
|
||||
Element fileMarksElem = new Element("filemarks");
|
||||
|
||||
List<FileMarks<Character, Mark>> files = new ArrayList<>(fileMarks.values());
|
||||
files.sort(Comparator.comparing(o -> o.getMyTimestamp()));
|
||||
files.sort(Comparator.comparing(FileMarks<Character, Mark>::getMyTimestamp));
|
||||
|
||||
if (files.size() > SAVE_MARK_COUNT) {
|
||||
files = files.subList(files.size() - SAVE_MARK_COUNT, files.size());
|
||||
|
@ -170,7 +170,7 @@ class PutGroup {
|
||||
var text = data.textData?.rawText ?: run {
|
||||
if (data.visualSelection != null) {
|
||||
val offset = editor.caretModel.primaryCaret.offset
|
||||
VimPlugin.getMark().setMark(editor, MARK_CHANGE_POS, offset)
|
||||
VimPlugin.getMark().setMark(editor.vim, MARK_CHANGE_POS, offset)
|
||||
VimPlugin.getMark().setChangeMarks(editor.vim, TextRange(offset, offset + 1))
|
||||
}
|
||||
return null
|
||||
@ -386,7 +386,7 @@ class PutGroup {
|
||||
startOffset + text.text.length
|
||||
) else startOffset + text.text.length
|
||||
VimPlugin.getMark().setChangeMarks(editor.vim, TextRange(startOffset, endOffset))
|
||||
VimPlugin.getMark().setMark(editor, MARK_CHANGE_POS, startOffset)
|
||||
VimPlugin.getMark().setMark(editor.vim, MARK_CHANGE_POS, startOffset)
|
||||
moveCaretToEndPosition(
|
||||
editor,
|
||||
caret,
|
||||
|
@ -56,7 +56,7 @@ class VimMachineImpl : VimMachine {
|
||||
editor.delete(range)
|
||||
|
||||
val start = normalizedRange.startOffset
|
||||
VimPlugin.getMark().setMark(editor.editor, MARK_CHANGE_POS, start)
|
||||
VimPlugin.getMark().setMark(editor, MARK_CHANGE_POS, start)
|
||||
VimPlugin.getMark().setChangeMarks(editor, TextRange(start, start + 1))
|
||||
|
||||
return operatedText
|
||||
|
@ -25,6 +25,7 @@ import com.maddyhome.idea.vim.ex.ranges.Ranges
|
||||
import com.maddyhome.idea.vim.helper.EditorHelper
|
||||
import com.maddyhome.idea.vim.helper.MessageHelper
|
||||
import com.maddyhome.idea.vim.helper.Msg
|
||||
import com.maddyhome.idea.vim.newapi.vim
|
||||
import com.maddyhome.idea.vim.vimscript.model.ExecutionResult
|
||||
|
||||
/**
|
||||
@ -39,7 +40,7 @@ data class MarkCommand(val ranges: Ranges, val argument: String) : Command.Singl
|
||||
val offset = EditorHelper.getLineStartOffset(editor, line)
|
||||
|
||||
val result = if (mark.isLetter() || mark in "'`") {
|
||||
VimPlugin.getMark().setMark(editor, mark, offset)
|
||||
VimPlugin.getMark().setMark(editor.vim, mark, offset)
|
||||
} else {
|
||||
VimPlugin.showMessage(MessageHelper.message(Msg.E191))
|
||||
false
|
||||
|
@ -51,7 +51,7 @@ class DeleteMarksCommandTest : VimTestCase() {
|
||||
|
||||
marks.forEachIndexed { index, c ->
|
||||
VimPlugin.getMark()
|
||||
.setMark(myFixture.editor, c, myFixture.editor.logicalPositionToOffset(LogicalPosition(index, 0)))
|
||||
.setMark(myFixture.editor.vim, c, myFixture.editor.logicalPositionToOffset(LogicalPosition(index, 0)))
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -20,6 +20,7 @@ package org.jetbrains.plugins.ideavim.ex.implementation.commands
|
||||
|
||||
import com.maddyhome.idea.vim.VimPlugin
|
||||
import com.maddyhome.idea.vim.helper.StringHelper.parseKeys
|
||||
import com.maddyhome.idea.vim.newapi.vim
|
||||
import org.jetbrains.plugins.ideavim.VimTestCase
|
||||
|
||||
class MarksCommandTest : VimTestCase() {
|
||||
@ -197,7 +198,7 @@ class MarksCommandTest : VimTestCase() {
|
||||
|hard by the torrent of a mountain pass.
|
||||
""".trimMargin()
|
||||
)
|
||||
VimPlugin.getMark().setMark(myFixture.editor, 'a', 100000)
|
||||
VimPlugin.getMark().setMark(myFixture.editor.vim, 'a', 100000)
|
||||
enterCommand("marks")
|
||||
assertExOutput(
|
||||
"""mark line col file/text
|
||||
|
@ -24,4 +24,24 @@ interface VimMarkGroup {
|
||||
* @return The jump or null if out of range.
|
||||
*/
|
||||
fun getJump(count: Int): Jump?
|
||||
fun createSystemMark(ch: Char, line: Int, col: Int, editor: VimEditor): Mark?
|
||||
|
||||
/**
|
||||
* Sets the specified mark to the specified location.
|
||||
*
|
||||
* @param editor The editor the mark is associated with
|
||||
* @param ch The mark to set
|
||||
* @param offset The offset to set the mark to
|
||||
* @return true if able to set the mark, false if not
|
||||
*/
|
||||
fun setMark(editor: VimEditor, ch: Char, offset: Int): Boolean
|
||||
|
||||
/**
|
||||
* Sets the specified mark to the caret position of the editor
|
||||
*
|
||||
* @param editor The editor to get the current position from
|
||||
* @param ch The mark set set
|
||||
* @return True if a valid, writable mark, false if not
|
||||
*/
|
||||
fun setMark(editor: VimEditor, ch: Char): Boolean
|
||||
}
|
||||
|
@ -148,4 +148,57 @@ abstract class VimMarkGroupBase : VimMarkGroup {
|
||||
jumps[index]
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the specified mark to the specified location.
|
||||
*
|
||||
* @param editor The editor the mark is associated with
|
||||
* @param ch The mark to set
|
||||
* @param offset The offset to set the mark to
|
||||
* @return true if able to set the mark, false if not
|
||||
*/
|
||||
override fun setMark(editor: VimEditor, ch: Char, offset: Int): Boolean {
|
||||
var myCh = ch
|
||||
if (myCh == '`') myCh = '\''
|
||||
val lp = editor.offsetToLogicalPosition(offset)
|
||||
|
||||
val path = editor.getPath() ?: return false
|
||||
|
||||
// File specific marks get added to the file
|
||||
if (VimMarkConstants.FILE_MARKS.indexOf(myCh) >= 0) {
|
||||
val fmarks = getFileMarks(path)
|
||||
|
||||
val mark = VimMark(myCh, lp.line, lp.column, path, editor.extractProtocol())
|
||||
fmarks[myCh] = mark
|
||||
} else if (VimMarkConstants.GLOBAL_MARKS.indexOf(myCh) >= 0) {
|
||||
val fmarks = getFileMarks(path)
|
||||
|
||||
var mark = createSystemMark(myCh, lp.line, lp.column, editor)
|
||||
if (mark == null) {
|
||||
mark = VimMark(myCh, lp.line, lp.column, path, editor.extractProtocol())
|
||||
}
|
||||
fmarks[myCh] = mark
|
||||
val oldMark = globalMarks.put(myCh, mark)
|
||||
if (oldMark is VimMark) {
|
||||
oldMark.clear()
|
||||
}
|
||||
}// Global marks get set to both the file and the global list of marks
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the specified mark to the caret position of the editor
|
||||
*
|
||||
* @param editor The editor to get the current position from
|
||||
* @param ch The mark set set
|
||||
* @return True if a valid, writable mark, false if not
|
||||
*/
|
||||
override fun setMark(editor: VimEditor, ch: Char): Boolean {
|
||||
return VimMarkConstants.VALID_SET_MARKS.indexOf(ch) >= 0 && setMark(
|
||||
editor,
|
||||
ch,
|
||||
editor.primaryCaret().offset.point
|
||||
)
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user