use of com.jetbrains.python.psi.PyImportElement 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.jetbrains.python.psi.PyImportElement 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.jetbrains.python.psi.PyImportElement in project intellij-community by JetBrains.
the class DocStringReferenceProvider method parseTypeReferences.
@NotNull
private static List<PsiReference> parseTypeReferences(@NotNull PsiElement anchor, @NotNull Substring s, int offset) {
final List<PsiReference> result = new ArrayList<>();
final PyTypeParser.ParseResult parseResult = PyTypeParser.parse(anchor, s.toString());
final Map<TextRange, ? extends PyType> types = parseResult.getTypes();
if (types.isEmpty()) {
result.add(new DocStringTypeReference(anchor, s.getTextRange().shiftRight(offset), s.getTextRange().shiftRight(offset), null, null));
}
offset = s.getTextRange().getStartOffset() + offset;
final Map<? extends PyType, TextRange> fullRanges = parseResult.getFullRanges();
for (Map.Entry<TextRange, ? extends PyType> pair : types.entrySet()) {
final PyType t = pair.getValue();
final TextRange range = pair.getKey().shiftRight(offset);
final TextRange fullRange = fullRanges.containsKey(t) ? fullRanges.get(t).shiftRight(offset) : range;
final PyImportElement importElement = parseResult.getImports().get(t);
result.add(new DocStringTypeReference(anchor, range, fullRange, t, importElement));
}
return result;
}
Aggregations