Comparator<RangeHighlighterEx> (EditorColorsScheme scheme) { return (o1, o2) -> { final int result = LayerComparator.INSTANCE.compare(o1, o2); if (result != 0) { return result; } // There is a possible case when more than one highlighter target the same region (e.g. 'identifier under caret' and 'identifier'). // We want to prefer the one that defines foreground color to the one that doesn't define (has either fore- or background colors // while the other one has only foreground color). See IDEA-85697 for concrete example. final TextAttributes a1 = o1.getTextAttributes(scheme); final TextAttributes a2 = o2.getTextAttributes(scheme); if (a1 == null ^ a2 == null) { return a1 == null ? 1 : -1; } if (a1 == null) { return result; } final Color fore1 = a1.getForegroundColor(); final Color fore2 = a2.getForegroundColor(); if (fore1 == null ^ fore2 == null) { return fore1 == null ? 1 : -1; } final Color back1 = a1.getBackgroundColor(); final Color back2 = a2.getBackgroundColor(); if (back1 == null ^ back2 == null) { return back1 == null ? 1 : -1; } return result; }; }