use of com.intellij.psi.scope.BaseScopeProcessor in project intellij-community by JetBrains.
the class PsiCodeBlockImpl method buildMaps.
// return Pair(classes, locals) or null if there was conflict
@Nullable
private Couple<Set<String>> buildMaps() {
Set<String> set1 = myClassesSet;
Set<String> set2 = myVariablesSet;
boolean wasConflict = myConflict;
if (set1 == null || set2 == null) {
final Set<String> localsSet = new THashSet<>();
final Set<String> classesSet = new THashSet<>();
final Ref<Boolean> conflict = new Ref<>(Boolean.FALSE);
PsiScopesUtil.walkChildrenScopes(this, new BaseScopeProcessor() {
@Override
public boolean execute(@NotNull PsiElement element, @NotNull ResolveState state) {
if (element instanceof PsiLocalVariable) {
final PsiLocalVariable variable = (PsiLocalVariable) element;
final String name = variable.getName();
if (!localsSet.add(name)) {
conflict.set(Boolean.TRUE);
localsSet.clear();
classesSet.clear();
}
} else if (element instanceof PsiClass) {
final PsiClass psiClass = (PsiClass) element;
final String name = psiClass.getName();
if (!classesSet.add(name)) {
conflict.set(Boolean.TRUE);
localsSet.clear();
classesSet.clear();
}
}
return !conflict.get();
}
}, ResolveState.initial(), this, this);
myClassesSet = set1 = classesSet.isEmpty() ? Collections.<String>emptySet() : classesSet;
myVariablesSet = set2 = localsSet.isEmpty() ? Collections.<String>emptySet() : localsSet;
myConflict = wasConflict = conflict.get();
}
return wasConflict ? null : Couple.of(set1, set2);
}
use of com.intellij.psi.scope.BaseScopeProcessor in project intellij-community by JetBrains.
the class BasicExpressionCompletionContributor method processDataflowExpressionTypes.
public static void processDataflowExpressionTypes(PsiElement position, @Nullable PsiType expectedType, final PrefixMatcher matcher, Consumer<LookupElement> consumer) {
final PsiExpression context = PsiTreeUtil.getParentOfType(position, PsiExpression.class);
if (context == null)
return;
final Map<PsiExpression, PsiType> map = GuessManager.getInstance(position.getProject()).getControlFlowExpressionTypes(context);
if (map.isEmpty()) {
return;
}
PsiScopesUtil.treeWalkUp(new BaseScopeProcessor() {
@Override
public boolean execute(@NotNull PsiElement element, @NotNull ResolveState state) {
if (element instanceof PsiLocalVariable) {
if (!matcher.prefixMatches(((PsiLocalVariable) element).getName())) {
return true;
}
final PsiExpression expression = ((PsiLocalVariable) element).getInitializer();
if (expression instanceof PsiTypeCastExpression) {
PsiTypeCastExpression typeCastExpression = (PsiTypeCastExpression) expression;
final PsiExpression operand = typeCastExpression.getOperand();
if (operand != null) {
final PsiType dfaCasted = map.get(operand);
if (dfaCasted != null && dfaCasted.equals(typeCastExpression.getType())) {
map.remove(operand);
}
}
}
}
return true;
}
}, context, context.getContainingFile());
for (final PsiExpression expression : map.keySet()) {
final PsiType castType = map.get(expression);
final PsiType baseType = expression.getType();
if (expectedType == null || (expectedType.isAssignableFrom(castType) && (baseType == null || !expectedType.isAssignableFrom(baseType)))) {
consumer.consume(CastingLookupElementDecorator.createCastingElement(expressionToLookupElement(expression), castType));
}
}
}
use of com.intellij.psi.scope.BaseScopeProcessor in project intellij-plugins by JetBrains.
the class FlashUmlDependencyProvider method computeUsedClasses.
public Collection<Pair<JSClass, FlashUmlRelationship>> computeUsedClasses() {
final Collection<Pair<JSClass, FlashUmlRelationship>> result = new ArrayList<>();
final JSElementVisitor visitor = new JSElementVisitor() {
JSVariable myVariable;
JSNewExpression myNewExpression;
boolean myInField;
boolean myInParameter;
@Override
public void visitJSReferenceExpression(final JSReferenceExpression node) {
if (PsiTreeUtil.getParentOfType(node, JSImportStatement.class) != null) {
return;
}
if (myVariable == null && myNewExpression == null && !myInParameter && isReturnTypeReference(node)) {
return;
}
PsiElement resolved = node.resolve();
if (myNewExpression != null && resolved instanceof JSFunction) {
if (((JSFunction) resolved).isConstructor()) {
resolved = JSResolveUtil.findParent(resolved);
}
}
if (resolved instanceof JSClass) {
FlashUmlRelationship relType;
if (node.getParent() instanceof JSReferenceExpression) {
relType = Factory.dependency(myInField ? myVariable.getName() : null, myVariable != null ? myVariable : node);
} else if (myNewExpression != null) {
if (node.getParent() instanceof JSGenericSignature) {
relType = Factory.dependency(myInField ? myVariable.getName() : null, myVariable != null ? myVariable : node);
} else {
relType = Factory.create(myNewExpression);
}
} else if (myInField && node.getParent() instanceof JSGenericSignature) {
assert myVariable != null;
String qName = ((JSClass) resolved).getQualifiedName();
if (FlashUmlVfsResolver.isVectorType(qName)) {
relType = Factory.dependency(myVariable.getName(), myVariable);
} else {
relType = Factory.oneToMany(myVariable.getName(), myVariable);
}
} else if (myInField) {
assert myVariable != null;
String qName = ((JSClass) resolved).getQualifiedName();
if (FlashUmlVfsResolver.isVectorType(qName)) {
relType = Factory.dependency(myVariable.getName(), myVariable);
} else {
relType = Factory.oneToOne(myVariable.getName(), myVariable);
}
} else {
relType = Factory.dependency(null, myVariable != null ? myVariable : node);
}
result.add(Pair.create((JSClass) resolved, relType));
}
super.visitJSReferenceExpression(node);
}
@Override
public void visitJSVariable(final JSVariable node) {
if (node instanceof JSParameter) {
myInParameter = true;
} else {
myVariable = node;
}
myInField = JSResolveUtil.findParent(node) instanceof JSClass;
try {
super.visitJSVariable(node);
} finally {
myVariable = null;
myInField = false;
myInParameter = false;
}
}
@Override
public void visitJSNewExpression(final JSNewExpression node) {
myNewExpression = node;
try {
super.visitJSNewExpression(node);
} finally {
myNewExpression = null;
}
}
@Override
public void visitElement(final PsiElement element) {
super.visitElement(element);
element.acceptChildren(this);
}
};
if (myClazz instanceof XmlBackedJSClassImpl) {
// TODO process attributes
((XmlBackedJSClassImpl) myClazz).processInjectedFiles(jsFile -> {
jsFile.accept(visitor);
return true;
});
myClazz.getParent().acceptChildren(new // don't visit parent tag
XmlElementVisitor() {
// also to prevent extra references resolve
private String myInClassAttributeName;
@Override
public void visitXmlTag(final XmlTag tag) {
XmlElementDescriptor descriptor = tag.getDescriptor();
if (descriptor != null) {
PsiElement declaration = descriptor.getDeclaration();
if (declaration instanceof XmlFile && JavaScriptSupportLoader.isFlexMxmFile((PsiFile) declaration)) {
declaration = XmlBackedJSClassFactory.getXmlBackedClass((XmlFile) declaration);
}
if (declaration instanceof JSClass) {
XmlAttribute id = tag.getAttribute("id");
FlashUmlRelationship type = id != null && StringUtil.isNotEmpty(id.getValue()) ? Factory.oneToOne(id.getValue(), id) : Factory.dependency(null, tag);
result.add(Pair.create((JSClass) declaration, type));
}
}
super.visitXmlTag(tag);
}
@Override
public void visitXmlAttribute(final XmlAttribute attribute) {
XmlAttributeDescriptor descriptor = attribute.getDescriptor();
if (descriptor instanceof AnnotationBackedDescriptor) {
if (FlexReferenceContributor.isClassReferenceType(((AnnotationBackedDescriptor) descriptor).getType())) {
myInClassAttributeName = StringUtil.notNullize(attribute.getName());
try {
super.visitXmlAttribute(attribute);
} finally {
myInClassAttributeName = null;
}
}
}
}
@Override
public void visitXmlAttributeValue(final XmlAttributeValue value) {
if (myInClassAttributeName != null) {
processReferenceSet(value.getReferences(), result, Factory.dependency(myInClassAttributeName, value.getParent()));
}
}
@Override
public void visitXmlText(final XmlText text) {
List<Pair<PsiElement, TextRange>> injectedFiles = InjectedLanguageManager.getInstance(text.getProject()).getInjectedPsiFiles(text);
if (injectedFiles != null) {
for (Pair<PsiElement, TextRange> pair : injectedFiles) {
if (CSS.is(pair.first.getLanguage())) {
pair.first.accept(new CssElementVisitor() {
// to prevent extra references resolve
private boolean myInClassReference;
@Override
public void visitElement(final PsiElement element) {
super.visitElement(element);
element.acceptChildren(this);
}
@Override
public void visitCssFunction(final CssFunction _function) {
if (FlexReferenceContributor.CLASS_REFERENCE.equals(_function.getName())) {
myInClassReference = true;
try {
super.visitCssFunction(_function);
} finally {
myInClassReference = false;
}
}
}
@Override
public void visitCssString(final CssString _string) {
if (myInClassReference) {
CssDeclaration declaration = PsiTreeUtil.getParentOfType(_string, CssDeclaration.class);
if (declaration != null) {
processReferenceSet(_string.getReferences(), result, Factory.dependency(declaration.getPropertyName(), declaration));
}
}
}
});
}
}
}
super.visitXmlText(text);
}
@Override
public void visitElement(final PsiElement element) {
super.visitElement(element);
element.acceptChildren(this);
}
});
}
myClazz.processDeclarations(new BaseScopeProcessor() {
@Override
public boolean execute(@NotNull final PsiElement element, @NotNull final ResolveState state) {
element.accept(visitor);
return true;
}
}, ResolveState.initial(), myClazz, myClazz);
return result;
}
use of com.intellij.psi.scope.BaseScopeProcessor in project intellij-community by JetBrains.
the class GroovyFileImpl method buildDeclarationCache.
@NotNull
private MostlySingularMultiMap<String, ResultWithContext> buildDeclarationCache() {
MostlySingularMultiMap<String, ResultWithContext> results = new MostlySingularMultiMap<>();
processDeclarationsNoGuess(new BaseScopeProcessor() {
@Override
public boolean execute(@NotNull PsiElement element, @NotNull ResolveState state) {
if (element instanceof PsiNamedElement) {
PsiElement context = state.get(ClassHint.RESOLVE_CONTEXT);
String name = getDeclarationName((PsiNamedElement) element, context);
if (name != null) {
results.add(name, new ResultWithContext((PsiNamedElement) element, context));
}
}
return true;
}
private String getDeclarationName(@NotNull PsiNamedElement element, @Nullable PsiElement context) {
String name = context instanceof GrImportStatement ? ((GrImportStatement) context).getImportedName() : null;
return name != null ? name : element.getName();
}
}, ResolveState.initial(), null, this);
return results;
}
Aggregations