Search in sources :

Example 1 with TokenType

use of com.sonar.sslr.api.TokenType 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

GenericTokenType (com.sonar.sslr.api.GenericTokenType)1 Token (com.sonar.sslr.api.Token)1 TokenType (com.sonar.sslr.api.TokenType)1 Trivia (com.sonar.sslr.api.Trivia)1 InputFile (org.sonar.api.batch.fs.InputFile)1 NewCpdTokens (org.sonar.api.batch.sensor.cpd.NewCpdTokens)1 NewHighlighting (org.sonar.api.batch.sensor.highlighting.NewHighlighting)1