Search in sources :

Example 6 with TextPointer

use of org.sonar.api.batch.fs.TextPointer 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 7 with TextPointer

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

the class MultilineIssuesSensor method parseIssues.

private static void parseIssues(InputFile file, SensorContext context, Map<Integer, TextPointer> startPositions, Map<Integer, TextPointer> endPositions) {
    int currentLine = 0;
    try {
        for (String lineStr : Files.readAllLines(file.path(), file.charset())) {
            currentLine++;
            Matcher m = START_ISSUE_PATTERN.matcher(lineStr);
            while (m.find()) {
                Integer issueId = Integer.parseInt(m.group(1));
                TextPointer newPointer = file.newPointer(currentLine, m.end());
                startPositions.put(issueId, newPointer);
            }
            m = END_ISSUE_PATTERN.matcher(lineStr);
            while (m.find()) {
                Integer issueId = Integer.parseInt(m.group(1));
                TextPointer newPointer = file.newPointer(currentLine, m.start());
                endPositions.put(issueId, newPointer);
            }
        }
    } catch (IOException e) {
        throw new IllegalStateException("Unable to read file", e);
    }
}
Also used : Matcher(java.util.regex.Matcher) TextPointer(org.sonar.api.batch.fs.TextPointer) IOException(java.io.IOException)

Example 8 with TextPointer

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

the class DefaultInputFile method selectLine.

@Override
public TextRange selectLine(int line) {
    checkMetadata();
    TextPointer startPointer = newPointer(line, 0);
    TextPointer endPointer = newPointer(line, lineLength(line));
    return newRangeValidPointers(startPointer, endPointer, true);
}
Also used : TextPointer(org.sonar.api.batch.fs.TextPointer)

Example 9 with TextPointer

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

the class DefaultInputFile method newRange.

@Override
public TextRange newRange(int startLine, int startLineOffset, int endLine, int endLineOffset) {
    checkMetadata();
    TextPointer start = newPointer(startLine, startLineOffset);
    TextPointer end = newPointer(endLine, endLineOffset);
    return newRangeValidPointers(start, end, false);
}
Also used : TextPointer(org.sonar.api.batch.fs.TextPointer)

Example 10 with TextPointer

use of org.sonar.api.batch.fs.TextPointer 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)

Aggregations

TextPointer (org.sonar.api.batch.fs.TextPointer)10 IOException (java.io.IOException)3 Matcher (java.util.regex.Matcher)3 TextRange (org.sonar.api.batch.fs.TextRange)3 ArrayList (java.util.ArrayList)2 TypeOfText (org.sonar.api.batch.sensor.highlighting.TypeOfText)2 ScannerReport (org.sonar.scanner.protocol.output.ScannerReport)2 HashMap (java.util.HashMap)1 Map (java.util.Map)1 CheckForNull (javax.annotation.CheckForNull)1 InputFile (org.sonar.api.batch.fs.InputFile)1 DefaultInputComponent (org.sonar.api.batch.fs.internal.DefaultInputComponent)1 DefaultInputFile (org.sonar.api.batch.fs.internal.DefaultInputFile)1 NewIssue (org.sonar.api.batch.sensor.issue.NewIssue)1 NewIssueLocation (org.sonar.api.batch.sensor.issue.NewIssueLocation)1 RuleKey (org.sonar.api.rule.RuleKey)1