public static void diagnosticsDiff( Map<Diagnostic, TextDiagnostic> diagnosticToExpectedDiagnostic, List<DiagnosedRange> expected, Collection<Diagnostic> actual, DiagnosticDiffCallbacks callbacks) { assertSameFile(actual); Iterator<DiagnosedRange> expectedDiagnostics = expected.iterator(); List<DiagnosticDescriptor> sortedDiagnosticDescriptors = getSortedDiagnosticDescriptors(actual); Iterator<DiagnosticDescriptor> actualDiagnostics = sortedDiagnosticDescriptors.iterator(); DiagnosedRange currentExpected = safeAdvance(expectedDiagnostics); DiagnosticDescriptor currentActual = safeAdvance(actualDiagnostics); while (currentExpected != null || currentActual != null) { if (currentExpected != null) { if (currentActual == null) { missingDiagnostics(callbacks, currentExpected); currentExpected = safeAdvance(expectedDiagnostics); } else { int expectedStart = currentExpected.getStart(); int actualStart = currentActual.getStart(); int expectedEnd = currentExpected.getEnd(); int actualEnd = currentActual.getEnd(); if (expectedStart < actualStart) { missingDiagnostics(callbacks, currentExpected); currentExpected = safeAdvance(expectedDiagnostics); } else if (expectedStart > actualStart) { unexpectedDiagnostics(currentActual.getDiagnostics(), callbacks); currentActual = safeAdvance(actualDiagnostics); } else if (expectedEnd > actualEnd) { assert expectedStart == actualStart; missingDiagnostics(callbacks, currentExpected); currentExpected = safeAdvance(expectedDiagnostics); } else if (expectedEnd < actualEnd) { assert expectedStart == actualStart; unexpectedDiagnostics(currentActual.getDiagnostics(), callbacks); currentActual = safeAdvance(actualDiagnostics); } else { compareDiagnostics( callbacks, currentExpected, currentActual, diagnosticToExpectedDiagnostic); currentExpected = safeAdvance(expectedDiagnostics); currentActual = safeAdvance(actualDiagnostics); } } } else { //noinspection ConstantConditions assert (currentActual != null); unexpectedDiagnostics(currentActual.getDiagnostics(), callbacks); currentActual = safeAdvance(actualDiagnostics); } } }
private static void compareDiagnostics( @NotNull DiagnosticDiffCallbacks callbacks, @NotNull DiagnosedRange currentExpected, @NotNull DiagnosticDescriptor currentActual, @NotNull Map<Diagnostic, TextDiagnostic> diagnosticToInput) { int expectedStart = currentExpected.getStart(); int expectedEnd = currentExpected.getEnd(); int actualStart = currentActual.getStart(); int actualEnd = currentActual.getEnd(); assert expectedStart == actualStart && expectedEnd == actualEnd; Map<Diagnostic, TextDiagnostic> actualDiagnostics = currentActual.getTextDiagnosticsMap(); List<TextDiagnostic> expectedDiagnostics = currentExpected.getDiagnostics(); for (TextDiagnostic expectedDiagnostic : expectedDiagnostics) { boolean diagnosticFound = false; for (Diagnostic actualDiagnostic : actualDiagnostics.keySet()) { TextDiagnostic actualTextDiagnostic = actualDiagnostics.get(actualDiagnostic); if (expectedDiagnostic.getName().equals(actualTextDiagnostic.getName())) { if (!compareTextDiagnostic(expectedDiagnostic, actualTextDiagnostic)) { callbacks.wrongParametersDiagnostic( expectedDiagnostic, actualTextDiagnostic, expectedStart, expectedEnd); } actualDiagnostics.remove(actualDiagnostic); diagnosticToInput.put(actualDiagnostic, expectedDiagnostic); diagnosticFound = true; break; } } if (!diagnosticFound) callbacks.missingDiagnostic(expectedDiagnostic, expectedStart, expectedEnd); } for (TextDiagnostic unexpectedDiagnostic : actualDiagnostics.values()) { callbacks.unexpectedDiagnostic(unexpectedDiagnostic, actualStart, actualEnd); } }
public static StringBuffer addDiagnosticMarkersToText( @NotNull final PsiFile psiFile, @NotNull Collection<Diagnostic> diagnostics, @NotNull Map<Diagnostic, TextDiagnostic> diagnosticToExpectedDiagnostic, @NotNull Function<PsiFile, String> getFileText) { String text = getFileText.fun(psiFile); StringBuffer result = new StringBuffer(); diagnostics = Collections2.filter( diagnostics, new Predicate<Diagnostic>() { @Override public boolean apply(Diagnostic diagnostic) { return psiFile.equals(diagnostic.getPsiFile()); } }); if (!diagnostics.isEmpty()) { List<DiagnosticDescriptor> diagnosticDescriptors = getSortedDiagnosticDescriptors(diagnostics); Stack<DiagnosticDescriptor> opened = new Stack<DiagnosticDescriptor>(); ListIterator<DiagnosticDescriptor> iterator = diagnosticDescriptors.listIterator(); DiagnosticDescriptor currentDescriptor = iterator.next(); for (int i = 0; i < text.length(); i++) { char c = text.charAt(i); while (!opened.isEmpty() && i == opened.peek().end) { closeDiagnosticString(result); opened.pop(); } while (currentDescriptor != null && i == currentDescriptor.start) { openDiagnosticsString(result, currentDescriptor, diagnosticToExpectedDiagnostic); if (currentDescriptor.getEnd() == i) { closeDiagnosticString(result); } else { opened.push(currentDescriptor); } if (iterator.hasNext()) { currentDescriptor = iterator.next(); } else { currentDescriptor = null; } } result.append(c); } if (currentDescriptor != null) { assert currentDescriptor.start == text.length(); assert currentDescriptor.end == text.length(); openDiagnosticsString(result, currentDescriptor, diagnosticToExpectedDiagnostic); opened.push(currentDescriptor); } while (!opened.isEmpty() && text.length() == opened.peek().end) { closeDiagnosticString(result); opened.pop(); } assert opened.isEmpty() : "Stack is not empty: " + opened; } else { result.append(text); } return result; }