Search in sources :

Example 11 with TextRange

use of org.sonar.api.batch.fs.TextRange in project sonarqube by SonarSource.

the class IssuePublisher method createReportExternalIssue.

private static ScannerReport.ExternalIssue createReportExternalIssue(ExternalIssue issue, int componentRef) {
    // primary location of an external issue must have a message
    String primaryMessage = issue.primaryLocation().message();
    Severity severity = Severity.valueOf(issue.severity().name());
    IssueType issueType = IssueType.valueOf(issue.type().name());
    ScannerReport.ExternalIssue.Builder builder = ScannerReport.ExternalIssue.newBuilder();
    ScannerReport.IssueLocation.Builder locationBuilder = IssueLocation.newBuilder();
    ScannerReport.TextRange.Builder textRangeBuilder = ScannerReport.TextRange.newBuilder();
    // non-null fields
    builder.setSeverity(severity);
    builder.setType(issueType);
    builder.setEngineId(issue.engineId());
    builder.setRuleId(issue.ruleId());
    builder.setMsg(primaryMessage);
    locationBuilder.setMsg(primaryMessage);
    locationBuilder.setComponentRef(componentRef);
    TextRange primaryTextRange = issue.primaryLocation().textRange();
    if (primaryTextRange != null) {
        builder.setTextRange(toProtobufTextRange(textRangeBuilder, primaryTextRange));
    }
    Long effort = issue.remediationEffort();
    if (effort != null) {
        builder.setEffort(effort);
    }
    applyFlows(builder::addFlow, locationBuilder, textRangeBuilder, issue.flows());
    return builder.build();
}
Also used : ExternalIssue(org.sonar.api.batch.sensor.issue.ExternalIssue) IssueType(org.sonar.scanner.protocol.output.ScannerReport.IssueType) Severity(org.sonar.scanner.protocol.Constants.Severity) TextRange(org.sonar.api.batch.fs.TextRange) IssueLocation(org.sonar.scanner.protocol.output.ScannerReport.IssueLocation)

Example 12 with TextRange

use of org.sonar.api.batch.fs.TextRange in project sonarqube by SonarSource.

the class DefaultSensorStorage method store.

@Override
public void store(NewSymbolTable newSymbolTable) {
    DefaultSymbolTable symbolTable = (DefaultSymbolTable) newSymbolTable;
    ScannerReportWriter writer = reportPublisher.getWriter();
    DefaultInputFile inputFile = (DefaultInputFile) symbolTable.inputFile();
    if (shouldSkipStorage(inputFile)) {
        return;
    }
    inputFile.setPublished(true);
    int componentRef = inputFile.scannerId();
    if (writer.hasComponentData(FileStructure.Domain.SYMBOLS, componentRef)) {
        throw new UnsupportedOperationException("Trying to save symbol table twice for the same file is not supported: " + symbolTable.inputFile());
    }
    final ScannerReport.Symbol.Builder builder = ScannerReport.Symbol.newBuilder();
    final ScannerReport.TextRange.Builder rangeBuilder = ScannerReport.TextRange.newBuilder();
    writer.writeComponentSymbols(componentRef, symbolTable.getReferencesBySymbol().entrySet().stream().map(input -> {
        builder.clear();
        rangeBuilder.clear();
        TextRange declaration = input.getKey();
        builder.setDeclaration(rangeBuilder.setStartLine(declaration.start().line()).setStartOffset(declaration.start().lineOffset()).setEndLine(declaration.end().line()).setEndOffset(declaration.end().lineOffset()).build());
        for (TextRange reference : input.getValue()) {
            builder.addReference(rangeBuilder.setStartLine(reference.start().line()).setStartOffset(reference.start().lineOffset()).setEndLine(reference.end().line()).setEndOffset(reference.end().lineOffset()).build());
        }
        return builder.build();
    }).collect(Collectors.toList()));
}
Also used : DefaultInputFile(org.sonar.api.batch.fs.internal.DefaultInputFile) TextRange(org.sonar.api.batch.fs.TextRange) ScannerReportWriter(org.sonar.scanner.protocol.output.ScannerReportWriter) DefaultSymbolTable(org.sonar.api.batch.sensor.symbol.internal.DefaultSymbolTable)

Example 13 with TextRange

use of org.sonar.api.batch.fs.TextRange in project sonarlint-core by SonarSource.

the class DefaultFilterableIssueTests method delegate_textRange_to_rawIssue.

@Test
void delegate_textRange_to_rawIssue() {
    TextRange textRange = new DefaultTextRange(new DefaultTextPointer(0, 1), new DefaultTextPointer(2, 3));
    var activeRule = mock(ActiveRuleAdapter.class);
    when(activeRule.ruleKey()).thenReturn(RuleKey.of("foo", "S123"));
    var rawIssue = new Issue(activeRule, null, textRange, null, null, null);
    FilterableIssue underTest = new DefaultFilterableIssue(rawIssue, mock(InputComponent.class));
    assertThat(underTest.textRange()).usingRecursiveComparison().isEqualTo(textRange);
}
Also used : InputComponent(org.sonar.api.batch.fs.InputComponent) FilterableIssue(org.sonar.api.scan.issue.filter.FilterableIssue) Issue(org.sonarsource.sonarlint.core.analysis.api.Issue) FilterableIssue(org.sonar.api.scan.issue.filter.FilterableIssue) DefaultTextRange(org.sonarsource.sonarlint.core.analysis.container.analysis.filesystem.DefaultTextRange) TextRange(org.sonar.api.batch.fs.TextRange) DefaultTextRange(org.sonarsource.sonarlint.core.analysis.container.analysis.filesystem.DefaultTextRange) DefaultTextPointer(org.sonarsource.sonarlint.core.analysis.container.analysis.filesystem.DefaultTextPointer) Test(org.junit.jupiter.api.Test)

Example 14 with TextRange

use of org.sonar.api.batch.fs.TextRange in project sonarqube by SonarSource.

the class DefaultInputFileTest method checkValidRangeUsingGlobalOffset.

@Test
public void checkValidRangeUsingGlobalOffset() {
    Metadata metadata = new Metadata(2, 2, "", new int[] { 0, 10 }, 15);
    DefaultInputFile file = new DefaultInputFile(new DefaultIndexedFile("ABCDE", Paths.get("module"), "src/Foo.php"), 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);
}
Also used : TextRange(org.sonar.api.batch.fs.TextRange) Test(org.junit.Test)

Example 15 with TextRange

use of org.sonar.api.batch.fs.TextRange in project sonarqube by SonarSource.

the class TaskResult method highlightingTypeFor.

/**
   * Get highlighting types at a given position in an inputfile
   * @param lineOffset 0-based offset in file
   */
public List<TypeOfText> highlightingTypeFor(InputFile file, int line, int lineOffset) {
    int ref = reportComponents.get(((DefaultInputFile) file).key()).getRef();
    if (!reader.hasSyntaxHighlighting(ref)) {
        return Collections.emptyList();
    }
    TextPointer pointer = file.newPointer(line, lineOffset);
    List<TypeOfText> result = new ArrayList<>();
    try (CloseableIterator<ScannerReport.SyntaxHighlightingRule> it = reader.readComponentSyntaxHighlighting(ref)) {
        while (it.hasNext()) {
            ScannerReport.SyntaxHighlightingRule rule = it.next();
            TextRange ruleRange = toRange(file, rule.getRange());
            if (ruleRange.start().compareTo(pointer) <= 0 && ruleRange.end().compareTo(pointer) > 0) {
                result.add(ScannerReportUtils.toBatchType(rule.getType()));
            }
        }
    } catch (Exception e) {
        throw new IllegalStateException("Can't read syntax highlighting for " + file.absolutePath(), e);
    }
    return result;
}
Also used : TypeOfText(org.sonar.api.batch.sensor.highlighting.TypeOfText) TextPointer(org.sonar.api.batch.fs.TextPointer) ArrayList(java.util.ArrayList) ScannerReport(org.sonar.scanner.protocol.output.ScannerReport) TextRange(org.sonar.api.batch.fs.TextRange) DefaultInputFile(org.sonar.api.batch.fs.internal.DefaultInputFile)

Aggregations

TextRange (org.sonar.api.batch.fs.TextRange)25 Test (org.junit.Test)6 DefaultInputFile (org.sonar.api.batch.fs.internal.DefaultInputFile)4 NewIssue (org.sonar.api.batch.sensor.issue.NewIssue)4 RuleKey (org.sonar.api.rule.RuleKey)4 TextPointer (org.sonar.api.batch.fs.TextPointer)3 DefaultInputComponent (org.sonar.api.batch.fs.internal.DefaultInputComponent)3 ExternalIssue (org.sonar.api.batch.sensor.issue.ExternalIssue)3 DefaultSymbolTable (org.sonar.api.batch.sensor.symbol.internal.DefaultSymbolTable)3 Severity (org.sonar.scanner.protocol.Constants.Severity)3 ScannerReport (org.sonar.scanner.protocol.output.ScannerReport)3 IssueLocation (org.sonar.scanner.protocol.output.ScannerReport.IssueLocation)3 ArrayList (java.util.ArrayList)2 TypeOfText (org.sonar.api.batch.sensor.highlighting.TypeOfText)2 Issue (org.sonar.api.batch.sensor.issue.Issue)2 Flow (org.sonar.api.batch.sensor.issue.Issue.Flow)2 ScannerReportWriter (org.sonar.scanner.protocol.output.ScannerReportWriter)2 Collections.unmodifiableMap (java.util.Collections.unmodifiableMap)1 Map (java.util.Map)1 Set (java.util.Set)1