void (String filter, @NlsSafe String text, @SimpleTextAttributes.StyleAttributeConstant int style, final Color foreground, final Color background, final SimpleColoredComponent textRenderer) { if (text == null) { return; } if (filter == null || filter.length() == 0) { textRenderer.setDynamicSearchMatchHighlighting(false); textRenderer.append(text, new SimpleTextAttributes(background, foreground, JBColor.RED, style)); } else { textRenderer.setDynamicSearchMatchHighlighting(true); //markup Set<String> quoted = new HashSet<>(); filter = processFilter(quoteStrictOccurrences(text, filter), quoted); final Int2ObjectRBTreeMap<String> indexToString = new Int2ObjectRBTreeMap<>(); for (String stripped : quoted) { int beg = 0; int idx; while ((idx = Strings.indexOfIgnoreCase(text, stripped, beg)) != -1) { indexToString.put(idx, text.substring(idx, idx + stripped.length())); beg = idx + stripped.length(); } } final List<String> selectedWords = new ArrayList<>(); int pos = 0; for (Int2ObjectMap.Entry<String> entry : Int2ObjectMaps.fastIterable(indexToString)) { String stripped = entry.getValue(); int start = entry.getIntKey(); if (pos > start) { final String highlighted = selectedWords.get(selectedWords.size() - 1); if (highlighted.length() < stripped.length()) { selectedWords.remove(highlighted); } else { continue; } } appendSelectedWords(text, selectedWords, pos, start, filter); selectedWords.add(stripped); pos = start + stripped.length(); } appendSelectedWords(text, selectedWords, pos, text.length(), filter); int idx = 0; for (String word : selectedWords) { text = text.substring(idx); final @NlsSafe String before = text.substring(0, text.indexOf(word)); if (before.length() > 0) { textRenderer.append(before, new SimpleTextAttributes(background, foreground, null, style)); } idx = text.indexOf(word) + word.length(); textRenderer.append(text.substring(idx - word.length(), idx), new SimpleTextAttributes(background, foreground, null, style | SimpleTextAttributes.STYLE_SEARCH_MATCH)); } final @NlsSafe String after = text.substring(idx); if (after.length() > 0) { textRenderer.append(after, new SimpleTextAttributes(background, foreground, null, style)); } } }