Search in sources :

Example 1 with TypeOfText

use of org.sonar.api.batch.sensor.highlighting.TypeOfText in project sonarqube by SonarSource.

the class SensorContextTester method highlightingTypeAt.

/**
 * Return list of syntax highlighting applied for a given position in a file. The result is a list because in theory you
 * can apply several styles to the same range.
 *
 * @param componentKey Key of the file like 'myProjectKey:src/foo.php'
 * @param line         Line you want to query
 * @param lineOffset   Offset you want to query.
 * @return List of styles applied to this position or empty list if there is no highlighting at this position.
 */
public List<TypeOfText> highlightingTypeAt(String componentKey, int line, int lineOffset) {
    DefaultHighlighting syntaxHighlightingData = (DefaultHighlighting) sensorStorage.highlightingByComponent.get(componentKey);
    if (syntaxHighlightingData == null) {
        return Collections.emptyList();
    }
    List<TypeOfText> result = new ArrayList<>();
    DefaultTextPointer location = new DefaultTextPointer(line, lineOffset);
    for (SyntaxHighlightingRule sortedRule : syntaxHighlightingData.getSyntaxHighlightingRuleSet()) {
        if (sortedRule.range().start().compareTo(location) <= 0 && sortedRule.range().end().compareTo(location) > 0) {
            result.add(sortedRule.getTextType());
        }
    }
    return result;
}
Also used : TypeOfText(org.sonar.api.batch.sensor.highlighting.TypeOfText) SyntaxHighlightingRule(org.sonar.api.batch.sensor.highlighting.internal.SyntaxHighlightingRule) DefaultHighlighting(org.sonar.api.batch.sensor.highlighting.internal.DefaultHighlighting) ArrayList(java.util.ArrayList) DefaultTextPointer(org.sonar.api.batch.fs.internal.DefaultTextPointer)

Example 2 with TypeOfText

use of org.sonar.api.batch.sensor.highlighting.TypeOfText 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 3 with TypeOfText

use of org.sonar.api.batch.sensor.highlighting.TypeOfText 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

ArrayList (java.util.ArrayList)3 TypeOfText (org.sonar.api.batch.sensor.highlighting.TypeOfText)3 TextPointer (org.sonar.api.batch.fs.TextPointer)2 TextRange (org.sonar.api.batch.fs.TextRange)2 ScannerReport (org.sonar.scanner.protocol.output.ScannerReport)2 DefaultInputComponent (org.sonar.api.batch.fs.internal.DefaultInputComponent)1 DefaultInputFile (org.sonar.api.batch.fs.internal.DefaultInputFile)1 DefaultTextPointer (org.sonar.api.batch.fs.internal.DefaultTextPointer)1 DefaultHighlighting (org.sonar.api.batch.sensor.highlighting.internal.DefaultHighlighting)1 SyntaxHighlightingRule (org.sonar.api.batch.sensor.highlighting.internal.SyntaxHighlightingRule)1