Search in sources :

Example 26 with GrImportStatement

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

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

Example 28 with GrImportStatement

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

the class GroovyCodeFragmentFactory method stripImports.

private static String stripImports(String text, GroovyFile toEval) {
    GrImportStatement[] imports = toEval.getImportStatements();
    for (int i = imports.length - 1; i >= 0; i--) {
        TextRange range = imports[i].getTextRange();
        text = text.substring(0, range.getStartOffset()) + text.substring(range.getEndOffset(), text.length());
    }
    return StringUtil.escapeStringCharacters(text);
}
Also used : TextRange(com.intellij.openapi.util.TextRange) GrImportStatement(org.jetbrains.plugins.groovy.lang.psi.api.toplevel.imports.GrImportStatement)

Example 29 with GrImportStatement

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

the class GroovyReferenceCopyPasteProcessor method addReferenceData.

@Override
protected void addReferenceData(PsiFile file, int startOffset, PsiElement element, ArrayList<ReferenceData> to) {
    if (element instanceof GrReferenceElement) {
        if (((GrReferenceElement) element).getQualifier() == null) {
            final GroovyResolveResult resolveResult = ((GrReferenceElement) element).advancedResolve();
            final PsiElement refElement = resolveResult.getElement();
            if (refElement != null) {
                if (refElement instanceof PsiClass) {
                    if (refElement.getContainingFile() != element.getContainingFile()) {
                        final String qName = ((PsiClass) refElement).getQualifiedName();
                        if (qName != null) {
                            addReferenceData(element, to, startOffset, qName, null);
                        }
                    }
                } else if (resolveResult.getCurrentFileResolveContext() instanceof GrImportStatement && ((GrImportStatement) resolveResult.getCurrentFileResolveContext()).isStatic()) {
                    final String classQName = ((PsiMember) refElement).getContainingClass().getQualifiedName();
                    final String name = ((PsiNamedElement) refElement).getName();
                    if (classQName != null && name != null) {
                        addReferenceData(element, to, startOffset, classQName, name);
                    }
                }
            }
        }
    }
}
Also used : GroovyResolveResult(org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult) GrImportStatement(org.jetbrains.plugins.groovy.lang.psi.api.toplevel.imports.GrImportStatement) GrReferenceElement(org.jetbrains.plugins.groovy.lang.psi.GrReferenceElement)

Example 30 with GrImportStatement

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

the class GroovyExtractMethodHandler method findConflicts.

private static boolean findConflicts(InitialInfo info) {
    //new ConflictsDialog()
    final MultiMap<PsiElement, String> conflicts = new MultiMap<>();
    final PsiElement declarationOwner = info.getContext().getParent();
    GroovyRecursiveElementVisitor visitor = new GroovyRecursiveElementVisitor() {

        @Override
        public void visitReferenceExpression(@NotNull GrReferenceExpression referenceExpression) {
            super.visitReferenceExpression(referenceExpression);
            GroovyResolveResult resolveResult = referenceExpression.advancedResolve();
            PsiElement resolveContext = resolveResult.getCurrentFileResolveContext();
            if (resolveContext != null && !(resolveContext instanceof GrImportStatement) && !PsiTreeUtil.isAncestor(declarationOwner, resolveContext, true) && !skipResult(resolveResult)) {
                conflicts.putValue(referenceExpression, GroovyRefactoringBundle.message("ref.0.will.not.be.resolved.outside.of.current.context", referenceExpression.getText()));
            }
        }

        //skip 'print' and 'println'
        private boolean skipResult(GroovyResolveResult result) {
            PsiElement element = result.getElement();
            if (element instanceof PsiMethod) {
                String name = ((PsiMethod) element).getName();
                if (!name.startsWith("print"))
                    return false;
                if (element instanceof GrGdkMethod)
                    element = ((GrGdkMethod) element).getStaticMethod();
                PsiClass aClass = ((PsiMethod) element).getContainingClass();
                if (aClass != null) {
                    String qname = aClass.getQualifiedName();
                    return GroovyCommonClassNames.DEFAULT_GROOVY_METHODS.equals(qname);
                }
            }
            return false;
        }
    };
    GrStatement[] statements = info.getStatements();
    for (GrStatement statement : statements) {
        statement.accept(visitor);
    }
    if (conflicts.isEmpty())
        return false;
    if (ApplicationManager.getApplication().isUnitTestMode()) {
        throw new BaseRefactoringProcessor.ConflictsInTestsException(conflicts.values());
    }
    ConflictsDialog dialog = new ConflictsDialog(info.getProject(), conflicts);
    dialog.show();
    return !dialog.isOK();
}
Also used : GroovyRecursiveElementVisitor(org.jetbrains.plugins.groovy.lang.psi.GroovyRecursiveElementVisitor) GrImportStatement(org.jetbrains.plugins.groovy.lang.psi.api.toplevel.imports.GrImportStatement) NotNull(org.jetbrains.annotations.NotNull) GrReferenceExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression) GrStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement) MultiMap(com.intellij.util.containers.MultiMap) GroovyResolveResult(org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult) GrGdkMethod(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrGdkMethod) ConflictsDialog(com.intellij.refactoring.ui.ConflictsDialog)

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