Search in sources :

Example 16 with TextRange

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

the class ModuleIssues method applyFlows.

private static void applyFlows(ScannerReport.Issue.Builder builder, ScannerReport.IssueLocation.Builder locationBuilder, ScannerReport.TextRange.Builder textRangeBuilder, Issue issue) {
    ScannerReport.Flow.Builder flowBuilder = ScannerReport.Flow.newBuilder();
    for (Flow flow : issue.flows()) {
        if (flow.locations().isEmpty()) {
            return;
        }
        flowBuilder.clear();
        for (org.sonar.api.batch.sensor.issue.IssueLocation location : flow.locations()) {
            locationBuilder.clear();
            locationBuilder.setComponentRef(((DefaultInputComponent) location.inputComponent()).batchId());
            String message = location.message();
            if (message != null) {
                locationBuilder.setMsg(message);
            }
            TextRange textRange = location.textRange();
            if (textRange != null) {
                locationBuilder.setTextRange(toProtobufTextRange(textRangeBuilder, textRange));
            }
            flowBuilder.addLocation(locationBuilder.build());
        }
        builder.addFlow(flowBuilder.build());
    }
}
Also used : TextRange(org.sonar.api.batch.fs.TextRange) Flow(org.sonar.api.batch.sensor.issue.Issue.Flow)

Example 17 with TextRange

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

the class DeprecatedDefaultSymbolTableTest method variable_length_references.

@Test
public void variable_length_references() {
    Symbolizable.SymbolTableBuilder symbolTableBuilder = new DeprecatedDefaultSymbolTable.Builder(new DefaultSymbolTable(null).onFile(inputFile));
    Symbol firstSymbol = symbolTableBuilder.newSymbol(10, 20);
    symbolTableBuilder.newReference(firstSymbol, 32);
    symbolTableBuilder.newReference(firstSymbol, 44, 47);
    DeprecatedDefaultSymbolTable symbolTable = (DeprecatedDefaultSymbolTable) symbolTableBuilder.build();
    assertThat(symbolTable.getWrapped().getReferencesBySymbol().keySet()).containsExactly(range(10, 20));
    Set<TextRange> references = symbolTable.getWrapped().getReferencesBySymbol().get(range(10, 20));
    assertThat(references).containsExactly(range(32, 42), range(44, 47));
}
Also used : Symbol(org.sonar.api.source.Symbol) DeprecatedDefaultSymbolTable(org.sonar.scanner.source.DeprecatedDefaultSymbolTable) TestInputFileBuilder(org.sonar.api.batch.fs.internal.TestInputFileBuilder) TextRange(org.sonar.api.batch.fs.TextRange) DeprecatedDefaultSymbolTable(org.sonar.scanner.source.DeprecatedDefaultSymbolTable) DefaultSymbolTable(org.sonar.api.batch.sensor.symbol.internal.DefaultSymbolTable) Symbolizable(org.sonar.api.source.Symbolizable) Test(org.junit.Test)

Example 18 with TextRange

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

the class JavaIssueTest method assertLocation.

private static void assertLocation(IssueLocation location, InputFile file, String message, int startLine, int startOffset, int endLine, int endOffset) {
    assertThat(location.inputComponent()).isEqualTo(file);
    assertThat(location.message()).isEqualTo(message);
    TextRange textRange = location.textRange();
    TextPointer start = textRange.start();
    assertThat(start.line()).isEqualTo(startLine);
    assertThat(start.lineOffset()).isEqualTo(startOffset);
    TextPointer end = textRange.end();
    assertThat(end.line()).isEqualTo(endLine);
    assertThat(end.lineOffset()).isEqualTo(endOffset);
}
Also used : TextPointer(org.sonar.api.batch.fs.TextPointer) TextRange(org.sonar.api.batch.fs.TextRange)

Example 19 with TextRange

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

the class OneCodeSmellIssuePerTestLineSensor method createIssues.

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 code smell issue is generated on each line of a test file")).save();
    }
}
Also used : RuleKey(org.sonar.api.rule.RuleKey) NewIssue(org.sonar.api.batch.sensor.issue.NewIssue) TextRange(org.sonar.api.batch.fs.TextRange)

Example 20 with TextRange

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

the class OneCodeSmellIssuePerLineSensor method createIssues.

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 code smell issue is generated on each line")).save();
    }
}
Also used : RuleKey(org.sonar.api.rule.RuleKey) NewIssue(org.sonar.api.batch.sensor.issue.NewIssue) TextRange(org.sonar.api.batch.fs.TextRange)

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