diff --git a/src/com/maddyhome/idea/vim/action/motion/search/SearchWholeWordBackwardAction.kt b/src/com/maddyhome/idea/vim/action/motion/search/SearchWholeWordBackwardAction.kt index bce83ac451..866911b872 100644 --- a/src/com/maddyhome/idea/vim/action/motion/search/SearchWholeWordBackwardAction.kt +++ b/src/com/maddyhome/idea/vim/action/motion/search/SearchWholeWordBackwardAction.kt @@ -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.* @@ -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 diff --git a/src/com/maddyhome/idea/vim/action/motion/search/SearchWholeWordForwardAction.kt b/src/com/maddyhome/idea/vim/action/motion/search/SearchWholeWordForwardAction.kt index 758c45419a..ef20c31d6e 100644 --- a/src/com/maddyhome/idea/vim/action/motion/search/SearchWholeWordForwardAction.kt +++ b/src/com/maddyhome/idea/vim/action/motion/search/SearchWholeWordForwardAction.kt @@ -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.* @@ -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 diff --git a/src/com/maddyhome/idea/vim/action/motion/search/SearchWordBackwardAction.kt b/src/com/maddyhome/idea/vim/action/motion/search/SearchWordBackwardAction.kt index 16922a6779..3457749d68 100644 --- a/src/com/maddyhome/idea/vim/action/motion/search/SearchWordBackwardAction.kt +++ b/src/com/maddyhome/idea/vim/action/motion/search/SearchWordBackwardAction.kt @@ -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.* @@ -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 diff --git a/src/com/maddyhome/idea/vim/action/motion/search/SearchWordForwardAction.kt b/src/com/maddyhome/idea/vim/action/motion/search/SearchWordForwardAction.kt index e6cade0050..66b89f8493 100644 --- a/src/com/maddyhome/idea/vim/action/motion/search/SearchWordForwardAction.kt +++ b/src/com/maddyhome/idea/vim/action/motion/search/SearchWordForwardAction.kt @@ -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.* @@ -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 diff --git a/src/com/maddyhome/idea/vim/extension/multiplecursors/VimMultipleCursorsExtension.kt b/src/com/maddyhome/idea/vim/extension/multiplecursors/VimMultipleCursorsExtension.kt index 96538913c6..cc5a9bb650 100644 --- a/src/com/maddyhome/idea/vim/extension/multiplecursors/VimMultipleCursorsExtension.kt +++ b/src/com/maddyhome/idea/vim/extension/multiplecursors/VimMultipleCursorsExtension.kt @@ -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 @@ -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 diff --git a/src/com/maddyhome/idea/vim/group/SearchGroup.java b/src/com/maddyhome/idea/vim/group/SearchGroup.java index caf55d5f6d..a47201499e 100644 --- a/src/com/maddyhome/idea/vim/group/SearchGroup.java +++ b/src/com/maddyhome/idea/vim/group/SearchGroup.java @@ -100,7 +100,7 @@ public void turnOff() { // This method is used in AceJump integration plugin @SuppressWarnings("unused") public int getLastDir() { - return lastDir; + return lastDir.toInt(); } public @Nullable String getLastPattern() { @@ -110,7 +110,7 @@ public int getLastDir() { public void resetState() { lastSearch = lastPattern = lastSubstitute = lastReplace = lastOffset = null; lastIgnoreSmartCase = false; - lastDir = 0; + lastDir = Direction.UNSET; resetShowSearchHighlight(); } @@ -228,12 +228,12 @@ public int search(@NotNull Editor editor, @NotNull Caret caret, @NotNull String } public int search(@NotNull Editor editor, @NotNull String command, int startOffset, int count, @NotNull EnumSet flags) { - int dir = DIR_FORWARDS; + Direction dir = Direction.FORWARDS; char type = '/'; String pattern = lastSearch; String offset = lastOffset; if (flags.contains(CommandFlags.FLAG_SEARCH_REV)) { - dir = DIR_BACKWARDS; + dir = Direction.BACKWARDS; type = '?'; } @@ -287,7 +287,7 @@ else if (command.length() == 1) { return findItOffset(editor, startOffset, count, lastDir); } - public int searchWord(@NotNull Editor editor, @NotNull Caret caret, int count, boolean whole, int dir) { + public int searchWord(@NotNull Editor editor, @NotNull Caret caret, int count, boolean whole, Direction dir) { TextRange range = SearchHelper.findWordUnderCursor(editor, caret); if (range == null) { logger.warn("No range was found"); @@ -322,16 +322,16 @@ public int searchNext(@NotNull Editor editor, @NotNull Caret caret, int count) { } public int searchPrevious(@NotNull Editor editor, @NotNull Caret caret, int count) { - return searchNextWithDirection(editor, caret, count, -lastDir); + return searchNextWithDirection(editor, caret, count, lastDir.reverse()); } public int searchNextFromOffset(@NotNull Editor editor, int offset, int count) { resetShowSearchHighlight(); updateSearchHighlights(); - return findItOffset(editor, offset, count, 1); + return findItOffset(editor, offset, count, Direction.FORWARDS); } - private int searchNextWithDirection(@NotNull Editor editor, @NotNull Caret caret, int count, int dir) { + private int searchNextWithDirection(@NotNull Editor editor, @NotNull Caret caret, int count, Direction dir) { resetShowSearchHighlight(); updateSearchHighlights(); final int startOffset = caret.getOffset(); @@ -429,7 +429,7 @@ public static TextRange findIt(@NotNull Editor editor, @Nullable String pattern, return null; } - int dir = searchOptions.contains(SearchOptions.BACKWARDS) ? DIR_BACKWARDS : DIR_FORWARDS; + Direction dir = searchOptions.contains(SearchOptions.BACKWARDS) ? Direction.BACKWARDS : Direction.FORWARDS; //RE sp; RegExp sp; @@ -467,7 +467,7 @@ public static TextRange findIt(@NotNull Editor editor, @Nullable String pattern, int loop; RegExp.lpos_T start_pos; boolean at_first_line; - int extra_col = dir == DIR_FORWARDS ? 1 : 0; + int extra_col = dir == Direction.FORWARDS ? 1 : 0; boolean match_ok; long nmatched; //int submatch = 0; @@ -491,7 +491,7 @@ public static TextRange findIt(@NotNull Editor editor, @Nullable String pattern, * Start searching in current line, unless searching backwards and * we're in column 0. */ - if (dir == DIR_BACKWARDS && start_pos.col == 0) { + if (dir == Direction.BACKWARDS && start_pos.col == 0) { lnum = pos.lnum - 1; at_first_line = false; } @@ -505,7 +505,7 @@ public static TextRange findIt(@NotNull Editor editor, @Nullable String pattern, startLine = lnum; endLine = lnum + 1; } - for (; lnum >= startLine && lnum < endLine; lnum += dir, at_first_line = false) { + for (; lnum >= startLine && lnum < endLine; lnum += dir.toInt(), at_first_line = false) { /* * Look for a match somewhere in the line. */ @@ -522,7 +522,7 @@ public static TextRange findIt(@NotNull Editor editor, @Nullable String pattern, * the start position. If not, continue at the end of the * match (this is vi compatible) or on the next char. */ - if (dir == DIR_FORWARDS && at_first_line) { + if (dir == Direction.FORWARDS && at_first_line) { match_ok = true; /* * When match lands on a NUL the cursor will be put @@ -560,7 +560,7 @@ public static TextRange findIt(@NotNull Editor editor, @Nullable String pattern, continue; } } - if (dir == DIR_BACKWARDS) { + if (dir == Direction.BACKWARDS) { /* * Now, if there are multiple matches on this line, * we have to get the last one. Or the last one before @@ -654,7 +654,7 @@ public static TextRange findIt(@NotNull Editor editor, @Nullable String pattern, * is redrawn. The keep_msg is cleared whenever another message is * written. */ - if (dir == DIR_BACKWARDS) /* start second loop at the other end */ { + if (dir == Direction.BACKWARDS) /* start second loop at the other end */ { lnum = lineCount - 1; //if (!shortmess(SHM_SEARCH) && (options & SEARCH_MSG)) // give_warning((char_u *)_(top_bot_msg), TRUE); @@ -694,7 +694,7 @@ else if (lnum <= 0) { new CharacterPosition(endpos.lnum, endpos.col).toOffset(editor)); } - private int findItOffset(@NotNull Editor editor, int startOffset, int count, int dir) { + private int findItOffset(@NotNull Editor editor, int startOffset, int count, Direction dir) { boolean wrap = OptionsManager.INSTANCE.getWrapscan().isSet(); logger.debug("Perform search. Direction: " + dir + " wrap: " + wrap); @@ -757,7 +757,7 @@ private int findItOffset(@NotNull Editor editor, int startOffset, int count, int } EnumSet searchOptions = EnumSet.of(SearchOptions.SHOW_MESSAGES, SearchOptions.WHOLE_FILE); - if (dir == DIR_BACKWARDS) searchOptions.add(SearchOptions.BACKWARDS); + if (dir == Direction.BACKWARDS) searchOptions.add(SearchOptions.BACKWARDS); if (lastIgnoreSmartCase) searchOptions.add(SearchOptions.IGNORE_SMARTCASE); if (wrap) searchOptions.add(SearchOptions.WRAP); if (hasEndOffset) searchOptions.add(SearchOptions.WANT_ENDPOS); @@ -1159,7 +1159,7 @@ public void saveData(@NotNull Element element) { search.addContent(createElementWithText("last-substitute", lastSubstitute)); } Element text = new Element("last-dir"); - text.addContent(Integer.toString(lastDir)); + text.addContent(Integer.toString(lastDir.toInt())); search.addContent(text); text = new Element("show-last"); @@ -1188,7 +1188,7 @@ public void readData(@NotNull Element element) { lastSubstitute = getSafeChildText(search, "last-substitute"); Element dir = search.getChild("last-dir"); - lastDir = Integer.parseInt(dir.getText()); + lastDir = Direction.Companion.fromInt(Integer.parseInt(dir.getText())); Element show = search.getChild("show-last"); final ListOption vimInfo = OptionsManager.INSTANCE.getViminfo(); @@ -1300,7 +1300,7 @@ public enum SearchOptions { private @Nullable String lastReplace; private @Nullable String lastOffset; private boolean lastIgnoreSmartCase; - private int lastDir; + private Direction lastDir; private boolean showSearchHighlight = OptionsManager.INSTANCE.getHlsearch().isSet(); private boolean do_all = false; /* do multiple substitutions per line */ @@ -1313,8 +1313,5 @@ public enum SearchOptions { private static final int RE_SEARCH = 2; private static final int RE_SUBST = 3; - private static final int DIR_FORWARDS = 1; - private static final int DIR_BACKWARDS = -1; - private static final Logger logger = Logger.getInstance(SearchGroup.class.getName()); } diff --git a/src/com/maddyhome/idea/vim/helper/SearchHelper.java b/src/com/maddyhome/idea/vim/helper/SearchHelper.java index 1926f755fd..474876f23d 100644 --- a/src/com/maddyhome/idea/vim/helper/SearchHelper.java +++ b/src/com/maddyhome/idea/vim/helper/SearchHelper.java @@ -103,7 +103,7 @@ public static int findUnmatchedBlock(@NotNull Editor editor, @NotNull Caret care int pos = caret.getOffset(); int loc = blockChars.indexOf(type); // What direction should we go now (-1 is backward, 1 is forward) - Direction dir = loc % 2 == 0 ? Direction.BACK : Direction.FORWARD; + Direction dir = loc % 2 == 0 ? Direction.BACKWARDS : Direction.FORWARDS; // Which character did we find and which should we now search for char match = blockChars.charAt(loc); char found = blockChars.charAt(loc - dir.toInt()); @@ -163,10 +163,10 @@ public static int findUnmatchedBlock(@NotNull Editor editor, @NotNull Caret care int endOffset = quoteRange.getEndOffset(); CharSequence subSequence = chars.subSequence(startOffset, endOffset); int inQuotePos = pos - startOffset; - int inQuoteStart = findBlockLocation(subSequence, close, type, Direction.BACK, inQuotePos, count, false); + int inQuoteStart = findBlockLocation(subSequence, close, type, Direction.BACKWARDS, inQuotePos, count, false); if (inQuoteStart != -1) { startPosInStringFound = true; - int inQuoteEnd = findBlockLocation(subSequence, type, close, Direction.FORWARD, inQuoteStart, 1, false); + int inQuoteEnd = findBlockLocation(subSequence, type, close, Direction.FORWARDS, inQuoteStart, 1, false); if (inQuoteEnd != -1) { bstart = inQuoteStart + startOffset; bend = inQuoteEnd + startOffset; @@ -176,9 +176,9 @@ public static int findUnmatchedBlock(@NotNull Editor editor, @NotNull Caret care } if (!startPosInStringFound) { - bstart = findBlockLocation(chars, close, type, Direction.BACK, pos, count, false); + bstart = findBlockLocation(chars, close, type, Direction.BACKWARDS, pos, count, false); if (bstart != -1) { - bend = findBlockLocation(chars, type, close, Direction.FORWARD, bstart, 1, false); + bend = findBlockLocation(chars, type, close, Direction.FORWARDS, bstart, 1, false); } } @@ -294,7 +294,7 @@ public static int findMatchingPairOnCurrentLine(@NotNull Editor editor, @NotNull // If we found one ... if (loc >= 0) { // What direction should we go now (-1 is backward, 1 is forward) - Direction dir = loc % 2 == 0 ? Direction.FORWARD : Direction.BACK; + Direction dir = loc % 2 == 0 ? Direction.FORWARDS : Direction.BACKWARDS; // Which character did we find and which should we now search for char found = getPairChars().charAt(loc); char match = getPairChars().charAt(loc + dir.toInt()); @@ -327,7 +327,7 @@ private static int findBlockLocation(@NotNull CharSequence chars, boolean allowInString) { int res = -1; int initialPos = pos; - Function inCheckPosF = x -> dir == Direction.BACK && x > 0 ? x - 1 : x + 1; + Function inCheckPosF = x -> dir == Direction.BACKWARDS && x > 0 ? x - 1 : x + 1; final int inCheckPos = inCheckPosF.apply(pos); boolean inString = checkInString(chars, inCheckPos, true); boolean initialInString = inString; @@ -391,30 +391,16 @@ private static boolean isQuoteWithoutEscape(@NotNull CharSequence chars, int pos return backslashCounter % 2 == 0; } - public enum Direction { - BACK(-1), FORWARD(1); - - private final int value; - - Direction(int i) { - value = i; - } - - public int toInt() { - return value; - } - } - public enum NumberType { BIN, OCT, DEC, HEX, ALPHA } private static int findNextQuoteInLine(@NotNull CharSequence chars, int pos, char quote) { - return findQuoteInLine(chars, pos, quote, Direction.FORWARD); + return findQuoteInLine(chars, pos, quote, Direction.FORWARDS); } private static int findPreviousQuoteInLine(@NotNull CharSequence chars, int pos, char quote) { - return findQuoteInLine(chars, pos, quote, Direction.BACK); + return findQuoteInLine(chars, pos, quote, Direction.BACKWARDS); } private static int findFirstQuoteInLine(@NotNull Editor editor, int pos, char quote) { @@ -428,8 +414,8 @@ private static int findQuoteInLine(@NotNull CharSequence chars, int pos, char qu private static int countCharactersInLine(@NotNull CharSequence chars, int pos, char c) { int cnt = 0; - while (pos > 0 && (chars.charAt(pos + Direction.BACK.toInt()) != '\n')) { - pos = findCharacterPosition(chars, pos + Direction.BACK.toInt(), c, false, true, Direction.BACK); + while (pos > 0 && (chars.charAt(pos + Direction.BACKWARDS.toInt()) != '\n')) { + pos = findCharacterPosition(chars, pos + Direction.BACKWARDS.toInt(), c, false, true, Direction.BACKWARDS); if (pos != -1) { cnt++; } diff --git a/src/com/maddyhome/idea/vim/helper/SearchHelperKt.kt b/src/com/maddyhome/idea/vim/helper/SearchHelperKt.kt index 90d3ddc178..2ec76b5c31 100644 --- a/src/com/maddyhome/idea/vim/helper/SearchHelperKt.kt +++ b/src/com/maddyhome/idea/vim/helper/SearchHelperKt.kt @@ -18,16 +18,34 @@ package com.maddyhome.idea.vim.helper -import com.maddyhome.idea.vim.helper.SearchHelper.Direction import com.maddyhome.idea.vim.helper.SearchHelper.findPositionOfFirstCharacter import com.maddyhome.idea.vim.option.OptionsManager.ignorecase import com.maddyhome.idea.vim.option.OptionsManager.smartcase +enum class Direction(private val value: Int) { + BACKWARDS(-1), FORWARDS(1), UNSET(0); + + fun toInt(): Int = value + fun reverse(): Direction = when (this) { + BACKWARDS -> FORWARDS + FORWARDS -> BACKWARDS + UNSET -> UNSET + } + + companion object { + fun fromInt(value: Int) = when (value) { + BACKWARDS.value -> BACKWARDS + FORWARDS.value -> FORWARDS + else -> UNSET + } + } +} + private data class State(val position: Int, val trigger: Char, val inQuote: Boolean?, val lastOpenSingleQuotePos: Int) // bounds are considered inside corresponding quotes fun checkInString(chars: CharSequence, currentPos: Int, str: Boolean): Boolean { - val begin = findPositionOfFirstCharacter(chars, currentPos, setOf('\n'), false, Direction.BACK)?.second ?: 0 + val begin = findPositionOfFirstCharacter(chars, currentPos, setOf('\n'), false, Direction.BACKWARDS)?.second ?: 0 val changes = quoteChanges(chars, begin) // TODO: here we need to keep only the latest element in beforePos (if any) and // don't need atAndAfterPos to be eagerly collected @@ -107,7 +125,7 @@ private fun quoteChanges(chars: CharSequence, begin: Int) = sequence { // in that situation it may be double quote inside single quotes, so we cannot threat it as double quote pair open/close var inQuote: Boolean? = false val charsToSearch = setOf('\'', '"', '\n') - var found = findPositionOfFirstCharacter(chars, begin, charsToSearch, false, Direction.FORWARD) + var found = findPositionOfFirstCharacter(chars, begin, charsToSearch, false, Direction.FORWARDS) while (found != null && found.first != '\n') { val i = found.second @@ -160,7 +178,7 @@ private fun quoteChanges(chars: CharSequence, begin: Int) = sequence { } } yield(State(i, c, inQuote, lastOpenSingleQuotePos)) - found = findPositionOfFirstCharacter(chars, i + Direction.FORWARD.toInt(), charsToSearch, false, Direction.FORWARD) + found = findPositionOfFirstCharacter(chars, i + Direction.FORWARDS.toInt(), charsToSearch, false, Direction.FORWARDS) } }