Search in sources :

Example 41 with GroovyFile

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

the class GrReferenceExpressionImpl method bindToElementViaStaticImport.

@Override
public GrReferenceExpression bindToElementViaStaticImport(@NotNull PsiMember member) {
    if (getQualifier() != null) {
        throw new IncorrectOperationException("Reference has qualifier");
    }
    if (StringUtil.isEmpty(getReferenceName())) {
        throw new IncorrectOperationException("Reference has empty name");
    }
    PsiClass containingClass = member.getContainingClass();
    if (containingClass == null) {
        throw new IncorrectOperationException("Member has no containing class");
    }
    final PsiFile file = getContainingFile();
    if (file instanceof GroovyFile) {
        GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(getProject());
        String text = "import static " + containingClass.getQualifiedName() + "." + member.getName();
        final GrImportStatement statement = factory.createImportStatementFromText(text);
        ((GroovyFile) file).addImport(statement);
    }
    return this;
}
Also used : GroovyPsiElementFactory(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory) IncorrectOperationException(com.intellij.util.IncorrectOperationException) GrImportStatement(org.jetbrains.plugins.groovy.lang.psi.api.toplevel.imports.GrImportStatement) GroovyFile(org.jetbrains.plugins.groovy.lang.psi.GroovyFile)

Example 42 with GroovyFile

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

the class GrTypeDefinitionImpl method isRenameFileOnClassRenaming.

private boolean isRenameFileOnClassRenaming() {
    final PsiFile file = getContainingFile();
    if (!(file instanceof GroovyFile))
        return false;
    final GroovyFile groovyFile = (GroovyFile) file;
    if (groovyFile.isScript())
        return false;
    final String name = getName();
    final VirtualFile vFile = groovyFile.getVirtualFile();
    return vFile != null && name != null && name.equals(vFile.getNameWithoutExtension());
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) GroovyFile(org.jetbrains.plugins.groovy.lang.psi.GroovyFile)

Example 43 with GroovyFile

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

the class GrTypeDefinitionImpl method getQualifiedName.

@Nullable
@Override
public String getQualifiedName() {
    final GrTypeDefinitionStub stub = getStub();
    if (stub != null) {
        return stub.getQualifiedName();
    }
    PsiElement parent = getParent();
    if (parent instanceof GroovyFile) {
        String packageName = ((GroovyFile) parent).getPackageName();
        return !packageName.isEmpty() ? packageName + "." + getName() : getName();
    }
    final PsiClass containingClass = getContainingClass();
    if (containingClass != null && containingClass.getQualifiedName() != null) {
        return containingClass.getQualifiedName() + "." + getName();
    }
    return null;
}
Also used : GrTypeDefinitionStub(org.jetbrains.plugins.groovy.lang.psi.stubs.GrTypeDefinitionStub) LeafPsiElement(com.intellij.psi.impl.source.tree.LeafPsiElement) GroovyFile(org.jetbrains.plugins.groovy.lang.psi.GroovyFile) Nullable(org.jetbrains.annotations.Nullable)

Example 44 with GroovyFile

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

the class GrClosableBlockImpl method getOwner.

private PsiVariable getOwner() {
    return CachedValuesManager.getCachedValue(this, () -> {
        final GroovyPsiElement context = PsiTreeUtil.getParentOfType(this, GrTypeDefinition.class, GrClosableBlock.class, GroovyFile.class);
        final PsiElementFactory factory = JavaPsiFacade.getInstance(getProject()).getElementFactory();
        PsiType type = null;
        if (context instanceof GrTypeDefinition) {
            type = factory.createType((PsiClass) context);
        } else if (context instanceof GrClosableBlock) {
            type = GrClosureType.create((GrClosableBlock) context, true);
        } else if (context instanceof GroovyFile) {
            final PsiClass scriptClass = ((GroovyFile) context).getScriptClass();
            if (scriptClass != null && GroovyNamesUtil.isIdentifier(scriptClass.getName()))
                type = factory.createType(scriptClass);
        }
        if (type == null) {
            type = TypesUtil.getJavaLangObject(this);
        }
        PsiVariable owner = new GrLightVariable(getManager(), OWNER_NAME, type, this);
        return CachedValueProvider.Result.create(owner, PsiModificationTracker.MODIFICATION_COUNT);
    });
}
Also used : GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement) GrTypeDefinition(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrTypeDefinition) GrClosableBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock) GrLightVariable(org.jetbrains.plugins.groovy.lang.psi.impl.synthetic.GrLightVariable) GroovyFile(org.jetbrains.plugins.groovy.lang.psi.GroovyFile)

Example 45 with GroovyFile

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

the class GrStubUtils method getAliasMapping.

@NotNull
private static Map<String, String> getAliasMapping(@Nullable PsiFile file) {
    if (!(file instanceof GroovyFile))
        return Collections.emptyMap();
    return CachedValuesManager.getCachedValue(file, () -> {
        Map<String, String> mapping = ContainerUtil.newHashMap();
        for (GrImportStatement importStatement : ((GroovyFile) file).getImportStatements()) {
            if (importStatement.getImportReference() != null && !importStatement.isStatic() && importStatement.isAliasedImport()) {
                String importName = importStatement.getImportReference().getClassNameText();
                String importedName = importStatement.getImportedName();
                if (importedName != null) {
                    mapping.put(importedName, importName);
                }
            }
        }
        return CachedValueProvider.Result.create(mapping, file);
    });
}
Also used : GrImportStatement(org.jetbrains.plugins.groovy.lang.psi.api.toplevel.imports.GrImportStatement) GroovyFile(org.jetbrains.plugins.groovy.lang.psi.GroovyFile) NotNull(org.jetbrains.annotations.NotNull)

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