Search in sources :

Example 1 with HighlightingBuilder

use of org.sonar.api.source.Highlightable.HighlightingBuilder in project sonarqube by SonarSource.

the class SyntaxHighlightingSensor method processFileHighlighting.

private void processFileHighlighting(InputFile inputFile, SensorContext context) {
    File ioFile = inputFile.file();
    File highlightingFile = new File(ioFile.getParentFile(), ioFile.getName() + HIGHLIGHTING_EXTENSION);
    if (highlightingFile.exists()) {
        LOG.debug("Processing " + highlightingFile.getAbsolutePath());
        try {
            List<String> lines = FileUtils.readLines(highlightingFile, context.fileSystem().encoding().name());
            int lineNumber = 0;
            Highlightable highlightable = perspectives.as(Highlightable.class, inputFile);
            if (highlightable != null) {
                HighlightingBuilder highlightingBuilder = highlightable.newHighlighting();
                for (String line : lines) {
                    lineNumber++;
                    if (StringUtils.isBlank(line) || line.startsWith("#")) {
                        continue;
                    }
                    processLine(highlightingFile, lineNumber, highlightingBuilder, line);
                }
                highlightingBuilder.done();
            }
        } catch (IOException e) {
            throw new IllegalStateException(e);
        }
    }
}
Also used : HighlightingBuilder(org.sonar.api.source.Highlightable.HighlightingBuilder) Highlightable(org.sonar.api.source.Highlightable) IOException(java.io.IOException) InputFile(org.sonar.api.batch.fs.InputFile) File(java.io.File)

Example 2 with HighlightingBuilder

use of org.sonar.api.source.Highlightable.HighlightingBuilder in project sonarqube by SonarSource.

the class SyntaxHighlightingSensorTest method testExecution.

@Test
public void testExecution() throws IOException {
    File symbol = new File(baseDir, "src/foo.xoo.highlighting");
    FileUtils.write(symbol, "1:4:k\n12:15:cppd\n\n#comment");
    DefaultInputFile inputFile = new TestInputFileBuilder("foo", "src/foo.xoo").setLanguage("xoo").setModuleBaseDir(baseDir.toPath()).initMetadata(" xoo\nazertyazer\nfoo").build();
    context.fileSystem().add(inputFile);
    Highlightable highlightable = mock(Highlightable.class);
    when(perspectives.as(Highlightable.class, inputFile)).thenReturn(highlightable);
    HighlightingBuilder builder = mock(Highlightable.HighlightingBuilder.class);
    when(highlightable.newHighlighting()).thenReturn(builder);
    sensor.execute(context);
    verify(builder).highlight(1, 4, "k");
    verify(builder).highlight(12, 15, "cppd");
    verify(builder).done();
}
Also used : TestInputFileBuilder(org.sonar.api.batch.fs.internal.TestInputFileBuilder) DefaultInputFile(org.sonar.api.batch.fs.internal.DefaultInputFile) HighlightingBuilder(org.sonar.api.source.Highlightable.HighlightingBuilder) Highlightable(org.sonar.api.source.Highlightable) DefaultInputFile(org.sonar.api.batch.fs.internal.DefaultInputFile) File(java.io.File) Test(org.junit.Test)

Aggregations

File (java.io.File)2 Highlightable (org.sonar.api.source.Highlightable)2 HighlightingBuilder (org.sonar.api.source.Highlightable.HighlightingBuilder)2 IOException (java.io.IOException)1 Test (org.junit.Test)1 InputFile (org.sonar.api.batch.fs.InputFile)1 DefaultInputFile (org.sonar.api.batch.fs.internal.DefaultInputFile)1 TestInputFileBuilder (org.sonar.api.batch.fs.internal.TestInputFileBuilder)1