@Override public int compareTo(TextPointer o) { if (this.line == o.line()) { return Integer.compare(this.lineOffset, o.lineOffset()); } return Integer.compare(this.line, o.line()); }
@Override public DefaultCpdTokens addToken(TextRange range, String image) { requireNonNull(range, "Range should not be null"); requireNonNull(image, "Image should not be null"); checkInputFileNotNull(); if (isExcludedForDuplication()) { return this; } checkState(lastRange == null || lastRange.end().compareTo(range.start()) <= 0, "Tokens of file %s should be provided in order.\nPrevious token: %s\nLast token: %s", inputFile, lastRange, range); String value = image; int line = range.start().line(); if (line != startLine) { addNewTokensLine(result, startIndex, currentIndex, startLine, sb); startIndex = currentIndex + 1; startLine = line; } currentIndex++; sb.append(value); lastRange = range; return this; }
@Override public DefaultSignificantCode addRange(TextRange range) { Preconditions.checkState(this.inputFile != null, "addRange() should be called after on()"); int line = range.start().line(); Preconditions.checkArgument(line == range.end().line(), "Ranges of significant code must be located in a single line"); Preconditions.checkState(!significantCodePerLine.containsKey(line), "Significant code was already reported for line '%s'. Can only report once per line.", line); significantCodePerLine.put(line, range); return this; }
private static void createIssues(InputFile file, SensorContext context, String repo) { RuleKey ruleKey = RuleKey.of(repo, RULE_KEY); for (int line = 1; line <= file.lines(); line++) { TextRange text = file.selectLine(line); // do not count empty lines, which can be a pain with end-of-file return if (text.end().lineOffset() == 0) { continue; } NewIssue newIssue = context.newIssue(); newIssue .forRule(ruleKey) .at(newIssue.newLocation() .on(file) .at(text) .message("This bug issue is generated on each line")) .save(); } }
private static TextRange newRangeValidPointers(TextPointer start, TextPointer end, boolean acceptEmptyRange) { Preconditions.checkArgument(acceptEmptyRange ? (start.compareTo(end) <= 0) : (start.compareTo(end) < 0), "Start pointer %s should be before end pointer %s", start, end); return new DefaultTextRange(start, end); }
private static boolean isOdd(FilterableIssue issue) { TextRange textRange = issue.textRange(); return textRange != null && textRange.start().line() % 2 == 1; } }
@Override public Integer getStartLineOffset() { return textRange != null ? textRange.start().lineOffset() : null; }
private void checkValid(TextPointer pointer, String owner) { Preconditions.checkArgument(pointer.line() >= 1, "%s is not a valid line for a file", pointer.line()); Preconditions.checkArgument(pointer.line() <= this.metadata.lines(), "%s is not a valid line for %s. File %s has %s line(s)", pointer.line(), owner, this, metadata.lines()); Preconditions.checkArgument(pointer.lineOffset() >= 0, "%s is not a valid line offset for a file", pointer.lineOffset()); int lineLength = lineLength(pointer.line()); Preconditions.checkArgument(pointer.lineOffset() <= lineLength, "%s is not a valid line offset for %s. File %s has %s character(s) at line %s", pointer.lineOffset(), owner, this, lineLength, pointer.line()); }
private static boolean noSonar(DefaultInputComponent inputComponent, Issue issue) { TextRange textRange = issue.primaryLocation().textRange(); return inputComponent.isFile() && textRange != null && ((DefaultInputFile) inputComponent).hasNoSonarAt(textRange.start().line()) && !StringUtils.containsIgnoreCase(issue.ruleKey().rule(), "nosonar"); }
@Override public Integer getEndLineOffset() { return textRange != null ? textRange.end().lineOffset() : null; }
@Override public DefaultCpdTokens addToken(TextRange range, String image) { requireNonNull(range, "Range should not be null"); requireNonNull(image, "Image should not be null"); checkInputFileNotNull(); if (isExcludedForDuplication()) { return this; } checkState(lastRange == null || lastRange.end().compareTo(range.start()) <= 0, "Tokens of file %s should be provided in order.\nPrevious token: %s\nLast token: %s", inputFile, lastRange, range); String value = image; int line = range.start().line(); if (line != startLine) { addNewTokensLine(result, startIndex, currentIndex, startLine, sb); startIndex = currentIndex + 1; startLine = line; } currentIndex++; sb.append(value); lastRange = range; return this; }
/** * Return list of symbol references ranges for the symbol at a given position in a file. * * @param componentKey Key of the file like 'myProjectKey:src/foo.php' * @param line Line you want to query * @param lineOffset Offset you want to query. * @return List of references for the symbol (potentially empty) or null if there is no symbol at this position. */ @CheckForNull public Collection<TextRange> referencesForSymbolAt(String componentKey, int line, int lineOffset) { DefaultSymbolTable symbolTable = sensorStorage.symbolsPerComponent.get(componentKey); if (symbolTable == null) { return null; } DefaultTextPointer location = new DefaultTextPointer(line, lineOffset); for (Map.Entry<TextRange, Set<TextRange>> symbol : symbolTable.getReferencesBySymbol().entrySet()) { if (symbol.getKey().start().compareTo(location) <= 0 && symbol.getKey().end().compareTo(location) > 0) { return symbol.getValue(); } } return null; }
private static ScannerReport.TextRange toProtobufTextRange(ScannerReport.TextRange.Builder textRangeBuilder, TextRange primaryTextRange) { textRangeBuilder.clear(); textRangeBuilder.setStartLine(primaryTextRange.start().line()); textRangeBuilder.setStartOffset(primaryTextRange.start().lineOffset()); textRangeBuilder.setEndLine(primaryTextRange.end().line()); textRangeBuilder.setEndOffset(primaryTextRange.end().lineOffset()); return textRangeBuilder.build(); }
@Test public void build_file_issue() { SensorStorage storage = mock(SensorStorage.class); DefaultExternalIssue issue = new DefaultExternalIssue(project, storage) .at(new DefaultIssueLocation() .on(inputFile) .at(inputFile.selectLine(1)) .message("Wrong way!")) .forRule(RuleKey.of("repo", "rule")) .remediationEffortMinutes(10l) .type(RuleType.BUG) .severity(Severity.BLOCKER); assertThat(issue.primaryLocation().inputComponent()).isEqualTo(inputFile); assertThat(issue.ruleKey()).isEqualTo(RuleKey.of("external_repo", "rule")); assertThat(issue.engineId()).isEqualTo("repo"); assertThat(issue.ruleId()).isEqualTo("rule"); assertThat(issue.primaryLocation().textRange().start().line()).isEqualTo(1); assertThat(issue.remediationEffort()).isEqualTo(10l); assertThat(issue.type()).isEqualTo(RuleType.BUG); assertThat(issue.severity()).isEqualTo(Severity.BLOCKER); assertThat(issue.primaryLocation().message()).isEqualTo("Wrong way!"); issue.save(); verify(storage).store(issue); }
private static void createIssues(InputFile file, SensorContext context, String repo) { RuleKey ruleKey = RuleKey.of(repo, RULE_KEY); for (int line = 1; line <= file.lines(); line++) { TextRange text = file.selectLine(line); // do not count empty lines, which can be a pain with end-of-file return if (text.end().lineOffset() == 0) { continue; } NewIssue newIssue = context.newIssue(); newIssue .forRule(ruleKey) .at(newIssue.newLocation() .on(file) .at(text) .message("This bug issue is generated on each line")) .save(); } }
@Override public NewSymbol newSymbol(TextRange range) { checkInputFileNotNull(); TreeSet<TextRange> references = new TreeSet<>((o1, o2) -> o1.start().compareTo(o2.start())); referencesBySymbol.put(range, references); return new DefaultSymbol(inputFile, range, references); }
@Test public void checkValidRangeUsingGlobalOffset() { Metadata metadata = new Metadata(2, 2, "", new int[] {0, 10}, new int[] {9, 15}, 16); DefaultInputFile file = new DefaultInputFile(new DefaultIndexedFile("ABCDE", Paths.get("module"), MODULE_RELATIVE_PATH, null), f -> f.setMetadata(metadata)); TextRange newRange = file.newRange(10, 13); assertThat(newRange.start().line()).isEqualTo(2); assertThat(newRange.start().lineOffset()).isEqualTo(0); assertThat(newRange.end().line()).isEqualTo(2); assertThat(newRange.end().lineOffset()).isEqualTo(3); }
@Test public void build_file_issue() { SensorStorage storage = mock(SensorStorage.class); DefaultIssue issue = new DefaultIssue(project, storage) .at(new DefaultIssueLocation() .on(inputFile) .at(inputFile.selectLine(1)) .message("Wrong way!")) .forRule(RuleKey.of("repo", "rule")) .gap(10.0); assertThat(issue.primaryLocation().inputComponent()).isEqualTo(inputFile); assertThat(issue.ruleKey()).isEqualTo(RuleKey.of("repo", "rule")); assertThat(issue.primaryLocation().textRange().start().line()).isEqualTo(1); assertThat(issue.gap()).isEqualTo(10.0); assertThat(issue.primaryLocation().message()).isEqualTo("Wrong way!"); issue.save(); verify(storage).store(issue); }
} else { TextRange range = targetFile.selectLine(lineNumber); if (IS_WINDOWS && (getContext().runtime().getProduct() == SonarProduct.SONARLINT) && (range.end().lineOffset() > 1)) { location.at(targetFile.newRange(lineNumber, 0, lineNumber, range.end().lineOffset() - 1)); } else { location.at(range);