Search in sources :

Example 1 with GrTypeDefinition

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

the class CreateClassActionBase method createClassByType.

@Nullable
public static GrTypeDefinition createClassByType(@NotNull final PsiDirectory directory, @NotNull final String name, @NotNull final PsiManager manager, @Nullable final PsiElement contextElement, @NotNull final String templateName, boolean allowReformatting) {
    return WriteAction.compute(() -> {
        try {
            GrTypeDefinition targetClass = null;
            try {
                PsiFile file = GroovyTemplatesFactory.createFromTemplate(directory, name, name + ".groovy", templateName, allowReformatting);
                for (PsiElement element : file.getChildren()) {
                    if (element instanceof GrTypeDefinition) {
                        targetClass = ((GrTypeDefinition) element);
                        break;
                    }
                }
                if (targetClass == null) {
                    throw new IncorrectOperationException(GroovyBundle.message("no.class.in.file.template"));
                }
            } catch (final IncorrectOperationException e) {
                ApplicationManager.getApplication().invokeLater(() -> Messages.showErrorDialog(GroovyBundle.message("cannot.create.class.error.text", name, e.getLocalizedMessage()), GroovyBundle.message("cannot.create.class.error.title")));
                return null;
            }
            PsiModifierList modifiers = targetClass.getModifierList();
            if (contextElement != null && !JavaPsiFacade.getInstance(manager.getProject()).getResolveHelper().isAccessible(targetClass, contextElement, null) && modifiers != null) {
                modifiers.setModifierProperty(PsiModifier.PUBLIC, true);
            }
            return targetClass;
        } catch (IncorrectOperationException e) {
            LOG.error(e);
            return null;
        }
    });
}
Also used : GrTypeDefinition(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrTypeDefinition) IncorrectOperationException(com.intellij.util.IncorrectOperationException) Nullable(org.jetbrains.annotations.Nullable)

Example 2 with GrTypeDefinition

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

the class GroovyStatementMover method checkAvailable.

@Override
public boolean checkAvailable(@NotNull Editor editor, @NotNull PsiFile file, @NotNull MoveInfo info, boolean down) {
    final Project project = file.getProject();
    if (!HandlerUtils.canBeInvoked(editor, project) || !(file instanceof GroovyFileBase))
        return false;
    LineRange range = getLineRangeFromSelection(editor);
    final Document document = editor.getDocument();
    final int offset = document.getLineStartOffset(range.startLine);
    final GrLiteral literal = PsiTreeUtil.findElementOfClassAtOffset(file, offset, GrLiteral.class, false);
    //multiline string
    if (literal != null && literal.textContains('\n'))
        return false;
    final GroovyPsiElement pivot = getElementToMove((GroovyFileBase) file, offset);
    if (pivot == null)
        return false;
    final LineRange pivotRange = getLineRange(pivot);
    range = new LineRange(Math.min(range.startLine, pivotRange.startLine), Math.max(range.endLine, pivotRange.endLine));
    final GroovyPsiElement scope = PsiTreeUtil.getParentOfType(pivot, GrMethod.class, GrTypeDefinitionBody.class, GroovyFileBase.class);
    final boolean stmtLevel = isStatement(pivot);
    boolean topLevel = pivot instanceof GrTypeDefinition && pivot.getParent() instanceof GroovyFileBase;
    final List<LineRange> allRanges = allRanges(scope, stmtLevel, topLevel);
    LineRange prev = null;
    LineRange next = null;
    for (LineRange each : allRanges) {
        if (each.endLine <= range.startLine) {
            prev = each;
        }
        if (each.containsLine(range.startLine)) {
            range = new LineRange(each.startLine, range.endLine);
        }
        if (each.startLine < range.endLine && each.endLine > range.endLine) {
            range = new LineRange(range.startLine, each.endLine);
        }
        if (each.startLine >= range.endLine && next == null) {
            next = each;
        }
    }
    info.toMove = range;
    info.toMove2 = down ? next : prev;
    return true;
}
Also used : Project(com.intellij.openapi.project.Project) GroovyFileBase(org.jetbrains.plugins.groovy.lang.psi.GroovyFileBase) GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement) GrTypeDefinition(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrTypeDefinition) Document(com.intellij.openapi.editor.Document) LineRange(com.intellij.codeInsight.editorActions.moveUpDown.LineRange) GrLiteral(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.literals.GrLiteral)

Example 3 with GrTypeDefinition

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

the class CreateParameterForFieldIntention method findFieldCandidates.

@Nullable
private static List<GrField> findFieldCandidates(PsiElement element) {
    final GrMethod constructor = PsiTreeUtil.getParentOfType(element, GrMethod.class);
    if (constructor == null || !constructor.isConstructor())
        return null;
    if (constructor.getBlock() == null)
        return null;
    if (PsiTreeUtil.isAncestor(constructor.getBlock(), element, false)) {
        return null;
    }
    final PsiClass clazz = constructor.getContainingClass();
    if (!(clazz instanceof GrTypeDefinition))
        return null;
    return findCandidatesCached(constructor, (GrTypeDefinition) clazz);
}
Also used : GrTypeDefinition(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrTypeDefinition) GrMethod(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod) Nullable(org.jetbrains.annotations.Nullable)

Example 4 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 5 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)

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