1
0
mirror of https://github.com/chylex/IntelliJ-IdeaVim.git synced 2025-03-07 21:32:52 +01:00

Use userData() for managing editor's user data

This commit is contained in:
Alex Plate 2019-06-25 16:48:46 +03:00
parent 000ebfaf2f
commit db77d133dc
No known key found for this signature in database
GPG Key ID: 0B97153C8FFEC09F
25 changed files with 179 additions and 355 deletions

View File

@ -37,8 +37,8 @@ import com.maddyhome.idea.vim.VimPlugin;
import com.maddyhome.idea.vim.action.change.insert.InsertExitModeAction;
import com.maddyhome.idea.vim.command.CommandState;
import com.maddyhome.idea.vim.helper.CommandStateHelper;
import com.maddyhome.idea.vim.helper.EditorData;
import com.maddyhome.idea.vim.helper.EditorDataContext;
import com.maddyhome.idea.vim.helper.EditorHelper;
import com.maddyhome.idea.vim.key.ShortcutOwner;
import com.maddyhome.idea.vim.option.ListOption;
import com.maddyhome.idea.vim.option.OptionsManager;
@ -174,7 +174,7 @@ public class VimShortcutKeyAction extends AnAction implements DumbAware {
return false;
}
// Debug watch, Python console, etc.
if (NON_FILE_EDITOR_KEYS.contains(keyStroke) && !EditorData.isFileEditor(editor)) {
if (NON_FILE_EDITOR_KEYS.contains(keyStroke) && !EditorHelper.isFileEditor(editor)) {
return false;
}
}
@ -215,7 +215,7 @@ public class VimShortcutKeyAction extends AnAction implements DumbAware {
private boolean isEnabledForEscape(@NotNull Editor editor) {
final CommandState.Mode mode = CommandState.getInstance(editor).getMode();
return isPrimaryEditor(editor) || (EditorData.isFileEditor(editor) && mode != CommandState.Mode.COMMAND);
return isPrimaryEditor(editor) || (EditorHelper.isFileEditor(editor) && mode != CommandState.Mode.COMMAND);
}
/**

View File

@ -28,7 +28,7 @@ import com.maddyhome.idea.vim.command.Command;
import com.maddyhome.idea.vim.command.CommandFlags;
import com.maddyhome.idea.vim.command.MappingMode;
import com.maddyhome.idea.vim.handler.MotionActionHandler;
import com.maddyhome.idea.vim.helper.CaretDataKt;
import com.maddyhome.idea.vim.helper.UserDataManager;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
@ -76,7 +76,7 @@ public class MotionColumnAction extends MotionEditorAction {
@NotNull Caret caret,
@NotNull DataContext context,
@NotNull Command cmd) {
CaretDataKt.setVimLastColumn(caret, cmd.getCount() - 1);
UserDataManager.setVimLastColumn(caret, cmd.getCount() - 1);
}
};
}

View File

@ -26,8 +26,8 @@ import com.maddyhome.idea.vim.action.MotionEditorAction;
import com.maddyhome.idea.vim.command.*;
import com.maddyhome.idea.vim.group.MotionGroup;
import com.maddyhome.idea.vim.handler.MotionActionHandler;
import com.maddyhome.idea.vim.helper.CaretDataKt;
import com.maddyhome.idea.vim.helper.CommandStateHelper;
import com.maddyhome.idea.vim.helper.UserDataManager;
import com.maddyhome.idea.vim.option.BoundStringOption;
import com.maddyhome.idea.vim.option.OptionsManager;
import org.jetbrains.annotations.NotNull;
@ -86,7 +86,7 @@ public class MotionLastScreenColumnAction extends MotionEditorAction {
@NotNull Caret caret,
@NotNull DataContext context,
@NotNull Command cmd) {
CaretDataKt.setVimLastColumn(caret, MotionGroup.LAST_COLUMN);
UserDataManager.setVimLastColumn(caret, MotionGroup.LAST_COLUMN);
}
};
}

View File

@ -20,7 +20,7 @@ package com.maddyhome.idea.vim.command;
import com.intellij.openapi.editor.Editor;
import com.maddyhome.idea.vim.VimPlugin;
import com.maddyhome.idea.vim.helper.EditorData;
import com.maddyhome.idea.vim.helper.UserDataManager;
import com.maddyhome.idea.vim.key.ParentNode;
import com.maddyhome.idea.vim.option.NumberOption;
import com.maddyhome.idea.vim.option.OptionsManager;
@ -64,10 +64,10 @@ public class CommandState {
return new CommandState();
}
CommandState res = EditorData.getCommandState(editor);
CommandState res = UserDataManager.getVimCommandState(editor);
if (res == null) {
res = new CommandState();
EditorData.setCommandState(editor, res);
UserDataManager.setVimCommandState(editor, res);
}
return res;

View File

@ -20,7 +20,7 @@ package com.maddyhome.idea.vim.ex;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.editor.Editor;
import com.maddyhome.idea.vim.helper.EditorData;
import com.maddyhome.idea.vim.helper.UserDataManager;
import com.maddyhome.idea.vim.ui.ExOutputPanel;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
@ -38,10 +38,10 @@ public class ExOutputModel {
@NotNull
public static ExOutputModel getInstance(@NotNull Editor editor) {
ExOutputModel model = EditorData.getExOutputModel(editor);
ExOutputModel model = UserDataManager.getVimExOutput(editor);
if (model == null) {
model = new ExOutputModel(editor);
EditorData.setExOutputModel(editor, model);
UserDataManager.setVimExOutput(editor, model);
}
return model;
}

View File

@ -26,7 +26,6 @@ import com.maddyhome.idea.vim.ex.ExCommand
import com.maddyhome.idea.vim.ex.ExOutputModel
import com.maddyhome.idea.vim.ex.commands
import com.maddyhome.idea.vim.ex.flags
import com.maddyhome.idea.vim.helper.EditorData
import com.maddyhome.idea.vim.helper.EditorHelper
import com.maddyhome.idea.vim.helper.StringHelper.stringToKeys
import com.maddyhome.idea.vim.helper.StringHelper.toKeyNotation
@ -51,7 +50,7 @@ class JumpsHandler : CommandHandler.SingleExecution() {
text.append((jump.col + 1).toString().padStart(3))
text.append(" ")
val vf = EditorData.getVirtualFile(editor)
val vf = EditorHelper.getVirtualFile(editor)
if (vf != null && vf.path == jump.filename) {
text.append(toKeyNotation(stringToKeys(EditorHelper.getLineText(editor, jump.logicalLine).trim())))
} else {

View File

@ -28,7 +28,6 @@ import com.maddyhome.idea.vim.ex.ExCommand
import com.maddyhome.idea.vim.ex.ExOutputModel
import com.maddyhome.idea.vim.ex.commands
import com.maddyhome.idea.vim.ex.flags
import com.maddyhome.idea.vim.helper.EditorData
import com.maddyhome.idea.vim.helper.EditorHelper
import com.maddyhome.idea.vim.helper.StringHelper.stringToKeys
import com.maddyhome.idea.vim.helper.StringHelper.toKeyNotation
@ -54,7 +53,7 @@ class MarksHandler : CommandHandler.SingleExecution() {
text.append(num.padStart(3))
text.append(" ")
val vf = EditorData.getVirtualFile(editor)
val vf = EditorHelper.getVirtualFile(editor)
if (vf != null && vf.path == mark.filename) {
text.append(toKeyNotation(stringToKeys(EditorHelper.getLineText(editor, mark.logicalLine).trim())))
} else {

View File

@ -196,7 +196,7 @@ public class ChangeGroup {
MotionGroup.moveCaret(editor, caret, VimPlugin.getMotion().moveCaretToLineEnd(editor, caret));
}
EditorData.setChangeSwitchMode(editor, CommandState.Mode.INSERT);
UserDataManager.setVimChangeActionSwitchMode(editor, CommandState.Mode.INSERT);
insertText(editor, caret, "\n" + IndentConfig.create(editor).createIndentBySize(col));
if (firstLiner) {
@ -233,7 +233,7 @@ public class ChangeGroup {
if (editor.isOneLineMode()) return;
MotionGroup.moveCaret(editor, caret, VimPlugin.getMotion().moveCaretToLineEnd(editor, caret));
EditorData.setChangeSwitchMode(editor, CommandState.Mode.INSERT);
UserDataManager.setVimChangeActionSwitchMode(editor, CommandState.Mode.INSERT);
insertText(editor, caret, "\n" + IndentConfig.create(editor).createIndentBySize(col));
}
@ -315,7 +315,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 = CaretDataKt.getVimInsertStart(caret).getStartOffset();
int deleteTo = UserDataManager.getVimInsertStart(caret).getStartOffset();
int offset = caret.getOffset();
if (offset == deleteTo) {
deleteTo = VimPlugin.getMotion().moveCaretToLineStartSkipLeading(editor, caret);
@ -358,7 +358,7 @@ public class ChangeGroup {
final CaretModel caretModel = editor.getCaretModel();
for (Caret caret : caretModel.getAllCarets()) {
CaretDataKt.setVimInsertStart(caret, editor.getDocument().createRangeMarker(caret.getOffset(), caret.getOffset()));
UserDataManager.setVimInsertStart(caret, editor.getDocument().createRangeMarker(caret.getOffset(), caret.getOffset()));
if (caret == caretModel.getPrimaryCaret()) {
VimPlugin.getMark().setMark(editor, MarkGroup.MARK_CHANGE_START, caret.getOffset());
}
@ -411,15 +411,15 @@ public class ChangeGroup {
public void editorCreated(@NotNull EditorFactoryEvent event) {
final Editor editor = event.getEditor();
eventFacade.addEditorMouseListener(editor, listener);
EditorData.setChangeGroup(editor, true);
UserDataManager.setVimChangeGroup(editor, true);
}
@Override
public void editorReleased(@NotNull EditorFactoryEvent event) {
final Editor editor = event.getEditor();
if (EditorData.getChangeGroup(editor)) {
if (UserDataManager.getVimChangeGroup(editor)) {
eventFacade.removeEditorMouseListener(editor, listener);
EditorData.setChangeGroup(editor, false);
UserDataManager.setVimChangeGroup(editor, false);
}
}
@ -702,7 +702,7 @@ public class ChangeGroup {
strokes.clear();
repeatCharsCount = 0;
for (Caret caret : editor.getCaretModel().getAllCarets()) {
CaretDataKt.setVimInsertStart(caret, editor.getDocument().createRangeMarker(caret.getOffset(), caret.getOffset()));
UserDataManager.setVimInsertStart(caret, editor.getDocument().createRangeMarker(caret.getOffset(), caret.getOffset()));
}
}
@ -1173,7 +1173,7 @@ public class ChangeGroup {
boolean res = deleteCharacter(editor, caret, count, true);
if (res) {
EditorData.setChangeSwitchMode(editor, CommandState.Mode.INSERT);
UserDataManager.setVimChangeActionSwitchMode(editor, CommandState.Mode.INSERT);
}
return res;
@ -1223,7 +1223,7 @@ public class ChangeGroup {
boolean res = deleteEndOfLine(editor, caret, count);
if (res) {
MotionGroup.moveCaret(editor, caret, VimPlugin.getMotion().moveCaretToLineEnd(editor, caret));
EditorData.setChangeSwitchMode(editor, CommandState.Mode.INSERT);
UserDataManager.setVimChangeActionSwitchMode(editor, CommandState.Mode.INSERT);
}
return res;
@ -1265,7 +1265,7 @@ public class ChangeGroup {
if (wordMotions.contains(id) && lastWordChar && motion.getCount() == 1) {
final boolean res = deleteCharacter(editor, caret, 1, true);
if (res) {
EditorData.setChangeSwitchMode(editor, CommandState.Mode.INSERT);
UserDataManager.setVimChangeActionSwitchMode(editor, CommandState.Mode.INSERT);
}
return res;
}
@ -1316,7 +1316,7 @@ public class ChangeGroup {
boolean res = deleteMotion(editor, caret, context, count, rawCount, argument, true);
if (res) {
EditorData.setChangeSwitchMode(editor, CommandState.Mode.INSERT);
UserDataManager.setVimChangeActionSwitchMode(editor, CommandState.Mode.INSERT);
}
return res;
@ -1372,7 +1372,7 @@ public class ChangeGroup {
}
else if (append) {
column += range.getMaxLength();
if (CaretDataKt.getVimLastColumn(caret) == MotionGroup.LAST_COLUMN) {
if (UserDataManager.getVimLastColumn(caret) == MotionGroup.LAST_COLUMN) {
column = MotionGroup.LAST_COLUMN;
}
}
@ -1476,7 +1476,7 @@ public class ChangeGroup {
if (type == SelectionType.BLOCK_WISE) {
lines = getLinesCountInVisualBlock(editor, range);
col = editor.offsetToLogicalPosition(range.getStartOffset()).column;
if (CaretDataKt.getVimLastColumn(caret) == MotionGroup.LAST_COLUMN) {
if (UserDataManager.getVimLastColumn(caret) == MotionGroup.LAST_COLUMN) {
col = MotionGroup.LAST_COLUMN;
}
}
@ -1502,7 +1502,7 @@ public class ChangeGroup {
if (type == SelectionType.BLOCK_WISE) {
setInsertRepeat(lines, col, false);
}
EditorData.setChangeSwitchMode(editor, CommandState.Mode.INSERT);
UserDataManager.setVimChangeActionSwitchMode(editor, CommandState.Mode.INSERT);
}
}
@ -1717,7 +1717,7 @@ public class ChangeGroup {
}
}
CaretDataKt.setVimLastColumn(caret, caret.getVisualPosition().column);
UserDataManager.setVimLastColumn(caret, caret.getVisualPosition().column);
}
/**

View File

@ -87,14 +87,13 @@ public class EditorGroup {
isBlockCursor = editor.getSettings().isBlockCursor();
isAnimatedScrolling = editor.getSettings().isAnimatedScrolling();
isRefrainFromScrolling = editor.getSettings().isRefrainFromScrolling();
EditorData.initializeEditor(editor);
DocumentManager.getInstance().addListeners(editor.getDocument());
VimPlugin.getKey().registerRequiredShortcutKeys(editor);
if (VimPlugin.isEnabled()) {
initLineNumbers(editor);
// Turn on insert mode if editor doesn't have any file
if (!EditorData.isFileEditor(editor) &&
if (!EditorHelper.isFileEditor(editor) &&
editor.getDocument().isWritable() &&
!CommandStateHelper.inInsertMode(editor)) {
VimPlugin.getChange().insertBeforeCursor(editor, new EditorDataContext(editor));
@ -110,7 +109,7 @@ public class EditorGroup {
public void editorReleased(@NotNull EditorFactoryEvent event) {
final Editor editor = event.getEditor();
deinitLineNumbers(editor);
EditorData.unInitializeEditor(editor);
UserDataManager.unInitializeEditor(editor);
VimPlugin.getKey().unregisterShortcutKeys(editor);
editor.getSettings().setAnimatedScrolling(isAnimatedScrolling);
editor.getSettings().setRefrainFromScrolling(isRefrainFromScrolling);
@ -125,7 +124,7 @@ public class EditorGroup {
setRefrainFromScrolling(REFRAIN_FROM_SCROLLING_VIM_VALUE);
for (Editor editor : EditorFactory.getInstance().getAllEditors()) {
if (!EditorData.getEditorGroup(editor)) {
if (!UserDataManager.getVimEditorGroup(editor)) {
initLineNumbers(editor);
}
}
@ -143,27 +142,27 @@ public class EditorGroup {
private void initLineNumbers(@NotNull final Editor editor) {
editor.getCaretModel().addCaretListener(myLineNumbersCaretListener);
EditorData.setEditorGroup(editor, true);
UserDataManager.setVimEditorGroup(editor, true);
final EditorSettings settings = editor.getSettings();
EditorData.setLineNumbersShown(editor, settings.isLineNumbersShown());
UserDataManager.setVimLineNumbersShown(editor, settings.isLineNumbersShown());
updateLineNumbers(editor);
}
private void deinitLineNumbers(@NotNull Editor editor) {
editor.getCaretModel().removeCaretListener(myLineNumbersCaretListener);
EditorData.setEditorGroup(editor, false);
UserDataManager.setVimEditorGroup(editor, false);
editor.getGutter().closeAllAnnotations();
final Project project = editor.getProject();
if (project == null || project.isDisposed()) return;
editor.getSettings().setLineNumbersShown(EditorData.isLineNumbersShown(editor));
editor.getSettings().setLineNumbersShown(UserDataManager.getVimLineNumbersShown(editor));
}
private void updateLineNumbers(@NotNull Editor editor) {
if (!EditorData.isFileEditor(editor)) {
if (!EditorHelper.isFileEditor(editor)) {
return;
}
@ -171,7 +170,7 @@ public class EditorGroup {
final boolean lineNumber = OptionsManager.INSTANCE.getNumber().isSet();
final EditorSettings settings = editor.getSettings();
final boolean showEditorLineNumbers = (EditorData.isLineNumbersShown(editor) || lineNumber) && !relativeLineNumber;
final boolean showEditorLineNumbers = (UserDataManager.getVimLineNumbersShown(editor) || lineNumber) && !relativeLineNumber;
if (settings.isLineNumbersShown() ^ showEditorLineNumbers) {
// Update line numbers later since it may be called from a caret listener
@ -276,7 +275,7 @@ public class EditorGroup {
@Nullable
@Override
public String getLineText(int line, @NotNull Editor editor) {
if (VimPlugin.isEnabled() && EditorData.isFileEditor(editor)) {
if (VimPlugin.isEnabled() && EditorHelper.isFileEditor(editor)) {
final boolean relativeLineNumber = OptionsManager.INSTANCE.getRelativenumber().isSet();
final boolean lineNumber = OptionsManager.INSTANCE.getNumber().isSet();
if (relativeLineNumber && lineNumber && isCaretLine(line, editor)) {

View File

@ -41,7 +41,6 @@ import com.maddyhome.idea.vim.KeyHandler;
import com.maddyhome.idea.vim.VimPlugin;
import com.maddyhome.idea.vim.command.CommandState;
import com.maddyhome.idea.vim.common.TextRange;
import com.maddyhome.idea.vim.helper.EditorData;
import com.maddyhome.idea.vim.helper.EditorHelper;
import com.maddyhome.idea.vim.helper.SearchHelper;
import com.maddyhome.idea.vim.helper.StringHelper;
@ -153,7 +152,7 @@ public class FileGroup {
if (project != null) {
final FileEditorManagerEx fileEditorManager = FileEditorManagerEx.getInstanceEx(project);
final EditorWindow window = fileEditorManager.getCurrentWindow();
final VirtualFile virtualFile = EditorData.getVirtualFile(editor);
final VirtualFile virtualFile = EditorHelper.getVirtualFile(editor);
if (virtualFile != null) {
window.closeFile(virtualFile);
@ -349,7 +348,7 @@ public class FileGroup {
public void displayFileInfo(@NotNull Editor editor, boolean fullPath) {
StringBuilder msg = new StringBuilder();
VirtualFile vf = EditorData.getVirtualFile(editor);
VirtualFile vf = EditorHelper.getVirtualFile(editor);
if (vf != null) {
msg.append('"');
if (fullPath) {

View File

@ -44,7 +44,6 @@ import com.maddyhome.idea.vim.command.CommandState;
import com.maddyhome.idea.vim.common.Jump;
import com.maddyhome.idea.vim.common.Mark;
import com.maddyhome.idea.vim.common.TextRange;
import com.maddyhome.idea.vim.helper.EditorData;
import com.maddyhome.idea.vim.helper.EditorHelper;
import com.maddyhome.idea.vim.helper.SearchHelper;
import com.maddyhome.idea.vim.option.OptionsManager;
@ -103,7 +102,7 @@ public class MarkGroup {
// Make sure this is a valid mark
if (VALID_GET_MARKS.indexOf(ch) < 0) return null;
VirtualFile vf = EditorData.getVirtualFile(editor);
VirtualFile vf = EditorHelper.getVirtualFile(editor);
if ("{}".indexOf(ch) >= 0 && vf != null) {
int offset = SearchHelper.findNextParagraph(editor, editor.getCaretModel().getPrimaryCaret(), ch == '{' ? -1 : 1,
false);
@ -205,7 +204,7 @@ public class MarkGroup {
if (ch == '`') ch = '\'';
LogicalPosition lp = editor.offsetToLogicalPosition(offset);
final VirtualFile vf = EditorData.getVirtualFile(editor);
final VirtualFile vf = EditorHelper.getVirtualFile(editor);
if (vf == null) {
return false;
}
@ -245,7 +244,7 @@ public class MarkGroup {
Bookmark bookmark = bookmarkManager.findEditorBookmark(editor.getDocument(), line);
if (bookmark != null && bookmark.getMnemonic() == ch) return;
final VirtualFile virtualFile = EditorData.getVirtualFile(editor);
final VirtualFile virtualFile = EditorHelper.getVirtualFile(editor);
if (virtualFile == null) return;
bookmark = bookmarkManager.addTextBookmark(virtualFile, line, "");
bookmarkManager.setMnemonic(bookmark, ch);
@ -302,7 +301,7 @@ public class MarkGroup {
}
private void addJump(@NotNull Editor editor, int offset, boolean reset) {
final VirtualFile vf = EditorData.getVirtualFile(editor);
final VirtualFile vf = EditorHelper.getVirtualFile(editor);
if (vf == null) {
return;
}

View File

@ -47,7 +47,10 @@ import com.maddyhome.idea.vim.common.Mark;
import com.maddyhome.idea.vim.common.TextRange;
import com.maddyhome.idea.vim.ex.ExOutputModel;
import com.maddyhome.idea.vim.group.visual.VisualGroupKt;
import com.maddyhome.idea.vim.helper.*;
import com.maddyhome.idea.vim.helper.CommandStateHelper;
import com.maddyhome.idea.vim.helper.EditorHelper;
import com.maddyhome.idea.vim.helper.SearchHelper;
import com.maddyhome.idea.vim.helper.UserDataManager;
import com.maddyhome.idea.vim.listener.VimListenerManager;
import com.maddyhome.idea.vim.option.NumberOption;
import com.maddyhome.idea.vim.option.OptionsManager;
@ -87,7 +90,7 @@ public class MotionGroup {
ApplicationManager.getApplication().invokeLater(() -> ApplicationManager.getApplication()
.invokeLater(() -> ApplicationManager.getApplication().invokeLater(() -> {
VimListenerManager.INSTANCE.addEditorListeners(editor);
EditorData.setMotionGroup(editor, true);
UserDataManager.setVimMotionGroup(editor, true);
})));
}
@ -95,9 +98,9 @@ public class MotionGroup {
public void editorReleased(@NotNull EditorFactoryEvent event) {
if (!VimPlugin.isEnabled()) return;
Editor editor = event.getEditor();
if (EditorData.getMotionGroup(editor)) {
if (UserDataManager.getVimMotionGroup(editor)) {
VimListenerManager.INSTANCE.removeEditorListeners(editor);
EditorData.setMotionGroup(editor, false);
UserDataManager.setVimMotionGroup(editor, false);
}
}
}, ApplicationManager.getApplication());
@ -106,9 +109,9 @@ public class MotionGroup {
public void turnOn() {
Editor[] editors = EditorFactory.getInstance().getAllEditors();
for (Editor editor : editors) {
if (!EditorData.getMotionGroup(editor)) {
if (!UserDataManager.getVimMotionGroup(editor)) {
VimListenerManager.INSTANCE.addEditorListeners(editor);
EditorData.setMotionGroup(editor, true);
UserDataManager.setVimMotionGroup(editor, true);
}
}
}
@ -116,9 +119,9 @@ public class MotionGroup {
public void turnOff() {
Editor[] editors = EditorFactory.getInstance().getAllEditors();
for (Editor editor : editors) {
if (EditorData.getMotionGroup(editor)) {
if (UserDataManager.getVimMotionGroup(editor)) {
VimListenerManager.INSTANCE.removeEditorListeners(editor);
EditorData.setMotionGroup(editor, false);
UserDataManager.setVimMotionGroup(editor, false);
}
}
}
@ -232,7 +235,7 @@ public class MotionGroup {
int col = editor.getCaretModel().getVisualPosition().column;
int oldColumn = col;
if (col >= EditorHelper.getLineLength(editor) - 1) {
col = CaretDataKt.getVimLastColumn(editor.getCaretModel().getPrimaryCaret());
col = UserDataManager.getVimLastColumn(editor.getCaretModel().getPrimaryCaret());
}
int visualColumn = EditorHelper.getVisualColumnAtLeftOfScreen(editor);
int caretColumn = col;
@ -254,7 +257,7 @@ public class MotionGroup {
int offset = EditorHelper.visualPositionToOffset(editor, new VisualPosition(newline, newColumn));
moveCaret(editor, editor.getCaretModel().getPrimaryCaret(), offset);
CaretDataKt.setVimLastColumn(editor.getCaretModel().getPrimaryCaret(), col);
UserDataManager.setVimLastColumn(editor.getCaretModel().getPrimaryCaret(), col);
}
}
@ -326,14 +329,14 @@ public class MotionGroup {
if (CommandStateHelper.inBlockSubMode(editor)) {
VisualGroupKt.vimMoveBlockSelectionToOffset(editor, offset);
Caret primaryCaret = editor.getCaretModel().getPrimaryCaret();
CaretDataKt.setVimLastColumn(primaryCaret, primaryCaret.getVisualPosition().column);
UserDataManager.setVimLastColumn(primaryCaret, primaryCaret.getVisualPosition().column);
scrollCaretIntoView(editor);
return;
}
if (caret.getOffset() != offset) {
caret.moveToOffset(offset);
CaretDataKt.setVimLastColumn(caret, caret.getVisualPosition().column);
UserDataManager.setVimLastColumn(caret, caret.getVisualPosition().column);
if (caret == editor.getCaretModel().getPrimaryCaret()) {
scrollCaretIntoView(editor);
}
@ -811,10 +814,10 @@ public class MotionGroup {
int dir = 1;
boolean selection = false;
if (CommandState.getInstance(editor).getMode() == CommandState.Mode.VISUAL) {
if (CaretDataKt.getVimSelectionStart(caret) > caret.getOffset()) {
if (UserDataManager.getVimSelectionStart(caret) > caret.getOffset()) {
dir = -1;
}
if (CaretDataKt.getVimSelectionStart(caret) != caret.getOffset()) {
if (UserDataManager.getVimSelectionStart(caret) != caret.getOffset()) {
selection = true;
}
}
@ -836,7 +839,7 @@ public class MotionGroup {
final Mark mark = VimPlugin.getMark().getMark(editor, ch);
if (mark == null) return -1;
final VirtualFile vf = EditorData.getVirtualFile(editor);
final VirtualFile vf = EditorHelper.getVirtualFile(editor);
if (vf == null) return -1;
final int line = mark.getLogicalLine();
@ -866,7 +869,7 @@ public class MotionGroup {
return -1;
}
final VirtualFile vf = EditorData.getVirtualFile(editor);
final VirtualFile vf = EditorHelper.getVirtualFile(editor);
if (vf == null) {
return -1;
}
@ -1116,7 +1119,7 @@ public class MotionGroup {
}
public int moveCaretToLine(@NotNull Editor editor, int logicalLine, @NotNull Caret caret) {
int col = CaretDataKt.getVimLastColumn(caret);
int col = UserDataManager.getVimLastColumn(caret);
int line = logicalLine;
if (logicalLine < 0) {
line = 0;
@ -1290,7 +1293,7 @@ public class MotionGroup {
return -1;
}
else {
int col = CaretDataKt.getVimLastColumn(caret);
int col = UserDataManager.getVimLastColumn(caret);
int line = EditorHelper.normalizeVisualLine(editor, pos.line + count);
final CommandState.Mode mode = CommandStateHelper.getMode(editor);
final int lastColumnCurrentLine = EditorHelper.lastColumnForLine(editor, logicalPosition.line, CommandStateHelper.isEndAllowed(mode));

View File

@ -54,8 +54,8 @@ import org.jetbrains.annotations.Nullable;
import java.awt.*;
import java.text.NumberFormat;
import java.text.ParsePosition;
import java.util.*;
import java.util.List;
import java.util.*;
public class SearchGroup {
public SearchGroup() {
@ -679,7 +679,7 @@ public class SearchGroup {
currentMatchOffset = findClosestMatch(editor, results, initialOffset, forwards);
highlightSearchResults(editor, pattern, results, currentMatchOffset);
}
EditorData.setLastSearch(editor, pattern);
UserDataManager.setVimLastSearch(editor, pattern);
}
else if (!showHighlights && initialOffset != -1) {
// Incremental search always highlights current match. We know it's incsearch if we have a valid initial offset
@ -702,14 +702,14 @@ public class SearchGroup {
* Remove current search highlights if hlSearch is false, or if the pattern is changed
*/
private boolean shouldRemoveSearchHighlight(@NotNull Editor editor, String newPattern, boolean hlSearch) {
return !hlSearch || (newPattern != null && !newPattern.equals(EditorData.getLastSearch(editor)));
return !hlSearch || (newPattern != null && !newPattern.equals(UserDataManager.getVimLastSearch(editor)));
}
/**
* Add search highlights if hlSearch is true and the pattern is changed
*/
private boolean shouldAddSearchHighlight(@NotNull Editor editor, @Nullable String newPattern, boolean hlSearch) {
return hlSearch && newPattern != null && !newPattern.equals(EditorData.getLastSearch(editor)) && !Objects.equals(newPattern, "");
return hlSearch && newPattern != null && !newPattern.equals(UserDataManager.getVimLastSearch(editor)) && !Objects.equals(newPattern, "");
}
private void highlightSearchLines(@NotNull Editor editor, int startLine, int endLine) {
@ -858,10 +858,10 @@ public class SearchGroup {
private static void highlightSearchResults(@NotNull Editor editor, @NotNull String pattern, List<TextRange> results,
int currentMatchOffset) {
Collection<RangeHighlighter> highlighters = EditorData.getLastHighlights(editor);
Collection<RangeHighlighter> highlighters = UserDataManager.getVimLastHighlighters(editor);
if (highlighters == null) {
highlighters = new ArrayList<>();
EditorData.setLastHighlights(editor, highlighters);
UserDataManager.setVimLastHighlighters(editor, highlighters);
}
for (TextRange range : results) {
@ -1254,7 +1254,7 @@ public class SearchGroup {
}
private static void removeSearchHighlight(@NotNull Editor editor) {
Collection<RangeHighlighter> ehl = EditorData.getLastHighlights(editor);
Collection<RangeHighlighter> ehl = UserDataManager.getVimLastHighlighters(editor);
if (ehl == null) {
return;
}
@ -1265,8 +1265,8 @@ public class SearchGroup {
ehl.clear();
EditorData.setLastHighlights(editor, null);
EditorData.setLastSearch(editor, null);
UserDataManager.setVimLastHighlighters(editor, null);
UserDataManager.setVimLastSearch(editor, null);
}
public void saveData(@NotNull Element element) {
@ -1357,7 +1357,7 @@ public class SearchGroup {
final Document document = event.getDocument();
for (Editor editor : EditorFactory.getInstance().getEditors(document, project)) {
Collection hls = EditorData.getLastHighlights(editor);
Collection hls = UserDataManager.getVimLastHighlighters(editor);
if (hls == null) {
continue;
}
@ -1385,7 +1385,7 @@ public class SearchGroup {
VimPlugin.getSearch().highlightSearchLines(editor, startPosition.line, endPosition.line);
if (logger.isDebugEnabled()) {
hls = EditorData.getLastHighlights(editor);
hls = UserDataManager.getVimLastHighlighters(editor);
logger.debug("sl=" + startPosition.line + ", el=" + endPosition.line);
logger.debug("hls=" + hls);
}

View File

@ -20,20 +20,20 @@ package com.maddyhome.idea.vim.group;
import com.intellij.openapi.actionSystem.DataContext;
import com.intellij.openapi.actionSystem.PlatformDataKeys;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.fileEditor.ex.FileEditorManagerEx;
import com.intellij.openapi.fileEditor.impl.EditorWindow;
import com.intellij.openapi.fileEditor.impl.EditorWithProviderComposite;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.vfs.VirtualFile;
import com.maddyhome.idea.vim.VimPlugin;
import com.maddyhome.idea.vim.helper.EditorData;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import javax.swing.*;
import java.awt.*;
import java.util.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
public class WindowGroup {

View File

@ -31,7 +31,6 @@ import com.maddyhome.idea.vim.command.SelectionType
import com.maddyhome.idea.vim.common.TextRange
import com.maddyhome.idea.vim.group.ChangeGroup
import com.maddyhome.idea.vim.group.MotionGroup
import com.maddyhome.idea.vim.helper.EditorData
import com.maddyhome.idea.vim.helper.EditorDataContext
import com.maddyhome.idea.vim.helper.EditorHelper
import com.maddyhome.idea.vim.helper.inBlockSubMode
@ -39,7 +38,9 @@ import com.maddyhome.idea.vim.helper.inSelectMode
import com.maddyhome.idea.vim.helper.inVisualMode
import com.maddyhome.idea.vim.helper.subMode
import com.maddyhome.idea.vim.helper.vimForEachCaret
import com.maddyhome.idea.vim.helper.vimKeepingVisualOperatorAction
import com.maddyhome.idea.vim.helper.vimLastColumn
import com.maddyhome.idea.vim.helper.vimLastSelectionType
import com.maddyhome.idea.vim.helper.vimLastVisualOperatorRange
import com.maddyhome.idea.vim.helper.vimSelectionStart
import com.maddyhome.idea.vim.helper.vimSelectionStartClear
@ -56,7 +57,7 @@ class VisualMotionGroup {
}
fun selectPreviousVisualMode(editor: Editor): Boolean {
val lastSelectionType = EditorData.getLastSelectionType(editor) ?: return false
val lastSelectionType = editor.vimLastSelectionType ?: return false
val visualMarks = VimPlugin.getMark().getVisualSelectionMarks(editor) ?: return false
editor.caretModel.removeSecondaryCarets()
@ -73,15 +74,14 @@ class VisualMotionGroup {
}
fun swapVisualSelections(editor: Editor): Boolean {
val lastSelectionType = EditorData.getLastSelectionType(editor) ?: return false
val lastSelectionType = editor.vimLastSelectionType ?: return false
val lastVisualRange = VimPlugin.getMark().getVisualSelectionMarks(editor) ?: return false
val primaryCaret = editor.caretModel.primaryCaret
editor.caretModel.removeSecondaryCarets()
val vimSelectionStart = primaryCaret.vimSelectionStart
val selectionType = SelectionType.fromSubMode(editor.subMode)
EditorData.setLastSelectionType(editor, selectionType)
editor.vimLastSelectionType = SelectionType.fromSubMode(editor.subMode)
VimPlugin.getMark().setVisualSelectionMarks(editor, TextRange(vimSelectionStart, primaryCaret.offset))
editor.subMode = lastSelectionType.toSubMode()
@ -362,13 +362,13 @@ class VisualMotionGroup {
editor.caretModel.allCarets.forEach { it.visualAttributes = editor.caretModel.primaryCaret.visualAttributes }
editor.caretModel.removeSecondaryCarets()
}
if (!EditorData.isKeepingVisualOperatorAction(editor)) {
if (!editor.vimKeepingVisualOperatorAction) {
editor.caretModel.allCarets.forEach(Caret::removeSelection)
}
}
if (editor.inVisualMode) {
EditorData.setLastSelectionType(editor, selectionType)
editor.vimLastSelectionType = selectionType
// FIXME: 2019-03-05 Make it multicaret
val primaryCaret = editor.caretModel.primaryCaret
val vimSelectionStart = primaryCaret.vimSelectionStart

View File

@ -26,7 +26,7 @@ import com.maddyhome.idea.vim.VimPlugin
import com.maddyhome.idea.vim.command.Argument
import com.maddyhome.idea.vim.command.Command
import com.maddyhome.idea.vim.command.CommandState
import com.maddyhome.idea.vim.helper.EditorData
import com.maddyhome.idea.vim.helper.vimChangeActionSwitchMode
sealed class ChangeEditorActionHandler : VimActionHandler.SingleExecution() {
@ -44,7 +44,7 @@ sealed class ChangeEditorActionHandler : VimActionHandler.SingleExecution() {
// to be worked after each task. So here we override the deprecated execute function which
// is called for each task and call the handlers for each caret, if implemented.
EditorData.setChangeSwitchMode(editor, null)
editor.vimChangeActionSwitchMode = null
val worked = Ref.create(true)
when (this) {
@ -65,7 +65,7 @@ sealed class ChangeEditorActionHandler : VimActionHandler.SingleExecution() {
CommandState.getInstance(editor).saveLastChangeCommand(cmd)
}
val toSwitch = EditorData.getChangeSwitchMode(editor)
val toSwitch = editor.vimChangeActionSwitchMode
if (toSwitch != null) {
VimPlugin.getChange().processPostChangeModeSwitch(editor, context, toSwitch)
}

View File

@ -34,12 +34,14 @@ import com.maddyhome.idea.vim.group.visual.VimSelection
import com.maddyhome.idea.vim.group.visual.VimSimpleSelection
import com.maddyhome.idea.vim.group.visual.VisualChange
import com.maddyhome.idea.vim.group.visual.VisualOperation
import com.maddyhome.idea.vim.helper.EditorData
import com.maddyhome.idea.vim.helper.inBlockSubMode
import com.maddyhome.idea.vim.helper.inRepeatMode
import com.maddyhome.idea.vim.helper.inVisualMode
import com.maddyhome.idea.vim.helper.vimChangeActionSwitchMode
import com.maddyhome.idea.vim.helper.vimForEachCaret
import com.maddyhome.idea.vim.helper.vimKeepingVisualOperatorAction
import com.maddyhome.idea.vim.helper.vimLastColumn
import com.maddyhome.idea.vim.helper.vimLastSelectionType
import com.maddyhome.idea.vim.helper.vimLastVisualOperatorRange
import com.maddyhome.idea.vim.helper.vimSelectionStart
@ -102,7 +104,7 @@ sealed class VisualOperatorActionHandler : VimActionHandler.SingleExecution() {
final override fun execute(editor: Editor, context: DataContext, cmd: Command): Boolean {
logger.info("Execute visual command $cmd")
EditorData.setChangeSwitchMode(editor, null)
editor.vimChangeActionSwitchMode = null
val selections = editor.collectSelections() ?: return false
if (logger.isDebugEnabled) {
@ -142,7 +144,7 @@ sealed class VisualOperatorActionHandler : VimActionHandler.SingleExecution() {
commandWrapper.finish(res.get())
EditorData.getChangeSwitchMode(editor)?.let {
editor.vimChangeActionSwitchMode?.let {
VimPlugin.getChange().processPostChangeModeSwitch(editor, context, it)
}
@ -153,7 +155,7 @@ sealed class VisualOperatorActionHandler : VimActionHandler.SingleExecution() {
return when {
this.inRepeatMode -> {
if (EditorData.getLastSelectionType(this) == SelectionType.BLOCK_WISE) {
if (this.vimLastSelectionType == SelectionType.BLOCK_WISE) {
val primaryCaret = caretModel.primaryCaret
val range = primaryCaret.vimLastVisualOperatorRange ?: return null
val end = VisualOperation.calculateRange(this, range, 1, primaryCaret)
@ -198,7 +200,7 @@ sealed class VisualOperatorActionHandler : VimActionHandler.SingleExecution() {
fun start() {
logger.debug("Preparing visual command")
EditorData.setKeepingVisualOperatorAction(editor, CommandFlags.FLAG_EXIT_VISUAL !in cmd.flags)
editor.vimKeepingVisualOperatorAction = CommandFlags.FLAG_EXIT_VISUAL !in cmd.flags
editor.vimForEachCaret {
val change = if (this@VisualStartFinishWrapper.editor.inVisualMode && !this@VisualStartFinishWrapper.editor.inRepeatMode) {
@ -230,7 +232,7 @@ sealed class VisualOperatorActionHandler : VimActionHandler.SingleExecution() {
editor.vimForEachCaret { caret -> visualChanges[caret]?.let { caret.vimLastVisualOperatorRange = it } }
}
EditorData.setKeepingVisualOperatorAction(editor, false)
editor.vimKeepingVisualOperatorAction = false
}
}

View File

@ -16,14 +16,21 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
@file:JvmName("UserDataManager")
@file:Suppress("ObjectPropertyName")
package com.maddyhome.idea.vim.helper
import com.intellij.openapi.editor.Caret
import com.intellij.openapi.editor.Editor
import com.intellij.openapi.editor.RangeMarker
import com.intellij.openapi.editor.markup.RangeHighlighter
import com.intellij.openapi.util.Key
import com.maddyhome.idea.vim.command.CommandState
import com.maddyhome.idea.vim.command.SelectionType
import com.maddyhome.idea.vim.ex.ExOutputModel
import com.maddyhome.idea.vim.group.visual.VisualChange
import com.maddyhome.idea.vim.ui.ExOutputPanel
/**
* @author Alex Plate
@ -59,3 +66,33 @@ var Caret.vimLastColumn: Int
var Caret.vimLastVisualOperatorRange: VisualChange? by userDataCaretToEditor()
var Caret.vimInsertStart: RangeMarker by userDataOr { (this as Caret).editor.document.createRangeMarker(this.offset, this.offset) }
//------------------ Editor
fun unInitializeEditor(editor: Editor) {
editor.vimLastSelectionType = null
editor.vimCommandState = null
editor.vimMorePanel = null
editor.vimExOutput = null
editor.vimLastHighlighters = null
}
var Editor.vimLastSearch: String? by userData()
var Editor.vimLastHighlighters: Collection<RangeHighlighter>? by userData()
/***
* @see :help visualmode()
*/
var Editor.vimLastSelectionType: SelectionType? by userData()
var Editor.vimCommandState: CommandState? by userData()
var Editor.vimChangeGroup: Boolean by userDataOr { false }
var Editor.vimMotionGroup: Boolean by userDataOr { false }
var Editor.vimEditorGroup: Boolean by userDataOr { false }
var Editor.vimLineNumbersShown: Boolean by userDataOr { false }
var Editor.vimMorePanel: ExOutputPanel? by userData()
var Editor.vimExOutput: ExOutputModel? by userData()
var Editor.vimTestInputModel: TestInputModel? by userData()
/**
* Checks whether a keeping visual mode visual operator action is performed on editor.
*/
var Editor.vimKeepingVisualOperatorAction: Boolean by userDataOr { false }
var Editor.vimChangeActionSwitchMode: CommandState.Mode? by userData()

View File

@ -1,247 +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.diagnostic.Logger;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.editor.markup.RangeHighlighter;
import com.intellij.openapi.fileEditor.FileDocumentManager;
import com.intellij.openapi.util.Key;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.testFramework.LightVirtualFile;
import com.maddyhome.idea.vim.command.CommandState;
import com.maddyhome.idea.vim.command.SelectionType;
import com.maddyhome.idea.vim.ex.ExOutputModel;
import com.maddyhome.idea.vim.ui.ExOutputPanel;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.Collection;
/**
* This class is used to manipulate editor specific data. Each editor has a user defined map associated with it.
* These methods provide convenient methods for working with that Vim Plugin specific data.
*/
public class EditorData {
/**
* This is used to initialize each new editor that gets created.
*
* @param editor The editor to initialize
*/
public static void initializeEditor(Editor editor) {
if (logger.isDebugEnabled()) logger.debug("editor created: " + editor);
}
/**
* This is used to clean up editors whenever they are closed.
*
* @param editor The editor to cleanup
*/
public static void unInitializeEditor(@NotNull Editor editor) {
if (logger.isDebugEnabled()) logger.debug("editor closed: " + editor);
editor.putUserData(COMMAND_STATE, null);
editor.putUserData(LAST_HIGHLIGHTS, null);
editor.putUserData(LAST_SELECTION_TYPE, null);
editor.putUserData(MORE_PANEL, null);
editor.putUserData(EX_OUTPUT_MODEL, null);
}
@Nullable
public static String getLastSearch(@NotNull Editor editor) {
return editor.getUserData(LAST_SEARCH);
}
public static void setLastSearch(@NotNull Editor editor, String search) {
editor.putUserData(LAST_SEARCH, search);
}
@Nullable
public static Collection<RangeHighlighter> getLastHighlights(@NotNull Editor editor) {
return editor.getUserData(LAST_HIGHLIGHTS);
}
public static void setLastHighlights(@NotNull Editor editor, Collection<RangeHighlighter> highlights) {
editor.putUserData(LAST_HIGHLIGHTS, highlights);
}
/***
* @see :help visualmode()
*/
@Nullable
public static SelectionType getLastSelectionType(@NotNull Editor editor) {
return editor.getDocument().getUserData(LAST_SELECTION_TYPE);
}
public static void setLastSelectionType(@NotNull Editor editor, @NotNull SelectionType selectionType) {
editor.getDocument().putUserData(LAST_SELECTION_TYPE, selectionType);
}
@Nullable
public static CommandState getCommandState(@NotNull Editor editor) {
return editor.getUserData(COMMAND_STATE);
}
public static void setCommandState(@NotNull Editor editor, CommandState state) {
editor.putUserData(COMMAND_STATE, state);
}
public static boolean getChangeGroup(@NotNull Editor editor) {
Boolean res = editor.getUserData(CHANGE_GROUP);
if (res != null) {
return res;
}
else {
return false;
}
}
public static void setChangeGroup(@NotNull Editor editor, boolean adapter) {
editor.putUserData(CHANGE_GROUP, adapter);
}
public static boolean getMotionGroup(@NotNull Editor editor) {
return editor.getUserData(MOTION_GROUP) == Boolean.TRUE;
}
public static void setMotionGroup(@NotNull Editor editor, boolean adapter) {
editor.putUserData(MOTION_GROUP, adapter);
}
public static boolean getEditorGroup(@NotNull Editor editor) {
return editor.getUserData(EDITOR_GROUP) == Boolean.TRUE;
}
public static void setEditorGroup(@NotNull Editor editor, boolean value) {
editor.putUserData(EDITOR_GROUP, value);
}
public static boolean isLineNumbersShown(@NotNull Editor editor) {
return editor.getUserData(LINE_NUMBERS_SHOWN) == Boolean.TRUE;
}
public static void setLineNumbersShown(@NotNull Editor editor, boolean value) {
editor.putUserData(LINE_NUMBERS_SHOWN, value);
}
@Nullable
public static ExOutputPanel getMorePanel(@NotNull Editor editor) {
return editor.getUserData(MORE_PANEL);
}
public static void setMorePanel(@NotNull Editor editor, @NotNull ExOutputPanel panel) {
editor.putUserData(MORE_PANEL, panel);
}
@Nullable
public static ExOutputModel getExOutputModel(@NotNull Editor editor) {
return editor.getUserData(EX_OUTPUT_MODEL);
}
public static void setExOutputModel(@NotNull Editor editor, @NotNull ExOutputModel model) {
editor.putUserData(EX_OUTPUT_MODEL, model);
}
/**
* Gets the virtual file associated with this editor
*
* @param editor The editor
* @return The virtual file for the editor
*/
@Nullable
public static VirtualFile getVirtualFile(@NotNull Editor editor) {
return FileDocumentManager.getInstance().getFile(editor.getDocument());
}
/**
* Checks whether a keeping visual mode visual operator action is performed on editor.
*/
public static boolean isKeepingVisualOperatorAction(@NotNull Editor editor) {
Boolean res = editor.getUserData(IS_KEEPING_VISUAL_OPERATOR_ACTION);
if (res == null) {
return false;
}
else {
return res;
}
}
/**
* Sets the keeping visual mode visual operator action flag for the editor.
*/
public static void setKeepingVisualOperatorAction(@NotNull Editor editor, boolean value) {
editor.putUserData(IS_KEEPING_VISUAL_OPERATOR_ACTION, value);
}
/**
* Gets the mode to which the editor should switch after a change/visual action.
*/
@Nullable
public static CommandState.Mode getChangeSwitchMode(@NotNull Editor editor) {
return editor.getUserData(CHANGE_ACTION_SWITCH_MODE);
}
/**
* Sets the mode to which the editor should switch after a change/visual action.
*/
public static void setChangeSwitchMode(@NotNull Editor editor, @Nullable CommandState.Mode mode) {
editor.putUserData(CHANGE_ACTION_SWITCH_MODE, mode);
}
/**
* This is a static helper - no instances needed
*/
private EditorData() {
}
private static final Key<SelectionType> LAST_SELECTION_TYPE = new Key<SelectionType>("lastSelectionType");
private static final Key<String> LAST_SEARCH = new Key<String>("lastSearch");
private static final Key<Collection<RangeHighlighter>> LAST_HIGHLIGHTS =
new Key<Collection<RangeHighlighter>>("lastHighlights");
private static final Key<CommandState> COMMAND_STATE = new Key<CommandState>("commandState");
private static final Key<Boolean> CHANGE_GROUP = new Key<Boolean>("changeGroup");
private static final Key<Boolean> MOTION_GROUP = new Key<Boolean>("motionGroup");
public static final Key<Boolean> EDITOR_GROUP = new Key<Boolean>("editorGroup");
public static final Key<Boolean> LINE_NUMBERS_SHOWN = new Key<Boolean>("lineNumbersShown");
private static final Key<ExOutputPanel> MORE_PANEL = new Key<ExOutputPanel>("IdeaVim.morePanel");
private static final Key<ExOutputModel> EX_OUTPUT_MODEL = new Key<ExOutputModel>("IdeaVim.exOutputModel");
private static final Key<TestInputModel> TEST_INPUT_MODEL = new Key<TestInputModel>("IdeaVim.testInputModel");
private static final Key<Boolean> IS_KEEPING_VISUAL_OPERATOR_ACTION = new Key<>("isKeepingVisualOperatorAction");
private static final Key<CommandState.Mode> CHANGE_ACTION_SWITCH_MODE = new Key<>("changeActionSwitchMode");
private static final Logger logger = Logger.getInstance(EditorData.class.getName());
/**
* Checks if editor is file editor, also it takes into account that editor can be placed in editors hierarchy
*/
public static boolean isFileEditor(@NotNull Editor editor) {
final VirtualFile virtualFile = EditorData.getVirtualFile(editor);
return virtualFile != null && !(virtualFile instanceof LightVirtualFile);
}
@Nullable
public static TestInputModel getTestInputModel(@NotNull Editor editor) {
return editor.getUserData(TEST_INPUT_MODEL);
}
public static void setTestInputModel(@NotNull Editor editor, @NotNull TestInputModel model) {
editor.putUserData(TEST_INPUT_MODEL, model);
}
}

View File

@ -43,7 +43,7 @@ public class EditorDataContext implements DataContext {
return editor.getProject();
}
else if (PlatformDataKeys.VIRTUAL_FILE.getName().equals(dataId)) {
return EditorData.getVirtualFile(editor);
return EditorHelper.getVirtualFile(editor);
}
return null;

View File

@ -24,6 +24,7 @@ import com.intellij.openapi.editor.impl.EditorImpl;
import com.intellij.openapi.fileEditor.FileDocumentManager;
import com.intellij.openapi.fileEditor.FileEditorManager;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.testFramework.LightVirtualFile;
import com.maddyhome.idea.vim.common.CharacterPosition;
import com.maddyhome.idea.vim.common.IndentConfig;
import com.maddyhome.idea.vim.common.TextRange;
@ -716,7 +717,7 @@ public class EditorHelper {
final LogicalPosition logicalPosition = caret.getLogicalPosition();
final int lastColumn = EditorHelper.lastColumnForLine(editor, logicalPosition.line, CommandStateHelper.isEndAllowed(CommandStateHelper.getMode(editor)));
int targetColumn = pos.column != lastColumn ? pos.column : prevLastColumn;
CaretDataKt.setVimLastColumn(caret, targetColumn);
UserDataManager.setVimLastColumn(caret, targetColumn);
}
private static int scrollFullPageDown(@NotNull final Editor editor, int pages) {
@ -812,4 +813,23 @@ public class EditorHelper {
}
return inlayHeight;
}
/**
* Gets the virtual file associated with this editor
*
* @param editor The editor
* @return The virtual file for the editor
*/
@Nullable
public static VirtualFile getVirtualFile(@NotNull Editor editor) {
return FileDocumentManager.getInstance().getFile(editor.getDocument());
}
/**
* Checks if editor is file editor, also it takes into account that editor can be placed in editors hierarchy
*/
public static boolean isFileEditor(@NotNull Editor editor) {
final VirtualFile virtualFile = getVirtualFile(editor);
return virtualFile != null && !(virtualFile instanceof LightVirtualFile);
}
}

View File

@ -62,6 +62,20 @@ annotation class VimBehaviourDiffers(
val shouldBeFixed: Boolean = true
)
/**
* Function for delegated properties.
* The property will be delegated to UserData and has nullable type.
*/
fun <T> userData(): ReadWriteProperty<UserDataHolder, T?> = object : UserDataReadWriteProperty<UserDataHolder, T?>() {
override fun getValue(thisRef: UserDataHolder, property: KProperty<*>): T? {
return thisRef.getUserData(getKey(property))
}
override fun setValue(thisRef: UserDataHolder, property: KProperty<*>, value: T?) {
thisRef.putUserData(getKey(property), value)
}
}
/**
* Function for delegated properties.
* The property will be saved to caret if this caret is not primary

View File

@ -115,7 +115,7 @@ public class PsiHelper {
@Nullable
public static PsiFile getFile(@NotNull Editor editor) {
VirtualFile vf = EditorData.getVirtualFile(editor);
VirtualFile vf = EditorHelper.getVirtualFile(editor);
if (vf != null) {
Project proj = editor.getProject();
if (proj != null) {

View File

@ -35,10 +35,10 @@ public class TestInputModel {
private TestInputModel() {}
public static TestInputModel getInstance(@NotNull Editor editor) {
TestInputModel model = EditorData.getTestInputModel(editor);
TestInputModel model = UserDataManager.getVimTestInputModel(editor);
if (model == null) {
model = new TestInputModel();
EditorData.setTestInputModel(editor, model);
UserDataManager.setVimTestInputModel(editor, model);
}
return model;
}

View File

@ -26,9 +26,9 @@ import com.intellij.openapi.project.Project;
import com.intellij.ui.components.JBScrollPane;
import com.intellij.util.IJSwingUtilities;
import com.maddyhome.idea.vim.VimPlugin;
import com.maddyhome.idea.vim.helper.EditorData;
import com.maddyhome.idea.vim.helper.EditorDataContext;
import com.maddyhome.idea.vim.helper.UiHelper;
import com.maddyhome.idea.vim.helper.UserDataManager;
import com.maddyhome.idea.vim.option.OptionsManager;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
@ -95,10 +95,10 @@ public class ExOutputPanel extends JPanel implements LafManagerListener {
@NotNull
public static ExOutputPanel getInstance(@NotNull Editor editor) {
ExOutputPanel panel = EditorData.getMorePanel(editor);
ExOutputPanel panel = UserDataManager.getVimMorePanel(editor);
if (panel == null) {
panel = new ExOutputPanel(editor);
EditorData.setMorePanel(editor, panel);
UserDataManager.setVimMorePanel(editor, panel);
}
return panel;
}