From f7d20af74c9ea7bf3df6c09df4596e767bd1f49c Mon Sep 17 00:00:00 2001 From: Maxim Medvedev Date: Tue, 16 Aug 2011 15:24:08 +0400 Subject: [PATCH] Properties should be before accessors in completion, current class members are more relevant than superclass members --- plugins/groovy/src/META-INF/plugin.xml | 9 +++- .../weighers/GrAccessorWeigher.java | 43 +++++++++++++++++++ .../weighers/GrTopClassMembersWeigher.java | 19 +++++--- .../completion/GroovyCompletionTest.groovy | 8 ++++ .../completion/PropertyBeforeAccessor.groovy | 6 +++ .../groovy/completion/SortOrder0.groovy | 27 ++++++++++++ 6 files changed, 104 insertions(+), 8 deletions(-) create mode 100644 plugins/groovy/src/org/jetbrains/plugins/groovy/lang/completion/weighers/GrAccessorWeigher.java create mode 100644 plugins/groovy/testdata/groovy/completion/PropertyBeforeAccessor.groovy create mode 100644 plugins/groovy/testdata/groovy/completion/SortOrder0.groovy diff --git a/plugins/groovy/src/META-INF/plugin.xml b/plugins/groovy/src/META-INF/plugin.xml index 655f745d5861..e29bb0f66a38 100644 --- a/plugins/groovy/src/META-INF/plugin.xml +++ b/plugins/groovy/src/META-INF/plugin.xml @@ -236,8 +236,13 @@ - - + + + + + diff --git a/plugins/groovy/src/org/jetbrains/plugins/groovy/lang/completion/weighers/GrAccessorWeigher.java b/plugins/groovy/src/org/jetbrains/plugins/groovy/lang/completion/weighers/GrAccessorWeigher.java new file mode 100644 index 000000000000..289d3158dee2 --- /dev/null +++ b/plugins/groovy/src/org/jetbrains/plugins/groovy/lang/completion/weighers/GrAccessorWeigher.java @@ -0,0 +1,43 @@ +/* + * Copyright 2000-2011 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.plugins.groovy.lang.completion.weighers; + +import com.intellij.codeInsight.completion.CompletionLocation; +import com.intellij.codeInsight.completion.CompletionWeigher; +import com.intellij.codeInsight.lookup.LookupElement; +import com.intellij.psi.PsiMethod; +import com.intellij.psi.ResolveResult; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.plugins.groovy.lang.psi.util.GroovyPropertyUtils; + +/** + * @author Max Medvedev + */ +public class GrAccessorWeigher extends CompletionWeigher { + @Override + public Integer weigh(@NotNull LookupElement element, @NotNull CompletionLocation location) { + Object o = element.getObject(); + if (o instanceof ResolveResult) { + o = ((ResolveResult)o).getElement(); + } + + if (o instanceof PsiMethod && + (GroovyPropertyUtils.isSimplePropertyAccessor((PsiMethod)o) || "setProperty".equals(((PsiMethod)o).getName()))) { + return -1; + } + return 0; + } +} diff --git a/plugins/groovy/src/org/jetbrains/plugins/groovy/lang/completion/weighers/GrTopClassMembersWeigher.java b/plugins/groovy/src/org/jetbrains/plugins/groovy/lang/completion/weighers/GrTopClassMembersWeigher.java index d7a9b34702d7..0a1d3b3455ef 100644 --- a/plugins/groovy/src/org/jetbrains/plugins/groovy/lang/completion/weighers/GrTopClassMembersWeigher.java +++ b/plugins/groovy/src/org/jetbrains/plugins/groovy/lang/completion/weighers/GrTopClassMembersWeigher.java @@ -22,13 +22,16 @@ import com.intellij.psi.*; import org.jetbrains.annotations.NotNull; import org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression; import org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression; +import org.jetbrains.plugins.groovy.lang.psi.util.PsiUtil; /** + * set priority to current class members + * * @author Maxim.Medvedev */ public class GrTopClassMembersWeigher extends CompletionWeigher { @Override - public Comparable weigh(@NotNull LookupElement element, @NotNull CompletionLocation location) { + public Integer weigh(@NotNull LookupElement element, @NotNull CompletionLocation location) { Object o = element.getObject(); if (o instanceof ResolveResult) { o = ((ResolveResult)o).getElement(); @@ -41,12 +44,16 @@ public class GrTopClassMembersWeigher extends CompletionWeigher { if (!(parent instanceof GrReferenceExpression)) return 0; final GrExpression qualifier = ((GrReferenceExpression)parent).getQualifierExpression(); - if (qualifier == null) return 0; + final PsiClass psiClass; + if (qualifier == null) { + psiClass = PsiUtil.getContextClass(position); + } + else { + final PsiType type = qualifier.getType(); + if (!(type instanceof PsiClassType)) return 0; - final PsiType type = qualifier.getType(); - if (!(type instanceof PsiClassType)) return 0; - - final PsiClass psiClass = ((PsiClassType)type).resolve(); + psiClass = ((PsiClassType)type).resolve(); + } if (psiClass == null) return 0; if (PsiManager.getInstance(location.getProject()).areElementsEquivalent(((PsiMember)o).getContainingClass(), psiClass)) { diff --git a/plugins/groovy/test/org/jetbrains/plugins/groovy/completion/GroovyCompletionTest.groovy b/plugins/groovy/test/org/jetbrains/plugins/groovy/completion/GroovyCompletionTest.groovy index 6f4e4b360d7a..e939db6bc65f 100644 --- a/plugins/groovy/test/org/jetbrains/plugins/groovy/completion/GroovyCompletionTest.groovy +++ b/plugins/groovy/test/org/jetbrains/plugins/groovy/completion/GroovyCompletionTest.groovy @@ -829,4 +829,12 @@ class X { public void testClassNameBeforeParentheses(){ doBasicTest() } + + public void testPropertyBeforeAccessor() { + doVariantableTest 'soSe', 'setSoSe' + } + + public void testSortOrder0() { + doVariantableTest 'se', 'setSe', 'setMetaClass', 'setProperty' + } } \ No newline at end of file diff --git a/plugins/groovy/testdata/groovy/completion/PropertyBeforeAccessor.groovy b/plugins/groovy/testdata/groovy/completion/PropertyBeforeAccessor.groovy new file mode 100644 index 000000000000..2fdd320664b7 --- /dev/null +++ b/plugins/groovy/testdata/groovy/completion/PropertyBeforeAccessor.groovy @@ -0,0 +1,6 @@ +class X { + def soSe + def foo() { + ss + } +} \ No newline at end of file diff --git a/plugins/groovy/testdata/groovy/completion/SortOrder0.groovy b/plugins/groovy/testdata/groovy/completion/SortOrder0.groovy new file mode 100644 index 000000000000..3ac39f344d56 --- /dev/null +++ b/plugins/groovy/testdata/groovy/completion/SortOrder0.groovy @@ -0,0 +1,27 @@ +class Foo extends GroovyObjectSupport{ + def se + + def a() { + se + } +} + +public abstract class GroovyObjectSupport { + public Object getProperty(String property) { + null + } + + public void setProperty(String property, Object newValue) { + } + + public Object invokeMethod(String name, Object args) { + null + } + + public MetaClass getMetaClass() { + null + } + + public void setMetaClass(MetaClass metaClass) { + } +}