Search in sources :

Example 26 with GroovyFileBase

use of org.jetbrains.plugins.groovy.lang.psi.GroovyFileBase in project intellij-community by JetBrains.

the class GrAliasImportIntention method doRefactoring.

private static void doRefactoring(@NotNull Project project, @NotNull GrImportStatement importStatement, @NotNull PsiMember member) {
    if (member instanceof GrAccessorMethod && !importStatement.isOnDemand() && !importStatement.getImportedName().equals(member.getName())) {
        member = ((GrAccessorMethod) member).getProperty();
    }
    final GroovyFileBase file = (GroovyFileBase) importStatement.getContainingFile();
    final List<UsageInfo> usages = findUsages(member, file);
    GrImportStatement templateImport = createTemplateImport(project, importStatement, member, file);
    if (ApplicationManager.getApplication().isUnitTestMode()) {
        if (!importStatement.isOnDemand()) {
            importStatement.delete();
        }
        updateRefs(usages, member.getName(), templateImport);
    } else {
        runTemplate(project, importStatement, member, file, usages, templateImport);
    }
}
Also used : GrAccessorMethod(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrAccessorMethod) GroovyFileBase(org.jetbrains.plugins.groovy.lang.psi.GroovyFileBase) GrImportStatement(org.jetbrains.plugins.groovy.lang.psi.api.toplevel.imports.GrImportStatement) UsageInfo(com.intellij.usageView.UsageInfo)

Example 27 with GroovyFileBase

use of org.jetbrains.plugins.groovy.lang.psi.GroovyFileBase in project intellij-community by JetBrains.

the class MyPredicate method processIntention.

@Override
protected void processIntention(@NotNull PsiElement element, @NotNull final Project project, Editor editor) throws IncorrectOperationException {
    final GrListOrMap map = (GrListOrMap) element;
    final GrNamedArgument[] namedArguments = map.getNamedArguments();
    LOG.assertTrue(map.getInitializers().length == 0);
    final PsiFile file = map.getContainingFile();
    final String packageName = file instanceof GroovyFileBase ? ((GroovyFileBase) file).getPackageName() : "";
    final CreateClassDialog dialog = new CreateClassDialog(project, GroovyBundle.message("create.class.family.name"), "", packageName, GrCreateClassKind.CLASS, true, ModuleUtilCore.findModuleForPsiElement(element));
    dialog.show();
    if (dialog.getExitCode() != DialogWrapper.OK_EXIT_CODE)
        return;
    boolean replaceReturnType = checkForReturnFromMethod(map);
    boolean variableDeclaration = checkForVariableDeclaration(map);
    final GrParameter methodParameter = checkForMethodParameter(map);
    final String qualifiedClassName = dialog.getClassName();
    final String selectedPackageName = StringUtil.getPackageName(qualifiedClassName);
    final String shortName = StringUtil.getShortName(qualifiedClassName);
    final GrTypeDefinition typeDefinition = createClass(project, namedArguments, selectedPackageName, shortName);
    final PsiClass generatedClass = CreateClassActionBase.createClassByType(dialog.getTargetDirectory(), typeDefinition.getName(), PsiManager.getInstance(project), map, GroovyTemplates.GROOVY_CLASS, true);
    final PsiClass replaced = (PsiClass) generatedClass.replace(typeDefinition);
    replaceMapWithClass(project, map, replaced, replaceReturnType, variableDeclaration, methodParameter);
}
Also used : GroovyFileBase(org.jetbrains.plugins.groovy.lang.psi.GroovyFileBase) GrNamedArgument(org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrNamedArgument) CreateClassDialog(com.intellij.codeInsight.intention.impl.CreateClassDialog) GrTypeDefinition(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrTypeDefinition) GrListOrMap(org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.GrListOrMap) GrParameter(org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter)

Example 28 with GroovyFileBase

use of org.jetbrains.plugins.groovy.lang.psi.GroovyFileBase in project intellij-community by JetBrains.

the class GroovyBlockGenerator method getGroovyChildren.

private static ASTNode[] getGroovyChildren(final ASTNode node) {
    PsiElement psi = node.getPsi();
    if (psi instanceof OuterLanguageElement) {
        TextRange range = node.getTextRange();
        ArrayList<ASTNode> childList = new ArrayList<>();
        PsiFile groovyFile = psi.getContainingFile().getViewProvider().getPsi(GroovyLanguage.INSTANCE);
        if (groovyFile instanceof GroovyFileBase) {
            addChildNodes(groovyFile, childList, range, psi);
        }
        return childList.toArray(new ASTNode[childList.size()]);
    }
    return node.getChildren(null);
}
Also used : OuterLanguageElement(com.intellij.psi.templateLanguages.OuterLanguageElement) GroovyFileBase(org.jetbrains.plugins.groovy.lang.psi.GroovyFileBase) ArrayList(java.util.ArrayList) ASTNode(com.intellij.lang.ASTNode) TextRange(com.intellij.openapi.util.TextRange) PsiFile(com.intellij.psi.PsiFile) PsiElement(com.intellij.psi.PsiElement) LeafPsiElement(com.intellij.psi.impl.source.tree.LeafPsiElement)

Example 29 with GroovyFileBase

use of org.jetbrains.plugins.groovy.lang.psi.GroovyFileBase in project intellij-community by JetBrains.

the class ControlFlowBuilderUtil method isCertainlyReturnStatement.

/**
   * check whether statement is return (the statement which provides return value) statement of method or closure.
   *
   * @param st
   * @return
   */
public static boolean isCertainlyReturnStatement(GrStatement st) {
    final PsiElement parent = st.getParent();
    if (parent instanceof GrOpenBlock) {
        if (st != ArrayUtil.getLastElement(((GrOpenBlock) parent).getStatements()))
            return false;
        PsiElement pparent = parent.getParent();
        if (pparent instanceof GrMethod) {
            return true;
        }
        if (pparent instanceof GrBlockStatement || pparent instanceof GrCatchClause || pparent instanceof GrLabeledStatement) {
            pparent = pparent.getParent();
        }
        if (pparent instanceof GrIfStatement || pparent instanceof GrControlStatement || pparent instanceof GrTryCatchStatement) {
            return isCertainlyReturnStatement((GrStatement) pparent);
        }
    } else if (parent instanceof GrClosableBlock) {
        return st == ArrayUtil.getLastElement(((GrClosableBlock) parent).getStatements());
    } else if (parent instanceof GroovyFileBase) {
        return st == ArrayUtil.getLastElement(((GroovyFileBase) parent).getStatements());
    } else if (parent instanceof GrForStatement || parent instanceof GrIfStatement && st != ((GrIfStatement) parent).getCondition() || parent instanceof GrSynchronizedStatement && st != ((GrSynchronizedStatement) parent).getMonitor() || parent instanceof GrWhileStatement && st != ((GrWhileStatement) parent).getCondition() || parent instanceof GrConditionalExpression && st != ((GrConditionalExpression) parent).getCondition() || parent instanceof GrElvisExpression) {
        return isCertainlyReturnStatement((GrStatement) parent);
    } else if (parent instanceof GrCaseSection) {
        final GrStatement[] statements = ((GrCaseSection) parent).getStatements();
        final GrStatement last = ArrayUtil.getLastElement(statements);
        final GrSwitchStatement switchStatement = (GrSwitchStatement) parent.getParent();
        if (last instanceof GrBreakStatement && statements.length > 1 && statements[statements.length - 2] == st) {
            return isCertainlyReturnStatement(switchStatement);
        } else if (st == last) {
            if (st instanceof GrBreakStatement || isLastStatementInCaseSection((GrCaseSection) parent, switchStatement)) {
                return isCertainlyReturnStatement(switchStatement);
            }
        }
    }
    return false;
}
Also used : GroovyFileBase(org.jetbrains.plugins.groovy.lang.psi.GroovyFileBase) GrMethod(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod) GrClosableBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock) GrBreakStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.branch.GrBreakStatement) GrCaseSection(org.jetbrains.plugins.groovy.lang.psi.api.statements.clauses.GrCaseSection) GrOpenBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrOpenBlock) GrControlStatement(org.jetbrains.plugins.groovy.lang.psi.api.formatter.GrControlStatement) PsiElement(com.intellij.psi.PsiElement)

Example 30 with GroovyFileBase

use of org.jetbrains.plugins.groovy.lang.psi.GroovyFileBase in project intellij-community by JetBrains.

the class GroovyClassNameInsertHandler method handleInsert.

@Override
public void handleInsert(InsertionContext context, JavaPsiClassReferenceElement item) {
    PsiFile file = context.getFile();
    Editor editor = context.getEditor();
    int endOffset = editor.getCaretModel().getOffset();
    if (PsiTreeUtil.findElementOfClassAtOffset(file, endOffset - 1, GrImportStatement.class, false) != null || !(file instanceof GroovyFileBase)) {
        AllClassesGetter.INSERT_FQN.handleInsert(context, item);
        return;
    }
    PsiDocumentManager.getInstance(context.getProject()).commitDocument(editor.getDocument());
    PsiElement position = file.findElementAt(endOffset - 1);
    boolean parens = shouldInsertParentheses(position);
    final PsiClass psiClass = item.getObject();
    if (isInVariable(position) || GroovyCompletionUtil.isInPossibleClosureParameter(position)) {
        Project project = context.getProject();
        String qname = psiClass.getQualifiedName();
        String shortName = psiClass.getName();
        if (qname == null)
            return;
        PsiClass aClass = JavaPsiFacade.getInstance(project).getResolveHelper().resolveReferencedClass(shortName, position);
        if (aClass == null) {
            ((GroovyFileBase) file).addImportForClass(psiClass);
            return;
        } else if (aClass == CompletionUtil.getOriginalOrSelf(psiClass)) {
            return;
        }
    }
    AllClassesGetter.TRY_SHORTENING.handleInsert(context, item);
    if (parens && context.getCompletionChar() != '[') {
        int identifierEnd = context.getTailOffset();
        GroovyPsiElement place = PsiTreeUtil.findElementOfClassAtOffset(context.getFile(), context.getStartOffset(), GroovyPsiElement.class, false);
        JavaCompletionUtil.insertParentheses(context, item, false, place != null && GroovyCompletionUtil.hasConstructorParameters(psiClass, place));
        if (context.getCompletionChar() == '<' || psiClass.hasTypeParameters() && context.getCompletionChar() != '(') {
            context.setAddCompletionChar(false);
            JavaCompletionUtil.promptTypeArgs(context, identifierEnd);
        }
    }
}
Also used : Project(com.intellij.openapi.project.Project) GroovyFileBase(org.jetbrains.plugins.groovy.lang.psi.GroovyFileBase) GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement) Editor(com.intellij.openapi.editor.Editor) GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement)

Aggregations

GroovyFileBase (org.jetbrains.plugins.groovy.lang.psi.GroovyFileBase)30 PsiElement (com.intellij.psi.PsiElement)12 GrTypeDefinition (org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrTypeDefinition)9 Project (com.intellij.openapi.project.Project)7 PsiFile (com.intellij.psi.PsiFile)7 NotNull (org.jetbrains.annotations.NotNull)7 Nullable (org.jetbrains.annotations.Nullable)6 GroovyPsiElement (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement)6 Editor (com.intellij.openapi.editor.Editor)5 Document (com.intellij.openapi.editor.Document)4 TextRange (com.intellij.openapi.util.TextRange)4 LineRange (com.intellij.codeInsight.editorActions.moveUpDown.LineRange)3 ArrayList (java.util.ArrayList)3 GrReferenceElement (org.jetbrains.plugins.groovy.lang.psi.GrReferenceElement)3 GrCaseSection (org.jetbrains.plugins.groovy.lang.psi.api.statements.clauses.GrCaseSection)3 GrMethod (org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod)3 ASTNode (com.intellij.lang.ASTNode)2 Module (com.intellij.openapi.module.Module)2 PsiRecursiveElementVisitor (com.intellij.psi.PsiRecursiveElementVisitor)2 GroovyFile (org.jetbrains.plugins.groovy.lang.psi.GroovyFile)2