Search in sources :

Example 1 with Token

use of com.sonar.sslr.api.Token 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 2 with Token

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

Example 3 with Token

use of com.sonar.sslr.api.Token in project sonar-java by SonarSource.

the class JavaNodeBuilder method createTrivias.

private static List<SyntaxTrivia> createTrivias(List<Trivia> trivias) {
    List<SyntaxTrivia> result = Lists.newArrayList();
    for (Trivia trivia : trivias) {
        Token trivialToken = trivia.getToken();
        result.add(InternalSyntaxTrivia.create(trivialToken.getValue(), trivialToken.getLine(), trivialToken.getColumn()));
    }
    return result;
}
Also used : SyntaxTrivia(org.sonar.plugins.java.api.tree.SyntaxTrivia) InternalSyntaxTrivia(org.sonar.java.model.InternalSyntaxTrivia) Token(com.sonar.sslr.api.Token) InternalSyntaxToken(org.sonar.java.model.InternalSyntaxToken) SyntaxTrivia(org.sonar.plugins.java.api.tree.SyntaxTrivia) Trivia(com.sonar.sslr.api.Trivia) InternalSyntaxTrivia(org.sonar.java.model.InternalSyntaxTrivia)

Aggregations

Token (com.sonar.sslr.api.Token)3 Trivia (com.sonar.sslr.api.Trivia)3 GenericTokenType (com.sonar.sslr.api.GenericTokenType)2 TokenType (com.sonar.sslr.api.TokenType)2 InputFile (org.sonar.api.batch.fs.InputFile)2 NewCpdTokens (org.sonar.api.batch.sensor.cpd.NewCpdTokens)2 NewHighlighting (org.sonar.api.batch.sensor.highlighting.NewHighlighting)2 IOException (java.io.IOException)1 InternalSyntaxToken (org.sonar.java.model.InternalSyntaxToken)1 InternalSyntaxTrivia (org.sonar.java.model.InternalSyntaxTrivia)1 SyntaxTrivia (org.sonar.plugins.java.api.tree.SyntaxTrivia)1