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) {