Search in sources :

Example 1 with PsiNamedElement

use of com.intellij.psi.PsiNamedElement in project intellij-community by JetBrains.

the class PyResolveProcessor method execute.

@Override
public boolean execute(@NotNull PsiElement element, @NotNull ResolveState state) {
    final PsiNamedElement namedElement = PyUtil.as(element, PsiNamedElement.class);
    if (namedElement != null && myName.equals(namedElement.getName())) {
        return tryAddResult(element, null);
    }
    final PyImportedNameDefiner importedNameDefiner = PyUtil.as(element, PyImportedNameDefiner.class);
    if (importedNameDefiner != null) {
        final List<RatedResolveResult> results = resolveInImportedNameDefiner(importedNameDefiner);
        if (!results.isEmpty()) {
            boolean cont = true;
            for (RatedResolveResult result : results) {
                final PsiElement resolved = result.getElement();
                if (resolved != null) {
                    cont = tryAddResult(resolved, importedNameDefiner) && cont;
                }
            }
            return cont;
        }
        final PyImportElement importElement = PyUtil.as(element, PyImportElement.class);
        if (importElement != null) {
            final String importName = importElement.getVisibleName();
            if (myName.equals(importName)) {
                return tryAddResult(null, importElement);
            }
        }
    }
    return myOwner == null || myOwner == ScopeUtil.getScopeOwner(element);
}
Also used : PsiNamedElement(com.intellij.psi.PsiNamedElement) PyImportElement(com.jetbrains.python.psi.PyImportElement) PyImportedNameDefiner(com.jetbrains.python.psi.PyImportedNameDefiner) PsiElement(com.intellij.psi.PsiElement)

Example 2 with PsiNamedElement

use of com.intellij.psi.PsiNamedElement in project intellij-community by JetBrains.

the class VariantsProcessor method execute.

@Override
public boolean execute(@NotNull PsiElement element, @NotNull ResolveState substitutor) {
    // skip whatever the filter rejects
    if (myNodeFilter != null && !myNodeFilter.value(element))
        return true;
    // TODO: refactor to look saner; much code duplication
    if (element instanceof PsiNamedElement) {
        final PsiNamedElement psiNamedElement = (PsiNamedElement) element;
        final String name = PyUtil.getElementNameWithoutExtension(psiNamedElement);
        if (name != null && nameIsAcceptable(name)) {
            addElement(name, psiNamedElement);
        }
    } else if (element instanceof PyReferenceExpression) {
        PyReferenceExpression expr = (PyReferenceExpression) element;
        String referencedName = expr.getReferencedName();
        if (nameIsAcceptable(referencedName)) {
            addElement(referencedName, expr);
        }
    } else if (element instanceof PyImportedNameDefiner) {
        boolean handledAsImported = false;
        if (element instanceof PyImportElement) {
            final PyImportElement importElement = (PyImportElement) element;
            handledAsImported = handleImportElement(importElement);
        }
        if (!handledAsImported) {
            final PyImportedNameDefiner definer = (PyImportedNameDefiner) element;
            for (PyElement expr : definer.iterateNames()) {
                if (expr != null && expr != myContext) {
                    // NOTE: maybe rather have SingleIterables skip nulls outright?
                    if (!expr.isValid()) {
                        throw new PsiInvalidElementAccessException(expr, "Definer: " + definer);
                    }
                    String referencedName = expr instanceof PyFile ? FileUtil.getNameWithoutExtension(((PyFile) expr).getName()) : expr.getName();
                    if (referencedName != null && nameIsAcceptable(referencedName)) {
                        addImportedElement(referencedName, expr);
                    }
                }
            }
        }
    }
    return true;
}
Also used : PsiInvalidElementAccessException(com.intellij.psi.PsiInvalidElementAccessException) PsiNamedElement(com.intellij.psi.PsiNamedElement)

Example 3 with PsiNamedElement

use of com.intellij.psi.PsiNamedElement in project intellij-community by JetBrains.

the class XmlAttributeDeclImpl method navigate.

@Override
public void navigate(final boolean requestFocus) {
    if (isPhysical()) {
        super.navigate(requestFocus);
        return;
    }
    final PsiNamedElement psiNamedElement = XmlUtil.findRealNamedElement(this);
    Navigatable navigatable = PsiNavigationSupport.getInstance().getDescriptor(psiNamedElement);
    if (psiNamedElement instanceof XmlEntityDecl) {
        navigatable = PsiNavigationSupport.getInstance().createNavigatable(psiNamedElement.getProject(), psiNamedElement.getContainingFile().getVirtualFile(), psiNamedElement.getTextRange().getStartOffset() + psiNamedElement.getText().indexOf(getName()));
    }
    if (navigatable != null) {
        navigatable.navigate(requestFocus);
    }
}
Also used : PsiNamedElement(com.intellij.psi.PsiNamedElement) Navigatable(com.intellij.pom.Navigatable)

Example 4 with PsiNamedElement

use of com.intellij.psi.PsiNamedElement in project intellij-community by JetBrains.

the class PyDunderAllReference method getVariants.

@NotNull
@Override
public Object[] getVariants() {
    final List<LookupElement> result = new ArrayList<>();
    PyFile containingFile = (PyFile) getElement().getContainingFile().getOriginalFile();
    final List<String> dunderAll = containingFile.getDunderAll();
    final Set<String> seenNames = new HashSet<>();
    if (dunderAll != null) {
        seenNames.addAll(dunderAll);
    }
    containingFile.processDeclarations(new PsiScopeProcessor() {

        @Override
        public boolean execute(@NotNull PsiElement element, @NotNull ResolveState state) {
            if (element instanceof PsiNamedElement && !(element instanceof LightNamedElement)) {
                final String name = ((PsiNamedElement) element).getName();
                if (name != null && PyUtil.getInitialUnderscores(name) == 0 && !seenNames.contains(name)) {
                    seenNames.add(name);
                    result.add(LookupElementBuilder.createWithSmartPointer(name, element).withIcon(element.getIcon(0)));
                }
            } else if (element instanceof PyImportElement) {
                final String visibleName = ((PyImportElement) element).getVisibleName();
                if (visibleName != null && !seenNames.contains(visibleName)) {
                    seenNames.add(visibleName);
                    result.add(LookupElementBuilder.createWithSmartPointer(visibleName, element));
                }
            }
            return true;
        }

        @Override
        public <T> T getHint(@NotNull Key<T> hintKey) {
            return null;
        }

        @Override
        public void handleEvent(@NotNull Event event, @Nullable Object associated) {
        }
    }, ResolveState.initial(), null, containingFile);
    return ArrayUtil.toObjectArray(result);
}
Also used : PsiNamedElement(com.intellij.psi.PsiNamedElement) PyImportElement(com.jetbrains.python.psi.PyImportElement) ArrayList(java.util.ArrayList) PsiScopeProcessor(com.intellij.psi.scope.PsiScopeProcessor) PyFile(com.jetbrains.python.psi.PyFile) LookupElement(com.intellij.codeInsight.lookup.LookupElement) ResolveState(com.intellij.psi.ResolveState) LightNamedElement(com.jetbrains.python.psi.impl.LightNamedElement) PsiElement(com.intellij.psi.PsiElement) HashSet(java.util.HashSet) NotNull(org.jetbrains.annotations.NotNull)

Example 5 with PsiNamedElement

use of com.intellij.psi.PsiNamedElement in project intellij-community by JetBrains.

the class PyMoveModuleMembersProcessor method performRefactoring.

@Override
protected void performRefactoring(@NotNull final UsageInfo[] usages) {
    final MultiMap<PsiElement, UsageInfo> usagesByElement = MultiMap.create();
    for (UsageInfo usage : usages) {
        usagesByElement.putValue(((MyUsageInfo) usage).myMovedElement, usage);
    }
    CommandProcessor.getInstance().executeCommand(myElements[0].getProject(), new Runnable() {

        public void run() {
            ApplicationManager.getApplication().runWriteAction(new Runnable() {

                public void run() {
                    final PyFile destination = PyUtil.getOrCreateFile(myDestination, myProject);
                    CommonRefactoringUtil.checkReadOnlyStatus(myProject, destination);
                    for (final PsiNamedElement e : myElements) {
                        // TODO: Check for resulting circular imports
                        CommonRefactoringUtil.checkReadOnlyStatus(myProject, e);
                        assert e instanceof PyClass || e instanceof PyFunction || e instanceof PyTargetExpression;
                        final String name = e.getName();
                        if (name == null) {
                            continue;
                        }
                        if (e instanceof PyClass && destination.findTopLevelClass(name) != null) {
                            throw new IncorrectOperationException(PyBundle.message("refactoring.move.error.destination.file.contains.class.$0", name));
                        }
                        if (e instanceof PyFunction && destination.findTopLevelFunction(name) != null) {
                            throw new IncorrectOperationException(PyBundle.message("refactoring.move.error.destination.file.contains.function.$0", name));
                        }
                        if (e instanceof PyTargetExpression && destination.findTopLevelAttribute(name) != null) {
                            throw new IncorrectOperationException(PyBundle.message("refactoring.move.error.destination.file.contains.global.variable.$0", name));
                        }
                        final Collection<UsageInfo> usageInfos = usagesByElement.get(e);
                        final boolean usedFromOutside = ContainerUtil.exists(usageInfos, new Condition<UsageInfo>() {

                            @Override
                            public boolean value(UsageInfo usageInfo) {
                                final PsiElement element = usageInfo.getElement();
                                return element != null && !PsiTreeUtil.isAncestor(e, element, false);
                            }
                        });
                        if (usedFromOutside) {
                            PyMoveRefactoringUtil.checkValidImportableFile(e, destination.getVirtualFile());
                        }
                        new PyMoveSymbolProcessor(e, destination, usageInfos, myElements).moveElement();
                    }
                }
            });
        }
    }, REFACTORING_NAME, null);
}
Also used : PsiNamedElement(com.intellij.psi.PsiNamedElement) IncorrectOperationException(com.intellij.util.IncorrectOperationException) PsiElement(com.intellij.psi.PsiElement) UsageInfo(com.intellij.usageView.UsageInfo)

Aggregations

PsiNamedElement (com.intellij.psi.PsiNamedElement)65 PsiElement (com.intellij.psi.PsiElement)29 NotNull (org.jetbrains.annotations.NotNull)16 ArrayList (java.util.ArrayList)14 PsiFile (com.intellij.psi.PsiFile)8 LookupElement (com.intellij.codeInsight.lookup.LookupElement)7 Pair (com.intellij.openapi.util.Pair)6 VirtualFile (com.intellij.openapi.vfs.VirtualFile)5 Project (com.intellij.openapi.project.Project)4 UsageInfo (com.intellij.usageView.UsageInfo)4 Nullable (org.jetbrains.annotations.Nullable)4 PsiFileSystemItem (com.intellij.psi.PsiFileSystemItem)3 PsiReference (com.intellij.psi.PsiReference)3 GlobalSearchScope (com.intellij.psi.search.GlobalSearchScope)3 PyClass (com.jetbrains.python.psi.PyClass)3 PyFunction (com.jetbrains.python.psi.PyFunction)3 BuildFile (com.google.idea.blaze.base.lang.buildfile.psi.BuildFile)2 LoadedSymbol (com.google.idea.blaze.base.lang.buildfile.psi.LoadedSymbol)2 JSClass (com.intellij.lang.javascript.psi.ecmal4.JSClass)2 JSQualifiedNamedElement (com.intellij.lang.javascript.psi.ecmal4.JSQualifiedNamedElement)2