use of com.intellij.psi.search.PsiElementProcessor in project intellij-community by JetBrains.
the class XmlElementDescriptorImpl method doCollectXmlDescriptors.
// Read-only action
@Override
protected final XmlElementDescriptor[] doCollectXmlDescriptors(final XmlTag context) {
final LinkedHashSet<XmlElementDescriptor> result = new LinkedHashSet<>();
final XmlElementContentSpec contentSpecElement = myElementDecl.getContentSpecElement();
final XmlNSDescriptor nsDescriptor = getNSDescriptor();
final XmlNSDescriptor NSDescriptor = nsDescriptor != null ? nsDescriptor : getNsDescriptorFrom(context);
XmlUtil.processXmlElements(contentSpecElement, new PsiElementProcessor() {
@Override
public boolean execute(@NotNull PsiElement child) {
if (child instanceof XmlToken) {
final XmlToken token = (XmlToken) child;
if (token.getTokenType() == XmlTokenType.XML_NAME) {
final String text = child.getText();
XmlElementDescriptor element = getElementDescriptor(text, NSDescriptor);
if (element != null) {
result.add(element);
}
} else if (token.getTokenType() == XmlTokenType.XML_CONTENT_ANY) {
if (NSDescriptor instanceof XmlNSDescriptorImpl) {
ContainerUtil.addAll(result, ((XmlNSDescriptorImpl) NSDescriptor).getElements());
} else if (NSDescriptor instanceof XmlNSDescriptorSequence) {
for (XmlNSDescriptor xmlNSDescriptor : ((XmlNSDescriptorSequence) NSDescriptor).getSequence()) {
if (xmlNSDescriptor instanceof XmlNSDescriptorImpl) {
ContainerUtil.addAll(result, ((XmlNSDescriptorImpl) xmlNSDescriptor).getElements());
}
}
}
}
}
return true;
}
}, true, false, XmlUtil.getContainingFile(getDeclaration()));
return result.toArray(new XmlElementDescriptor[result.size()]);
}
use of com.intellij.psi.search.PsiElementProcessor in project intellij-community by JetBrains.
the class XmlNSDescriptorImpl method validate.
@Override
public void validate(@NotNull XmlDocument document, @NotNull ValidationHost host) {
if (document.getLanguage() == DTDLanguage.INSTANCE) {
final List<XmlElementDecl> decls = new ArrayList<>(3);
XmlUtil.processXmlElements(document, new PsiElementProcessor() {
@Override
public boolean execute(@NotNull final PsiElement element) {
if (element instanceof XmlElementDecl)
decls.add((XmlElementDecl) element);
return true;
}
}, false);
XmlUtil.doDuplicationCheckForElements(decls.toArray(new XmlElementDecl[decls.size()]), new HashMap<>(decls.size()), XML_ELEMENT_DECL_PROVIDER, host);
return;
}
ExternalDocumentValidator.doValidation(document, host);
}
use of com.intellij.psi.search.PsiElementProcessor in project intellij-community by JetBrains.
the class XmlAttributeDeclImpl method getDefaultValueText.
@Override
public String getDefaultValueText() {
XmlAttributeValue value = getDefaultValue();
if (value == null)
return null;
String text = value.getText();
if (text.indexOf('%') == -1 && text.indexOf('&') == -1)
return text;
final StringBuilder builder = new StringBuilder();
value.processElements(new PsiElementProcessor() {
@Override
public boolean execute(@NotNull PsiElement element) {
builder.append(element.getText());
return true;
}
}, null);
return builder.toString();
}
use of com.intellij.psi.search.PsiElementProcessor in project intellij-community by JetBrains.
the class ImportFromToImportIntention method doInvoke.
@Override
public void doInvoke(@NotNull Project project, Editor editor, PsiFile file) throws IncorrectOperationException {
InfoHolder info = InfoHolder.collect(getElementFromEditor(editor, file));
try {
// we don't always qualify with module name
String qualifier;
sure(info.myModuleReference);
sure(info.myModuleName);
// [0] is remaining import path, [1] is imported module name
String[] relative_names = null;
if (info.myRelativeLevel > 0) {
relative_names = getRelativeNames(true, info);
if (relative_names == null)
throw new IncorrectOperationException("failed to get relative names");
qualifier = relative_names[1];
} else
qualifier = info.myModuleName;
// find all unqualified references that lead to one of our import elements
final PyImportElement[] ielts = info.myFromImportStatement.getImportElements();
final PyStarImportElement star_ielt = info.myFromImportStatement.getStarImportElement();
final Map<PsiReference, PyImportElement> references = new HashMap<>();
final List<PsiReference> star_references = new ArrayList<>();
PsiTreeUtil.processElements(file, new PsiElementProcessor() {
public boolean execute(@NotNull PsiElement element) {
PyPsiUtils.assertValid(element);
if (element instanceof PyReferenceExpression && PsiTreeUtil.getParentOfType(element, PyImportElement.class) == null) {
PyReferenceExpression ref = (PyReferenceExpression) element;
if (!ref.isQualified()) {
ResolveResult[] resolved = ref.getReference().multiResolve(false);
for (ResolveResult rr : resolved) {
if (rr.isValidResult()) {
if (rr.getElement() == star_ielt)
star_references.add(ref.getReference());
for (PyImportElement ielt : ielts) {
if (rr.getElement() == ielt)
references.put(ref.getReference(), ielt);
}
}
}
}
}
return true;
}
});
// check that at every replacement site our topmost qualifier name is visible
PyQualifiedExpression top_qualifier;
PyExpression feeler = info.myModuleReference;
do {
// if for some crazy reason module name refers to numbers, etc, no point to continue.
sure(feeler instanceof PyQualifiedExpression);
top_qualifier = (PyQualifiedExpression) feeler;
feeler = top_qualifier.getQualifier();
} while (feeler != null);
String top_name = top_qualifier.getName();
Collection<PsiReference> possible_targets = references.keySet();
if (star_references.size() > 0) {
possible_targets = new ArrayList<>(references.keySet().size() + star_references.size());
possible_targets.addAll(references.keySet());
possible_targets.addAll(star_references);
}
final Set<PsiElement> ignored = Sets.<PsiElement>newHashSet(Arrays.asList(info.myFromImportStatement.getImportElements()));
if (top_name != null && showConflicts(project, findDefinitions(top_name, possible_targets, ignored), top_name, info.myModuleName)) {
// got conflicts
return;
}
// add qualifiers
PyElementGenerator generator = PyElementGenerator.getInstance(project);
for (Map.Entry<PsiReference, PyImportElement> entry : references.entrySet()) {
PsiElement referring_elt = entry.getKey().getElement();
// else we won't add it
assert referring_elt.isValid();
ASTNode target_node = referring_elt.getNode();
// else it won't be valid
assert target_node != null;
PyImportElement ielt = entry.getValue();
if (ielt.getAsNameElement() != null) {
// we have an alias, replace it with real name
PyReferenceExpression refex = ielt.getImportReferenceExpression();
// else we won't resolve to this ielt
assert refex != null;
String real_name = refex.getReferencedName();
ASTNode new_qualifier = generator.createExpressionFromText(real_name).getNode();
assert new_qualifier != null;
//ASTNode first_under_target = target_node.getFirstChildNode();
//if (first_under_target != null) new_qualifier.addChildren(first_under_target, null, null); // save the children if any
target_node.getTreeParent().replaceChild(target_node, new_qualifier);
target_node = new_qualifier;
}
qualifyTarget(target_node, project, qualifier);
}
for (PsiReference reference : star_references) {
PsiElement referring_elt = reference.getElement();
// else we won't add it
assert referring_elt.isValid();
ASTNode target_node = referring_elt.getNode();
// else it won't be valid
assert target_node != null;
qualifyTarget(target_node, project, qualifier);
}
// transform the import statement
PyStatement new_import;
if (info.myRelativeLevel == 0) {
new_import = sure(generator.createFromText(LanguageLevel.getDefault(), PyImportStatement.class, "import " + info.myModuleName));
} else {
new_import = sure(generator.createFromText(LanguageLevel.getDefault(), PyFromImportStatement.class, "from " + relative_names[0] + " import " + relative_names[1]));
}
ASTNode parent = sure(info.myFromImportStatement.getParent().getNode());
ASTNode old_node = sure(info.myFromImportStatement.getNode());
parent.replaceChild(old_node, sure(new_import.getNode()));
//myFromImportStatement.replace(new_import);
} catch (IncorrectOperationException ignored) {
PyUtil.showBalloon(project, PyBundle.message("QFIX.action.failed"), MessageType.WARNING);
}
}
use of com.intellij.psi.search.PsiElementProcessor in project intellij-community by JetBrains.
the class XmlHighlightingTest method testXsiSchemaLocation3.
public void testXsiSchemaLocation3() throws Exception {
configureByFile(getFullRelativeTestName(".xsd"), null);
doDoTest(true, false);
final List<XmlTag> tags = new ArrayList<>();
XmlUtil.processXmlElements(((XmlFile) myFile).getDocument(), new PsiElementProcessor() {
@Override
public boolean execute(@NotNull final PsiElement element) {
if (element instanceof XmlTag && ((XmlTag) element).getName().equals("xs:include")) {
tags.add((XmlTag) element);
}
return true;
}
}, true);
assertEquals("Should be three tags", 3, tags.size());
String location = "xslt-1_0.xsd";
checkOneTagForSchemaAttribute(tags.get(2), "schemaLocation", location);
}
Aggregations