org.sonar.plugins.protobuf.api.visitors.SyntaxHighlighterVisitor.java Source code

Java tutorial

Introduction

Here is the source code for org.sonar.plugins.protobuf.api.visitors.SyntaxHighlighterVisitor.java

Source

/*
 * SonarQube Protocol Buffers Plugin
 * Copyright (C) 2015 SonarSource
 * sonarqube@googlegroups.com
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 3 of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02
 */
package org.sonar.plugins.protobuf.api.visitors;

import java.io.File;
import java.util.List;
import java.util.Set;

import org.sonar.api.batch.fs.FileSystem;
import org.sonar.api.component.ResourcePerspectives;
import org.sonar.api.source.Highlightable;
import org.sonar.plugins.protobuf.api.tree.ProtoBufUnitTree;
import org.sonar.plugins.protobuf.api.tree.lexical.SyntaxToken;
import org.sonar.plugins.protobuf.api.tree.lexical.SyntaxTrivia;
import org.sonar.protobuf.api.ProtoBufKeyword;

import com.google.common.collect.ImmutableSet;

public class SyntaxHighlighterVisitor extends AbstractHighlighterVisitor {

    private Highlightable.HighlightingBuilder highlighting;
    private final Set<String> keywords;

    public SyntaxHighlighterVisitor(ResourcePerspectives resourcePerspectives, FileSystem fs) {
        super(resourcePerspectives, fs);

        ImmutableSet.Builder<String> keywordsBuilder = ImmutableSet.builder();
        keywordsBuilder.add(ProtoBufKeyword.getKeywordValues());
        keywords = keywordsBuilder.build();
    }

    @Override
    public void visitToken(SyntaxToken token) {
        String text = token.text();
        if (keywords.contains(text)) {
            highlighting.highlight(start(token), end(token), "k");
        }
        scan(token);
    }

    @Override
    public void visitTrivia(SyntaxTrivia syntaxTrivia) {
        highlighting.highlight(start(syntaxTrivia), end(syntaxTrivia), "cppd");
    }

    @Override
    public List<Issue> analyze(File file, ProtoBufUnitTree tree) {
        Highlightable highlightable = resourcePerspectives.as(Highlightable.class, inputFromIOFile(file));
        highlighting = highlightable.newHighlighting();
        lineStart = startLines(file, this.charset);

        List<Issue> issues = super.analyze(file, tree);

        highlighting.done();
        return issues;
    }

}