Search in sources :

Example 1 with NewHighlighting

use of org.sonar.api.batch.sensor.highlighting.NewHighlighting in project sonar-java by SonarSource.

the class SonarComponentsTest method test_sonar_components.

@Test
public void test_sonar_components() {
    SensorContextTester sensorContextTester = spy(SensorContextTester.create(new File("")));
    DefaultFileSystem fs = sensorContextTester.fileSystem();
    JavaTestClasspath javaTestClasspath = mock(JavaTestClasspath.class);
    ImmutableList<File> javaTestClasspathList = ImmutableList.of();
    when(javaTestClasspath.getElements()).thenReturn(javaTestClasspathList);
    File file = new File("foo.java");
    fs.add(new TestInputFileBuilder("", "foo.java").build());
    FileLinesContext fileLinesContext = mock(FileLinesContext.class);
    when(fileLinesContextFactory.createFor(any(InputFile.class))).thenReturn(fileLinesContext);
    SonarComponents sonarComponents = new SonarComponents(fileLinesContextFactory, fs, null, javaTestClasspath, checkFactory);
    sonarComponents.setSensorContext(sensorContextTester);
    JavaCheck[] visitors = sonarComponents.checkClasses();
    assertThat(visitors).hasSize(0);
    Collection<JavaCheck> testChecks = sonarComponents.testCheckClasses();
    assertThat(testChecks).hasSize(0);
    assertThat(sonarComponents.getFileSystem()).isEqualTo(fs);
    assertThat(sonarComponents.getJavaClasspath()).isEmpty();
    assertThat(sonarComponents.getJavaTestClasspath()).isEqualTo(javaTestClasspathList);
    NewHighlighting newHighlighting = sonarComponents.highlightableFor(file);
    assertThat(newHighlighting).isNotNull();
    verify(sensorContextTester, times(1)).newHighlighting();
    NewSymbolTable newSymbolTable = sonarComponents.symbolizableFor(file);
    assertThat(newSymbolTable).isNotNull();
    verify(sensorContextTester, times(1)).newSymbolTable();
    assertThat(sonarComponents.fileLinesContextFor(file)).isEqualTo(fileLinesContext);
    JavaClasspath javaClasspath = mock(JavaClasspath.class);
    List<File> list = mock(List.class);
    when(javaClasspath.getElements()).thenReturn(list);
    sonarComponents = new SonarComponents(fileLinesContextFactory, fs, javaClasspath, javaTestClasspath, checkFactory);
    assertThat(sonarComponents.getJavaClasspath()).isEqualTo(list);
}
Also used : SensorContextTester(org.sonar.api.batch.sensor.internal.SensorContextTester) JavaCheck(org.sonar.plugins.java.api.JavaCheck) NewHighlighting(org.sonar.api.batch.sensor.highlighting.NewHighlighting) NewSymbolTable(org.sonar.api.batch.sensor.symbol.NewSymbolTable) InputFile(org.sonar.api.batch.fs.InputFile) DefaultInputFile(org.sonar.api.batch.fs.internal.DefaultInputFile) TestInputFileBuilder(org.sonar.api.batch.fs.internal.TestInputFileBuilder) InputFile(org.sonar.api.batch.fs.InputFile) DefaultInputFile(org.sonar.api.batch.fs.internal.DefaultInputFile) File(java.io.File) DefaultFileSystem(org.sonar.api.batch.fs.internal.DefaultFileSystem) FileLinesContext(org.sonar.api.measures.FileLinesContext) Test(org.junit.Test)

Example 2 with NewHighlighting

use of org.sonar.api.batch.sensor.highlighting.NewHighlighting 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;
            NewHighlighting highlighting = context.newHighlighting().onFile(inputFile);
            for (String line : lines) {
                lineNumber++;
                if (StringUtils.isBlank(line) || line.startsWith("#")) {
                    continue;
                }
                processLine(highlightingFile, lineNumber, highlighting, line);
            }
            highlighting.save();
        } catch (IOException e) {
            throw new IllegalStateException(e);
        }
    }
}
Also used : IOException(java.io.IOException) InputFile(org.sonar.api.batch.fs.InputFile) File(java.io.File) NewHighlighting(org.sonar.api.batch.sensor.highlighting.NewHighlighting)

Example 3 with NewHighlighting

use of org.sonar.api.batch.sensor.highlighting.NewHighlighting in project sonar-web by SonarSource.

the class HtmlTokensVisitor method highlightAndDuplicate.

private void highlightAndDuplicate() {
    if (!getHtmlSourceCode().shouldComputeMetric()) {
        return;
    }
    NewHighlighting highlighting = context.newHighlighting();
    InputFile inputFile = getHtmlSourceCode().inputFile();
    highlighting.onFile(inputFile);
    NewCpdTokens cpdTokens = context.newCpdTokens();
    cpdTokens.onFile(inputFile);
    String fileContent;
    try {
        fileContent = inputFile.contents();
    } catch (IOException e) {
        throw new IllegalStateException("Cannot read " + inputFile, e);
    }
    for (Token token : HtmlLexer.create(context.fileSystem().encoding()).lex(fileContent)) {
        TokenType tokenType = token.getType();
        if (!tokenType.equals(GenericTokenType.EOF)) {
            TokenLocation tokenLocation = new TokenLocation(token);
            cpdTokens.addToken(tokenLocation.startLine(), tokenLocation.startCharacter(), tokenLocation.endLine(), tokenLocation.endCharacter(), token.getValue());
        }
        if (tokenType.equals(HtmlTokenType.DOCTYPE)) {
            highlight(highlighting, token, TypeOfText.STRUCTURED_COMMENT);
        } else if (tokenType.equals(HtmlTokenType.EXPRESSION)) {
            highlight(highlighting, token, TypeOfText.ANNOTATION);
        } else if (tokenType.equals(HtmlTokenType.TAG)) {
            highlight(highlighting, token, TypeOfText.KEYWORD);
        } else if (tokenType.equals(HtmlTokenType.ATTRIBUTE)) {
            TokenLocation tokenLocation = new TokenLocation(token);
            highlighting.highlight(tokenLocation.startLine(), tokenLocation.startCharacter() + /* = */
            1, tokenLocation.endLine(), tokenLocation.endCharacter(), TypeOfText.STRING);
        }
        for (Trivia trivia : token.getTrivia()) {
            highlight(highlighting, trivia.getToken(), TypeOfText.COMMENT);
        }
    }
    highlighting.save();
    cpdTokens.save();
}
Also used : GenericTokenType(com.sonar.sslr.api.GenericTokenType) TokenType(com.sonar.sslr.api.TokenType) Token(com.sonar.sslr.api.Token) IOException(java.io.IOException) Trivia(com.sonar.sslr.api.Trivia) NewHighlighting(org.sonar.api.batch.sensor.highlighting.NewHighlighting) NewCpdTokens(org.sonar.api.batch.sensor.cpd.NewCpdTokens) InputFile(org.sonar.api.batch.fs.InputFile)

Example 4 with NewHighlighting

use of org.sonar.api.batch.sensor.highlighting.NewHighlighting in project sonar-web by SonarSource.

the class WebTokensVisitor method highlightAndDuplicate.

private void highlightAndDuplicate() {
    NewHighlighting highlighting = context.newHighlighting();
    InputFile inputFile = getWebSourceCode().inputFile();
    highlighting.onFile(inputFile);
    NewCpdTokens cpdTokens = context.newCpdTokens();
    cpdTokens.onFile(inputFile);
    for (Token token : WebLexer.create(context.fileSystem().encoding()).lex(inputFile.file())) {
        TokenType tokenType = token.getType();
        if (!tokenType.equals(GenericTokenType.EOF)) {
            TokenLocation tokenLocation = new TokenLocation(token);
            cpdTokens.addToken(tokenLocation.startLine(), tokenLocation.startCharacter(), tokenLocation.endLine(), tokenLocation.endCharacter(), token.getValue());
        }
        if (tokenType.equals(WebTokenType.DOCTYPE)) {
            highlight(highlighting, token, TypeOfText.STRUCTURED_COMMENT);
        } else if (tokenType.equals(WebTokenType.EXPRESSION)) {
            highlight(highlighting, token, TypeOfText.ANNOTATION);
        } else if (tokenType.equals(WebTokenType.TAG)) {
            highlight(highlighting, token, TypeOfText.KEYWORD);
        } else if (tokenType.equals(WebTokenType.ATTRIBUTE)) {
            TokenLocation tokenLocation = new TokenLocation(token);
            highlighting.highlight(tokenLocation.startLine(), tokenLocation.startCharacter() + /* = */
            1, tokenLocation.endLine(), tokenLocation.endCharacter(), TypeOfText.STRING);
        }
        for (Trivia trivia : token.getTrivia()) {
            highlight(highlighting, trivia.getToken(), TypeOfText.COMMENT);
        }
    }
    highlighting.save();
    cpdTokens.save();
}
Also used : GenericTokenType(com.sonar.sslr.api.GenericTokenType) TokenType(com.sonar.sslr.api.TokenType) Token(com.sonar.sslr.api.Token) Trivia(com.sonar.sslr.api.Trivia) NewHighlighting(org.sonar.api.batch.sensor.highlighting.NewHighlighting) NewCpdTokens(org.sonar.api.batch.sensor.cpd.NewCpdTokens) InputFile(org.sonar.api.batch.fs.InputFile)

Aggregations

InputFile (org.sonar.api.batch.fs.InputFile)4 NewHighlighting (org.sonar.api.batch.sensor.highlighting.NewHighlighting)4 GenericTokenType (com.sonar.sslr.api.GenericTokenType)2 Token (com.sonar.sslr.api.Token)2 TokenType (com.sonar.sslr.api.TokenType)2 Trivia (com.sonar.sslr.api.Trivia)2 File (java.io.File)2 IOException (java.io.IOException)2 NewCpdTokens (org.sonar.api.batch.sensor.cpd.NewCpdTokens)2 Test (org.junit.Test)1 DefaultFileSystem (org.sonar.api.batch.fs.internal.DefaultFileSystem)1 DefaultInputFile (org.sonar.api.batch.fs.internal.DefaultInputFile)1 TestInputFileBuilder (org.sonar.api.batch.fs.internal.TestInputFileBuilder)1 SensorContextTester (org.sonar.api.batch.sensor.internal.SensorContextTester)1 NewSymbolTable (org.sonar.api.batch.sensor.symbol.NewSymbolTable)1 FileLinesContext (org.sonar.api.measures.FileLinesContext)1 JavaCheck (org.sonar.plugins.java.api.JavaCheck)1