Extracted BackingFieldHighlightingVisitor and corresponding abstract superclasses from JetPsiChecker.

This commit is contained in:
Evgeny Gerashchenko
2012-03-29 14:00:52 +04:00
parent 99a8ab6b01
commit 3328dda81a
4 changed files with 126 additions and 40 deletions
@@ -0,0 +1,29 @@
/*
* Copyright 2010-2012 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.jet.plugin.highlighter;
import com.intellij.lang.annotation.AnnotationHolder;
import org.jetbrains.jet.lang.resolve.BindingContext;
abstract class AfterAnalysisHighlightingVisitor extends HighlightingVisitor {
protected BindingContext bindingContext;
protected AfterAnalysisHighlightingVisitor(AnnotationHolder holder, BindingContext bindingContext) {
super(holder);
this.bindingContext = bindingContext;
}
}
@@ -0,0 +1,68 @@
/*
* Copyright 2010-2012 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.jet.plugin.highlighter;
import com.intellij.lang.annotation.AnnotationHolder;
import com.intellij.psi.PsiElement;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.jet.lang.descriptors.PropertyDescriptor;
import org.jetbrains.jet.lang.descriptors.VariableDescriptor;
import org.jetbrains.jet.lang.psi.*;
import org.jetbrains.jet.lang.resolve.BindingContext;
class BackingFieldHighlightingVisitor extends AfterAnalysisHighlightingVisitor {
BackingFieldHighlightingVisitor(AnnotationHolder holder, BindingContext bindingContext) {
super(holder, bindingContext);
}
@Override
public void visitProperty(@NotNull JetProperty property) {
VariableDescriptor propertyDescriptor = bindingContext.get(BindingContext.VARIABLE, property);
if (propertyDescriptor instanceof PropertyDescriptor) {
Boolean backingFieldRequired = bindingContext.get(BindingContext.BACKING_FIELD_REQUIRED, (PropertyDescriptor)propertyDescriptor);
if (Boolean.TRUE.equals(backingFieldRequired)) {
putBackingFieldAnnotation(holder, property);
}
}
}
@Override
public void visitParameter(@NotNull JetParameter parameter) {
PropertyDescriptor propertyDescriptor = bindingContext.get(BindingContext.PRIMARY_CONSTRUCTOR_PARAMETER, parameter);
if (propertyDescriptor != null) {
Boolean backingFieldRequired = bindingContext.get(BindingContext.BACKING_FIELD_REQUIRED, propertyDescriptor);
if (Boolean.TRUE.equals(backingFieldRequired)) {
putBackingFieldAnnotation(holder, parameter);
}
}
}
@Override
public void visitJetElement(@NotNull JetElement element) {
element.acceptChildren(this);
}
private static void putBackingFieldAnnotation(@NotNull AnnotationHolder holder, @NotNull JetNamedDeclaration element) {
PsiElement nameIdentifier = element.getNameIdentifier();
if (nameIdentifier != null) {
holder.createInfoAnnotation(
nameIdentifier,
"This property has a backing field")
.setTextAttributes(JetHighlightingColors.INSTANCE_PROPERTY_WITH_BACKING_FIELD);
}
}
}
@@ -0,0 +1,28 @@
/*
* Copyright 2010-2012 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.jet.plugin.highlighter;
import com.intellij.lang.annotation.AnnotationHolder;
import org.jetbrains.jet.lang.psi.JetVisitorVoid;
abstract class HighlightingVisitor extends JetVisitorVoid {
protected AnnotationHolder holder;
protected HighlightingVisitor(AnnotationHolder holder) {
this.holder = holder;
}
}
@@ -32,7 +32,6 @@ import com.intellij.psi.PsiReference;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.jet.lang.descriptors.DeclarationDescriptor;
import org.jetbrains.jet.lang.descriptors.PropertyDescriptor;
import org.jetbrains.jet.lang.descriptors.ValueParameterDescriptor;
import org.jetbrains.jet.lang.descriptors.VariableDescriptor;
import org.jetbrains.jet.lang.diagnostics.*;
@@ -84,7 +83,7 @@ public class JetPsiChecker implements Annotator {
}
}
highlightBackingFields(holder, file, bindingContext);
file.acceptChildren(new BackingFieldHighlightingVisitor(holder, bindingContext));
file.acceptChildren(new JetVisitorVoid() {
@Override
@@ -261,42 +260,4 @@ public class JetPsiChecker implements Annotator {
if (textRanges.isEmpty()) return null;
return holder.createErrorAnnotation(textRanges.get(0), getMessage(diagnostic));
}
private void highlightBackingFields(@NotNull final AnnotationHolder holder, @NotNull JetFile file, @NotNull final BindingContext bindingContext) {
file.acceptChildren(new JetVisitorVoid() {
@Override
public void visitProperty(@NotNull JetProperty property) {
VariableDescriptor propertyDescriptor = bindingContext.get(BindingContext.VARIABLE, property);
if (propertyDescriptor instanceof PropertyDescriptor) {
if (bindingContext.get(BindingContext.BACKING_FIELD_REQUIRED, (PropertyDescriptor) propertyDescriptor)) {
putBackingfieldAnnotation(holder, property);
}
}
}
@Override
public void visitParameter(@NotNull JetParameter parameter) {
PropertyDescriptor propertyDescriptor = bindingContext.get(BindingContext.PRIMARY_CONSTRUCTOR_PARAMETER, parameter);
if (propertyDescriptor != null && bindingContext.get(BindingContext.BACKING_FIELD_REQUIRED, propertyDescriptor)) {
putBackingfieldAnnotation(holder, parameter);
}
}
@Override
public void visitJetElement(@NotNull JetElement element) {
element.acceptChildren(this);
}
});
}
private void putBackingfieldAnnotation(@NotNull AnnotationHolder holder, @NotNull JetNamedDeclaration element) {
PsiElement nameIdentifier = element.getNameIdentifier();
if (nameIdentifier != null) {
holder.createInfoAnnotation(
nameIdentifier,
"This property has a backing field")
.setTextAttributes(JetHighlightingColors.INSTANCE_PROPERTY_WITH_BACKING_FIELD);
}
}
}