use of com.intellij.psi.search.PsiElementProcessor in project intellij-community by JetBrains.
the class FetchExtResourceAction method extractEmbeddedFileReferences.
private static Set<String> extractEmbeddedFileReferences(XmlFile file, XmlFile context, final String url) {
final Set<String> result = new LinkedHashSet<>();
if (context != null) {
XmlEntityCache.copyEntityCaches(file, context);
}
XmlUtil.processXmlElements(file, new PsiElementProcessor() {
@Override
public boolean execute(@NotNull PsiElement element) {
if (element instanceof XmlEntityDecl) {
String candidateName = null;
for (PsiElement e = element.getLastChild(); e != null; e = e.getPrevSibling()) {
if (e instanceof XmlAttributeValue && candidateName == null) {
candidateName = e.getText().substring(1, e.getTextLength() - 1);
} else if (e instanceof XmlToken && candidateName != null && (((XmlToken) e).getTokenType() == XmlTokenType.XML_DOCTYPE_PUBLIC || ((XmlToken) e).getTokenType() == XmlTokenType.XML_DOCTYPE_SYSTEM)) {
if (!result.contains(candidateName)) {
result.add(candidateName);
}
break;
}
}
} else if (element instanceof XmlTag) {
final XmlTag tag = (XmlTag) element;
String schemaLocation = tag.getAttributeValue(XmlUtil.SCHEMA_LOCATION_ATT);
if (schemaLocation != null) {
// processing xsd:import && xsd:include
final PsiReference[] references = tag.getAttribute(XmlUtil.SCHEMA_LOCATION_ATT).getValueElement().getReferences();
if (references.length > 0) {
String extension = FileUtilRt.getExtension(new File(url).getName());
final String namespace = tag.getAttributeValue("namespace");
if (namespace != null && schemaLocation.indexOf('/') == -1 && !extension.equals(FileUtilRt.getExtension(schemaLocation))) {
result.add(namespace.substring(0, namespace.lastIndexOf('/') + 1) + schemaLocation);
} else {
result.add(schemaLocation);
}
}
} else {
schemaLocation = tag.getAttributeValue(XmlUtil.SCHEMA_LOCATION_ATT, XmlUtil.XML_SCHEMA_INSTANCE_URI);
if (schemaLocation != null) {
final StringTokenizer tokenizer = new StringTokenizer(schemaLocation);
while (tokenizer.hasMoreTokens()) {
tokenizer.nextToken();
if (!tokenizer.hasMoreTokens())
break;
String location = tokenizer.nextToken();
result.add(location);
}
}
}
}
return true;
}
}, true, true);
return result;
}
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 RelaxIncludeIndex method processRelatedFiles.
private static boolean processRelatedFiles(PsiFile file, VirtualFile[] files, PsiElementProcessor<XmlFile> processor) {
Project project = file.getProject();
final PsiManager psiManager = PsiManager.getInstance(project);
final PsiFile[] psiFiles = ContainerUtil.map2Array(files, PsiFile.class, (NullableFunction<VirtualFile, PsiFile>) file1 -> psiManager.findFile(file1));
for (final PsiFile psiFile : psiFiles) {
if (!processFile(psiFile, processor)) {
return false;
}
}
return true;
}
use of com.intellij.psi.search.PsiElementProcessor in project intellij-community by JetBrains.
the class XmlTagImpl method calculateAttributes.
@NotNull
private XmlAttribute[] calculateAttributes() {
final List<XmlAttribute> result = new ArrayList<>(10);
processChildren(new PsiElementProcessor() {
@Override
public boolean execute(@NotNull PsiElement element) {
if (element instanceof XmlAttribute) {
XmlAttribute attribute = (XmlAttribute) element;
result.add(attribute);
if (!myHasNamespaceDeclarations && attribute.isNamespaceDeclaration())
myHasNamespaceDeclarations = true;
} else if (element instanceof XmlToken && ((XmlToken) element).getTokenType() == XmlTokenType.XML_TAG_END) {
return false;
}
return true;
}
});
if (result.isEmpty()) {
return XmlAttribute.EMPTY_ARRAY;
} else {
return ContainerUtil.toArray(result, new XmlAttribute[result.size()]);
}
}
use of com.intellij.psi.search.PsiElementProcessor in project intellij-community by JetBrains.
the class XmlTagValueImpl method createXmlTagValue.
public static XmlTagValue createXmlTagValue(XmlTag tag) {
final List<XmlTagChild> bodyElements = new ArrayList<>();
tag.processElements(new PsiElementProcessor() {
boolean insideBody;
@Override
public boolean execute(@NotNull PsiElement element) {
final ASTNode treeElement = element.getNode();
if (insideBody) {
if (treeElement != null && treeElement.getElementType() == XmlTokenType.XML_END_TAG_START)
return false;
if (!(element instanceof XmlTagChild))
return true;
bodyElements.add((XmlTagChild) element);
} else if (treeElement != null && treeElement.getElementType() == XmlTokenType.XML_TAG_END)
insideBody = true;
return true;
}
}, tag);
XmlTagChild[] tagChildren = ContainerUtil.toArray(bodyElements, new XmlTagChild[bodyElements.size()]);
return new XmlTagValueImpl(tagChildren, tag);
}
Aggregations