Search in sources :

Example 1 with GrTypeDefinitionBody

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

the class GroovyTypeDefinitionBodySelectioner method select.

@Override
public List<TextRange> select(PsiElement e, CharSequence editorText, int cursorOffset, Editor editor) {
    List<TextRange> result = super.select(e, editorText, cursorOffset, editor);
    if (e instanceof GrTypeDefinitionBody) {
        GrTypeDefinitionBody block = ((GrTypeDefinitionBody) e);
        int startOffset = findOpeningBrace(block);
        int endOffset = findClosingBrace(block, startOffset);
        TextRange range = new TextRange(startOffset, endOffset);
        result.addAll(expandToWholeLine(editorText, range));
    }
    return result;
}
Also used : GrTypeDefinitionBody(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrTypeDefinitionBody) TextRange(com.intellij.openapi.util.TextRange)

Example 2 with GrTypeDefinitionBody

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

the class GrIntroduceFieldProcessor method getAnchorForDeclaration.

@Nullable
private static PsiElement getAnchorForDeclaration(@NotNull GrTypeDefinition targetClass) {
    final GrTypeDefinitionBody body = targetClass.getBody();
    if (body == null)
        return null;
    PsiElement anchor = body.getLBrace();
    final GrMembersDeclaration[] declarations = targetClass.getMemberDeclarations();
    for (GrMembersDeclaration declaration : declarations) {
        if (declaration instanceof GrVariableDeclaration)
            anchor = declaration;
        if (!(declaration instanceof GrVariableDeclaration))
            return anchor;
    }
    return anchor;
}
Also used : GrTypeDefinitionBody(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrTypeDefinitionBody) GrMembersDeclaration(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMembersDeclaration) Nullable(org.jetbrains.annotations.Nullable)

Example 3 with GrTypeDefinitionBody

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

the class ReplaceAbstractClassInstanceByMapIntention method processIntention.

@Override
protected void processIntention(@NotNull PsiElement psiElement, @NotNull Project project, Editor editor) throws IncorrectOperationException {
    PsiDocumentManager.getInstance(project).commitAllDocuments();
    GrCodeReferenceElement ref = (GrCodeReferenceElement) psiElement;
    final GrAnonymousClassDefinition anonymous = (GrAnonymousClassDefinition) ref.getParent();
    final GrNewExpression newExpr = (GrNewExpression) anonymous.getParent();
    final PsiElement resolved = ref.resolve();
    // && ((PsiClass)resolved).isInterface();
    assert resolved instanceof PsiClass;
    GrTypeDefinitionBody body = anonymous.getBody();
    assert body != null;
    List<Pair<PsiMethod, GrOpenBlock>> methods = new ArrayList<>();
    for (GrMethod method : body.getMethods()) {
        methods.add(new Pair<>(method, method.getBlock()));
    }
    final PsiClass iface = (PsiClass) resolved;
    final Collection<CandidateInfo> collection = OverrideImplementExploreUtil.getMethodsToOverrideImplement(anonymous, true);
    for (CandidateInfo info : collection) {
        methods.add(new Pair<>((PsiMethod) info.getElement(), null));
    }
    StringBuilder buffer = new StringBuilder();
    if (methods.size() == 1) {
        final Pair<PsiMethod, GrOpenBlock> pair = methods.get(0);
        appendClosureTextByMethod(pair.getFirst(), buffer, pair.getSecond(), newExpr);
        if (!GroovyConfigUtils.getInstance().isVersionAtLeast(psiElement, GroovyConfigUtils.GROOVY2_2)) {
            buffer.append(" as ").append(iface.getQualifiedName());
        }
    } else {
        buffer.append("[");
        buffer.append("\n");
        for (Pair<PsiMethod, GrOpenBlock> pair : methods) {
            final PsiMethod method = pair.getFirst();
            final GrOpenBlock block = pair.getSecond();
            buffer.append(method.getName()).append(": ");
            appendClosureTextByMethod(method, buffer, block, newExpr);
            buffer.append(",\n");
        }
        if (!methods.isEmpty()) {
            buffer.delete(buffer.length() - 2, buffer.length());
            buffer.append('\n');
        }
        buffer.append("]");
        buffer.append(" as ").append(iface.getQualifiedName());
    }
    createAndAdjustNewExpression(project, newExpr, buffer);
}
Also used : CandidateInfo(com.intellij.psi.infos.CandidateInfo) GrAnonymousClassDefinition(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrAnonymousClassDefinition) GrMethod(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod) GrNewExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrNewExpression) GrTypeDefinitionBody(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrTypeDefinitionBody) GrCodeReferenceElement(org.jetbrains.plugins.groovy.lang.psi.api.types.GrCodeReferenceElement) GrOpenBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrOpenBlock) GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement) Pair(com.intellij.openapi.util.Pair)

Example 4 with GrTypeDefinitionBody

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

the class GroovyFoldingBuilder method isSingleHighLevelClassBody.

private static boolean isSingleHighLevelClassBody(PsiElement element) {
    if (!(element instanceof GrTypeDefinitionBody))
        return false;
    final PsiElement parent = element.getParent();
    if (!(parent instanceof GrTypeDefinition))
        return false;
    final GrTypeDefinition clazz = (GrTypeDefinition) parent;
    if (clazz.isAnonymous() || clazz.getContainingClass() != null)
        return false;
    final PsiFile file = element.getContainingFile();
    return file instanceof GroovyFile && ((GroovyFile) file).getClasses().length == 1;
}
Also used : GrTypeDefinitionBody(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrTypeDefinitionBody) GrTypeDefinition(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrTypeDefinition) LeafPsiElement(com.intellij.psi.impl.source.tree.LeafPsiElement) GroovyFile(org.jetbrains.plugins.groovy.lang.psi.GroovyFile)

Example 5 with GrTypeDefinitionBody

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

the class GrTypeDefinitionImpl method add.

@Override
public PsiElement add(@NotNull PsiElement psiElement) throws IncorrectOperationException {
    final GrTypeDefinitionBody body = getBody();
    if (body == null)
        throw new IncorrectOperationException("Class must have body");
    final PsiElement lBrace = body.getLBrace();
    if (lBrace == null)
        throw new IncorrectOperationException("No left brace");
    PsiMember member = getAnyMember(psiElement);
    PsiElement anchor = member != null ? getDefaultAnchor(body, member) : null;
    if (anchor == null) {
        anchor = lBrace.getNextSibling();
    }
    if (anchor != null) {
        ASTNode node = anchor.getNode();
        assert node != null;
        if (GroovyTokenTypes.mSEMI.equals(node.getElementType())) {
            anchor = anchor.getNextSibling();
        }
        if (psiElement instanceof GrField) {
            //add field with modifiers which are in its parent
            int i = ArrayUtilRt.find(((GrVariableDeclaration) psiElement.getParent()).getVariables(), psiElement);
            psiElement = body.addBefore(psiElement.getParent(), anchor);
            GrVariable[] vars = ((GrVariableDeclaration) psiElement).getVariables();
            for (int j = 0; j < vars.length; j++) {
                if (i != j)
                    vars[i].delete();
            }
            psiElement = vars[i];
        } else {
            psiElement = body.addBefore(psiElement, anchor);
        }
    } else {
        psiElement = body.add(psiElement);
    }
    return psiElement;
}
Also used : GrTypeDefinitionBody(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrTypeDefinitionBody) GrVariable(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable) GrField(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrField) GrVariableDeclaration(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariableDeclaration) ASTNode(com.intellij.lang.ASTNode) IncorrectOperationException(com.intellij.util.IncorrectOperationException) LeafPsiElement(com.intellij.psi.impl.source.tree.LeafPsiElement)

Aggregations

GrTypeDefinitionBody (org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrTypeDefinitionBody)11 LeafPsiElement (com.intellij.psi.impl.source.tree.LeafPsiElement)4 GrVariableDeclaration (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariableDeclaration)4 PsiElement (com.intellij.psi.PsiElement)3 GroovyFile (org.jetbrains.plugins.groovy.lang.psi.GroovyFile)3 GroovyPsiElement (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement)3 GrTypeDefinition (org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrTypeDefinition)3 GrMethod (org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod)3 ASTNode (com.intellij.lang.ASTNode)2 CandidateInfo (com.intellij.psi.infos.CandidateInfo)2 ArrayList (java.util.ArrayList)2 GrVariable (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable)2 GrCodeBlock (org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrCodeBlock)2 GrCodeReferenceElement (org.jetbrains.plugins.groovy.lang.psi.api.types.GrCodeReferenceElement)2 LineRange (com.intellij.codeInsight.editorActions.moveUpDown.LineRange)1 Pair (com.intellij.openapi.util.Pair)1 TextRange (com.intellij.openapi.util.TextRange)1 VirtualFile (com.intellij.openapi.vfs.VirtualFile)1 LanguageLevel (com.intellij.pom.java.LanguageLevel)1 PsiFile (com.intellij.psi.PsiFile)1