use of com.intellij.psi.scope.PsiScopeProcessor 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.scope.PsiScopeProcessor in project intellij-community by JetBrains.
the class StaticImportInsertHandler method importAlreadyExists.
private static boolean importAlreadyExists(final PsiMember member, final GroovyFile file, final PsiElement place) {
final PsiManager manager = file.getManager();
PsiScopeProcessor processor = new PsiScopeProcessor() {
@Override
public boolean execute(@NotNull PsiElement element, @NotNull ResolveState state) {
return !manager.areElementsEquivalent(element, member);
}
@Override
public <T> T getHint(@NotNull Key<T> hintKey) {
return null;
}
@Override
public void handleEvent(@NotNull Event event, Object associated) {
}
};
boolean skipStaticImports = member instanceof PsiClass;
final GrImportStatement[] imports = file.getImportStatements();
final ResolveState initial = ResolveState.initial();
for (GrImportStatement anImport : imports) {
if (skipStaticImports == anImport.isStatic())
continue;
if (!anImport.processDeclarations(processor, initial, null, place))
return true;
}
return false;
}
use of com.intellij.psi.scope.PsiScopeProcessor in project intellij-community by JetBrains.
the class PyFileImpl method processDeclarations.
@Override
public boolean processDeclarations(@NotNull final PsiScopeProcessor processor, @NotNull ResolveState resolveState, PsiElement lastParent, @NotNull PsiElement place) {
final List<String> dunderAll = getDunderAll();
final List<String> remainingDunderAll = dunderAll == null ? null : new ArrayList<>(dunderAll);
PsiScopeProcessor wrapper = new DelegatingScopeProcessor(processor) {
@Override
public boolean execute(@NotNull PsiElement element, @NotNull ResolveState state) {
if (!super.execute(element, state))
return false;
if (remainingDunderAll != null && element instanceof PyElement) {
remainingDunderAll.remove(((PyElement) element).getName());
}
return true;
}
};
Set<PyFile> pyFiles = resolveState.get(PROCESSED_FILES);
if (pyFiles == null) {
pyFiles = new HashSet<>();
resolveState = resolveState.put(PROCESSED_FILES, pyFiles);
}
if (pyFiles.contains(this))
return true;
pyFiles.add(this);
for (PyClass c : getTopLevelClasses()) {
if (c == lastParent)
continue;
if (!wrapper.execute(c, resolveState))
return false;
}
for (PyFunction f : getTopLevelFunctions()) {
if (f == lastParent)
continue;
if (!wrapper.execute(f, resolveState))
return false;
}
for (PyTargetExpression e : getTopLevelAttributes()) {
if (e == lastParent)
continue;
if (!wrapper.execute(e, resolveState))
return false;
}
for (PyImportElement e : getImportTargets()) {
if (e == lastParent)
continue;
if (!wrapper.execute(e, resolveState))
return false;
}
for (PyFromImportStatement e : getFromImports()) {
if (e == lastParent)
continue;
if (!e.processDeclarations(wrapper, resolveState, null, this))
return false;
}
if (remainingDunderAll != null) {
for (String s : remainingDunderAll) {
if (!PyNames.isIdentifier(s)) {
continue;
}
if (!processor.execute(new LightNamedElement(myManager, PythonLanguage.getInstance(), s), resolveState))
return false;
}
}
return true;
}
use of com.intellij.psi.scope.PsiScopeProcessor in project intellij-community by JetBrains.
the class CanonicalPsiTypeConverterImpl method getReferences.
public PsiReference[] getReferences(@Nullable PsiType type, String typeText, int startOffsetInText, @NotNull final PsiElement element) {
final ElementManipulator<PsiElement> manipulator = ElementManipulators.getManipulator(element);
assert manipulator != null;
String trimmed = typeText.trim();
int offset = manipulator.getRangeInElement(element).getStartOffset() + startOffsetInText + typeText.indexOf(trimmed);
if (trimmed.startsWith(ARRAY_PREFIX)) {
offset += ARRAY_PREFIX.length();
if (trimmed.endsWith(";")) {
trimmed = trimmed.substring(ARRAY_PREFIX.length(), trimmed.length() - 1);
} else {
trimmed = trimmed.substring(ARRAY_PREFIX.length());
}
}
if (type != null) {
type = type.getDeepComponentType();
}
final boolean isPrimitiveType = type instanceof PsiPrimitiveType;
return new JavaClassReferenceSet(trimmed, element, offset, false, CLASS_REFERENCE_PROVIDER) {
@Override
@NotNull
protected JavaClassReference createReference(int refIndex, @NotNull String subRefText, @NotNull TextRange textRange, boolean staticImport) {
return new JavaClassReference(this, textRange, refIndex, subRefText, staticImport) {
@Override
public boolean isSoft() {
return true;
}
@Override
@NotNull
public JavaResolveResult advancedResolve(final boolean incompleteCode) {
if (isPrimitiveType) {
return new CandidateInfo(element, PsiSubstitutor.EMPTY, false, false, element);
}
return super.advancedResolve(incompleteCode);
}
@Override
public void processVariants(@NotNull final PsiScopeProcessor processor) {
if (processor instanceof JavaCompletionProcessor) {
((JavaCompletionProcessor) processor).setCompletionElements(getVariants());
} else {
super.processVariants(processor);
}
}
@Override
@NotNull
public Object[] getVariants() {
final Object[] variants = super.getVariants();
if (myIndex == 0) {
return ArrayUtil.mergeArrays(variants, PRIMITIVES, ArrayUtil.OBJECT_ARRAY_FACTORY);
}
return variants;
}
};
}
}.getAllReferences();
}
use of com.intellij.psi.scope.PsiScopeProcessor in project intellij-community by JetBrains.
the class JavaClassReference method processVariants.
@Override
public void processVariants(@NotNull final PsiScopeProcessor processor) {
if (processor instanceof JavaCompletionProcessor) {
final Map<CustomizableReferenceProvider.CustomizationKey, Object> options = getOptions();
if (options != null && (JavaClassReferenceProvider.EXTEND_CLASS_NAMES.getValue(options) != null || JavaClassReferenceProvider.NOT_INTERFACE.getBooleanValue(options) || JavaClassReferenceProvider.CONCRETE.getBooleanValue(options)) || JavaClassReferenceProvider.CLASS_KIND.getValue(options) != null) {
((JavaCompletionProcessor) processor).setCompletionElements(getVariants());
return;
}
}
PsiScopeProcessor processorToUse = processor;
if (myInStaticImport) {
// allows to complete members
processor.handleEvent(JavaScopeProcessorEvent.CHANGE_LEVEL, null);
} else {
if (isDefinitelyStatic()) {
processor.handleEvent(JavaScopeProcessorEvent.START_STATIC, null);
}
processorToUse = new PsiScopeProcessor() {
@Override
public boolean execute(@NotNull PsiElement element, @NotNull ResolveState state) {
return !(element instanceof PsiClass || element instanceof PsiPackage) || processor.execute(element, state);
}
@Override
public <V> V getHint(@NotNull Key<V> hintKey) {
return processor.getHint(hintKey);
}
@Override
public void handleEvent(@NotNull Event event, Object associated) {
processor.handleEvent(event, associated);
}
};
}
super.processVariants(processorToUse);
}
Aggregations