Search in sources :

Example 36 with GrTypeDefinition

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

the class GroovyPositionManager method getClassNameForJvm.

@Nullable
private static String getClassNameForJvm(@NotNull final PsiClass typeDefinition) {
    String suffix = typeDefinition instanceof GrTypeDefinition && ((GrTypeDefinition) typeDefinition).isTrait() ? "$Trait$Helper" : "";
    final PsiClass psiClass = typeDefinition.getContainingClass();
    if (psiClass != null) {
        String parent = getClassNameForJvm(psiClass);
        return parent == null ? null : parent + "$" + typeDefinition.getName() + suffix;
    }
    PsiFile file = typeDefinition.getContainingFile();
    if (file instanceof GroovyFile && ((GroovyFile) file).isScript()) {
        for (ScriptPositionManagerHelper helper : ScriptPositionManagerHelper.EP_NAME.getExtensions()) {
            String s = helper.isAppropriateScriptFile((GroovyFile) file) ? helper.customizeClassName(typeDefinition) : null;
            if (s != null) {
                return s;
            }
        }
    }
    String qname = typeDefinition.getQualifiedName();
    return qname == null ? null : qname + suffix;
}
Also used : GrTypeDefinition(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrTypeDefinition) PsiClass(com.intellij.psi.PsiClass) ScriptPositionManagerHelper(org.jetbrains.plugins.groovy.extensions.debugger.ScriptPositionManagerHelper) PsiFile(com.intellij.psi.PsiFile) GroovyFile(org.jetbrains.plugins.groovy.lang.psi.GroovyFile) Nullable(org.jetbrains.annotations.Nullable)

Example 37 with GrTypeDefinition

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

the class GroovyPositionManager method getAllClasses.

@Override
@NotNull
public List<ReferenceType> getAllClasses(@NotNull final SourcePosition position) throws NoDataException {
    if (LOG.isDebugEnabled()) {
        LOG.debug("getAllClasses: " + position);
    }
    checkGroovyFile(position);
    List<ReferenceType> result = ApplicationManager.getApplication().runReadAction(new Computable<List<ReferenceType>>() {

        @Override
        public List<ReferenceType> compute() {
            GroovyPsiElement sourceImage = findReferenceTypeSourceImage(position);
            if (sourceImage instanceof GrTypeDefinition && !((GrTypeDefinition) sourceImage).isAnonymous()) {
                String qName = getClassNameForJvm((GrTypeDefinition) sourceImage);
                if (qName != null)
                    return myDebugProcess.getVirtualMachineProxy().classesByName(qName);
            } else if (sourceImage == null) {
                final String scriptName = getScriptQualifiedName(position);
                if (scriptName != null)
                    return myDebugProcess.getVirtualMachineProxy().classesByName(scriptName);
            } else {
                String enclosingName = findEnclosingName(position);
                if (enclosingName == null)
                    return null;
                final List<ReferenceType> outers = myDebugProcess.getVirtualMachineProxy().classesByName(enclosingName);
                final List<ReferenceType> result = new ArrayList<>(outers.size());
                for (ReferenceType outer : outers) {
                    final ReferenceType nested = findNested(outer, sourceImage, position);
                    if (nested != null) {
                        result.add(nested);
                    }
                }
                return result;
            }
            return null;
        }
    });
    if (LOG.isDebugEnabled()) {
        LOG.debug("getAllClasses = " + result);
    }
    if (result == null)
        throw NoDataException.INSTANCE;
    return result;
}
Also used : GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement) GrTypeDefinition(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrTypeDefinition) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) ReferenceType(com.sun.jdi.ReferenceType) NotNull(org.jetbrains.annotations.NotNull)

Example 38 with GrTypeDefinition

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

the class GroovyStatementMover method allRanges.

private List<LineRange> allRanges(final GroovyPsiElement scope, final boolean stmtLevel, final boolean topLevel) {
    final ArrayList<LineRange> result = new ArrayList<>();
    scope.accept(new PsiRecursiveElementVisitor() {

        int lastStart = -1;

        private void addRange(int endLine) {
            if (lastStart >= 0) {
                result.add(new LineRange(lastStart, endLine));
            }
            lastStart = endLine;
        }

        @Override
        public void visitElement(PsiElement element) {
            if (stmtLevel && element instanceof GrCodeBlock) {
                final PsiElement lBrace = ((GrCodeBlock) element).getLBrace();
                if (nlsAfter(lBrace)) {
                    assert lBrace != null;
                    addRange(new LineRange(lBrace).endLine);
                }
                addChildRanges(((GrCodeBlock) element).getStatements());
                final PsiElement rBrace = ((GrCodeBlock) element).getRBrace();
                if (nlsAfter(rBrace)) {
                    assert rBrace != null;
                    final int endLine = new LineRange(rBrace).endLine;
                    if (lastStart >= 0) {
                        for (int i = lastStart + 1; i < endLine; i++) {
                            addRange(i);
                        }
                    }
                }
            } else if (stmtLevel && element instanceof GrCaseSection) {
                final GrCaseLabel[] allLabels = ((GrCaseSection) element).getCaseLabels();
                final GrCaseLabel label = allLabels[0];
                if (nlsAfter(label)) {
                    addRange(new LineRange(label).endLine);
                }
                addChildRanges(((GrCaseSection) element).getStatements());
            } else if (element instanceof GroovyFileBase) {
                addChildRanges(((GroovyFileBase) element).getTopStatements());
            } else if (!stmtLevel && !topLevel && element instanceof GrTypeDefinitionBody) {
                addChildRanges(((GrTypeDefinitionBody) element).getMemberDeclarations());
            } else {
                super.visitElement(element);
            }
        }

        private boolean shouldDigInside(GroovyPsiElement statement) {
            if (stmtLevel && (statement instanceof GrMethod || statement instanceof GrTypeDefinition)) {
                return false;
            }
            if (statement instanceof GrVariableDeclaration && !stmtLevel) {
                return false;
            }
            return true;
        }

        private void addChildRanges(GroovyPsiElement[] statements) {
            for (int i = 0; i < statements.length; i++) {
                GroovyPsiElement statement = statements[i];
                if (nlsAfter(statement)) {
                    final LineRange range = getLineRange(statement);
                    if ((i == 0 || isStatement(statements[i - 1])) && isStatement(statement)) {
                        for (int j = lastStart; j < range.startLine; j++) {
                            addRange(j + 1);
                        }
                    }
                    lastStart = range.startLine;
                    if (shouldDigInside(statement)) {
                        statement.accept(this);
                    }
                    addRange(range.endLine);
                }
            }
        }
    });
    return result;
}
Also used : PsiRecursiveElementVisitor(com.intellij.psi.PsiRecursiveElementVisitor) GroovyFileBase(org.jetbrains.plugins.groovy.lang.psi.GroovyFileBase) GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement) GrCaseLabel(org.jetbrains.plugins.groovy.lang.psi.api.statements.clauses.GrCaseLabel) ArrayList(java.util.ArrayList) GrMethod(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod) LineRange(com.intellij.codeInsight.editorActions.moveUpDown.LineRange) GrTypeDefinitionBody(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrTypeDefinitionBody) GrVariableDeclaration(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariableDeclaration) GrTypeDefinition(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrTypeDefinition) GrCaseSection(org.jetbrains.plugins.groovy.lang.psi.api.statements.clauses.GrCaseSection) PsiElement(com.intellij.psi.PsiElement) GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement) GrCodeBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrCodeBlock)

Example 39 with GrTypeDefinition

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

the class GrHighlightHandlerFactory method createHighlightUsagesHandler.

@Override
public HighlightUsagesHandlerBase createHighlightUsagesHandler(@NotNull Editor editor, @NotNull PsiFile file, @NotNull PsiElement target) {
    ASTNode node = target.getNode();
    if (node == null)
        return null;
    IElementType type = node.getElementType();
    if (type == GroovyTokenTypes.kIMPLEMENTS || type == GroovyTokenTypes.kEXTENDS) {
        PsiElement parent = target.getParent();
        if (!(parent instanceof GrReferenceList))
            return null;
        PsiElement grand = parent.getParent();
        if (!(grand instanceof GrTypeDefinition))
            return null;
        return new GrHighlightOverridingMethodsHandler(editor, file, target, (GrTypeDefinition) grand);
    } else if (type == GroovyTokenTypes.kRETURN || type == GroovyTokenTypes.kTHROW) {
        return new GrHighlightExitPointHandler(editor, file, target);
    }
    return null;
}
Also used : IElementType(com.intellij.psi.tree.IElementType) GrTypeDefinition(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrTypeDefinition) ASTNode(com.intellij.lang.ASTNode) GrReferenceList(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrReferenceList) PsiElement(com.intellij.psi.PsiElement)

Example 40 with GrTypeDefinition

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

the class GrIntroduceValidatorEngine method validateOccurrencesDown.

/**
   * @param startElement Container to start checking conflicts from
   * @param conflicts    Conflict accumulator
   * @param varName      Variable name
   * @param startOffset
   */
private void validateOccurrencesDown(@NotNull PsiElement startElement, @NotNull MultiMap<PsiElement, String> conflicts, @NotNull String varName, double startOffset) {
    PsiElement child = startElement.getFirstChild();
    while (child != null) {
        // Do not check defined classes, methods, closures and blocks before
        if (child instanceof GrTypeDefinition || child instanceof GrMethod || GroovyRefactoringUtil.isAppropriateContainerForIntroduceVariable(child) && child.getTextRange().getEndOffset() < startOffset) {
            myReporter.check(child, conflicts, varName);
            child = child.getNextSibling();
            continue;
        }
        if (child instanceof GrVariable) {
            myReporter.check(child, conflicts, varName);
            validateOccurrencesDown(child, conflicts, varName, startOffset);
        } else {
            validateOccurrencesDown(child, conflicts, varName, startOffset);
        }
        child = child.getNextSibling();
    }
}
Also used : GrVariable(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable) GrTypeDefinition(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrTypeDefinition) GrMethod(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod) PsiElement(com.intellij.psi.PsiElement)

Aggregations

GrTypeDefinition (org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrTypeDefinition)78 PsiElement (com.intellij.psi.PsiElement)17 GrMethod (org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod)17 PsiClass (com.intellij.psi.PsiClass)16 NotNull (org.jetbrains.annotations.NotNull)15 Nullable (org.jetbrains.annotations.Nullable)14 GroovyFile (org.jetbrains.plugins.groovy.lang.psi.GroovyFile)9 PsiFile (com.intellij.psi.PsiFile)8 GroovyFileBase (org.jetbrains.plugins.groovy.lang.psi.GroovyFileBase)8 GroovyPsiElement (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement)8 GrField (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrField)8 GrReferenceExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression)8 GrClosableBlock (org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock)7 GrExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression)7 Project (com.intellij.openapi.project.Project)6 ArrayList (java.util.ArrayList)6 GrParameter (org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter)6 Editor (com.intellij.openapi.editor.Editor)5 GrOpenBlock (org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrOpenBlock)5 IncorrectOperationException (com.intellij.util.IncorrectOperationException)4