use of com.intellij.psi.ResolveState 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.ResolveState in project intellij-elixir by KronicDeth.
the class CallDefinitionClause method execute.
/*
* Private Instance Methods
*/
private boolean execute(@NotNull Call element, @NotNull final ResolveState state) {
boolean keepProcessing = true;
if (org.elixir_lang.structure_view.element.CallDefinitionClause.is(element)) {
keepProcessing = executeOnCallDefinitionClause(element, state);
} else if (Import.is(element)) {
final ResolveState importState = state.put(IMPORT_CALL, element);
try {
Import.callDefinitionClauseCallWhile(element, new Function<Call, Boolean>() {
@Override
public Boolean fun(Call callDefinitionClause) {
return executeOnCallDefinitionClause(callDefinitionClause, importState);
}
});
} catch (StackOverflowError stackOverflowError) {
Logger.error(CallDefinitionClause.class, "StackOverflowError while processing import", element);
}
} else if (Module.is(element)) {
Call[] childCalls = macroChildCalls(element);
if (childCalls != null) {
for (Call childCall : childCalls) {
if (!execute(childCall, state)) {
break;
}
}
}
// Only check MultiResolve.keepProcessing at the end of a Module to all multiple arities
keepProcessing = keepProcessing();
if (keepProcessing) {
// the implicit `import Kernel` and `import Kernel.SpecialForms`
keepProcessing = implicitImports(element, state);
}
}
return keepProcessing;
}
use of com.intellij.psi.ResolveState in project intellij-plugins by JetBrains.
the class ActionScriptSmartCompletionContributor method getEventsMap.
public static Map<String, String> getEventsMap(JSClass clazzToProcess) {
if (clazzToProcess == null)
return Collections.emptyMap();
final Map<String, String> eventsMap = new THashMap<>();
class EventsDataCollector extends ResolveProcessor implements ActionScriptResolveUtil.MetaDataProcessor {
public EventsDataCollector() {
super(null);
setToProcessHierarchy(true);
setToProcessMembers(false);
setTypeContext(true);
setLocalResolve(true);
}
@Override
public boolean process(@NotNull final JSAttribute jsAttribute) {
if ("Event".equals(jsAttribute.getName())) {
final JSAttributeNameValuePair eventAttr = jsAttribute.getValueByName("name");
JSAttributeNameValuePair typeAttr = jsAttribute.getValueByName("type");
if (eventAttr != null && typeAttr != null) {
final String simpleValue = eventAttr.getSimpleValue();
if (simpleValue != null) {
eventsMap.put(simpleValue, typeAttr.getSimpleValue());
}
}
}
return true;
}
@Override
public boolean handleOtherElement(final PsiElement el, final PsiElement context, final Ref<PsiElement> continuePassElement) {
return true;
}
@Override
public boolean execute(@NotNull PsiElement element, @NotNull ResolveState state) {
if (element instanceof JSClass) {
ActionScriptResolveUtil.processMetaAttributesForClass(element, this, true);
}
return true;
}
}
final EventsDataCollector eventsDataCollector = new EventsDataCollector();
if (clazzToProcess instanceof XmlBackedJSClassImpl) {
XmlFile file = (XmlFile) clazzToProcess.getParent().getContainingFile();
if (file != null && JavaScriptSupportLoader.isFlexMxmFile(file)) {
final XmlDocument xmlDocument = file.getDocument();
final XmlTag rootTag = xmlDocument == null ? null : xmlDocument.getRootTag();
final XmlTag[] tags = rootTag == null ? XmlTag.EMPTY : MxmlJSClass.findLanguageSubTags(rootTag, FlexPredefinedTagNames.METADATA);
JSResolveUtil.JSInjectedFilesVisitor injectedFilesVisitor = new JSResolveUtil.JSInjectedFilesVisitor() {
@Override
protected void process(JSFile file) {
for (PsiElement element : file.getChildren()) {
if (element instanceof JSAttributeList) {
ActionScriptResolveUtil.processAttributeList(eventsDataCollector, null, (JSAttributeList) element, true, true);
}
}
}
};
for (XmlTag tag : tags) {
JSResolveUtil.processInjectedFileForTag(tag, injectedFilesVisitor);
}
}
}
clazzToProcess.processDeclarations(eventsDataCollector, ResolveState.initial(), clazzToProcess, clazzToProcess);
return eventsMap;
}
use of com.intellij.psi.ResolveState in project Perl5-IDEA by Camelcade.
the class PerlXNamedValue method computeMySourcePosition.
protected boolean computeMySourcePosition(@Nullable XNavigatable navigatable, @Nullable final XInlineDebuggerDataCallback callback) {
String name = myPerlValueDescriptor.getName();
if (StringUtil.isEmpty(name) || name.length() < 2) {
return false;
}
final PerlVariableType variableType = PerlVariableType.bySigil(name.charAt(0));
if (variableType == null || variableType == PerlVariableType.CODE) {
return false;
}
final String variableName = name.substring(1);
final XSourcePosition sourcePosition = myStackFrame.getSourcePosition();
if (sourcePosition == null) {
return false;
}
final Project project = myStackFrame.getPerlExecutionStack().getSuspendContext().getDebugSession().getProject();
final VirtualFile virtualFile = sourcePosition.getFile();
PsiFile psiFile = PsiManager.getInstance(project).findFile(virtualFile);
if (!(psiFile instanceof PerlFileImpl)) {
return false;
}
PsiElement element = psiFile.findElementAt(sourcePosition.getOffset());
if (element == null) {
return false;
}
if (navigatable != null) {
PerlVariableDeclarationSearcher variableProcessor = new PerlVariableDeclarationSearcher(variableName, variableType, element);
PerlResolveUtil.treeWalkUp(element, variableProcessor);
PerlVariableDeclarationElement result = variableProcessor.getResult();
if (result == null) {
return false;
}
navigatable.setSourcePosition(XSourcePositionImpl.createByElement(result));
} else if (callback != null) {
final Document document = psiFile.getViewProvider().getDocument();
if (document == null) {
return false;
}
final boolean[] found = new boolean[] { false };
PerlVariableDeclarationSearcher variableProcessor = new PerlVariableDeclarationSearcher(variableName, variableType, element) {
@Override
public boolean execute(@NotNull PsiElement possibleElement, @NotNull ResolveState state) {
boolean result = super.execute(possibleElement, state);
if (!result) {
registerElement(getResult());
} else if (possibleElement instanceof PerlVariable && ((PerlVariable) possibleElement).getActualType() == variableType && StringUtil.equals(variableName, ((PerlVariable) possibleElement).getName())) {
registerElement(possibleElement);
}
return result;
}
private void registerElement(@Nullable PsiElement targetElement) {
if (targetElement == null) {
return;
}
found[0] = true;
try {
if (mySourcePositionMethod != null) {
mySourcePositionMethod.invoke(callback, XSourcePositionImpl.createByElement(targetElement));
} else if (myLegacyMethod != null) {
myLegacyMethod.invoke(callback, virtualFile, document, document.getLineNumber(targetElement.getTextOffset()));
} else {
found[0] = false;
}
} catch (InvocationTargetException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
};
PerlResolveUtil.treeWalkUp(element, variableProcessor);
return found[0];
}
return true;
}
use of com.intellij.psi.ResolveState in project intellij-plugins by JetBrains.
the class ActionScriptGenerateDelegatesHandler method invoke.
@Override
public void invoke(@NotNull Project project, @NotNull Editor editor, @NotNull PsiFile file) {
final JSClass jsClass = findClass(file, editor);
if (jsClass == null)
return;
Collection<JSField> fields = findCandidateFields(jsClass);
final JSField field;
if (ApplicationManager.getApplication().isUnitTestMode()) {
LOG.assertTrue(fields.size() == 1);
field = fields.iterator().next();
} else {
final MemberChooser<JSNamedElementNode> targetChooser = createMemberChooserDialog(project, jsClass, wrap(fields), false, false, CodeInsightBundle.message("generate.delegate.target.chooser.title"));
targetChooser.show();
if (targetChooser.getExitCode() != DialogWrapper.OK_EXIT_CODE)
return;
field = (JSField) targetChooser.getSelectedElements().get(0).getPsiElement();
}
JSType fieldType = field.getType();
if (fieldType == null)
return;
JSClass fieldClass = fieldType.resolveClass();
if (fieldClass == null)
return;
final boolean allowPackageLocal = !JSPsiImplUtils.differentPackageName(StringUtil.getPackageName(fieldClass.getQualifiedName()), StringUtil.getPackageName(jsClass.getQualifiedName()));
// don't add members along with their supers
class MemberDescriptor {
private final String name;
@Nullable
private final JSFunction.FunctionKind kind;
public MemberDescriptor(JSFunction method) {
name = method.getName();
kind = method.getKind();
}
public MemberDescriptor(JSVariable field) {
name = field.getName();
kind = null;
}
@Override
public boolean equals(Object o) {
if (this == o)
return true;
if (o == null || getClass() != o.getClass())
return false;
MemberDescriptor that = (MemberDescriptor) o;
if (kind != that.kind)
return false;
if (!name.equals(that.name))
return false;
return true;
}
@Override
public int hashCode() {
int result = name.hashCode();
result = 31 * result + (kind != null ? kind.hashCode() : 0);
return result;
}
}
final Map<MemberDescriptor, JSNamedElement> memberCandidates = new HashMap<>();
ResolveProcessor p = new ResolveProcessor(null) {
{
setToProcessHierarchy(true);
}
@Override
public boolean execute(@NotNull PsiElement element, @NotNull ResolveState state) {
JSClass clazz = JSUtils.getMemberContainingClass(element);
if (clazz == null || JSResolveUtil.isObjectClass(clazz) || clazz == jsClass) {
return true;
}
if (element instanceof JSFunction) {
JSFunction method = (JSFunction) element;
if (memberCandidates.containsKey(method.getName())) {
return true;
}
JSAttributeList attributeList = method.getAttributeList();
if (attributeList.getAccessType() == JSAttributeList.AccessType.PRIVATE || attributeList.getAccessType() == JSAttributeList.AccessType.PROTECTED) {
return true;
}
if (!allowPackageLocal && attributeList.getNamespace() == null && attributeList.getAccessType() == JSAttributeList.AccessType.PACKAGE_LOCAL) {
return true;
}
if (method.getKind() == JSFunction.FunctionKind.CONSTRUCTOR) {
return true;
}
if (attributeList.hasModifier(JSAttributeList.ModifierType.STATIC)) {
return true;
}
if (JSInheritanceUtil.findMethodInClass(method, jsClass, true) != null) {
return true;
}
memberCandidates.put(new MemberDescriptor(method), method);
} else if (element instanceof JSVariable) {
JSVariable f = (JSVariable) element;
if (memberCandidates.containsKey(f.getName())) {
return true;
}
JSAttributeList attributeList = f.getAttributeList();
if (attributeList.getAccessType() == JSAttributeList.AccessType.PRIVATE || attributeList.getAccessType() == JSAttributeList.AccessType.PROTECTED) {
return true;
}
if (!allowPackageLocal && attributeList.getAccessType() == JSAttributeList.AccessType.PACKAGE_LOCAL) {
return true;
}
if (jsClass.findFunctionByName(f.getName()) != null) {
return true;
}
memberCandidates.put(new MemberDescriptor(f), f);
}
return true;
}
};
fieldClass.processDeclarations(p, ResolveState.initial(), fieldClass, fieldClass);
Collection<JSNamedElementNode> selected;
if (ApplicationManager.getApplication().isUnitTestMode()) {
LOG.assertTrue(!memberCandidates.isEmpty());
selected = wrap(memberCandidates.values());
} else {
final MemberChooser<JSNamedElementNode> methodsChooser = createMemberChooserDialog(project, jsClass, wrap(memberCandidates.values()), false, true, CodeInsightBundle.message("generate.delegate.method.chooser.title"));
methodsChooser.show();
if (methodsChooser.getExitCode() != DialogWrapper.OK_EXIT_CODE)
return;
selected = methodsChooser.getSelectedElements();
}
BaseCreateMethodsFix fix = new BaseCreateMethodsFix<JSNamedElement>(jsClass) {
final JavaScriptGenerateAccessorHandler.MyBaseCreateMethodsFix generateGetterFix = new JavaScriptGenerateAccessorHandler.MyBaseCreateMethodsFix(JSGetterSetterGenerationMode.Getter, jsClass, null, false, field.getName());
final JavaScriptGenerateAccessorHandler.MyBaseCreateMethodsFix generateSetterFix = new JavaScriptGenerateAccessorHandler.MyBaseCreateMethodsFix(JSGetterSetterGenerationMode.Setter, jsClass, null, false, field.getName());
@Override
protected void adjustAttributeList(JSAttributeListWrapper attributeListWrapper, JSNamedElement function) {
attributeListWrapper.overrideAccessType(JSAttributeList.AccessType.PUBLIC);
attributeListWrapper.overrideModifier(JSAttributeList.ModifierType.STATIC, field.getAttributeList().hasModifier(JSAttributeList.ModifierType.STATIC));
for (JSAttributeList.ModifierType modifierType : new JSAttributeList.ModifierType[] { JSAttributeList.ModifierType.NATIVE, JSAttributeList.ModifierType.DYNAMIC, JSAttributeList.ModifierType.FINAL, JSAttributeList.ModifierType.OVERRIDE, JSAttributeList.ModifierType.VIRTUAL }) {
attributeListWrapper.overrideModifier(modifierType, false);
}
}
@Override
protected void processElements(Project project, MultiMap<String, String> types, Set<JSNamedElement> elementsToProcess) {
for (JSNamedElement e : elementsToProcess) {
if (e instanceof JSFunction) {
anchor = doAddOneMethod(project, buildFunctionText(e, types), anchor);
} else {
anchor = doAddOneMethod(project, generateGetterFix.buildFunctionText(e, types), anchor);
anchor = doAddOneMethod(project, generateSetterFix.buildFunctionText(e, types), anchor);
}
}
}
@Override
protected String buildFunctionBodyText(final String retType, final JSParameterList parameterList, final JSNamedElement element) {
return OverrideMethodsFix.buildDelegatingText(retType, parameterList, ((JSFunction) element), field.getName(), anchor != null ? anchor : myJsClass);
}
};
doInvoke(project, editor, file, selected, fix);
}
Aggregations