Search in sources :

Example 1 with ClosureParameterInfo

use of org.jetbrains.plugins.groovy.lang.completion.closureParameters.ClosureParameterInfo in project intellij-community by JetBrains.

the class ClosureCompleter method runTemplate.

public static void runTemplate(List<ClosureParameterInfo> parameters, GrClosableBlock block, PsiSubstitutor substitutor, PsiMethod method, final Project project, final Editor editor) {
    if (method instanceof ClsMethodImpl)
        method = ((ClsMethodImpl) method).getSourceMirrorMethod();
    assert block.getArrow() == null;
    if (parameters.isEmpty())
        return;
    StringBuilder buffer = new StringBuilder();
    buffer.append("{");
    List<PsiType> paramTypes = ContainerUtil.newArrayList();
    for (ClosureParameterInfo parameter : parameters) {
        final String type = parameter.getType();
        final String name = parameter.getName();
        if (type != null) {
            final PsiType fromText = JavaPsiFacade.getElementFactory(project).createTypeFromText(type, method);
            final PsiType substituted = substitutor.substitute(fromText);
            paramTypes.add(substituted);
            buffer.append(substituted.getCanonicalText()).append(" ");
        } else {
            buffer.append("def ");
        }
        buffer.append(name);
        buffer.append(", ");
    }
    buffer.replace(buffer.length() - 2, buffer.length(), " ->}");
    final Document document = editor.getDocument();
    final PsiFile file = PsiDocumentManager.getInstance(project).getPsiFile(document);
    assert file != null;
    final GrClosableBlock closure = GroovyPsiElementFactory.getInstance(project).createClosureFromText(buffer.toString());
    final GrClosableBlock templateClosure = (GrClosableBlock) block.replaceWithExpression(closure, false);
    final TemplateBuilderImpl builder = new TemplateBuilderImpl(templateClosure);
    int i = 0;
    for (GrParameter p : templateClosure.getParameters()) {
        final GrTypeElement typeElement = p.getTypeElementGroovy();
        final PsiElement nameIdentifier = p.getNameIdentifierGroovy();
        if (typeElement != null) {
            final TypeConstraint[] typeConstraints = { SupertypeConstraint.create(paramTypes.get(i++)) };
            final ChooseTypeExpression expression = new ChooseTypeExpression(typeConstraints, PsiManager.getInstance(project), nameIdentifier.getResolveScope());
            builder.replaceElement(typeElement, expression);
        } else {
            final ChooseTypeExpression expression = new ChooseTypeExpression(TypeConstraint.EMPTY_ARRAY, PsiManager.getInstance(project), nameIdentifier.getResolveScope());
            builder.replaceElement(p.getModifierList(), expression);
        }
        builder.replaceElement(nameIdentifier, new ParameterNameExpression(nameIdentifier.getText()));
    }
    final GrClosableBlock afterPostprocess = CodeInsightUtilCore.forcePsiPostprocessAndRestoreElement(templateClosure);
    final Template template = builder.buildTemplate();
    TextRange range = afterPostprocess.getTextRange();
    document.deleteString(range.getStartOffset(), range.getEndOffset());
    TemplateEditingListener templateListener = new TemplateEditingAdapter() {

        @Override
        public void templateFinished(Template template, boolean brokenOff) {
            ApplicationManager.getApplication().runWriteAction(() -> {
                PsiDocumentManager.getInstance(project).commitDocument(document);
                final CaretModel caretModel = editor.getCaretModel();
                final int offset = caretModel.getOffset();
                GrClosableBlock block1 = PsiTreeUtil.findElementOfClassAtOffset(file, offset - 1, GrClosableBlock.class, false);
                if (block1 != null) {
                    final PsiElement arrow = block1.getArrow();
                    if (arrow != null) {
                        caretModel.moveToOffset(arrow.getTextRange().getEndOffset());
                    }
                    // fix space before closure lbrace
                    final TextRange range1 = block1.getTextRange();
                    CodeStyleManager.getInstance(project).reformatRange(block1.getParent(), range1.getStartOffset() - 1, range1.getEndOffset(), true);
                }
            });
        }
    };
    TemplateManager manager = TemplateManager.getInstance(project);
    manager.startTemplate(editor, template, templateListener);
}
Also used : GrTypeElement(org.jetbrains.plugins.groovy.lang.psi.api.types.GrTypeElement) CaretModel(com.intellij.openapi.editor.CaretModel) ClsMethodImpl(com.intellij.psi.impl.compiled.ClsMethodImpl) GrClosableBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock) TextRange(com.intellij.openapi.util.TextRange) Document(com.intellij.openapi.editor.Document) GrParameter(org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter) SupertypeConstraint(org.jetbrains.plugins.groovy.lang.psi.expectedTypes.SupertypeConstraint) TypeConstraint(org.jetbrains.plugins.groovy.lang.psi.expectedTypes.TypeConstraint) ChooseTypeExpression(org.jetbrains.plugins.groovy.template.expressions.ChooseTypeExpression) ClosureParameterInfo(org.jetbrains.plugins.groovy.lang.completion.closureParameters.ClosureParameterInfo) TypeConstraint(org.jetbrains.plugins.groovy.lang.psi.expectedTypes.TypeConstraint) ParameterNameExpression(org.jetbrains.plugins.groovy.template.expressions.ParameterNameExpression)

Example 2 with ClosureParameterInfo

use of org.jetbrains.plugins.groovy.lang.completion.closureParameters.ClosureParameterInfo in project intellij-community by JetBrains.

the class EachWithIndexClosureCompleter method getParameterInfos.

@Nullable
@Override
protected List<ClosureParameterInfo> getParameterInfos(InsertionContext context, PsiMethod method, PsiSubstitutor substitutor, PsiElement place) {
    final String name = method.getName();
    if (!"eachWithIndex".equals(name))
        return null;
    if (method instanceof GrGdkMethod)
        method = ((GrGdkMethod) method).getStaticMethod();
    final PsiClass containingClass = method.getContainingClass();
    if (containingClass == null)
        return null;
    final String qname = containingClass.getQualifiedName();
    if (!GroovyCommonClassNames.DEFAULT_GROOVY_METHODS.equals(qname))
        return null;
    final PsiParameter[] parameters = method.getParameterList().getParameters();
    if (parameters.length != 2)
        return null;
    final PsiType type = parameters[0].getType();
    final PsiType collection = substitutor.substitute(type);
    final PsiType iterable = getIteratedType(place, collection);
    if (iterable != null) {
        return Arrays.asList(new ClosureParameterInfo(iterable.getCanonicalText(), "entry"), new ClosureParameterInfo("int", "i"));
    }
    if (InheritanceUtil.isInheritor(collection, CommonClassNames.JAVA_UTIL_MAP)) {
        final PsiType[] typeParams = ((PsiClassType) collection).getParameters();
        final Project project = context.getProject();
        final PsiClass entry = JavaPsiFacade.getInstance(project).findClass(CommonClassNames.JAVA_UTIL_MAP_ENTRY, place.getResolveScope());
        if (entry == null)
            return null;
        final PsiClassType entryType = JavaPsiFacade.getElementFactory(project).createType(entry, typeParams);
        return Arrays.asList(new ClosureParameterInfo(entryType.getCanonicalText(), "entry"), new ClosureParameterInfo("int", "i"));
    }
    return Arrays.asList(new ClosureParameterInfo(collection.getCanonicalText(), "entry"), new ClosureParameterInfo("int", "i"));
}
Also used : Project(com.intellij.openapi.project.Project) GrGdkMethod(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrGdkMethod) ClosureParameterInfo(org.jetbrains.plugins.groovy.lang.completion.closureParameters.ClosureParameterInfo) Nullable(org.jetbrains.annotations.Nullable)

Aggregations

ClosureParameterInfo (org.jetbrains.plugins.groovy.lang.completion.closureParameters.ClosureParameterInfo)2 CaretModel (com.intellij.openapi.editor.CaretModel)1 Document (com.intellij.openapi.editor.Document)1 Project (com.intellij.openapi.project.Project)1 TextRange (com.intellij.openapi.util.TextRange)1 ClsMethodImpl (com.intellij.psi.impl.compiled.ClsMethodImpl)1 Nullable (org.jetbrains.annotations.Nullable)1 GrClosableBlock (org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock)1 GrParameter (org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter)1 GrGdkMethod (org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrGdkMethod)1 GrTypeElement (org.jetbrains.plugins.groovy.lang.psi.api.types.GrTypeElement)1 SupertypeConstraint (org.jetbrains.plugins.groovy.lang.psi.expectedTypes.SupertypeConstraint)1 TypeConstraint (org.jetbrains.plugins.groovy.lang.psi.expectedTypes.TypeConstraint)1 ChooseTypeExpression (org.jetbrains.plugins.groovy.template.expressions.ChooseTypeExpression)1 ParameterNameExpression (org.jetbrains.plugins.groovy.template.expressions.ParameterNameExpression)1