use of com.intellij.psi.search.PsiElementProcessor in project intellij-community by JetBrains.
the class PropertiesFileRenameReferenceTest method testRenamePropertiesFile.
public void testRenamePropertiesFile() {
final PsiFile[] files = myFixture.configureByFiles("i18n.properties", "MyClass.java");
final PsiFile propertiesFile = files[0];
final PsiFile javaSourceFile = files[1];
myFixture.renameElement(propertiesFile, "i19n.properties");
boolean[] found = { false };
PsiTreeUtil.processElements(javaSourceFile, new PsiElementProcessor() {
@Override
public boolean execute(@NotNull PsiElement element) {
if (PlatformPatterns.psiElement(PsiField.class).withName("BUNDLE_NAME").accepts(element)) {
assertEquals("i19n", ((PsiLiteralExpression) ((PsiField) element).getInitializer()).getValue());
found[0] = true;
}
return true;
}
});
assertTrue(found[0]);
}
use of com.intellij.psi.search.PsiElementProcessor in project intellij-community by JetBrains.
the class XmlElementImpl method getNameFromEntityRef.
@Nullable
protected static String getNameFromEntityRef(final CompositeElement compositeElement, final IElementType xmlEntityDeclStart) {
final ASTNode node = compositeElement.findChildByType(xmlEntityDeclStart);
if (node == null)
return null;
ASTNode name = node.getTreeNext();
if (name != null && name.getElementType() == TokenType.WHITE_SPACE) {
name = name.getTreeNext();
}
if (name != null && name.getElementType() == XmlElementType.XML_ENTITY_REF) {
final StringBuilder builder = new StringBuilder();
((XmlElement) name.getPsi()).processElements(new PsiElementProcessor() {
@Override
public boolean execute(@NotNull final PsiElement element) {
builder.append(element.getText());
return true;
}
}, name.getPsi());
if (builder.length() > 0)
return builder.toString();
}
return null;
}
use of com.intellij.psi.search.PsiElementProcessor in project intellij-community by JetBrains.
the class XmlCompletionContributor method addEntityRefCompletions.
private static void addEntityRefCompletions(PsiElement context, CompletionResultSet resultSet) {
XmlFile containingFile = null;
XmlFile descriptorFile = null;
final XmlTag tag = PsiTreeUtil.getParentOfType(context, XmlTag.class);
if (tag != null) {
containingFile = (XmlFile) tag.getContainingFile();
descriptorFile = findDescriptorFile(tag, containingFile);
}
if (HtmlUtil.isHtml5Context(tag)) {
descriptorFile = XmlUtil.findXmlFile(containingFile, Html5SchemaProvider.getCharsDtdLocation());
} else if (tag == null) {
final XmlDocument document = PsiTreeUtil.getParentOfType(context, XmlDocument.class);
if (document != null) {
containingFile = (XmlFile) document.getContainingFile();
final FileType ft = containingFile.getFileType();
if (HtmlUtil.isHtml5Document(document)) {
descriptorFile = XmlUtil.findXmlFile(containingFile, Html5SchemaProvider.getCharsDtdLocation());
} else if (ft != StdFileTypes.XML) {
final String namespace = ft == StdFileTypes.XHTML || ft == StdFileTypes.JSPX ? XmlUtil.XHTML_URI : XmlUtil.HTML_URI;
final XmlNSDescriptor nsDescriptor = document.getDefaultNSDescriptor(namespace, true);
if (nsDescriptor != null) {
descriptorFile = nsDescriptor.getDescriptorFile();
}
}
}
}
if (descriptorFile != null && containingFile != null) {
final boolean acceptSystemEntities = containingFile.getFileType() == StdFileTypes.XML;
final PsiElementProcessor processor = new PsiElementProcessor() {
@Override
public boolean execute(@NotNull final PsiElement element) {
if (element instanceof XmlEntityDecl) {
final XmlEntityDecl xmlEntityDecl = (XmlEntityDecl) element;
if (xmlEntityDecl.isInternalReference() || acceptSystemEntities) {
final LookupElementBuilder _item = buildEntityLookupItem(xmlEntityDecl);
if (_item != null) {
resultSet.addElement(_item);
resultSet.stopHere();
}
}
}
return true;
}
};
XmlUtil.processXmlElements(descriptorFile, processor, true);
if (descriptorFile != containingFile && acceptSystemEntities) {
final XmlProlog element = containingFile.getDocument().getProlog();
if (element != null)
XmlUtil.processXmlElements(element, processor, true);
}
}
}
use of com.intellij.psi.search.PsiElementProcessor in project intellij-community by JetBrains.
the class DtdCompletionContributor method addEntityCompletions.
private static void addEntityCompletions(@NotNull final CompletionResultSet result, PsiElement position) {
final PsiElementProcessor processor = new PsiElementProcessor() {
@Override
public boolean execute(@NotNull final PsiElement element) {
if (element instanceof XmlEntityDecl) {
final XmlEntityDecl xmlEntityDecl = (XmlEntityDecl) element;
String name = xmlEntityDecl.getName();
if (name != null && xmlEntityDecl.isInternalReference()) {
result.addElement(LookupElementBuilder.create(name).withInsertHandler(XmlCompletionContributor.ENTITY_INSERT_HANDLER));
}
}
return true;
}
};
XmlUtil.processXmlElements((XmlFile) position.getContainingFile().getOriginalFile(), processor, true);
}
use of com.intellij.psi.search.PsiElementProcessor in project intellij-community by JetBrains.
the class ScriptSupportUtil method processDeclarations.
public static boolean processDeclarations(final XmlFile element, PsiScopeProcessor processor, ResolveState state, PsiElement lastParent, PsiElement place) {
CachedValue<XmlTag[]> myCachedScriptTags = element.getUserData(CachedScriptTagsKey);
if (myCachedScriptTags == null) {
myCachedScriptTags = CachedValuesManager.getManager(element.getProject()).createCachedValue(() -> {
final List<XmlTag> scriptTags = new ArrayList<>();
final XmlDocument document = HtmlPsiUtil.getRealXmlDocument(element.getDocument());
if (document != null) {
PsiElementProcessor psiElementProcessor = new PsiElementProcessor() {
@Override
public boolean execute(@NotNull final PsiElement element1) {
if (element1 instanceof XmlTag) {
final XmlTag tag = (XmlTag) element1;
if (HtmlUtil.SCRIPT_TAG_NAME.equalsIgnoreCase(tag.getName())) {
final XmlElementDescriptor descriptor = tag.getDescriptor();
if (descriptor != null && HtmlUtil.SCRIPT_TAG_NAME.equals(descriptor.getName())) {
scriptTags.add(tag);
}
}
}
return true;
}
};
XmlPsiUtil.processXmlElements(document, psiElementProcessor, true);
}
return new CachedValueProvider.Result<>(scriptTags.toArray(new XmlTag[scriptTags.size()]), element);
}, false);
element.putUserData(CachedScriptTagsKey, myCachedScriptTags);
}
if (ProcessingDeclarationsFlag.get() != null)
return true;
try {
ProcessingDeclarationsFlag.set("");
for (XmlTag tag : myCachedScriptTags.getValue()) {
final XmlTagChild[] children = tag.getValue().getChildren();
for (XmlTagChild child : children) {
if (!child.processDeclarations(processor, state, null, place))
return false;
}
if (tag.getAttributeValue("src") != null) {
final XmlAttribute attribute = tag.getAttribute("src", null);
if (attribute != null) {
final PsiFile psiFile = FileReferenceUtil.findFile(attribute.getValueElement());
if (psiFile != null && psiFile.isValid()) {
if (!psiFile.processDeclarations(processor, state, null, place)) {
return false;
}
}
}
}
}
} finally {
ProcessingDeclarationsFlag.set(null);
}
return true;
}
Aggregations