Search in sources :

Example 26 with GrParameter

use of org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter 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 27 with GrParameter

use of org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter in project intellij-community by JetBrains.

the class GroovyOverrideImplementUtil method setupAnnotations.

private static void setupAnnotations(@NotNull GrTypeDefinition aClass, @NotNull PsiMethod method, @NotNull GrMethod result) {
    if (OverrideImplementUtil.isInsertOverride(method, aClass)) {
        result.getModifierList().addAnnotation(CommonClassNames.JAVA_LANG_OVERRIDE);
    }
    final GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(method.getProject());
    final PsiParameter[] originalParams = method.getParameterList().getParameters();
    GrParameter[] parameters = result.getParameters();
    for (int i = 0; i < parameters.length; i++) {
        GrParameter parameter = parameters[i];
        PsiParameter original = originalParams[i];
        for (PsiAnnotation annotation : original.getModifierList().getAnnotations()) {
            final GrModifierList modifierList = parameter.getModifierList();
            String qname = annotation.getQualifiedName();
            if (qname != null && modifierList.findAnnotation(qname) == null) {
                if (annotation instanceof GrAnnotation) {
                    modifierList.add(annotation);
                } else {
                    modifierList.add(factory.createAnnotationFromText(annotation.getText()));
                }
            }
        }
    }
}
Also used : GroovyPsiElementFactory(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory) GrModifierList(org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.modifiers.GrModifierList) GrAnnotation(org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.modifiers.annotation.GrAnnotation) GrParameter(org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter)

Example 28 with GrParameter

use of org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter in project intellij-community by JetBrains.

the class TrySurrounder method getSurroundSelectionRange.

@Override
protected TextRange getSurroundSelectionRange(GroovyPsiElement element) {
    assert element instanceof GrTryCatchStatement;
    int endOffset = element.getTextRange().getEndOffset();
    GrTryCatchStatement tryCatchStatement = (GrTryCatchStatement) element;
    GrCatchClause[] catchClauses = tryCatchStatement.getCatchClauses();
    if (catchClauses != null && catchClauses.length > 0) {
        GrParameter parameter = catchClauses[0].getParameter();
        if (parameter == null) {
            GrOpenBlock block = catchClauses[0].getBody();
            assert block != null;
            endOffset = block.getTextRange().getEndOffset();
        } else {
            endOffset = parameter.getTextRange().getStartOffset();
            parameter.getParent().getNode().removeChild(parameter.getNode());
        }
    } else {
        GrOpenBlock block = tryCatchStatement.getTryBlock();
        if (block != null) {
            GrStatement[] statements = block.getStatements();
            if (statements.length > 0) {
                endOffset = statements[0].getTextRange().getStartOffset();
            }
        }
    }
    return new TextRange(endOffset, endOffset);
}
Also used : TextRange(com.intellij.openapi.util.TextRange) GrTryCatchStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrTryCatchStatement) GrCatchClause(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrCatchClause) GrOpenBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrOpenBlock) GrParameter(org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter) GrStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement)

Example 29 with GrParameter

use of org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter in project intellij-community by JetBrains.

the class GroovyParameterInfoHandler method appendParameterText.

private static void appendParameterText(PsiParameter param, PsiSubstitutor substitutor, StringBuilder buffer) {
    if (param instanceof GrParameter) {
        GrParameter grParam = (GrParameter) param;
        GroovyPresentationUtil.appendParameterPresentation(grParam, substitutor, TypePresentation.PRESENTABLE, buffer);
        final GrExpression initializer = grParam.getInitializerGroovy();
        if (initializer != null) {
            buffer.append(" = ").append(initializer.getText());
        } else if (grParam.isOptional()) {
            buffer.append(" = null");
        }
    } else {
        PsiType t = param.getType();
        PsiType paramType = substitutor.substitute(t);
        buffer.append(paramType.getPresentableText());
        String name = param.getName();
        if (name != null) {
            buffer.append(" ");
            buffer.append(name);
        }
    }
}
Also used : GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) GrParameter(org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter)

Example 30 with GrParameter

use of org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter in project intellij-plugins by JetBrains.

the class GrStepDefinition method getVariableNames.

@Override
public List<String> getVariableNames() {
    PsiElement element = getElement();
    if (element instanceof GrMethodCall) {
        GrClosableBlock[] closures = ((GrMethodCall) element).getClosureArguments();
        assert closures.length == 1;
        GrParameter[] parameters = closures[0].getParameterList().getParameters();
        ArrayList<String> result = new ArrayList<>();
        for (GrParameter parameter : parameters) {
            result.add(parameter.getName());
        }
        return result;
    }
    return Collections.emptyList();
}
Also used : GrMethodCall(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrMethodCall) ArrayList(java.util.ArrayList) GrClosableBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock) GrParameter(org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter) PsiElement(com.intellij.psi.PsiElement)

Aggregations

GrParameter (org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter)99 GrClosableBlock (org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock)22 NotNull (org.jetbrains.annotations.NotNull)20 PsiElement (com.intellij.psi.PsiElement)19 GroovyPsiElementFactory (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory)19 GrMethod (org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod)18 GrReferenceExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression)16 GrParameterList (org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameterList)14 GroovyPsiElement (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement)13 GrExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression)12 ArrayList (java.util.ArrayList)11 GrVariable (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable)10 TextRange (com.intellij.openapi.util.TextRange)9 GrOpenBlock (org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrOpenBlock)9 Nullable (org.jetbrains.annotations.Nullable)8 GroovyResolveResult (org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult)7 Project (com.intellij.openapi.project.Project)6 IncorrectOperationException (com.intellij.util.IncorrectOperationException)6 GroovyFile (org.jetbrains.plugins.groovy.lang.psi.GroovyFile)6 GrField (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrField)6