From aab311b9ac1c12156f3de4ccdb3eb232e80fdc58 Mon Sep 17 00:00:00 2001 From: Sergey Ignatov Date: Mon, 20 May 2019 16:33:52 +0300 Subject: [PATCH] use StringSearcher for highlighting text occurrences in sh files GitOrigin-RevId: b7b07cf90b0470d1e22df70d5a7c0273a1d54b59 --- .../highlighting/ShTextOccurrencesUtil.java | 24 ++++++++++--------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/plugins/sh/src/com/intellij/sh/highlighting/ShTextOccurrencesUtil.java b/plugins/sh/src/com/intellij/sh/highlighting/ShTextOccurrencesUtil.java index 4fc511b50321..5376482a4fdf 100644 --- a/plugins/sh/src/com/intellij/sh/highlighting/ShTextOccurrencesUtil.java +++ b/plugins/sh/src/com/intellij/sh/highlighting/ShTextOccurrencesUtil.java @@ -8,6 +8,7 @@ import com.intellij.openapi.util.TextRange; import com.intellij.openapi.util.text.StringUtil; import com.intellij.util.CollectConsumer; import com.intellij.util.Consumer; +import com.intellij.util.text.StringSearcher; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -26,8 +27,8 @@ public class ShTextOccurrencesUtil { return StringUtil.isEmptyOrSpaces(subSequence) || StringUtil.contains(subSequence, "\n") ? null : textRange; } return SelectWordUtil.getWordSelectionRange(editor.getDocument().getImmutableCharSequence(), - caret.getOffset(), - ShTextOccurrencesUtil::isWordPartCondition); + caret.getOffset(), + ShTextOccurrencesUtil::isWordPartCondition); } private static boolean isWordPartCondition(char ch) { @@ -38,7 +39,7 @@ public class ShTextOccurrencesUtil { public static Collection findAllOccurrences(@NotNull CharSequence documentText, @NotNull CharSequence textToFind, boolean matchExactWordsOnly) { - CollectConsumer consumer= new CollectConsumer<>(); + CollectConsumer consumer = new CollectConsumer<>(); consumeAllOccurrences(documentText, textToFind, matchExactWordsOnly, consumer); return consumer.getResult(); } @@ -47,14 +48,15 @@ public class ShTextOccurrencesUtil { @NotNull CharSequence textToFind, boolean matchExactWordsOnly, Consumer consumer) { - int offset = StringUtil.indexOf(documentText, textToFind); - while (offset >= 0) { - TextRange textRange = TextRange.create(offset, offset + textToFind.length()); - if (!matchExactWordsOnly || !isWordExpandableOutside(documentText, textRange)) { - consumer.consume(textRange); - } - offset = StringUtil.indexOf(documentText, textToFind, offset + textToFind.length()); - } + String pattern = textToFind.toString(); + int length = pattern.length(); + StringSearcher searcher = new StringSearcher(pattern, true, true); + searcher.processOccurrences(documentText, value -> { + TextRange tr = TextRange.create(value, value + length); + if (matchExactWordsOnly && isWordExpandableOutside(documentText, tr)) return true; + consumer.consume(tr); + return true; + }); } private static boolean isWordExpandableOutside(@NotNull CharSequence documentText, @NotNull TextRange textRange) {