Search in sources :

Example 21 with TextRange

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

the class IssuePublisher method applyFlows.

private static void applyFlows(Consumer<ScannerReport.Flow> consumer, ScannerReport.IssueLocation.Builder locationBuilder, ScannerReport.TextRange.Builder textRangeBuilder, Collection<Flow> flows) {
    ScannerReport.Flow.Builder flowBuilder = ScannerReport.Flow.newBuilder();
    for (Flow flow : flows) {
        if (flow.locations().isEmpty()) {
            return;
        }
        flowBuilder.clear();
        for (org.sonar.api.batch.sensor.issue.IssueLocation location : flow.locations()) {
            int locationComponentRef = ((DefaultInputComponent) location.inputComponent()).scannerId();
            locationBuilder.clear();
            locationBuilder.setComponentRef(locationComponentRef);
            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());
        }
        consumer.accept(flowBuilder.build());
    }
}
Also used : DefaultInputComponent(org.sonar.api.batch.fs.internal.DefaultInputComponent) TextRange(org.sonar.api.batch.fs.TextRange) Flow(org.sonar.api.batch.sensor.issue.Issue.Flow)

Example 22 with TextRange

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

the class AnalysisResult 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 = ((DefaultInputComponent) file).scannerId();
    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, 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) DefaultInputComponent(org.sonar.api.batch.fs.internal.DefaultInputComponent)

Example 23 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 startLine, int startLineOffset, int endLine, int endLineOffset, TypeOfText typeOfText) {
    checkInputFileNotNull();
    TextRange newRange;
    try {
        newRange = inputFile.newRange(startLine, startLineOffset, endLine, endLineOffset);
    } 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)

Example 24 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 25 with TextRange

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

the class DefaultCpdTokens method addToken.

@Override
public NewCpdTokens addToken(int startLine, int startLineOffset, int endLine, int endLineOffset, String image) {
    checkInputFileNotNull();
    TextRange newRange;
    try {
        newRange = inputFile.newRange(startLine, startLineOffset, endLine, endLineOffset);
    } catch (Exception e) {
        throw new IllegalArgumentException("Unable to register token in file " + inputFile, e);
    }
    return addToken(newRange, image);
}
Also used : 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