use of com.intellij.psi.xml.XmlFile in project intellij-community by JetBrains.
the class XmlNSDescriptorImpl method doFindIn.
private TypeDescriptor doFindIn(final XmlTag[] tags, final String name, final String namespace, final Pair<QNameKey, XmlTag> pair, final XmlTag rootTag) {
for (final XmlTag tag : tags) {
if (equalsToSchemaName(tag, "complexType")) {
if (name == null) {
CachedValue<TypeDescriptor> value = createAndPutTypesCachedValue(tag, pair);
return value.getValue();
}
String nameAttribute = tag.getAttributeValue("name");
if (isSameName(name, namespace, nameAttribute)) {
CachedValue<TypeDescriptor> cachedValue = createAndPutTypesCachedValue(tag, pair);
return cachedValue.getValue();
}
} else if (equalsToSchemaName(tag, "simpleType")) {
if (name == null) {
CachedValue<TypeDescriptor> value = createAndPutTypesCachedValueSimpleType(tag, pair);
return value.getValue();
}
String nameAttribute = tag.getAttributeValue("name");
if (isSameName(name, namespace, nameAttribute)) {
CachedValue<TypeDescriptor> cachedValue = createAndPutTypesCachedValue(tag, pair);
return cachedValue.getValue();
}
} else if (equalsToSchemaName(tag, INCLUDE_TAG_NAME) || (equalsToSchemaName(tag, IMPORT_TAG_NAME) && (namespace == null || !namespace.equals(getDefaultNamespace())))) {
final String schemaLocation = tag.getAttributeValue("schemaLocation");
if (schemaLocation != null) {
final XmlFile xmlFile = XmlUtil.findNamespace(rootTag.getContainingFile(), schemaLocation);
if (xmlFile != null) {
final XmlDocument document = xmlFile.getDocument();
if (document != null) {
final CachedValue<TypeDescriptor> value = CachedValuesManager.getManager(tag.getProject()).createCachedValue(() -> {
final String currentName = tag.getAttributeValue("name");
if ((currentName != null && !currentName.equals(XmlUtil.findLocalNameByQualifiedName(name))) || !xmlFile.isValid() || xmlFile.getDocument() == null) {
myTypesMap.remove(pair);
return new CachedValueProvider.Result<>(null, PsiModificationTracker.MODIFICATION_COUNT);
}
final XmlDocument document1 = xmlFile.getDocument();
final XmlNSDescriptorImpl nsDescriptor = findNSDescriptor(tag, document1);
if (nsDescriptor == null) {
myTypesMap.remove(pair);
return new CachedValueProvider.Result<>(null, PsiModificationTracker.MODIFICATION_COUNT);
}
final XmlTag rTag = document1.getRootTag();
final TypeDescriptor complexTypeDescriptor = nsDescriptor.findTypeDescriptorImpl(rTag, name, namespace);
return new CachedValueProvider.Result<>(complexTypeDescriptor, rTag);
}, false);
if (value.getValue() != null) {
myTypesMap.put(pair, value);
return value.getValue();
}
}
}
}
} else if (equalsToSchemaName(tag, REDEFINE_TAG_NAME)) {
final XmlTag[] subTags = tag.getSubTags();
TypeDescriptor descriptor = doFindIn(subTags, name, namespace, pair, rootTag);
if (descriptor != null)
return descriptor;
final XmlNSDescriptorImpl nsDescriptor = getRedefinedElementDescriptor(tag);
if (nsDescriptor != null) {
final XmlTag redefinedRootTag = ((XmlDocument) nsDescriptor.getDeclaration()).getRootTag();
descriptor = doFindIn(redefinedRootTag.getSubTags(), name, namespace, pair, redefinedRootTag);
if (descriptor != null)
return descriptor;
}
}
}
return null;
}
use of com.intellij.psi.xml.XmlFile in project intellij-community by JetBrains.
the class XmlNSDescriptorImpl method getAttributeImpl.
@Nullable
private XmlAttributeDescriptor getAttributeImpl(String localName, String namespace, @Nullable Set<XmlTag> visited) {
if (myTag == null)
return null;
XmlNSDescriptor nsDescriptor = myTag.getNSDescriptor(namespace, true);
if (nsDescriptor != this && nsDescriptor instanceof XmlNSDescriptorImpl) {
return ((XmlNSDescriptorImpl) nsDescriptor).getAttributeImpl(localName, namespace, visited);
}
if (visited == null)
visited = new HashSet<>(1);
else if (visited.contains(myTag))
return null;
visited.add(myTag);
XmlTag[] tags = myTag.getSubTags();
for (XmlTag tag : tags) {
if (equalsToSchemaName(tag, ATTRIBUTE_TAG_NAME)) {
String name = tag.getAttributeValue("name");
if (name != null) {
if (checkElementNameEquivalence(localName, namespace, name, tag)) {
return createAttributeDescriptor(tag);
}
}
} else if (equalsToSchemaName(tag, INCLUDE_TAG_NAME) || (equalsToSchemaName(tag, IMPORT_TAG_NAME) && namespace.equals(tag.getAttributeValue("namespace")))) {
final String schemaLocation = tag.getAttributeValue("schemaLocation");
if (schemaLocation != null) {
final XmlFile xmlFile = XmlUtil.findNamespace(myTag.getContainingFile(), schemaLocation);
if (xmlFile != null) {
final XmlDocument includedDocument = xmlFile.getDocument();
if (includedDocument != null) {
final PsiMetaData data = includedDocument.getMetaData();
if (data instanceof XmlNSDescriptorImpl) {
final XmlAttributeDescriptor attributeDescriptor = ((XmlNSDescriptorImpl) data).getAttributeImpl(localName, namespace, visited);
if (attributeDescriptor != null) {
final CachedValue<XmlAttributeDescriptor> value = CachedValuesManager.getManager(includedDocument.getProject()).createCachedValue(() -> {
Object[] deps = attributeDescriptor.getDependences();
if (deps.length == 0) {
LOG.error(attributeDescriptor + " (" + attributeDescriptor.getClass() + ") returned no dependencies");
}
return new CachedValueProvider.Result<>(attributeDescriptor, deps);
}, false);
return value.getValue();
}
}
}
}
}
}
}
return null;
}
use of com.intellij.psi.xml.XmlFile in project intellij-community by JetBrains.
the class JavaFxPropertyRenameHandler method getReferences.
@NotNull
private static PsiReference[] getReferences(DataContext dataContext) {
final Editor editor = CommonDataKeys.EDITOR.getData(dataContext);
PsiFile file = CommonDataKeys.PSI_FILE.getData(dataContext);
if (file == null && editor != null && ApplicationManager.getApplication().isUnitTestMode()) {
final Project project = editor.getProject();
if (project != null) {
file = PsiDocumentManager.getInstance(project).getPsiFile(editor.getDocument());
}
}
if (editor != null && file instanceof XmlFile && JavaFxFileTypeFactory.isFxml(file)) {
final int offset = editor.getCaretModel().getOffset();
final PsiReference reference = file.findReferenceAt(offset);
if (reference instanceof PsiMultiReference) {
return ((PsiMultiReference) reference).getReferences();
}
if (isKnown(reference))
return new PsiReference[] { reference };
}
return PsiReference.EMPTY_ARRAY;
}
use of com.intellij.psi.xml.XmlFile in project intellij-community by JetBrains.
the class XsltExtractFunctionAction method extractImpl.
protected boolean extractImpl(XPathExpression expression, Set<XPathExpression> matchingExpressions, List<XmlTag> otherMatches, RefactoringOptions dlg) {
final XmlAttribute attribute = PsiTreeUtil.getContextOfType(expression, XmlAttribute.class, true);
assert attribute != null;
try {
final String name = dlg.getName();
final XmlTag rootTag = ((XmlFile) attribute.getParent().getContainingFile()).getRootTag();
final XmlTag[] templates = rootTag.findSubTags("template", XsltSupport.XSLT_NS);
final XmlTag insertionPoint = templates.length > 0 ? templates[0] : rootTag.getSubTags()[0];
final XmlTag parentTag = insertionPoint.getParentTag();
assert parentTag != null : "Could not locate position to create function at";
final XmlTag xmlTag = parentTag.createChildTag("function", XsltSupport.XSLT_NS, null, false);
xmlTag.setAttribute("name", name);
final XPathType type = ExpectedTypeUtil.mapType(expression, expression.getType());
xmlTag.setAttribute("as", prefixedName(type, insertionPoint));
final StringBuilder argList = new StringBuilder();
final List<XPathVariableReference> references = RefactoringUtil.collectVariableReferences(expression);
for (XPathVariableReference reference : references) {
final XPathVariable variable = reference.resolve();
if (variable instanceof XsltVariable) {
// don't pass through global parameters and variables
if (XsltCodeInsightUtil.getTemplateTag(variable, false) != null) {
final XmlTag param = parentTag.createChildTag("param", XsltSupport.XSLT_NS, null, false);
param.setAttribute("name", variable.getName());
if (!variable.getType().isAbstract()) {
param.setAttribute("as", prefixedName(ExpectedTypeUtil.mapType(expression, variable.getType()), parentTag));
}
RefactoringUtil.addParameter(xmlTag, param);
if (argList.length() > 0) {
argList.append(", ");
}
argList.append("$").append(variable.getName());
}
}
}
final XmlTag seqTag = parentTag.createChildTag("sequence", XsltSupport.XSLT_NS, null, false);
seqTag.setAttribute("select", expression.getText());
xmlTag.add(seqTag);
// TODO: revisit the formatting
final PsiElement element = parentTag.addBefore(xmlTag, insertionPoint);
final ASTNode node1 = parentTag.getNode();
assert node1 != null;
final ASTNode node2 = element.getNode();
assert node2 != null;
CodeStyleManager.getInstance(xmlTag.getManager().getProject()).reformatNewlyAddedElement(node1, node2);
final XPathExpression var = XPathChangeUtil.createExpression(expression, name + "(" + argList + ")");
expression.replace(var);
return true;
} catch (IncorrectOperationException e) {
Logger.getInstance(getClass().getName()).error(e);
return false;
}
}
use of com.intellij.psi.xml.XmlFile in project intellij-community by JetBrains.
the class XsltImplicitUsagesProvider method isImplicitUsage.
public boolean isImplicitUsage(PsiElement element) {
if (!(element instanceof XmlAttribute)) {
return false;
}
final XmlAttribute attr = (XmlAttribute) element;
if (!attr.isNamespaceDeclaration()) {
return false;
}
final PsiFile file = attr.getContainingFile();
if (!(file instanceof XmlFile)) {
return false;
}
// ContextProvider.hasXPathInjections() is an optimization that avoids to run the references search on totally XPath-free XML files
if (!ContextProvider.hasXPathInjections((XmlFile) file) && !XsltSupport.isXsltFile(file)) {
return false;
}
// This need to catch both prefix references from injected XPathFiles and prefixes from mode declarations/references:
// <xsl:template match="*" mode="prefix:name" />
// BTW: Almost the same logic applies to other XML dialects (RELAX-NG).
// Pull this class into the platform?
final String prefix = attr.getLocalName();
final SchemaPrefix target = new SchemaPrefix(attr, TextRange.from("xmlns:".length(), prefix.length()), prefix);
final Query<PsiReference> q = ReferencesSearch.search(target, new LocalSearchScope(attr.getParent()));
return !q.forEach(psiReference -> {
if (psiReference.getElement() == attr) {
return true;
}
return false;
});
}
Aggregations