Search in sources :

Example 41 with GrImportStatement

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

the class GroovyMoveClassToInnerHandler method filterUsagesInImportStatements.

private static void filterUsagesInImportStatements(final List<UsageInfo> usages, final List<PsiElement> importStatements) {
    for (Iterator<UsageInfo> iterator = usages.iterator(); iterator.hasNext(); ) {
        UsageInfo usage = iterator.next();
        PsiElement element = usage.getElement();
        if (element == null)
            continue;
        GrImportStatement stmt = PsiTreeUtil.getParentOfType(element, GrImportStatement.class);
        if (stmt != null) {
            importStatements.add(stmt);
            iterator.remove();
        }
    }
}
Also used : GrImportStatement(org.jetbrains.plugins.groovy.lang.psi.api.toplevel.imports.GrImportStatement) UsageInfo(com.intellij.usageView.UsageInfo) NonCodeUsageInfo(com.intellij.refactoring.util.NonCodeUsageInfo)

Example 42 with GrImportStatement

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

the class GroovyCodeFragment method importsToString.

/**
   * @return list of imports in format "qname[:imported_name](,qname[:imported_name])*"
   */
@Override
public String importsToString() {
    if (myPseudoImports.isEmpty())
        return "";
    StringBuilder buffer = new StringBuilder();
    for (Map.Entry<String, GrImportStatement> entry : myPseudoImports.entrySet()) {
        final String importedName = entry.getKey();
        final GrImportStatement anImport = entry.getValue();
        //buffer.append(anImport.isStatic() ? "+" : "-");
        final String qname = anImport.getImportReference().getClassNameText();
        buffer.append(qname);
        buffer.append(':').append(importedName);
        buffer.append(',');
    }
    for (GrImportStatement anImport : myOnDemandImports) {
        //buffer.append(anImport.isStatic() ? "+" : "-");
        String packName = anImport.getImportReference().getClassNameText();
        buffer.append(packName);
        buffer.append(',');
    }
    buffer.deleteCharAt(buffer.length() - 1);
    return buffer.toString();
}
Also used : GrImportStatement(org.jetbrains.plugins.groovy.lang.psi.api.toplevel.imports.GrImportStatement) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Example 43 with GrImportStatement

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

the class GroovyFileImpl method buildDeclarationCache.

@NotNull
private MostlySingularMultiMap<String, ResultWithContext> buildDeclarationCache() {
    MostlySingularMultiMap<String, ResultWithContext> results = new MostlySingularMultiMap<>();
    processDeclarationsNoGuess(new BaseScopeProcessor() {

        @Override
        public boolean execute(@NotNull PsiElement element, @NotNull ResolveState state) {
            if (element instanceof PsiNamedElement) {
                PsiElement context = state.get(ClassHint.RESOLVE_CONTEXT);
                String name = getDeclarationName((PsiNamedElement) element, context);
                if (name != null) {
                    results.add(name, new ResultWithContext((PsiNamedElement) element, context));
                }
            }
            return true;
        }

        private String getDeclarationName(@NotNull PsiNamedElement element, @Nullable PsiElement context) {
            String name = context instanceof GrImportStatement ? ((GrImportStatement) context).getImportedName() : null;
            return name != null ? name : element.getName();
        }
    }, ResolveState.initial(), null, this);
    return results;
}
Also used : ResultWithContext(com.intellij.psi.impl.source.resolve.SymbolCollectingProcessor.ResultWithContext) MostlySingularMultiMap(com.intellij.util.containers.MostlySingularMultiMap) BaseScopeProcessor(com.intellij.psi.scope.BaseScopeProcessor) GrImportStatement(org.jetbrains.plugins.groovy.lang.psi.api.toplevel.imports.GrImportStatement) NotNull(org.jetbrains.annotations.NotNull)

Example 44 with GrImportStatement

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

the class GroovyFileImpl method addImportForClass.

@Override
public GrImportStatement addImportForClass(@NotNull PsiClass aClass) {
    try {
        // Calculating position
        GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(getProject());
        String qname = aClass.getQualifiedName();
        if (qname != null) {
            GrImportStatement importStatement = factory.createImportStatementFromText(qname, false, false, null);
            return addImport(importStatement);
        }
    } catch (IncorrectOperationException e) {
        LOG.error(e);
    }
    return null;
}
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)

Example 45 with GrImportStatement

use of org.jetbrains.plugins.groovy.lang.psi.api.toplevel.imports.GrImportStatement 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)

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