Merged code into one visitor

This commit is contained in:
Valentin Kipyatkov
2016-03-28 15:08:12 +03:00
parent f4e3e33bd3
commit 210626a6e1
4 changed files with 16 additions and 82 deletions
@@ -20,7 +20,9 @@ import com.intellij.lang.annotation.AnnotationHolder
import com.intellij.openapi.application.ApplicationManager
import com.intellij.openapi.util.TextRange
import com.intellij.psi.PsiElement
import org.jetbrains.kotlin.kdoc.psi.impl.KDocLink
import org.jetbrains.kotlin.lexer.KtTokens
import org.jetbrains.kotlin.psi.KtExpressionWithLabel
import org.jetbrains.kotlin.psi.KtLambdaExpression
import org.jetbrains.kotlin.psi.KtValueArgument
import org.jetbrains.kotlin.psi.KtVisitorVoid
@@ -31,16 +33,16 @@ internal class BeforeResolveHighlightingVisitor(private val holder: AnnotationHo
override fun visitElement(element: PsiElement) {
val elementType = element.node.elementType
val attributes = when (elementType) {
in KtTokens.SOFT_KEYWORDS -> {
val attributes = when {
element is KDocLink -> KotlinHighlightingColors.KDOC_LINK
elementType in KtTokens.SOFT_KEYWORDS -> {
when (elementType) {
in KtTokens.MODIFIER_KEYWORDS -> KotlinHighlightingColors.BUILTIN_ANNOTATION
else -> KotlinHighlightingColors.KEYWORD
}
}
KtTokens.SAFE_ACCESS -> KotlinHighlightingColors.SAFE_ACCESS
elementType == KtTokens.SAFE_ACCESS -> KotlinHighlightingColors.SAFE_ACCESS
else -> return
}
@@ -71,4 +73,11 @@ internal class BeforeResolveHighlightingVisitor(private val holder: AnnotationHo
val eq = argument.equalsToken ?: return
holder.createInfoAnnotation(TextRange(argumentName.startOffset, eq.endOffset), null).textAttributes = KotlinHighlightingColors.NAMED_ARGUMENT
}
override fun visitExpressionWithLabel(expression: KtExpressionWithLabel) {
val targetLabel = expression.getTargetLabel()
if (targetLabel != null) {
NameHighlighter.highlightName(holder, targetLabel, KotlinHighlightingColors.LABEL)
}
}
}
@@ -33,7 +33,6 @@ import com.intellij.openapi.project.Project
import com.intellij.psi.PsiElement
import com.intellij.psi.PsiFile
import com.intellij.psi.PsiRecursiveElementVisitor
import org.jetbrains.kotlin.idea.kdoc.KDocHighlightingVisitor
import org.jetbrains.kotlin.psi.KtFile
class KotlinBeforeResolveHighlightingPass(
@@ -45,15 +44,11 @@ class KotlinBeforeResolveHighlightingPass(
override fun doCollectInformation(progress: ProgressIndicator) {
val annotationHolder = AnnotationHolderImpl(AnnotationSession(file))
val visitors = listOf(
BeforeResolveHighlightingVisitor(annotationHolder),
LabelsHighlightingVisitor(annotationHolder),
KDocHighlightingVisitor(annotationHolder)
)
val visitor = BeforeResolveHighlightingVisitor(annotationHolder)
file.accept(object : PsiRecursiveElementVisitor(){
override fun visitElement(element: PsiElement) {
super.visitElement(element)
visitors.forEach { element.accept(it) }
element.accept(visitor)
}
})
this.annotationHolder = annotationHolder
@@ -1,39 +0,0 @@
/*
* Copyright 2010-2015 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jetbrains.kotlin.idea.highlighter;
import com.intellij.lang.annotation.AnnotationHolder;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.kotlin.psi.KtExpressionWithLabel;
import org.jetbrains.kotlin.psi.KtSimpleNameExpression;
import org.jetbrains.kotlin.psi.KtVisitorVoid;
class LabelsHighlightingVisitor extends KtVisitorVoid {
private final AnnotationHolder holder;
LabelsHighlightingVisitor(AnnotationHolder holder) {
this.holder = holder;
}
@Override
public void visitExpressionWithLabel(@NotNull KtExpressionWithLabel expression) {
KtSimpleNameExpression targetLabel = expression.getTargetLabel();
if (targetLabel != null) {
NameHighlighter.highlightName(holder, targetLabel, KotlinHighlightingColors.LABEL);
}
}
}
@@ -1,31 +0,0 @@
/*
* Copyright 2010-2015 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jetbrains.kotlin.idea.kdoc
import com.intellij.lang.annotation.AnnotationHolder
import com.intellij.psi.PsiElement
import com.intellij.psi.PsiElementVisitor
import org.jetbrains.kotlin.idea.highlighter.KotlinHighlightingColors
import org.jetbrains.kotlin.kdoc.psi.impl.KDocLink
class KDocHighlightingVisitor(private val holder: AnnotationHolder): PsiElementVisitor() {
override fun visitElement(element: PsiElement) {
if (element is KDocLink) {
holder.createInfoAnnotation(element, null).textAttributes = KotlinHighlightingColors.KDOC_LINK
}
}
}