Skip to content

Commit

Permalink
Extract and use common Direction enum
Browse files Browse the repository at this point in the history
  • Loading branch information
citizenmatt authored and AlexPl292 committed Dec 4, 2020
1 parent 3c8b7e2 commit 4ea7c42
Show file tree
Hide file tree
Showing 8 changed files with 63 additions and 62 deletions.
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
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
43 changes: 20 additions & 23 deletions src/com/maddyhome/idea/vim/group/SearchGroup.java
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -110,7 +110,7 @@ public int getLastDir() {
public void resetState() {
lastSearch = lastPattern = lastSubstitute = lastReplace = lastOffset = null;
lastIgnoreSmartCase = false;
lastDir = 0;
lastDir = Direction.UNSET;
resetShowSearchHighlight();
}

Expand Down Expand Up @@ -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<CommandFlags> 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 = '?';
}

Expand Down Expand Up @@ -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");
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -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;
}
Expand All @@ -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.
*/
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -757,7 +757,7 @@ private int findItOffset(@NotNull Editor editor, int startOffset, int count, int
}

EnumSet<SearchOptions> 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);
Expand Down Expand Up @@ -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");
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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 */
Expand All @@ -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());
}
36 changes: 11 additions & 25 deletions src/com/maddyhome/idea/vim/helper/SearchHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down Expand Up @@ -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;
Expand All @@ -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);
}
}

Expand Down Expand Up @@ -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());
Expand Down Expand Up @@ -327,7 +327,7 @@ private static int findBlockLocation(@NotNull CharSequence chars,
boolean allowInString) {
int res = -1;
int initialPos = pos;
Function<Integer, Integer> inCheckPosF = x -> dir == Direction.BACK && x > 0 ? x - 1 : x + 1;
Function<Integer, Integer> 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;
Expand Down Expand Up @@ -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) {
Expand All @@ -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++;
}
Expand Down
Loading

0 comments on commit 4ea7c42

Please sign in to comment.