Search in sources :

Example 1 with GrCodeBlock

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

the class GroovyPlainEnterProcessor method doEnter.

@Override
public boolean doEnter(PsiElement psiElement, PsiFile file, @NotNull Editor editor, boolean modified) {
    GrCodeBlock block = getControlStatementBlock(editor.getCaretModel().getOffset(), psiElement);
    if (block != null) {
        PsiElement firstElement = block.getFirstChild().getNextSibling();
        final int offset = firstElement != null ? firstElement.getTextRange().getStartOffset() - 1 : block.getTextRange().getEndOffset();
        editor.getCaretModel().moveToOffset(offset);
    }
    plainEnter(editor);
    return true;
}
Also used : PsiElement(com.intellij.psi.PsiElement) GrCodeBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrCodeBlock)

Example 2 with GrCodeBlock

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

the class GroovySmartEnterProcessor method reformat.

@Override
protected void reformat(PsiElement atCaret) throws IncorrectOperationException {
    PsiElement parent = atCaret.getParent();
    if (parent instanceof GrCodeBlock) {
        final GrCodeBlock block = (GrCodeBlock) parent;
        if (block.getStatements().length > 0 && block.getStatements()[0] == atCaret) {
            atCaret = block;
        }
    } else if (parent instanceof GrForStatement || parent instanceof GrSwitchStatement) {
        atCaret = parent;
    }
    super.reformat(atCaret);
}
Also used : GrForStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrForStatement) GrSwitchStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrSwitchStatement) GrCodeBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrCodeBlock)

Example 3 with GrCodeBlock

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

the class GrMethodBodyFixer method apply.

@Override
public void apply(@NotNull Editor editor, @NotNull GroovySmartEnterProcessor processor, @NotNull PsiElement psiElement) {
    if (!(psiElement instanceof GrMethod))
        return;
    GrMethod method = (GrMethod) psiElement;
    final PsiClass aClass = method.getContainingClass();
    if (aClass != null && aClass.isInterface() || method.hasModifierProperty(PsiModifier.ABSTRACT))
        return;
    final GrCodeBlock body = method.getBlock();
    final Document doc = editor.getDocument();
    if (body != null) {
        // See IDEADEV-1093. This is quite hacky heuristic but it seem to be best we can do.
        String bodyText = body.getText();
        if (StringUtil.startsWithChar(bodyText, '{')) {
            final GrStatement[] statements = body.getStatements();
            if (statements.length > 0) {
            //          [todo]
            //          if (statements[0] instanceof PsiDeclarationStatement) {
            //            if (PsiTreeUtil.getDeepestLast(statements[0]) instanceof PsiErrorElement) {
            //              if (method.getContainingClass().getRBrace() == null) {
            //                doc.insertString(body.getTextRange().getStartOffset() + 1, "\n}");
            //              }
            //            }
            //          }
            }
        }
        return;
    }
    int endOffset = method.getTextRange().getEndOffset();
    if (StringUtil.endsWithChar(method.getText(), ';')) {
        doc.deleteString(endOffset - 1, endOffset);
        endOffset--;
    }
    doc.insertString(endOffset, "{\n}");
}
Also used : GrMethod(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod) PsiClass(com.intellij.psi.PsiClass) Document(com.intellij.openapi.editor.Document) GrCodeBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrCodeBlock) GrStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement)

Example 4 with GrCodeBlock

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

the class GroovyMethodInliner method isOnExpressionOrReturnPlace.

/*
  Method call is used as expression in some enclosing expression or
  is method return result
  */
private static boolean isOnExpressionOrReturnPlace(@NotNull GrCallExpression call) {
    PsiElement parent = call.getParent();
    if (!(parent instanceof GrVariableDeclarationOwner)) {
        return true;
    }
    // tail calls in methods and closures
    GrVariableDeclarationOwner owner = (GrVariableDeclarationOwner) parent;
    if (owner instanceof GrClosableBlock || owner instanceof GrOpenBlock && owner.getParent() instanceof GrMethod) {
        GrStatement[] statements = ((GrCodeBlock) owner).getStatements();
        assert statements.length > 0;
        GrStatement last = statements[statements.length - 1];
        if (last == call)
            return true;
        if (last instanceof GrReturnStatement && call == ((GrReturnStatement) last).getReturnValue()) {
            return true;
        }
    }
    return false;
}
Also used : GrMethod(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod) GrClosableBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock) GrOpenBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrOpenBlock) GrReturnStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.branch.GrReturnStatement) GrVariableDeclarationOwner(org.jetbrains.plugins.groovy.lang.psi.api.util.GrVariableDeclarationOwner) GrCodeBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrCodeBlock) GrStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement)

Example 5 with GrCodeBlock

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

the class AddMethodFix method doFix.

@Override
protected void doFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) throws IncorrectOperationException {
    if (myClass.isInterface()) {
        final GrMethod method = GroovyPsiElementFactory.getInstance(project).createMethodFromText("def " + myClass.getName() + " " + myMethodName + "();");
        myClass.add(method);
    } else {
        String templName = JavaTemplateUtil.TEMPLATE_IMPLEMENTED_METHOD_BODY;
        final FileTemplate template = FileTemplateManager.getInstance(project).getCodeTemplate(templName);
        Properties properties = new Properties();
        String returnType = generateTypeText(myClass);
        properties.setProperty(FileTemplate.ATTRIBUTE_RETURN_TYPE, returnType);
        properties.setProperty(FileTemplate.ATTRIBUTE_DEFAULT_RETURN_VALUE, PsiTypesUtil.getDefaultValueOfType(JavaPsiFacade.getElementFactory(project).createType(myClass)));
        properties.setProperty(FileTemplate.ATTRIBUTE_CALL_SUPER, "");
        properties.setProperty(FileTemplate.ATTRIBUTE_CLASS_NAME, myClass.getQualifiedName());
        properties.setProperty(FileTemplate.ATTRIBUTE_SIMPLE_CLASS_NAME, myClass.getName());
        properties.setProperty(FileTemplate.ATTRIBUTE_METHOD_NAME, myMethodName);
        try {
            String bodyText = StringUtil.replace(template.getText(properties), ";", "");
            final GrCodeBlock newBody = GroovyPsiElementFactory.getInstance(project).createMethodBodyFromText("\n" + bodyText + "\n");
            final GrMethod method = GroovyPsiElementFactory.getInstance(project).createMethodFromText("", myMethodName, returnType, ArrayUtil.EMPTY_STRING_ARRAY, myClass);
            method.setBlock(newBody);
            myClass.add(method);
        } catch (IOException e) {
            LOG.error(e);
        }
    }
}
Also used : FileTemplate(com.intellij.ide.fileTemplates.FileTemplate) GrMethod(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod) IOException(java.io.IOException) Properties(java.util.Properties) GrCodeBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrCodeBlock)

Aggregations

GrCodeBlock (org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrCodeBlock)22 GrMethod (org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod)10 PsiElement (com.intellij.psi.PsiElement)6 GrClosableBlock (org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock)5 GrOpenBlock (org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrOpenBlock)5 GroovyPsiElement (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement)4 GrStatement (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement)4 LeafPsiElement (com.intellij.psi.impl.source.tree.LeafPsiElement)3 IncorrectOperationException (com.intellij.util.IncorrectOperationException)3 GroovyPsiElementFactory (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory)3 GrCaseSection (org.jetbrains.plugins.groovy.lang.psi.api.statements.clauses.GrCaseSection)3 ASTNode (com.intellij.lang.ASTNode)2 ArrayList (java.util.ArrayList)2 GrField (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrField)2 GrArgumentLabel (org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentLabel)2 GrString (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.literals.GrString)2 GrTypeDefinitionBody (org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrTypeDefinitionBody)2 GrStatementOwner (org.jetbrains.plugins.groovy.lang.psi.api.util.GrStatementOwner)2 LineRange (com.intellij.codeInsight.editorActions.moveUpDown.LineRange)1 FileTemplate (com.intellij.ide.fileTemplates.FileTemplate)1