From a1f96bf9167a06a43b3a895cc7d1be102444fd9f Mon Sep 17 00:00:00 2001 From: Max Medvedev Date: Mon, 17 Dec 2012 18:55:32 +0400 Subject: [PATCH] IDEA-97659 Groovy color style for "Method declaration" is not in use --- .../groovy/annotator/GrHighlightUtil.java | 49 +++++++++++++------ .../GrKeywordAndDeclarationHighlighter.java | 2 +- .../annotator/GrReferenceHighlighter.java | 4 +- .../highlighter/DefaultHighlighter.java | 7 +++ .../highlighter/GroovyColorsAndFontsPage.java | 7 ++- .../InnerClassConstructorThis.groovy | 2 +- 6 files changed, 52 insertions(+), 19 deletions(-) diff --git a/plugins/groovy/src/org/jetbrains/plugins/groovy/annotator/GrHighlightUtil.java b/plugins/groovy/src/org/jetbrains/plugins/groovy/annotator/GrHighlightUtil.java index 954c99bf63be..e8859d8d868e 100644 --- a/plugins/groovy/src/org/jetbrains/plugins/groovy/annotator/GrHighlightUtil.java +++ b/plugins/groovy/src/org/jetbrains/plugins/groovy/annotator/GrHighlightUtil.java @@ -27,7 +27,7 @@ import com.intellij.psi.util.PsiTreeUtil; import com.intellij.util.containers.ContainerUtil; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; -import org.jetbrains.plugins.groovy.highlighter.DefaultHighlighter; +import org.jetbrains.plugins.groovy.lang.lexer.GroovyTokenTypes; import org.jetbrains.plugins.groovy.lang.psi.GrReferenceElement; import org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.modifiers.GrModifierList; import org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable; @@ -47,6 +47,8 @@ import org.jetbrains.plugins.groovy.lang.resolve.ResolveUtil; import java.util.Set; +import static org.jetbrains.plugins.groovy.highlighter.DefaultHighlighter.*; + /** * @author Max Medvedev */ @@ -103,44 +105,63 @@ public class GrHighlightUtil { } @Nullable - static TextAttributesKey getDeclarationHighlightingAttribute(PsiElement resolved) { + static TextAttributesKey getDeclarationHighlightingAttribute(PsiElement resolved, @Nullable PsiElement refElement) { if (resolved instanceof PsiField || resolved instanceof GrVariable && ResolveUtil.isScriptField((GrVariable)resolved)) { boolean isStatic = ((PsiVariable)resolved).hasModifierProperty(PsiModifier.STATIC); - return isStatic ? DefaultHighlighter.STATIC_FIELD : DefaultHighlighter.INSTANCE_FIELD; + return isStatic ? STATIC_FIELD : INSTANCE_FIELD; } else if (resolved instanceof GrAccessorMethod) { boolean isStatic = ((GrAccessorMethod)resolved).hasModifierProperty(PsiModifier.STATIC); - return isStatic ? DefaultHighlighter.STATIC_PROPERTY_REFERENCE : DefaultHighlighter.INSTANCE_PROPERTY_REFERENCE; + return isStatic ? STATIC_PROPERTY_REFERENCE : INSTANCE_PROPERTY_REFERENCE; } else if (resolved instanceof PsiMethod) { - if (!((PsiMethod)resolved).isConstructor()) { - boolean isStatic = ((PsiMethod)resolved).hasModifierProperty(PsiModifier.STATIC); - if (GroovyPropertyUtils.isSimplePropertyAccessor((PsiMethod)resolved)) { - return isStatic ? DefaultHighlighter.STATIC_PROPERTY_REFERENCE : DefaultHighlighter.INSTANCE_PROPERTY_REFERENCE; + if (((PsiMethod)resolved).isConstructor()) { + if (refElement != null) { + if (refElement.getNode().getElementType() == GroovyTokenTypes.kTHIS || //don't highlight this() or super() + refElement.getNode().getElementType() == GroovyTokenTypes.kSUPER) { + return null; + } + else { + return CONSTRUCTOR_CALL; + } } else { - return isStatic ? DefaultHighlighter.STATIC_METHOD_ACCESS : DefaultHighlighter.METHOD_CALL; + return CONSTRUCTOR_DECLARATION; + } + } + else { + boolean isStatic = ((PsiMethod)resolved).hasModifierProperty(PsiModifier.STATIC); + if (GroovyPropertyUtils.isSimplePropertyAccessor((PsiMethod)resolved)) { + return isStatic ? STATIC_PROPERTY_REFERENCE : INSTANCE_PROPERTY_REFERENCE; + } + else { + if (refElement != null) { + return isStatic ? STATIC_METHOD_ACCESS : METHOD_CALL; + } + else { + return METHOD_DECLARATION; + } } } } else if (resolved instanceof PsiTypeParameter) { - return DefaultHighlighter.TYPE_PARAMETER; + return TYPE_PARAMETER; } else if (resolved instanceof PsiClass) { if (((PsiClass)resolved).isAnnotationType()) { - return DefaultHighlighter.ANNOTATION; + return ANNOTATION; } else { - return DefaultHighlighter.CLASS_REFERENCE; + return CLASS_REFERENCE; } } else if (resolved instanceof GrParameter) { boolean reassigned = isReassigned((GrParameter)resolved); - return reassigned ? DefaultHighlighter.REASSIGNED_PARAMETER : DefaultHighlighter.PARAMETER; + return reassigned ? REASSIGNED_PARAMETER : PARAMETER; } else if (resolved instanceof GrVariable) { boolean reassigned = isReassigned((GrVariable)resolved); - return reassigned ? DefaultHighlighter.REASSIGNED_LOCAL_VARIABLE : DefaultHighlighter.LOCAL_VARIABLE; + return reassigned ? REASSIGNED_LOCAL_VARIABLE : LOCAL_VARIABLE; } return null; } diff --git a/plugins/groovy/src/org/jetbrains/plugins/groovy/annotator/GrKeywordAndDeclarationHighlighter.java b/plugins/groovy/src/org/jetbrains/plugins/groovy/annotator/GrKeywordAndDeclarationHighlighter.java index a3d3c44638a9..6d02d25a0efe 100644 --- a/plugins/groovy/src/org/jetbrains/plugins/groovy/annotator/GrKeywordAndDeclarationHighlighter.java +++ b/plugins/groovy/src/org/jetbrains/plugins/groovy/annotator/GrKeywordAndDeclarationHighlighter.java @@ -136,6 +136,6 @@ public class GrKeywordAndDeclarationHighlighter extends TextEditorHighlightingPa //don't highlight local vars and parameters here because their highlighting needs index. if (GroovyRefactoringUtil.isLocalVariable(parent) || parent instanceof GrParameter) return null; - return GrHighlightUtil.getDeclarationHighlightingAttribute(parent); + return GrHighlightUtil.getDeclarationHighlightingAttribute(parent, null); } } diff --git a/plugins/groovy/src/org/jetbrains/plugins/groovy/annotator/GrReferenceHighlighter.java b/plugins/groovy/src/org/jetbrains/plugins/groovy/annotator/GrReferenceHighlighter.java index d535e352c03f..4e958017c951 100644 --- a/plugins/groovy/src/org/jetbrains/plugins/groovy/annotator/GrReferenceHighlighter.java +++ b/plugins/groovy/src/org/jetbrains/plugins/groovy/annotator/GrReferenceHighlighter.java @@ -82,7 +82,7 @@ public class GrReferenceHighlighter extends TextEditorHighlightingPass { super.visitVariable(variable); if (GroovyRefactoringUtil.isLocalVariable(variable) || variable instanceof GrParameter) { - final TextAttributesKey attribute = GrHighlightUtil.getDeclarationHighlightingAttribute(variable); + final TextAttributesKey attribute = GrHighlightUtil.getDeclarationHighlightingAttribute(variable, null); if (attribute != null) { final PsiElement nameElement = variable.getNameIdentifierGroovy(); myInfos.add(HighlightInfo.createHighlightInfo(HighlightInfoType.INFORMATION, nameElement, null, attribute)); @@ -93,7 +93,7 @@ public class GrReferenceHighlighter extends TextEditorHighlightingPass { private void visit(GrReferenceElement element) { final PsiElement resolved = element.resolve(); - final TextAttributesKey attribute = GrHighlightUtil.getDeclarationHighlightingAttribute(resolved); + final TextAttributesKey attribute = GrHighlightUtil.getDeclarationHighlightingAttribute(resolved, element); if (attribute != null) { final PsiElement refNameElement = GrHighlightUtil.getElementToHighlight(element); myInfos.add(HighlightInfo.createHighlightInfo(HighlightInfoType.INFORMATION, refNameElement, null, attribute)); diff --git a/plugins/groovy/src/org/jetbrains/plugins/groovy/highlighter/DefaultHighlighter.java b/plugins/groovy/src/org/jetbrains/plugins/groovy/highlighter/DefaultHighlighter.java index 85134718d550..84f16ad06749 100644 --- a/plugins/groovy/src/org/jetbrains/plugins/groovy/highlighter/DefaultHighlighter.java +++ b/plugins/groovy/src/org/jetbrains/plugins/groovy/highlighter/DefaultHighlighter.java @@ -115,12 +115,19 @@ public class DefaultHighlighter { TextAttributesKey.createTextAttributesKey("Groovy method declaration", HighlightInfoType.METHOD_DECLARATION.getAttributesKey().getDefaultAttributes()); + public static final TextAttributesKey CONSTRUCTOR_DECLARATION = TextAttributesKey + .createTextAttributesKey("Groovy constructor declaration", + HighlightInfoType.CONSTRUCTOR_DECLARATION.getAttributesKey().getDefaultAttributes()); + public static final TextAttributesKey INSTANCE_FIELD = TextAttributesKey.createTextAttributesKey(INSTANCE_FIELD_ID, HighlightInfoType.INSTANCE_FIELD.getAttributesKey().getDefaultAttributes()); public static final TextAttributesKey METHOD_CALL = TextAttributesKey.createTextAttributesKey(METHOD_CALL_ID, HighlightInfoType.METHOD_CALL.getAttributesKey().getDefaultAttributes()); + public static final TextAttributesKey CONSTRUCTOR_CALL = TextAttributesKey + .createTextAttributesKey("Groovy constructor call", HighlightInfoType.CONSTRUCTOR_CALL.getAttributesKey().getDefaultAttributes()); + public static final TextAttributesKey STATIC_FIELD = TextAttributesKey.createTextAttributesKey(STATIC_FIELD_ID, HighlightInfoType.STATIC_FINAL_FIELD.getAttributesKey().getDefaultAttributes()); diff --git a/plugins/groovy/src/org/jetbrains/plugins/groovy/highlighter/GroovyColorsAndFontsPage.java b/plugins/groovy/src/org/jetbrains/plugins/groovy/highlighter/GroovyColorsAndFontsPage.java index 2c26a5ca2e6b..cfa140b9245d 100644 --- a/plugins/groovy/src/org/jetbrains/plugins/groovy/highlighter/GroovyColorsAndFontsPage.java +++ b/plugins/groovy/src/org/jetbrains/plugins/groovy/highlighter/GroovyColorsAndFontsPage.java @@ -73,9 +73,11 @@ public class GroovyColorsAndFontsPage implements ColorSettingsPage { new AttributesDescriptor("Reassigned parameter", DefaultHighlighter.REASSIGNED_PARAMETER), new AttributesDescriptor("Static field", DefaultHighlighter.STATIC_FIELD), new AttributesDescriptor("Instance field", DefaultHighlighter.INSTANCE_FIELD), + new AttributesDescriptor("Constructor call", DefaultHighlighter.CONSTRUCTOR_CALL), new AttributesDescriptor("Instance method call", DefaultHighlighter.METHOD_CALL), new AttributesDescriptor("Static method call", DefaultHighlighter.STATIC_METHOD_ACCESS), new AttributesDescriptor("Method declaration", DefaultHighlighter.METHOD_DECLARATION), + new AttributesDescriptor("Constructor declaration", DefaultHighlighter.CONSTRUCTOR_DECLARATION), new AttributesDescriptor("Class reference", DefaultHighlighter.CLASS_REFERENCE), new AttributesDescriptor("Type parameter reference", DefaultHighlighter.TYPE_PARAMETER), new AttributesDescriptor("Map key accessed as a property", DefaultHighlighter.MAP_KEY), @@ -107,6 +109,7 @@ public class GroovyColorsAndFontsPage implements ColorSettingsPage { " */\n" + "@SpecialBean \n" + "class Demo {\n" + + " public Demo() {}\n" + " def property\n" + "//This is a line comment\n" + "/* This is a block comment */\n" + @@ -129,7 +132,7 @@ public class GroovyColorsAndFontsPage implements ColorSettingsPage { ":def f1 = []\n" + "f1 = [2]\n" + "File f=['path']\n" + - "print new Demo().property\n" + + "print new Demo().property\n" + "print '\\n \\x'" ; @@ -142,6 +145,7 @@ public class GroovyColorsAndFontsPage implements ColorSettingsPage { map.put("annotation", DefaultHighlighter.ANNOTATION); map.put("statmet", DefaultHighlighter.STATIC_METHOD_ACCESS); map.put("instmet", DefaultHighlighter.METHOD_CALL); + map.put("constructorCall", DefaultHighlighter.CONSTRUCTOR_CALL); map.put("statfield", DefaultHighlighter.STATIC_FIELD); map.put("instfield", DefaultHighlighter.INSTANCE_FIELD); map.put("gdoc", DefaultHighlighter.DOC_COMMENT_CONTENT); @@ -160,6 +164,7 @@ public class GroovyColorsAndFontsPage implements ColorSettingsPage { map.put("param", DefaultHighlighter.PARAMETER); map.put("reParam", DefaultHighlighter.REASSIGNED_PARAMETER); map.put("method", DefaultHighlighter.METHOD_DECLARATION); + map.put("constructor", DefaultHighlighter.CONSTRUCTOR_DECLARATION); map.put("label", DefaultHighlighter.LABEL); return map; } diff --git a/plugins/groovy/testdata/highlighting/InnerClassConstructorThis.groovy b/plugins/groovy/testdata/highlighting/InnerClassConstructorThis.groovy index 0d7147c49a35..b2f7d04f9bd5 100644 --- a/plugins/groovy/testdata/highlighting/InnerClassConstructorThis.groovy +++ b/plugins/groovy/testdata/highlighting/InnerClassConstructorThis.groovy @@ -16,6 +16,6 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -B () { this.i = 42 } +B() { this.i = 42 } } } \ No newline at end of file