Search in sources :

Example 6 with GrImportStatement

use of org.jetbrains.plugins.groovy.lang.psi.api.toplevel.imports.GrImportStatement in project intellij-community by JetBrains.

the class GrAliasImportIntention method updateRefs.

private static void updateRefs(List<UsageInfo> usages, final String memberName, final GrImportStatement updatedImport) {
    if (updatedImport == null)
        return;
    final String name = ApplicationManager.getApplication().runReadAction(new Computable<String>() {

        @Nullable
        @Override
        public String compute() {
            return updatedImport.getImportedName();
        }
    });
    for (final UsageInfo usage : usages) {
        ApplicationManager.getApplication().runWriteAction(() -> {
            final PsiElement usageElement = usage.getElement();
            if (usageElement == null)
                return;
            if (usageElement.getParent() instanceof GrImportStatement)
                return;
            if (usageElement instanceof GrReferenceElement) {
                final GrReferenceElement ref = (GrReferenceElement) usageElement;
                final PsiElement qualifier = ref.getQualifier();
                if (qualifier == null) {
                    final String refName = ref.getReferenceName();
                    if (refName == null)
                        return;
                    if (memberName.equals(refName)) {
                        ref.handleElementRename(name);
                    } else if (refName.equals(GroovyPropertyUtils.getPropertyNameByAccessorName(memberName))) {
                        final String newPropName = GroovyPropertyUtils.getPropertyNameByAccessorName(name);
                        if (newPropName != null) {
                            ref.handleElementRename(newPropName);
                        } else {
                            ref.handleElementRename(name);
                        }
                    } else if (refName.equals(GroovyPropertyUtils.getGetterNameBoolean(memberName))) {
                        final String getterName = GroovyPropertyUtils.getGetterNameBoolean(name);
                        ref.handleElementRename(getterName);
                    } else if (refName.equals(GroovyPropertyUtils.getGetterNameNonBoolean(memberName))) {
                        final String getterName = GroovyPropertyUtils.getGetterNameNonBoolean(name);
                        ref.handleElementRename(getterName);
                    } else if (refName.equals(GroovyPropertyUtils.getSetterName(memberName))) {
                        final String getterName = GroovyPropertyUtils.getSetterName(name);
                        ref.handleElementRename(getterName);
                    }
                }
            }
        });
    }
}
Also used : GrImportStatement(org.jetbrains.plugins.groovy.lang.psi.api.toplevel.imports.GrImportStatement) Nullable(org.jetbrains.annotations.Nullable) UsageInfo(com.intellij.usageView.UsageInfo) GrReferenceElement(org.jetbrains.plugins.groovy.lang.psi.GrReferenceElement)

Example 7 with GrImportStatement

use of org.jetbrains.plugins.groovy.lang.psi.api.toplevel.imports.GrImportStatement in project intellij-community by JetBrains.

the class MoveGroovyClassHandler method preprocessUsages.

@Override
public void preprocessUsages(Collection<UsageInfo> results) {
    //remove all alias-imported usages from collection
    for (Iterator<UsageInfo> iterator = results.iterator(); iterator.hasNext(); ) {
        UsageInfo info = iterator.next();
        if (info == null) {
            LOG.debug("info==null");
            continue;
        }
        final PsiReference ref = info.getReference();
        if (ref == null)
            continue;
        final PsiElement element = ref.getElement();
        if (!(element instanceof GrReferenceElement))
            continue;
        final GroovyResolveResult resolveResult = ((GrReferenceElement) element).advancedResolve();
        final PsiElement context = resolveResult.getCurrentFileResolveContext();
        if (!(context instanceof GrImportStatement))
            continue;
        if (!((GrImportStatement) context).isAliasedImport())
            continue;
        iterator.remove();
    }
}
Also used : GroovyResolveResult(org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult) GrImportStatement(org.jetbrains.plugins.groovy.lang.psi.api.toplevel.imports.GrImportStatement) UsageInfo(com.intellij.usageView.UsageInfo) GrReferenceElement(org.jetbrains.plugins.groovy.lang.psi.GrReferenceElement)

Example 8 with GrImportStatement

use of org.jetbrains.plugins.groovy.lang.psi.api.toplevel.imports.GrImportStatement in project intellij-community by JetBrains.

the class MoveGroovyClassHandler method doMoveClass.

@Override
public PsiClass doMoveClass(@NotNull PsiClass aClass, @NotNull PsiDirectory moveDestination) throws IncorrectOperationException {
    if (!aClass.getLanguage().equals(GroovyLanguage.INSTANCE))
        return null;
    PsiFile file = aClass.getContainingFile();
    if (!(file instanceof GroovyFile))
        return null;
    final PsiPackage newPackage = JavaDirectoryService.getInstance().getPackage(moveDestination);
    LOG.assertTrue(newPackage != null);
    PsiClass newClass = null;
    final String newPackageName = newPackage.getQualifiedName();
    if (aClass instanceof GroovyScriptClass) {
        final PsiClass[] classes = ((GroovyFile) file).getClasses();
        if (classes.length == 1) {
            if (!moveDestination.equals(file.getContainingDirectory())) {
                Project project = file.getProject();
                MoveFilesOrDirectoriesUtil.doMoveFile(file, moveDestination);
                DumbService.getInstance(project).completeJustSubmittedTasks();
                file = moveDestination.findFile(file.getName());
                assert file != null;
                ((PsiClassOwner) file).setPackageName(newPackageName);
            }
            return ((GroovyFile) file).getScriptClass();
        }
        //script class is moved the first from the file due to MoveClassOrPackageProcessor:88 (element sort)
        correctSelfReferences(aClass, newPackage);
        final GroovyFile newFile = generateNewScript((GroovyFile) file, newPackage);
        for (PsiElement child : file.getChildren()) {
            if (!(child instanceof GrTopStatement || child instanceof PsiComment))
                continue;
            if (child instanceof PsiClass || child instanceof GrImportStatement || child instanceof GrPackageDefinition)
                continue;
            if (child instanceof GrDocComment) {
                final GrDocCommentOwner owner = GrDocCommentUtil.findDocOwner((GrDocComment) child);
                if (owner instanceof PsiClass)
                    continue;
            }
            child.delete();
        }
        if (!moveDestination.equals(file.getContainingDirectory())) {
            moveDestination.add(newFile);
        //aClass.getManager().moveFile(newFile, moveDestination);
        }
        newClass = newFile.getClasses()[0];
        correctOldClassReferences(newClass, aClass);
    } else {
        if (!moveDestination.equals(file.getContainingDirectory()) && moveDestination.findFile(file.getName()) != null) {
            // moving second of two classes which were in the same file to a different directory (IDEADEV-3089)
            correctSelfReferences(aClass, newPackage);
            PsiFile newFile = moveDestination.findFile(file.getName());
            final FileASTNode fileNode = newFile.getNode();
            fileNode.addChild(Factory.createSingleLeafElement(GroovyTokenTypes.mNLS, "\n\n", 0, 2, null, aClass.getManager()));
            final PsiDocComment docComment = aClass.getDocComment();
            if (docComment != null) {
                newFile.add(docComment);
                fileNode.addChild(Factory.createSingleLeafElement(GroovyTokenTypes.mNLS, "\n", 0, 1, null, aClass.getManager()));
            }
            newClass = (GrTypeDefinition) newFile.add(aClass);
            correctOldClassReferences(newClass, aClass);
            aClass.delete();
        } else if (((GroovyFile) file).getClasses().length > 1) {
            correctSelfReferences(aClass, newPackage);
            Project project = aClass.getProject();
            PsiFileFactory fileFactory = PsiFileFactory.getInstance(project);
            GroovyFile newFile = (GroovyFile) moveDestination.add(fileFactory.createFileFromText(aClass.getName() + "." + GroovyFileType.DEFAULT_EXTENSION, GroovyLanguage.INSTANCE, "class XXX {}"));
            final PsiClass created = newFile.getClasses()[0];
            PsiDocComment docComment = aClass.getDocComment();
            if (docComment != null) {
                newFile.addBefore(docComment, created);
                docComment.delete();
            }
            newClass = (PsiClass) created.replace(aClass);
            setPackageDefinition((GroovyFile) file, newFile, newPackageName);
            correctOldClassReferences(newClass, aClass);
            aClass.delete();
        }
    }
    return newClass;
}
Also used : PsiDocComment(com.intellij.psi.javadoc.PsiDocComment) GrDocCommentOwner(org.jetbrains.plugins.groovy.lang.groovydoc.psi.api.GrDocCommentOwner) GrImportStatement(org.jetbrains.plugins.groovy.lang.psi.api.toplevel.imports.GrImportStatement) GrDocComment(org.jetbrains.plugins.groovy.lang.groovydoc.psi.api.GrDocComment) Project(com.intellij.openapi.project.Project) FileASTNode(com.intellij.lang.FileASTNode) GroovyScriptClass(org.jetbrains.plugins.groovy.lang.psi.impl.synthetic.GroovyScriptClass) GrPackageDefinition(org.jetbrains.plugins.groovy.lang.psi.api.toplevel.packaging.GrPackageDefinition) GroovyFile(org.jetbrains.plugins.groovy.lang.psi.GroovyFile) GrTopStatement(org.jetbrains.plugins.groovy.lang.psi.api.toplevel.GrTopStatement)

Example 9 with GrImportStatement

use of org.jetbrains.plugins.groovy.lang.psi.api.toplevel.imports.GrImportStatement in project intellij-community by JetBrains.

the class RenameAliasedUsagesUtil method findAliasedName.

private static String findAliasedName(Map<GroovyFile, String> map, GroovyFile containingFile, PsiElement elementToResolve) {
    final String s = map.get(containingFile);
    if (s != null)
        return s;
    final GrImportStatement[] imports = containingFile.getImportStatements();
    final PsiManager manager = elementToResolve.getManager();
    for (GrImportStatement anImport : imports) {
        if (anImport.isAliasedImport()) {
            final ResolverProcessor processor = getProcessor(elementToResolve, containingFile);
            anImport.processDeclarations(processor, ResolveState.initial(), null, containingFile);
            final GroovyResolveResult[] results = processor.getCandidates();
            for (GroovyResolveResult result : results) {
                if (manager.areElementsEquivalent(elementToResolve, result.getElement())) {
                    final String importedName = anImport.getImportedName();
                    if (importedName != null) {
                        map.put(containingFile, importedName);
                        return importedName;
                    }
                }
            }
        }
    }
    map.put(containingFile, EMPTY_ALIAS);
    return EMPTY_ALIAS;
}
Also used : GroovyResolveResult(org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult) MethodResolverProcessor(org.jetbrains.plugins.groovy.lang.resolve.processors.MethodResolverProcessor) PropertyResolverProcessor(org.jetbrains.plugins.groovy.lang.resolve.processors.PropertyResolverProcessor) ClassResolverProcessor(org.jetbrains.plugins.groovy.lang.resolve.processors.ClassResolverProcessor) ResolverProcessor(org.jetbrains.plugins.groovy.lang.resolve.processors.ResolverProcessor) GrImportStatement(org.jetbrains.plugins.groovy.lang.psi.api.toplevel.imports.GrImportStatement)

Example 10 with GrImportStatement

use of org.jetbrains.plugins.groovy.lang.psi.api.toplevel.imports.GrImportStatement in project intellij-community by JetBrains.

the class MoveGroovyClassHandler method generateNewScript.

private static GroovyFile generateNewScript(GroovyFile file, PsiPackage newPackage) {
    for (GrImportStatement importStatement : file.getImportStatements()) {
        importStatement.delete();
    }
    final GroovyFile newFile = GroovyPsiElementFactory.getInstance(file.getProject()).createGroovyFile("", true, null);
    newFile.addRange(file.getFirstChild(), file.getLastChild());
    final PsiClass[] newFileClasses = newFile.getClasses();
    for (PsiClass psiClass : newFileClasses) {
        if (psiClass instanceof GroovyScriptClass)
            continue;
        final GrDocComment docComment = GrDocCommentUtil.findDocComment((GrDocCommentOwner) psiClass);
        if (docComment != null)
            docComment.delete();
        psiClass.delete();
    }
    final GrPackageDefinition packageDefinition = newFile.getPackageDefinition();
    if (packageDefinition != null)
        packageDefinition.delete();
    PsiElement cur = newFile.getFirstChild();
    while (cur != null && PsiImplUtil.isWhiteSpaceOrNls(cur)) {
        cur = cur.getNextSibling();
    }
    if (cur != null && cur != newFile.getFirstChild()) {
        cur = cur.getPrevSibling();
        newFile.deleteChildRange(newFile.getFirstChild(), cur);
    }
    cur = newFile.getLastChild();
    while (cur != null && PsiImplUtil.isWhiteSpaceOrNls(cur)) {
        cur = cur.getPrevSibling();
    }
    if (cur != null && cur != newFile.getLastChild()) {
        cur = cur.getNextSibling();
        newFile.deleteChildRange(cur, newFile.getLastChild());
    }
    newFile.setName(file.getName());
    setPackageDefinition(file, newFile, newPackage.getQualifiedName());
    GroovyChangeContextUtil.decodeContextInfo(newFile, null, null);
    return newFile;
}
Also used : GroovyScriptClass(org.jetbrains.plugins.groovy.lang.psi.impl.synthetic.GroovyScriptClass) GrPackageDefinition(org.jetbrains.plugins.groovy.lang.psi.api.toplevel.packaging.GrPackageDefinition) GrImportStatement(org.jetbrains.plugins.groovy.lang.psi.api.toplevel.imports.GrImportStatement) GroovyFile(org.jetbrains.plugins.groovy.lang.psi.GroovyFile) GrDocComment(org.jetbrains.plugins.groovy.lang.groovydoc.psi.api.GrDocComment)

Aggregations

GrImportStatement (org.jetbrains.plugins.groovy.lang.psi.api.toplevel.imports.GrImportStatement)49 GroovyFile (org.jetbrains.plugins.groovy.lang.psi.GroovyFile)15 GroovyResolveResult (org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult)11 NotNull (org.jetbrains.annotations.NotNull)9 GrReferenceElement (org.jetbrains.plugins.groovy.lang.psi.GrReferenceElement)9 PsiElement (com.intellij.psi.PsiElement)7 GrReferenceExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression)6 GrPackageDefinition (org.jetbrains.plugins.groovy.lang.psi.api.toplevel.packaging.GrPackageDefinition)6 TextRange (com.intellij.openapi.util.TextRange)4 VirtualFile (com.intellij.openapi.vfs.VirtualFile)4 UsageInfo (com.intellij.usageView.UsageInfo)4 GrTypeDefinition (org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrTypeDefinition)4 GrTopStatement (org.jetbrains.plugins.groovy.lang.psi.api.toplevel.GrTopStatement)4 GrCodeReferenceElement (org.jetbrains.plugins.groovy.lang.psi.api.types.GrCodeReferenceElement)4 ASTNode (com.intellij.lang.ASTNode)3 Document (com.intellij.openapi.editor.Document)3 PsiFile (com.intellij.psi.PsiFile)3 GroovyScriptClass (org.jetbrains.plugins.groovy.lang.psi.impl.synthetic.GroovyScriptClass)3 DaemonCodeAnalyzerImpl (com.intellij.codeInsight.daemon.impl.DaemonCodeAnalyzerImpl)2 IntentionAction (com.intellij.codeInsight.intention.IntentionAction)2