Search in sources :

Example 1 with Highlightable

use of org.sonar.api.source.Highlightable 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 Highlightable

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

the class HighlightableBuilderTest method project_should_not_be_highlightable.

@Test
public void project_should_not_be_highlightable() {
    HighlightableBuilder builder = new HighlightableBuilder(mock(SensorStorage.class), mock(AnalysisMode.class));
    Highlightable perspective = builder.loadPerspective(Highlightable.class, new DefaultInputModule("struts"));
    assertThat(perspective).isNull();
}
Also used : DefaultInputModule(org.sonar.api.batch.fs.internal.DefaultInputModule) Highlightable(org.sonar.api.source.Highlightable) AnalysisMode(org.sonar.api.batch.AnalysisMode) SensorStorage(org.sonar.api.batch.sensor.internal.SensorStorage) Test(org.junit.Test)

Example 3 with Highlightable

use of org.sonar.api.source.Highlightable 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)

Example 4 with Highlightable

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

the class HighlightableBuilderTest method should_load_default_perspective.

@Test
public void should_load_default_perspective() {
    HighlightableBuilder builder = new HighlightableBuilder(mock(SensorStorage.class), mock(AnalysisMode.class));
    Highlightable perspective = builder.loadPerspective(Highlightable.class, new TestInputFileBuilder("foo", "foo.c").build());
    assertThat(perspective).isNotNull().isInstanceOf(DefaultHighlightable.class);
}
Also used : TestInputFileBuilder(org.sonar.api.batch.fs.internal.TestInputFileBuilder) Highlightable(org.sonar.api.source.Highlightable) AnalysisMode(org.sonar.api.batch.AnalysisMode) SensorStorage(org.sonar.api.batch.sensor.internal.SensorStorage) Test(org.junit.Test)

Aggregations

Highlightable (org.sonar.api.source.Highlightable)4 Test (org.junit.Test)3 File (java.io.File)2 AnalysisMode (org.sonar.api.batch.AnalysisMode)2 TestInputFileBuilder (org.sonar.api.batch.fs.internal.TestInputFileBuilder)2 SensorStorage (org.sonar.api.batch.sensor.internal.SensorStorage)2 HighlightingBuilder (org.sonar.api.source.Highlightable.HighlightingBuilder)2 IOException (java.io.IOException)1 InputFile (org.sonar.api.batch.fs.InputFile)1 DefaultInputFile (org.sonar.api.batch.fs.internal.DefaultInputFile)1 DefaultInputModule (org.sonar.api.batch.fs.internal.DefaultInputModule)1