Search in sources :

Example 1 with DefaultHighlighting

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

the class DefaultHighlightableTest method should_store_highlighting_rules.

@Test
public void should_store_highlighting_rules() {
    SensorStorage sensorStorage = mock(SensorStorage.class);
    DefaultInputFile inputFile = new TestInputFileBuilder("foo", "src/Foo.php").initMetadata("azerty\nbla bla").build();
    DefaultHighlightable highlightablePerspective = new DefaultHighlightable(inputFile, sensorStorage, mock(AnalysisMode.class));
    highlightablePerspective.newHighlighting().highlight(0, 6, "k").highlight(7, 10, "cppd").done();
    ArgumentCaptor<DefaultHighlighting> argCaptor = ArgumentCaptor.forClass(DefaultHighlighting.class);
    verify(sensorStorage).store(argCaptor.capture());
    assertThat(argCaptor.getValue().getSyntaxHighlightingRuleSet()).hasSize(2);
}
Also used : TestInputFileBuilder(org.sonar.api.batch.fs.internal.TestInputFileBuilder) DefaultInputFile(org.sonar.api.batch.fs.internal.DefaultInputFile) DefaultHighlighting(org.sonar.api.batch.sensor.highlighting.internal.DefaultHighlighting) AnalysisMode(org.sonar.api.batch.AnalysisMode) SensorStorage(org.sonar.api.batch.sensor.internal.SensorStorage) DefaultHighlightable(org.sonar.scanner.source.DefaultHighlightable) Test(org.junit.Test)

Example 2 with DefaultHighlighting

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

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

the class DefaultSensorStorageTest method should_skip_highlighting_on_pr_when_file_status_is_SAME.

@Test
public void should_skip_highlighting_on_pr_when_file_status_is_SAME() {
    DefaultInputFile file = new TestInputFileBuilder("foo", "src/Foo.php").setContents("// comment").setStatus(InputFile.Status.SAME).build();
    when(branchConfiguration.isPullRequest()).thenReturn(true);
    DefaultHighlighting highlighting = new DefaultHighlighting(underTest).onFile(file).highlight(1, 0, 1, 1, TypeOfText.KEYWORD);
    underTest.store(highlighting);
    assertThat(reportWriter.hasComponentData(FileStructure.Domain.SYNTAX_HIGHLIGHTINGS, file.scannerId())).isFalse();
}
Also used : TestInputFileBuilder(org.sonar.api.batch.fs.internal.TestInputFileBuilder) DefaultInputFile(org.sonar.api.batch.fs.internal.DefaultInputFile) DefaultHighlighting(org.sonar.api.batch.sensor.highlighting.internal.DefaultHighlighting) Test(org.junit.Test)

Example 4 with DefaultHighlighting

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

the class DefaultSensorStorage method store.

@Override
public void store(NewHighlighting newHighlighting) {
    DefaultHighlighting highlighting = (DefaultHighlighting) newHighlighting;
    ScannerReportWriter writer = reportPublisher.getWriter();
    DefaultInputFile inputFile = (DefaultInputFile) highlighting.inputFile();
    if (shouldSkipStorage(inputFile)) {
        return;
    }
    inputFile.setPublished(true);
    int componentRef = inputFile.scannerId();
    if (writer.hasComponentData(FileStructure.Domain.SYNTAX_HIGHLIGHTINGS, componentRef)) {
        throw new UnsupportedOperationException("Trying to save highlighting twice for the same file is not supported: " + inputFile);
    }
    final ScannerReport.SyntaxHighlightingRule.Builder builder = ScannerReport.SyntaxHighlightingRule.newBuilder();
    final ScannerReport.TextRange.Builder rangeBuilder = ScannerReport.TextRange.newBuilder();
    writer.writeComponentSyntaxHighlighting(componentRef, highlighting.getSyntaxHighlightingRuleSet().stream().map(input -> {
        builder.setRange(rangeBuilder.setStartLine(input.range().start().line()).setStartOffset(input.range().start().lineOffset()).setEndLine(input.range().end().line()).setEndOffset(input.range().end().lineOffset()).build());
        builder.setType(ScannerReportUtils.toProtocolType(input.getTextType()));
        return builder.build();
    }).collect(toList()));
}
Also used : DefaultInputFile(org.sonar.api.batch.fs.internal.DefaultInputFile) DefaultHighlighting(org.sonar.api.batch.sensor.highlighting.internal.DefaultHighlighting) TextRange(org.sonar.api.batch.fs.TextRange) ScannerReportWriter(org.sonar.scanner.protocol.output.ScannerReportWriter)

Example 5 with DefaultHighlighting

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

the class DefaultHighlightable method newHighlighting.

@Override
public HighlightingBuilder newHighlighting() {
    if (analysisMode.isIssues()) {
        return NO_OP_BUILDER;
    }
    DefaultHighlighting defaultHighlighting = new DefaultHighlighting(sensorStorage);
    defaultHighlighting.onFile(inputFile);
    return new DefaultHighlightingBuilder(defaultHighlighting);
}
Also used : DefaultHighlighting(org.sonar.api.batch.sensor.highlighting.internal.DefaultHighlighting)

Aggregations

DefaultHighlighting (org.sonar.api.batch.sensor.highlighting.internal.DefaultHighlighting)8 DefaultInputFile (org.sonar.api.batch.fs.internal.DefaultInputFile)5 Test (org.junit.Test)4 TestInputFileBuilder (org.sonar.api.batch.fs.internal.TestInputFileBuilder)4 ArrayList (java.util.ArrayList)1 AnalysisMode (org.sonar.api.batch.AnalysisMode)1 InputFile (org.sonar.api.batch.fs.InputFile)1 TextRange (org.sonar.api.batch.fs.TextRange)1 DefaultTextPointer (org.sonar.api.batch.fs.internal.DefaultTextPointer)1 TypeOfText (org.sonar.api.batch.sensor.highlighting.TypeOfText)1 SyntaxHighlightingRule (org.sonar.api.batch.sensor.highlighting.internal.SyntaxHighlightingRule)1 SensorStorage (org.sonar.api.batch.sensor.internal.SensorStorage)1 ScannerReportWriter (org.sonar.scanner.protocol.output.ScannerReportWriter)1 DefaultHighlightable (org.sonar.scanner.source.DefaultHighlightable)1