@NotNull private static List<DiagnosticDescriptor> getSortedDiagnosticDescriptors( @NotNull Collection<Diagnostic> diagnostics) { LinkedListMultimap<TextRange, Diagnostic> diagnosticsGroupedByRanges = LinkedListMultimap.create(); for (Diagnostic diagnostic : diagnostics) { if (!diagnostic.isValid()) continue; for (TextRange textRange : diagnostic.getTextRanges()) { diagnosticsGroupedByRanges.put(textRange, diagnostic); } } List<DiagnosticDescriptor> diagnosticDescriptors = Lists.newArrayList(); for (TextRange range : diagnosticsGroupedByRanges.keySet()) { diagnosticDescriptors.add( new DiagnosticDescriptor( range.getStartOffset(), range.getEndOffset(), diagnosticsGroupedByRanges.get(range))); } Collections.sort( diagnosticDescriptors, new Comparator<DiagnosticDescriptor>() { @Override public int compare(@NotNull DiagnosticDescriptor d1, @NotNull DiagnosticDescriptor d2) { // Start early -- go first; start at the same offset, the one who end later is the // outer, i.e. goes first return (d1.start != d2.start) ? d1.start - d2.start : d2.end - d1.end; } }); return diagnosticDescriptors; }
private static void unexpectedDiagnostics( List<Diagnostic> actual, DiagnosticDiffCallbacks callbacks) { for (Diagnostic diagnostic : actual) { List<TextRange> textRanges = diagnostic.getTextRanges(); for (TextRange textRange : textRanges) { callbacks.unexpectedDiagnostic( TextDiagnostic.asTextDiagnostic(diagnostic), textRange.getStartOffset(), textRange.getEndOffset()); } } }
@Override public int compare(@NotNull Diagnostic o1, @NotNull Diagnostic o2) { List<TextRange> ranges1 = o1.getTextRanges(); List<TextRange> ranges2 = o2.getTextRanges(); int minNumberOfRanges = ranges1.size() < ranges2.size() ? ranges1.size() : ranges2.size(); for (int i = 0; i < minNumberOfRanges; i++) { TextRange range1 = ranges1.get(i); TextRange range2 = ranges2.get(i); int startOffset1 = range1.getStartOffset(); int startOffset2 = range2.getStartOffset(); if (startOffset1 != startOffset2) { // Start early -- go first return startOffset1 - range2.getStartOffset(); } int endOffset1 = range1.getEndOffset(); int endOffset2 = range2.getEndOffset(); if (endOffset1 != endOffset2) { // start at the same offset, the one who end later is the outer, i.e. goes first return endOffset2 - endOffset1; } } return ranges1.size() - ranges2.size(); }