use StringSearcher for highlighting text occurrences in sh files

GitOrigin-RevId: b7b07cf90b0470d1e22df70d5a7c0273a1d54b59
This commit is contained in:
Sergey Ignatov
2019-05-20 18:09:43 +03:00
committed by intellij-monorepo-bot
parent 4e89d564c4
commit aab311b9ac
@@ -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<TextRange> findAllOccurrences(@NotNull CharSequence documentText,
@NotNull CharSequence textToFind,
boolean matchExactWordsOnly) {
CollectConsumer<TextRange> consumer= new CollectConsumer<>();
CollectConsumer<TextRange> consumer = new CollectConsumer<>();
consumeAllOccurrences(documentText, textToFind, matchExactWordsOnly, consumer);
return consumer.getResult();
}
@@ -47,14 +48,15 @@ public class ShTextOccurrencesUtil {
@NotNull CharSequence textToFind,
boolean matchExactWordsOnly,
Consumer<? super TextRange> 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) {