Search in sources :

Example 6 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)

Example 7 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 8 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 9 with TextRange

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

the class DefaultSymbolTable method newSymbol.

@Override
public NewSymbol newSymbol(int startLine, int startLineOffset, int endLine, int endLineOffset) {
    checkInputFileNotNull();
    TextRange declarationRange;
    try {
        declarationRange = inputFile.newRange(startLine, startLineOffset, endLine, endLineOffset);
    } catch (Exception e) {
        throw new IllegalArgumentException("Unable to create symbol on file " + inputFile, e);
    }
    return newSymbol(declarationRange);
}
Also used : TextRange(org.sonar.api.batch.fs.TextRange)

Example 10 with TextRange

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

the class DefaultHighlighting method highlight.

@Override
public DefaultHighlighting highlight(int startOffset, int endOffset, TypeOfText typeOfText) {
    checkInputFileNotNull();
    TextRange newRange;
    try {
        newRange = inputFile.newRange(startOffset, endOffset);
    } catch (Exception e) {
        throw new IllegalArgumentException("Unable to highlight file " + inputFile, e);
    }
    return highlight(newRange, typeOfText);
}
Also used : TextRange(org.sonar.api.batch.fs.TextRange)

Aggregations

TextRange (org.sonar.api.batch.fs.TextRange)10 Test (org.junit.Test)2 DefaultInputFile (org.sonar.api.batch.fs.internal.DefaultInputFile)2 DefaultSymbolTable (org.sonar.api.batch.sensor.symbol.internal.DefaultSymbolTable)2 ScannerReport (org.sonar.scanner.protocol.output.ScannerReport)2 ImmutableMap (com.google.common.collect.ImmutableMap)1 ArrayList (java.util.ArrayList)1 Map (java.util.Map)1 Set (java.util.Set)1 CheckForNull (javax.annotation.CheckForNull)1 TextPointer (org.sonar.api.batch.fs.TextPointer)1 DefaultInputComponent (org.sonar.api.batch.fs.internal.DefaultInputComponent)1 DefaultTextPointer (org.sonar.api.batch.fs.internal.DefaultTextPointer)1 TestInputFileBuilder (org.sonar.api.batch.fs.internal.TestInputFileBuilder)1 ActiveRule (org.sonar.api.batch.rule.ActiveRule)1 Rule (org.sonar.api.batch.rule.Rule)1 TypeOfText (org.sonar.api.batch.sensor.highlighting.TypeOfText)1 Issue (org.sonar.api.batch.sensor.issue.Issue)1 Flow (org.sonar.api.batch.sensor.issue.Issue.Flow)1 Symbol (org.sonar.api.source.Symbol)1