@CheckForNull private ScannerReport.SyntaxHighlightingRule getNextHighlightingMatchingLine(int line) { // Get next element (if exists) if (currentItem == null && lineHighlightingIterator.hasNext()) { currentItem = lineHighlightingIterator.next(); } // Return current element if lines match if (currentItem != null && currentItem.getRange().getStartLine() == line) { return currentItem; } return null; }
public String offsetToString(ScannerReport.TextRange range, int lineIndex, int lineLength) { validateOffsetOrder(range, lineIndex); validateStartOffsetNotGreaterThanLineLength(range, lineLength, lineIndex); validateEndOffsetNotGreaterThanLineLength(range, lineLength, lineIndex); int startOffset = range.getStartLine() == lineIndex ? range.getStartOffset() : 0; int endOffset = range.getEndLine() == lineIndex ? range.getEndOffset() : lineLength; StringBuilder element = new StringBuilder(); if (startOffset < endOffset) { element.append(startOffset).append(OFFSET_SEPARATOR); element.append(endOffset); } return element.toString(); }
private static void putForTextRange(SetMultimap<Integer, ScannerReport.Symbol> res, ScannerReport.Symbol symbol, ScannerReport.TextRange declaration) { for (int i = declaration.getStartLine(); i <= declaration.getEndLine(); i++) { res.put(i, symbol); } }
/** * Get list of all start positions of a symbol in an inputfile * * @param symbolStartLine 0-based start offset for the symbol in file * @param symbolStartLineOffset 0-based end offset for the symbol in file */ @CheckForNull public List<ScannerReport.TextRange> symbolReferencesFor(InputFile file, int symbolStartLine, int symbolStartLineOffset) { int ref = ((DefaultInputComponent) file).scannerId(); try (CloseableIterator<Symbol> symbols = getReportReader().readComponentSymbols(ref)) { while (symbols.hasNext()) { Symbol symbol = symbols.next(); if (symbol.getDeclaration().getStartLine() == symbolStartLine && symbol.getDeclaration().getStartOffset() == symbolStartLineOffset) { return symbol.getReferenceList(); } } } return Collections.emptyList(); }
@Test public void testOneIssuePerLine() throws Exception { File projectDir = new File("test-resources/mediumtest/xoo/sample"); File tmpDir = temp.newFolder(); FileUtils.copyDirectory(projectDir, tmpDir); AnalysisResult result = tester .newAnalysis(new File(tmpDir, "sonar-project.properties")) .property(OneExternalIssuePerLineSensor.ACTIVATE, "true") .execute(); List<Issue> issues = result.issuesFor(result.inputFile("xources/hello/HelloJava.xoo")); assertThat(issues).isEmpty(); List<ExternalIssue> externalIssues = result.externalIssuesFor(result.inputFile("xources/hello/HelloJava.xoo")); assertThat(externalIssues).hasSize(8 /* lines */); ExternalIssue issue = externalIssues.get(0); assertThat(issue.getTextRange().getStartLine()).isEqualTo(issue.getTextRange().getStartLine()); assertThat(result.adHocRules()).isEmpty(); }
private static boolean matchLine(ScannerReport.TextRange range, int line) { return range.getStartLine() <= line && range.getEndLine() >= line; }
private static TextRange toRange(InputFile file, ScannerReport.TextRange reportRange) { return file.newRange(file.newPointer(reportRange.getStartLine(), reportRange.getStartOffset()), file.newPointer(reportRange.getEndLine(), reportRange.getEndOffset())); }
private DetailedTextBlock convert(ScannerReport.TextRange textRange, int id) { return new DetailedTextBlock(id, textRange.getStartLine(), textRange.getEndLine()); } }
private TextBlock convert(ScannerReport.TextRange textRange) { return new TextBlock(textRange.getStartLine(), textRange.getEndLine()); } }
private static void validateStartOffsetNotGreaterThanLineLength(ScannerReport.TextRange range, int lineLength, int line) { checkExpression(range.getStartLine() != line || range.getStartOffset() <= lineLength, "Start offset %s is defined outside the length (%s) of the line %s", range.getStartOffset(), lineLength, line); }
private static void validateOffsetOrder(ScannerReport.TextRange range, int line) { checkExpression(range.getStartLine() != range.getEndLine() || range.getStartOffset() <= range.getEndOffset(), "End offset %s cannot be defined before start offset %s on line %s", range.getEndOffset(), range.getStartOffset(), line); }
@Override public TextRange textRange() { if (!rawIssue.hasTextRange()) { return null; } return new DefaultTextRange( new DefaultTextPointer(rawIssue.getTextRange().getStartLine(), rawIssue.getTextRange().getStartOffset()), new DefaultTextPointer(rawIssue.getTextRange().getEndLine(), rawIssue.getTextRange().getEndOffset())); }
@Test public void testMultilineIssueRange() throws Exception { List<Issue> issues = result.issuesFor(result.inputFile("xources/hello/Multiline.xoo")); assertThat(issues).hasSize(1); Issue issue = issues.get(0); assertThat(issue.getMsg()).isEqualTo("Primary location"); assertThat(issue.getTextRange().getStartLine()).isEqualTo(6); assertThat(issue.getTextRange().getStartOffset()).isEqualTo(23); assertThat(issue.getTextRange().getEndLine()).isEqualTo(7); assertThat(issue.getTextRange().getEndOffset()).isEqualTo(23); }
@Override public int compare(ScannerReport.Symbol o1, ScannerReport.Symbol o2) { if (o1.getDeclaration().getStartLine() == o2.getDeclaration().getStartLine()) { return Integer.compare(o1.getDeclaration().getStartOffset(), o2.getDeclaration().getStartOffset()); } else { return Integer.compare(o1.getDeclaration().getStartLine(), o2.getDeclaration().getStartLine()); } } }
private void assertDuplication(Duplication d, int originStartLine, int originEndLine, @Nullable Integer otherFileRef, int rangeStartLine, int rangeEndLine) { assertThat(d.getOriginPosition().getStartLine()).isEqualTo(originStartLine); assertThat(d.getOriginPosition().getEndLine()).isEqualTo(originEndLine); assertThat(d.getDuplicateList()).hasSize(1); if (otherFileRef != null) { assertThat(d.getDuplicate(0).getOtherFileRef()).isEqualTo(otherFileRef); } else { assertThat(d.getDuplicate(0).getOtherFileRef()).isEqualTo(0); } assertThat(d.getDuplicate(0).getRange().getStartLine()).isEqualTo(rangeStartLine); assertThat(d.getDuplicate(0).getRange().getEndLine()).isEqualTo(rangeEndLine); }
private void assertDuplication(Duplication d, int originStartLine, int originEndLine, int numDuplicates) { assertThat(d.getOriginPosition().getStartLine()).isEqualTo(originStartLine); assertThat(d.getOriginPosition().getEndLine()).isEqualTo(originEndLine); assertThat(d.getDuplicateList()).hasSize(numDuplicates); }
private void assertDuplicate(Duplicate d, int otherFileRef, int rangeStartLine, int rangeEndLine) { assertThat(d.getOtherFileRef()).isEqualTo(otherFileRef); assertThat(d.getRange().getStartLine()).isEqualTo(rangeStartLine); assertThat(d.getRange().getEndLine()).isEqualTo(rangeEndLine); }
@Deprecated @Override public Integer line() { return rawIssue.hasTextRange() ? rawIssue.getTextRange().getStartLine() : null; }
private DbCommons.TextRange.Builder convertTextRange(ScannerReport.TextRange sourceRange) { DbCommons.TextRange.Builder targetRange = DbCommons.TextRange.newBuilder(); targetRange.setStartLine(sourceRange.getStartLine()); targetRange.setStartOffset(sourceRange.getStartOffset()); targetRange.setEndLine(sourceRange.getEndLine()); targetRange.setEndOffset(sourceRange.getEndOffset()); return targetRange; } }
@Test public void testIssueRange() throws Exception { List<Issue> issues = result.issuesFor(result.inputFile("xources/hello/Single.xoo")); assertThat(issues).hasSize(1); Issue issue = issues.get(0); assertThat(issue.getMsg()).isEqualTo("Primary location"); assertThat(issue.getTextRange().getStartLine()).isEqualTo(6); assertThat(issue.getTextRange().getStartOffset()).isEqualTo(23); assertThat(issue.getTextRange().getEndLine()).isEqualTo(6); assertThat(issue.getTextRange().getEndOffset()).isEqualTo(50); }