Search in sources :

Example 16 with GrTypeDefinition

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

the class GrIntroduceValidatorEngine method validateVariableOccurrencesUp.

private void validateVariableOccurrencesUp(PsiElement startElement, MultiMap<PsiElement, String> conflicts, @NotNull String varName, double startOffset) {
    PsiElement prevSibling = startElement.getPrevSibling();
    while (prevSibling != null) {
        if (!(GroovyRefactoringUtil.isAppropriateContainerForIntroduceVariable(prevSibling) && prevSibling.getTextRange().getEndOffset() < startOffset)) {
            validateOccurrencesDown(prevSibling, conflicts, varName, startOffset);
        }
        prevSibling = prevSibling.getPrevSibling();
    }
    PsiElement parent = startElement.getParent();
    // Do not check context out of method, type definition and directories
    if (parent == null || parent instanceof GrMethod || parent instanceof GrTypeDefinition || parent instanceof GroovyFileBase || parent instanceof PsiDirectory) {
        return;
    }
    validateVariableOccurrencesUp(parent, conflicts, varName, startOffset);
}
Also used : GroovyFileBase(org.jetbrains.plugins.groovy.lang.psi.GroovyFileBase) GrTypeDefinition(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrTypeDefinition) PsiDirectory(com.intellij.psi.PsiDirectory) GrMethod(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod) PsiElement(com.intellij.psi.PsiElement)

Example 17 with GrTypeDefinition

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

the class GroovyDocumentationProvider method generateDocumentationContentStub.

@Override
public String generateDocumentationContentStub(PsiComment contextComment) {
    if (!(contextComment instanceof GrDocComment)) {
        return null;
    }
    final GrDocCommentOwner owner = GrDocCommentUtil.findDocOwner((GrDocComment) contextComment);
    if (owner == null)
        return null;
    Project project = contextComment.getProject();
    final CodeDocumentationAwareCommenter commenter = (CodeDocumentationAwareCommenter) LanguageCommenters.INSTANCE.forLanguage(owner.getLanguage());
    StringBuilder builder = StringBuilderSpinAllocator.alloc();
    try {
        if (owner instanceof GrMethod) {
            final GrMethod method = (GrMethod) owner;
            JavaDocumentationProvider.generateParametersTakingDocFromSuperMethods(project, builder, commenter, method);
            final PsiType returnType = method.getInferredReturnType();
            if ((returnType != null || method.getModifierList().hasModifierProperty(GrModifier.DEF)) && !PsiType.VOID.equals(returnType)) {
                builder.append(CodeDocumentationUtil.createDocCommentLine(RETURN_TAG, project, commenter));
                builder.append(LINE_SEPARATOR);
            }
            final PsiClassType[] references = method.getThrowsList().getReferencedTypes();
            for (PsiClassType reference : references) {
                builder.append(CodeDocumentationUtil.createDocCommentLine(THROWS_TAG, project, commenter));
                builder.append(reference.getClassName());
                builder.append(LINE_SEPARATOR);
            }
        } else if (owner instanceof GrTypeDefinition) {
            final PsiTypeParameterList typeParameterList = ((PsiClass) owner).getTypeParameterList();
            if (typeParameterList != null) {
                JavaDocumentationProvider.createTypeParamsListComment(builder, project, commenter, typeParameterList);
            }
        }
        return builder.length() > 0 ? builder.toString() : null;
    } finally {
        StringBuilderSpinAllocator.dispose(builder);
    }
}
Also used : Project(com.intellij.openapi.project.Project) CodeDocumentationAwareCommenter(com.intellij.lang.CodeDocumentationAwareCommenter) GrTypeDefinition(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrTypeDefinition) GrDocCommentOwner(org.jetbrains.plugins.groovy.lang.groovydoc.psi.api.GrDocCommentOwner) GrMethod(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod) GrDocComment(org.jetbrains.plugins.groovy.lang.groovydoc.psi.api.GrDocComment)

Example 18 with GrTypeDefinition

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

the class GroovyRefactoringUtil method getExpressionOccurrences.

public static PsiElement[] getExpressionOccurrences(@NotNull PsiElement expr, @NotNull PsiElement scope) {
    ArrayList<PsiElement> occurrences = new ArrayList<>();
    Comparator<PsiElement> comparator = (element1, element2) -> {
        if (element1 != null && element1.equals(element2))
            return 0;
        if (element1 instanceof GrParameter && element2 instanceof GrParameter) {
            final String name1 = ((GrParameter) element1).getName();
            final String name2 = ((GrParameter) element2).getName();
            return name1.compareTo(name2);
        }
        return 1;
    };
    if (scope instanceof GrLoopStatement) {
        PsiElement son = expr;
        while (son.getParent() != null && !(son.getParent() instanceof GrLoopStatement)) {
            son = son.getParent();
        }
        assert scope.equals(son.getParent());
        collectOccurrences(expr, son, occurrences, comparator, false);
    } else {
        collectOccurrences(expr, scope, occurrences, comparator, scope instanceof GrTypeDefinition || scope instanceof GroovyFileBase);
    }
    return PsiUtilCore.toPsiElementArray(occurrences);
}
Also used : Language(com.intellij.lang.Language) IElementType(com.intellij.psi.tree.IElementType) GrClosableBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock) GrCodeReferenceElement(org.jetbrains.plugins.groovy.lang.psi.api.types.GrCodeReferenceElement) EditorColorsManager(com.intellij.openapi.editor.colors.EditorColorsManager) GrVariableDeclarationOwner(org.jetbrains.plugins.groovy.lang.psi.api.util.GrVariableDeclarationOwner) GrContinueStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.branch.GrContinueStatement) HighlightManager(com.intellij.codeInsight.highlighting.HighlightManager) org.jetbrains.plugins.groovy.lang.psi(org.jetbrains.plugins.groovy.lang.psi) TokenSets(org.jetbrains.plugins.groovy.lang.lexer.TokenSets) RangeHighlighter(com.intellij.openapi.editor.markup.RangeHighlighter) Logger(com.intellij.openapi.diagnostic.Logger) PsiEquivalenceUtil(com.intellij.codeInsight.PsiEquivalenceUtil) GrCallExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.path.GrCallExpression) GroovyTokenTypes(org.jetbrains.plugins.groovy.lang.lexer.GroovyTokenTypes) GrMethodCallExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.path.GrMethodCallExpression) GrParameter(org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter) IncorrectOperationException(com.intellij.util.IncorrectOperationException) GrTypeArgumentList(org.jetbrains.plugins.groovy.lang.psi.api.types.GrTypeArgumentList) TextRange(com.intellij.openapi.util.TextRange) GrCaseSection(org.jetbrains.plugins.groovy.lang.psi.api.statements.clauses.GrCaseSection) Nullable(org.jetbrains.annotations.Nullable) org.jetbrains.plugins.groovy.lang.psi.api.statements(org.jetbrains.plugins.groovy.lang.psi.api.statements) TextAttributes(com.intellij.openapi.editor.markup.TextAttributes) GrMethod(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod) com.intellij.psi(com.intellij.psi) NotNull(org.jetbrains.annotations.NotNull) Ref(com.intellij.openapi.util.Ref) java.util(java.util) ArrayUtil(com.intellij.util.ArrayUtil) GroovyLanguage(org.jetbrains.plugins.groovy.GroovyLanguage) GrStatementOwner(org.jetbrains.plugins.groovy.lang.psi.api.util.GrStatementOwner) ContainerUtil(com.intellij.util.containers.ContainerUtil) GrCodeBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrCodeBlock) JavaCodeStyleManager(com.intellij.psi.codeStyle.JavaCodeStyleManager) GrOpenBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrOpenBlock) GrReturnStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.branch.GrReturnStatement) Project(com.intellij.openapi.project.Project) GrTypeDefinition(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrTypeDefinition) StringUtil(com.intellij.openapi.util.text.StringUtil) com.intellij.psi.util(com.intellij.psi.util) LeafPsiElement(com.intellij.psi.impl.source.tree.LeafPsiElement) GrBreakStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.branch.GrBreakStatement) Editor(com.intellij.openapi.editor.Editor) EditorColors(com.intellij.openapi.editor.colors.EditorColors) RefactoringUtil(com.intellij.refactoring.util.RefactoringUtil) GrEnumConstant(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrEnumConstant) org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions) GrAnonymousClassDefinition(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrAnonymousClassDefinition) GrTypeDefinition(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrTypeDefinition) GrParameter(org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter) LeafPsiElement(com.intellij.psi.impl.source.tree.LeafPsiElement)

Example 19 with GrTypeDefinition

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

the class ClassItemGeneratorImpl method collectMethods.

@Override
public Collection<PsiMethod> collectMethods(PsiClass typeDefinition) {
    List<PsiMethod> result = ContainerUtil.filter(typeDefinition.getMethods(), m -> !GroovyObjectTransformationSupport.isGroovyObjectSupportMethod(m));
    if (typeDefinition instanceof GroovyScriptClass) {
        final GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(context.project);
        final String name = typeDefinition.getName();
        GrTypeDefinition tempClass = factory.createTypeDefinition("class " + name + " extends groovy.lang.Script {\n" + "  def " + name + "(groovy.lang.Binding binding){\n" + "    super(binding);\n" + "  }\n" + "  def " + name + "(){\n" + "    super();\n" + "  }\n" + "}");
        ContainerUtil.addAll(result, tempClass.getCodeConstructors());
    }
    return result;
}
Also used : GroovyPsiElementFactory(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory) GroovyScriptClass(org.jetbrains.plugins.groovy.lang.psi.impl.synthetic.GroovyScriptClass) GrTypeDefinition(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrTypeDefinition)

Example 20 with GrTypeDefinition

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

the class GrMemberInfoStorage method buildSubClassesMap.

@Override
protected void buildSubClassesMap(PsiClass aClass) {
    if (aClass instanceof GrTypeDefinition) {
        final GrExtendsClause extendsList = ((GrTypeDefinition) aClass).getExtendsClause();
        if (extendsList != null) {
            buildSubClassesMapForList(extendsList.getReferencedTypes(), (GrTypeDefinition) aClass);
        }
        final GrImplementsClause implementsList = ((GrTypeDefinition) aClass).getImplementsClause();
        if (implementsList != null) {
            buildSubClassesMapForList(implementsList.getReferencedTypes(), (GrTypeDefinition) aClass);
        }
    }
}
Also used : GrExtendsClause(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrExtendsClause) GrTypeDefinition(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrTypeDefinition) GrImplementsClause(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrImplementsClause)

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