Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extract SearchHighlightsHelper from SearchGroup #257

Merged
merged 6 commits into from
Dec 4, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import com.maddyhome.idea.vim.command.Argument
import com.maddyhome.idea.vim.command.Command
import com.maddyhome.idea.vim.ex.CommandParser
import com.maddyhome.idea.vim.ex.ExException
import com.maddyhome.idea.vim.group.RegisterGroup
import com.maddyhome.idea.vim.handler.VimActionHandler

class PlaybackRegisterAction : VimActionHandler.SingleExecution() {
Expand All @@ -44,7 +45,7 @@ class PlaybackRegisterAction : VimActionHandler.SingleExecution() {
'@' -> {
application.runWriteAction { res.set(VimPlugin.getMacro().playbackLastRegister(editor, context, project, cmd.count)) }
}
':' -> { // No write action
RegisterGroup.LAST_COMMAND_REGISTER -> { // No write action
try {
res.set(CommandParser.getInstance().processLastCommand(editor, context, cmd.count))
} catch (e: ExException) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import com.maddyhome.idea.vim.command.Argument
import com.maddyhome.idea.vim.command.CommandFlags
import com.maddyhome.idea.vim.command.MotionType
import com.maddyhome.idea.vim.handler.MotionActionHandler
import com.maddyhome.idea.vim.helper.Direction
import com.maddyhome.idea.vim.helper.enumSetOf
import java.util.*

Expand All @@ -37,7 +38,7 @@ class SearchWholeWordBackwardAction : MotionActionHandler.ForEachCaret() {
count: Int,
rawCount: Int,
argument: Argument?): Int {
return VimPlugin.getSearch().searchWord(editor, caret, count, true, -1)
return VimPlugin.getSearch().searchWord(editor, caret, count, true, Direction.BACKWARDS)
}

override val motionType: MotionType = MotionType.EXCLUSIVE
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import com.maddyhome.idea.vim.command.Argument
import com.maddyhome.idea.vim.command.CommandFlags
import com.maddyhome.idea.vim.command.MotionType
import com.maddyhome.idea.vim.handler.MotionActionHandler
import com.maddyhome.idea.vim.helper.Direction
import com.maddyhome.idea.vim.helper.enumSetOf
import java.util.*

Expand All @@ -37,7 +38,7 @@ class SearchWholeWordForwardAction : MotionActionHandler.ForEachCaret() {
count: Int,
rawCount: Int,
argument: Argument?): Int {
return VimPlugin.getSearch().searchWord(editor, caret, count, true, 1)
return VimPlugin.getSearch().searchWord(editor, caret, count, true, Direction.FORWARDS)
}

override val motionType: MotionType = MotionType.EXCLUSIVE
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import com.maddyhome.idea.vim.command.Argument
import com.maddyhome.idea.vim.command.CommandFlags
import com.maddyhome.idea.vim.command.MotionType
import com.maddyhome.idea.vim.handler.MotionActionHandler
import com.maddyhome.idea.vim.helper.Direction
import com.maddyhome.idea.vim.helper.enumSetOf
import java.util.*

Expand All @@ -37,7 +38,7 @@ class SearchWordBackwardAction : MotionActionHandler.ForEachCaret() {
count: Int,
rawCount: Int,
argument: Argument?): Int {
return VimPlugin.getSearch().searchWord(editor, caret, count, false, -1)
return VimPlugin.getSearch().searchWord(editor, caret, count, false, Direction.BACKWARDS)
}

override val motionType: MotionType = MotionType.EXCLUSIVE
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import com.maddyhome.idea.vim.command.Argument
import com.maddyhome.idea.vim.command.CommandFlags
import com.maddyhome.idea.vim.command.MotionType
import com.maddyhome.idea.vim.handler.MotionActionHandler
import com.maddyhome.idea.vim.helper.Direction
import com.maddyhome.idea.vim.helper.enumSetOf
import java.util.*

Expand All @@ -37,7 +38,7 @@ class SearchWordForwardAction : MotionActionHandler.ForEachCaret() {
count: Int,
rawCount: Int,
argument: Argument?): Int {
return VimPlugin.getSearch().searchWord(editor, caret, count, false, 1)
return VimPlugin.getSearch().searchWord(editor, caret, count, false, Direction.FORWARDS)
}

override val motionType: MotionType = MotionType.EXCLUSIVE
Expand Down
5 changes: 2 additions & 3 deletions src/com/maddyhome/idea/vim/ex/CommandParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@
import com.maddyhome.idea.vim.VimPlugin;
import com.maddyhome.idea.vim.command.SelectionType;
import com.maddyhome.idea.vim.common.Register;
import com.maddyhome.idea.vim.common.TextRange;
import com.maddyhome.idea.vim.ex.handler.GotoLineHandler;
import com.maddyhome.idea.vim.ex.ranges.Range;
import com.maddyhome.idea.vim.ex.ranges.Ranges;
import com.maddyhome.idea.vim.group.HistoryGroup;
import com.maddyhome.idea.vim.group.RegisterGroup;
import com.maddyhome.idea.vim.helper.MessageHelper;
import com.maddyhome.idea.vim.helper.Msg;
import org.jetbrains.annotations.NotNull;
Expand Down Expand Up @@ -177,8 +177,7 @@ private void processCommand(@NotNull Editor editor, @NotNull DataContext context
ThrowableComputable<Object, ExException> runCommand = () -> {
boolean ok = handler.process(editor, context, command, count);
if (ok && !handler.getArgFlags().getFlags().contains(CommandHandler.Flag.DONT_SAVE_LAST)) {
VimPlugin.getRegister().storeTextInternal(editor, new TextRange(-1, -1), cmd,
SelectionType.CHARACTER_WISE, ':', false);
VimPlugin.getRegister().storeTextSpecial(RegisterGroup.LAST_COMMAND_REGISTER, cmd);
}
return null;
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,9 @@ import com.maddyhome.idea.vim.extension.VimExtensionFacade.putKeyMapping
import com.maddyhome.idea.vim.extension.VimExtensionHandler
import com.maddyhome.idea.vim.group.MotionGroup
import com.maddyhome.idea.vim.group.visual.vimSetSelection
import com.maddyhome.idea.vim.helper.EditorHelper
import com.maddyhome.idea.vim.helper.MessageHelper
import com.maddyhome.idea.vim.helper.*
import com.maddyhome.idea.vim.helper.SearchHelper.findWordUnderCursor
import com.maddyhome.idea.vim.helper.StringHelper.parseKeys
import com.maddyhome.idea.vim.helper.endOffsetInclusive
import com.maddyhome.idea.vim.helper.exitVisualMode
import com.maddyhome.idea.vim.helper.inVisualMode
import com.maddyhome.idea.vim.option.OptionsManager
import org.jetbrains.annotations.NonNls
import java.lang.Integer.min
Expand Down Expand Up @@ -242,7 +238,7 @@ class VimMultipleCursorsExtension : VimExtension {
val wordRange = VimPlugin.getMotion().getWordRange(editor, caret, 1, false, false)
caret.vimSetSelection(wordRange.startOffset, wordRange.endOffsetInclusive, true)

val offset = VimPlugin.getSearch().searchWord(editor, caret, 1, whole, 1)
val offset = VimPlugin.getSearch().searchWord(editor, caret, 1, whole, Direction.FORWARDS)
MotionGroup.moveCaret(editor, caret, range.endOffset - 1)

return offset
Expand Down
78 changes: 59 additions & 19 deletions src/com/maddyhome/idea/vim/group/RegisterGroup.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@

package com.maddyhome.idea.vim.group;

import com.google.common.collect.ImmutableList;
import com.intellij.codeInsight.editorActions.CopyPastePostProcessor;
import com.intellij.codeInsight.editorActions.CopyPastePreProcessor;
import com.intellij.codeInsight.editorActions.TextBlockTransferable;
Expand Down Expand Up @@ -81,18 +80,33 @@
@Storage(value = "$APP_CONFIG$/vim_settings_local.xml", roamingType = RoamingType.DISABLED)
})
public class RegisterGroup implements PersistentStateComponent<Element> {
private static final @NonNls String WRITABLE_REGISTERS = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-*+_/\"";
private static final String READONLY_REGISTERS = ":.%#=/";
private static final @NonNls String RECORDABLE_REGISTER = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
private static final String PLAYBACK_REGISTER = RECORDABLE_REGISTER + "\".*+";
public static final char UNNAMED_REGISTER = '"';
public static final char LAST_SEARCH_REGISTER = '/'; // IdeaVim does not supporting writing to this register
public static final char LAST_COMMAND_REGISTER = ':';
private static final char LAST_INSERTED_TEXT_REGISTER = '.';
public static final char SMALL_DELETION_REGISTER = '-';
private static final char BLACK_HOLE_REGISTER = '_';
private static final char ALTERNATE_BUFFER_REGISTER = '#'; // Not supported
private static final char EXPRESSION_BUFFER_REGISTER = '='; // Not supported
private static final char CURRENT_FILENAME_REGISTER = '%'; // Not supported
private static final @NonNls String CLIPBOARD_REGISTERS = "*+";
private static final @NonNls String NUMBERED_REGISTERS = "0123456789";
private static final @NonNls String NAMED_REGISTERS = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";

private static final @NonNls String WRITABLE_REGISTERS = NUMBERED_REGISTERS + NAMED_REGISTERS + CLIPBOARD_REGISTERS
+ SMALL_DELETION_REGISTER + BLACK_HOLE_REGISTER + UNNAMED_REGISTER + LAST_SEARCH_REGISTER;
private static final String READONLY_REGISTERS = ""
+ CURRENT_FILENAME_REGISTER + LAST_COMMAND_REGISTER + LAST_INSERTED_TEXT_REGISTER + ALTERNATE_BUFFER_REGISTER
+ EXPRESSION_BUFFER_REGISTER; // Expression buffer is not actually readonly
private static final @NonNls String RECORDABLE_REGISTERS = NUMBERED_REGISTERS + NAMED_REGISTERS;
private static final String PLAYBACK_REGISTERS = RECORDABLE_REGISTERS + UNNAMED_REGISTER + CLIPBOARD_REGISTERS + LAST_INSERTED_TEXT_REGISTER;
private static final String VALID_REGISTERS = WRITABLE_REGISTERS + READONLY_REGISTERS;
private static final List<Character> CLIPBOARD_REGISTERS = ImmutableList.of('*', '+');

private static final Logger logger = Logger.getInstance(RegisterGroup.class.getName());

public static final char UNNAMED_REGISTER = '"';
private final @NotNull HashMap<Character, Register> registers = new HashMap<>();
private char defaultRegister = UNNAMED_REGISTER;
private char lastRegister = defaultRegister;
private final @NotNull HashMap<Character, Register> registers = new HashMap<>();
private char recordRegister = 0;
private @Nullable List<KeyStroke> recordList = null;

Expand Down Expand Up @@ -173,10 +187,35 @@ public boolean storeText(@NotNull Editor editor, @NotNull TextRange range, @NotN
return false;
}

public boolean storeTextInternal(@NotNull Editor editor, @NotNull TextRange range, @NotNull String text,
@NotNull SelectionType type, char register, boolean isDelete) {
// Null register doesn't get saved
if (lastRegister == '_') return true;
/**
* Stores text, character wise, in the given special register
*
* <p>This method is intended to support writing to registers when the text cannot be yanked from an editor. This is
* expected to only be used to update the search and command registers. It will not update named registers.</p>
*
* <p>While this method allows setting the unnamed register, this should only be done from tests, and only when it's
* not possible to yank or cut from the fixture editor. This method will skip additional text processing, and won't
* update other registers such as the small delete register or reorder the numbered registers. It is much more
* preferable to yank from the fixture editor.</p>
*
* @param register The register to use for storing the text. Cannot be a normal text register
* @param text The text to store, without further processing
* @return True if the text is stored, false if the passed register is not supported
*/
public boolean storeTextSpecial(char register, @NotNull String text) {
if (READONLY_REGISTERS.indexOf(register) == -1 && register != LAST_SEARCH_REGISTER
&& register != UNNAMED_REGISTER) {
return false;
}
registers.put(register, new Register(register, SelectionType.CHARACTER_WISE, text, new ArrayList<>()));
if (logger.isDebugEnabled()) logger.debug("register '" + register + "' contains: \"" + text + "\"");
return true;
}

private boolean storeTextInternal(@NotNull Editor editor, @NotNull TextRange range, @NotNull String text,
@NotNull SelectionType type, char register, boolean isDelete) {
// Null register doesn't get saved, but acts like it was
if (lastRegister == BLACK_HOLE_REGISTER) return true;

int start = range.getStartOffset();
int end = range.getEndOffset();
Expand Down Expand Up @@ -219,7 +258,7 @@ public boolean storeTextInternal(@NotNull Editor editor, @NotNull TextRange rang
if (logger.isDebugEnabled()) logger.debug("register '" + register + "' contains: \"" + processedText + "\"");
}

if (CLIPBOARD_REGISTERS.contains(register)) {
if (CLIPBOARD_REGISTERS.indexOf(register) >= 0) {
ClipboardHandler.setClipboardText(processedText, new ArrayList<>(transferableData), text);
}

Expand Down Expand Up @@ -248,7 +287,7 @@ public boolean storeTextInternal(@NotNull Editor editor, @NotNull TextRange rang

// Deletes smaller than one line and without specified register go the the "-" register
if (smallInlineDeletion && register == defaultRegister) {
registers.put('-', new Register('-', type, processedText, new ArrayList<>(transferableData)));
registers.put(SMALL_DELETION_REGISTER, new Register(SMALL_DELETION_REGISTER, type, processedText, new ArrayList<>(transferableData)));
}
}
// Yanks also go to register 0 if the default register was used
Expand Down Expand Up @@ -338,7 +377,7 @@ private boolean isSmallDeletionSpecialCase(Editor editor) {
}

public @Nullable Register getPlaybackRegister(char r) {
if (PLAYBACK_REGISTER.indexOf(r) != 0) {
if (PLAYBACK_REGISTERS.indexOf(r) != 0) {
return getRegister(r);
}
else {
Expand All @@ -351,15 +390,15 @@ private boolean isSmallDeletionSpecialCase(Editor editor) {
if (Character.isUpperCase(r)) {
r = Character.toLowerCase(r);
}
return CLIPBOARD_REGISTERS.contains(r) ? refreshClipboardRegister(r) : registers.get(r);
return CLIPBOARD_REGISTERS.indexOf(r) >= 0 ? refreshClipboardRegister(r) : registers.get(r);
}

public void saveRegister(char r, Register register) {
// Uppercase registers actually get the lowercase register
if (Character.isUpperCase(r)) {
r = Character.toLowerCase(r);
}
if (CLIPBOARD_REGISTERS.contains(r)) {
if (CLIPBOARD_REGISTERS.indexOf(r) >= 0) {
ClipboardHandler.setClipboardText(register.getText(), new ArrayList<>(register.getTransferableData()), register.getRawText());
}
registers.put(r, register);
Expand All @@ -383,7 +422,8 @@ public char getDefaultRegister() {

public @NotNull List<Register> getRegisters() {
final List<Register> res = new ArrayList<>(registers.values());
for (Character r : CLIPBOARD_REGISTERS) {
for (int i = 0; i < CLIPBOARD_REGISTERS.length(); i++) {
final char r = CLIPBOARD_REGISTERS.charAt(i);
final Register register = refreshClipboardRegister(r);
if (register != null) {
res.add(register);
Expand All @@ -394,7 +434,7 @@ public char getDefaultRegister() {
}

public boolean startRecording(Editor editor, char register) {
if (RECORDABLE_REGISTER.indexOf(register) != -1) {
if (RECORDABLE_REGISTERS.indexOf(register) != -1) {
CommandState.getInstance(editor).setRecording(true);
recordRegister = register;
recordList = new ArrayList<>();
Expand Down
Loading