Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add method override autocompletion for Groovy Plugin #239

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
/*
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A test would be appreciated =)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't really know what to test here, since suggestion candidates are provided by OverrideImplementExploreUtil.getMethodsToOverrideImplement(psiClass, implement) and then filtered by IDEA. I could try testing if it should suggest some methods on different places (class definition, body definition, etc.), but to be honest I don't know how to do that.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, no problem =)
Actually if you have no time or don't want to fix all that things I wrote about I can accept the pull request and fix them by myself but it will take a while. So I don't guarantee the feature to be included into the nearest release.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have fixed the other stuff you mentioned, but I honestly don't know what to test here as most of the functionality is delegated to already implemented and tested methods, and I don't know how to test what is left. I can upload what I already fixed.

* Copyright 2000-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.plugins.groovy.lang.completion;

import com.intellij.codeInsight.completion.*;
import com.intellij.codeInsight.generation.OverrideImplementExploreUtil;
import com.intellij.codeInsight.lookup.LookupElement;
import com.intellij.codeInsight.lookup.LookupElementBuilder;
import com.intellij.icons.AllIcons;
import com.intellij.patterns.PlatformPatterns;
import com.intellij.psi.*;
import com.intellij.psi.infos.CandidateInfo;
import com.intellij.psi.util.PsiFormatUtil;
import com.intellij.psi.util.PsiFormatUtilBase;
import com.intellij.psi.util.PsiTreeUtil;
import com.intellij.ui.RowIcon;
import com.intellij.util.ProcessingContext;
import com.intellij.util.VisibilityUtil;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.plugins.groovy.lang.completion.handlers.GroovyMethodOverrideHandler;

import javax.swing.*;
import java.util.Collection;

import static com.intellij.patterns.PlatformPatterns.psiElement;

/**
* Created by Arasthel on 29/01/15.
*/

class GrMethodOverrideCompletionProvider extends CompletionProvider<CompletionParameters> {

@Override
protected void addCompletions(@NotNull CompletionParameters parameters, ProcessingContext context, @NotNull CompletionResultSet result) {
PsiElement position = parameters.getPosition();

if(psiElement()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't need to check psiElement().inside(PsiClass.class) since we search for containing class and check if it is not null on the next line.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was done because otherwise you could get override or implement suggestions while inside a method definition, which I don't think really make sense.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

psiElement.inside(PsiClass.class) is fully equivalent to PsiTreeUtil.getParentOfType(position, PsiClass.class) != null. Right now completion inside method body suggests overriding variants.

The correct place to put the pattern is register method where currently you provide PlatformPatterns.psiElement(PsiElement.class). See a describing comment there

.inside(PsiClass.class)
.accepts(position)) {

PsiClass currentClass = PsiTreeUtil.getParentOfType(position, PsiClass.class);

if (currentClass != null) {
addSuperMethods(currentClass, result, false);
addSuperMethods(currentClass, result, true);
}
}
}

public static void register(CompletionContributor contributor) {
contributor.extend(CompletionType.BASIC, PlatformPatterns.psiElement(PsiElement.class), new GrMethodOverrideCompletionProvider());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The pattern here should be comprehensive pretty much. We expect to see overriding completion variants in the class body only. So you should check something like psiElement().withParent(GrTypeDefinitionBody.class) and so on. You can find more examples in GroovySmartCompletionContributor

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

BTW it should work in the case like this:

class A {
    void foo() {}
}

class B extends A {
    voi<caret> // invoke completion at the <caret> position. void foo(){...} should be suggested
}```

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed the .inside(PsiClass.class) to your suggestion. I don't think I understand what you meant on your 2nd message, writing voi does trigger the override autocompletion of void methods.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a screenshot with completion in java. In Groovy it should work the same way. All you need is to provide a correct pattern in contributor.extend()

https://www.dropbox.com/s/as6hfjyuw30qemz/Screen%20Shot%202015-02-03%20at%2013.41.43%20.png?dl=0

}

private void addSuperMethods(final PsiClass psiClass, CompletionResultSet completionResultSet, boolean implement) {
Collection<CandidateInfo> candidates = OverrideImplementExploreUtil.getMethodsToOverrideImplement(psiClass, implement);
for(CandidateInfo candidateInfo : candidates) {
final PsiMethod method = (PsiMethod) candidateInfo.getElement();
PsiSubstitutor substitutor = candidateInfo.getSubstitutor();

RowIcon icon = new RowIcon(2);
icon.setIcon(method.getIcon(0), 0);
icon.setIcon(implement ? AllIcons.Gutter.ImplementingMethod : AllIcons.Gutter.OverridingMethod, 1);

if(!method.isConstructor()) {
completionResultSet.addElement(createLookUpElement(method, substitutor, psiClass, icon, new GroovyMethodOverrideHandler(psiClass)));
}
}
}

private LookupElementBuilder createLookUpElement(final PsiMethod method, PsiSubstitutor substitutor, PsiClass currentClass, Icon icon,
InsertHandler<LookupElement> insertHandler) {
String parameters = PsiFormatUtil.formatMethod(method, substitutor, PsiFormatUtilBase.SHOW_PARAMETERS, PsiFormatUtilBase.SHOW_NAME);

String visibility = VisibilityUtil.getVisibilityModifier(method.getModifierList());
String modifiers = (visibility == PsiModifier.PACKAGE_LOCAL ? "" : visibility + " ");

PsiType type = substitutor.substitute(method.getReturnType());

String parentClassName = currentClass == null ? "" : ((PsiNamedElement) currentClass).getName();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cast to PsiNamedElement is unnecsesary here


String signature = modifiers + (type == null ? "" : type.getPresentableText() + " ") + method.getName();

LookupElementBuilder elementBuilder = LookupElementBuilder.create(method, signature)
.withLookupString(signature)
.appendTailText(parameters, false)
.appendTailText("{...}", true)
.withTypeText(parentClassName)
.withIcon(icon)
.withInsertHandler(insertHandler);

return elementBuilder;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ public class GroovyCompletionContributor extends CompletionContributor {


public GroovyCompletionContributor() {
GrMethodOverrideCompletionProvider.register(this);
GrThisSuperCompletionProvider.register(this);
MapArgumentCompletionProvider.register(this);
GroovyConfigSlurperCompletionProvider.register(this);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* Copyright 2000-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.plugins.groovy.lang.completion.handlers;

import com.intellij.codeInsight.completion.InsertHandler;
import com.intellij.codeInsight.completion.InsertionContext;
import com.intellij.codeInsight.generation.GenerateMembersUtil;
import com.intellij.codeInsight.generation.OverrideImplementUtil;
import com.intellij.codeInsight.lookup.LookupElement;
import com.intellij.psi.PsiClass;
import com.intellij.psi.PsiMethod;

import java.util.List;

/**
* Created by Arasthel on 29/01/15.
*/
public class GroovyMethodOverrideHandler implements InsertHandler<LookupElement> {

private PsiClass myPsiClass;

public GroovyMethodOverrideHandler(PsiClass aClass) {
this.myPsiClass = aClass;
}

@Override
public void handleInsert(InsertionContext context, LookupElement item) {
context.getDocument().deleteString(context.getStartOffset(), context.getTailOffset());
PsiMethod method = (PsiMethod) item.getObject();
List<PsiMethod> prototypes = OverrideImplementUtil.overrideOrImplementMethod(myPsiClass, method, false);
context.commitDocument();
GenerateMembersUtil.insertMembersAtOffset(context.getFile(), context.getStartOffset(),
OverrideImplementUtil.convert2GenerationInfos(prototypes));
}
}