Search in sources :

Example 11 with GroovyFile

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

the class RenameAliasedUsagesUtil method filterAliasedRefs.

public static Collection<PsiReference> filterAliasedRefs(Collection<PsiReference> refs, PsiElement element) {
    Map<GroovyFile, String> aliases = new HashMap<>();
    ArrayList<PsiReference> result = new ArrayList<>();
    for (PsiReference ref : refs) {
        final PsiElement e = ref.getElement();
        if (e == null)
            continue;
        if (skipReference(element, aliases, e))
            continue;
        result.add(ref);
    }
    return result;
}
Also used : HashMap(com.intellij.util.containers.hash.HashMap) ArrayList(java.util.ArrayList) GroovyFile(org.jetbrains.plugins.groovy.lang.psi.GroovyFile) GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement)

Example 12 with GroovyFile

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

the class RenameGroovyScriptProcessor method findExistingNameConflicts.

@Override
public void findExistingNameConflicts(PsiElement element, String newName, MultiMap<PsiElement, String> conflicts) {
    final String scriptName = FileUtil.getNameWithoutExtension(newName);
    if (!StringUtil.isJavaIdentifier(scriptName)) {
        final PsiClass script = ((GroovyFile) element).getScriptClass();
        conflicts.putValue(script, GroovyRefactoringBundle.message("cannot.rename.script.class.to.0", script.getName(), scriptName));
    }
}
Also used : PsiClass(com.intellij.psi.PsiClass) GroovyFile(org.jetbrains.plugins.groovy.lang.psi.GroovyFile)

Example 13 with GroovyFile

use of org.jetbrains.plugins.groovy.lang.psi.GroovyFile 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)

Example 14 with GroovyFile

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

the class MoveGroovyFileHandler method prepareMovedFile.

@Override
public void prepareMovedFile(PsiFile file, PsiDirectory moveDestination, Map<PsiElement, PsiElement> oldToNewMap) {
    final GroovyFile groovyFile = (GroovyFile) file;
    GroovyChangeContextUtil.encodeContextInfo(groovyFile);
    for (PsiClass psiClass : groovyFile.getClasses()) {
        oldToNewMap.put(psiClass, MoveClassesOrPackagesUtil.doMoveClass(psiClass, moveDestination));
    }
}
Also used : GroovyFile(org.jetbrains.plugins.groovy.lang.psi.GroovyFile)

Example 15 with GroovyFile

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

the class CompleteReferenceExpression method getBindings.

private void getBindings() {
    final PsiClass containingClass = PsiTreeUtil.getParentOfType(myRefExpr, PsiClass.class);
    if (containingClass != null)
        return;
    final PsiFile file = FileContextUtil.getContextFile(myRefExpr);
    if (file instanceof GroovyFile) {
        ((GroovyFile) file).accept(new GroovyRecursiveElementVisitor() {

            @Override
            public void visitAssignmentExpression(@NotNull GrAssignmentExpression expression) {
                super.visitAssignmentExpression(expression);
                final GrExpression value = expression.getLValue();
                if (value instanceof GrReferenceExpression && !((GrReferenceExpression) value).isQualified()) {
                    final PsiElement resolved = ((GrReferenceExpression) value).resolve();
                    if (resolved instanceof GrBindingVariable) {
                        myProcessor.execute(resolved, ResolveState.initial());
                    } else if (resolved == null) {
                        myProcessor.execute(new GrBindingVariable((GroovyFile) file, ((GrReferenceExpression) value).getReferenceName(), true), ResolveState.initial());
                    }
                }
            }

            @Override
            public void visitTypeDefinition(@NotNull GrTypeDefinition typeDefinition) {
            //don't go into classes
            }
        });
    }
}
Also used : GrTypeDefinition(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrTypeDefinition) GrAssignmentExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrAssignmentExpression) GrBindingVariable(org.jetbrains.plugins.groovy.lang.psi.impl.synthetic.GrBindingVariable) GroovyRecursiveElementVisitor(org.jetbrains.plugins.groovy.lang.psi.GroovyRecursiveElementVisitor) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) GroovyFile(org.jetbrains.plugins.groovy.lang.psi.GroovyFile) GrReferenceExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression)

Aggregations

GroovyFile (org.jetbrains.plugins.groovy.lang.psi.GroovyFile)91 PsiFile (com.intellij.psi.PsiFile)26 PsiElement (com.intellij.psi.PsiElement)21 NotNull (org.jetbrains.annotations.NotNull)17 GrImportStatement (org.jetbrains.plugins.groovy.lang.psi.api.toplevel.imports.GrImportStatement)17 VirtualFile (com.intellij.openapi.vfs.VirtualFile)13 Project (com.intellij.openapi.project.Project)10 GrTypeDefinition (org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrTypeDefinition)10 PsiClass (com.intellij.psi.PsiClass)9 GrClosableBlock (org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock)9 Nullable (org.jetbrains.annotations.Nullable)8 GroovyPsiElement (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement)8 GroovyScriptClass (org.jetbrains.plugins.groovy.lang.psi.impl.synthetic.GroovyScriptClass)8 GroovyPsiElementFactory (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory)7 Module (com.intellij.openapi.module.Module)6 IncorrectOperationException (com.intellij.util.IncorrectOperationException)6 GrReferenceElement (org.jetbrains.plugins.groovy.lang.psi.GrReferenceElement)6 GrReferenceExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression)6 GrParameter (org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter)6 ASTNode (com.intellij.lang.ASTNode)5