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);
}
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;
}
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);
}
}
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);
}
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);
}
Aggregations