use of com.intellij.psi.xml.XmlElement in project intellij-community by JetBrains.
the class XPathEvalAction method execute.
private void execute(Editor editor) {
final Project project = editor.getProject();
final PsiFile psiFile = PsiDocumentManager.getInstance(project).getPsiFile(editor.getDocument());
if (psiFile == null) {
return;
}
InputExpressionDialog.Context input;
XmlElement contextNode = null;
final Config cfg = XPathAppComponent.getInstance().getConfig();
do {
RangeHighlighter contextHighlighter = null;
if (cfg.isUseContextAtCursor()) {
// find out current context node
contextNode = MyPsiUtil.findContextNode(psiFile, editor);
if (contextNode != null) {
contextHighlighter = HighlighterUtil.highlightNode(editor, contextNode, cfg.getContextAttributes(), cfg);
}
}
if (contextNode == null) {
// in XPath data model, / is the document itself, including comments, PIs and the root element
contextNode = ((XmlFile) psiFile).getDocument();
if (contextNode == null) {
FileViewProvider fileViewProvider = psiFile.getViewProvider();
if (fileViewProvider instanceof TemplateLanguageFileViewProvider) {
Language dataLanguage = ((TemplateLanguageFileViewProvider) fileViewProvider).getTemplateDataLanguage();
PsiFile templateDataFile = fileViewProvider.getPsi(dataLanguage);
if (templateDataFile instanceof XmlFile)
contextNode = ((XmlFile) templateDataFile).getDocument();
}
}
}
input = inputXPathExpression(project, contextNode);
if (contextHighlighter != null) {
contextHighlighter.dispose();
}
if (input == null) {
return;
}
HighlighterUtil.clearHighlighters(editor);
} while (contextNode != null && evaluateExpression(input, contextNode, editor, cfg));
}
use of com.intellij.psi.xml.XmlElement in project intellij-community by JetBrains.
the class QNameUtil method createQName.
public static QName createQName(@NotNull String qname, @NotNull PsiElement context) {
final String[] strings = qname.split(":", 2);
if (strings.length == 1) {
return new QName(null, qname);
}
final XmlElement ctx = PsiTreeUtil.getParentOfType(context, XmlElement.class, false);
if (ctx == null)
return UNRESOLVED;
final String uri = XsltNamespaceContext.getNamespaceUriStatic(strings[0], ctx);
if (uri == null)
return UNRESOLVED;
return new QName(uri, strings[1], strings[0]);
}
use of com.intellij.psi.xml.XmlElement in project intellij-community by JetBrains.
the class RngDocumentationProvider method generateDoc.
@Override
@Nullable
public String generateDoc(PsiElement element, @Nullable PsiElement originalElement) {
final XmlElement c = PsiTreeUtil.getParentOfType(originalElement, XmlTag.class, XmlAttribute.class);
if (c != null && c.getManager() == null) {
LOG.warn("Invalid context element passed to generateDoc()", new Throwable("<stack trace>"));
return null;
}
if (c instanceof XmlTag) {
final XmlTag xmlElement = (XmlTag) c;
final XmlElementDescriptor descriptor = xmlElement.getDescriptor();
if (descriptor instanceof CompositeDescriptor) {
final StringBuilder sb = new StringBuilder();
final CompositeDescriptor d = (CompositeDescriptor) descriptor;
final DElementPattern[] patterns = d.getElementPatterns();
final THashSet<PsiElement> elements = ContainerUtil.newIdentityTroveSet();
for (DElementPattern pattern : patterns) {
final PsiElement psiElement = d.getDeclaration(pattern.getLocation());
if (psiElement instanceof XmlTag && elements.add(psiElement)) {
if (sb.length() > 0) {
sb.append("<hr>");
}
sb.append(getDocumentationFromTag((XmlTag) psiElement, xmlElement.getLocalName(), "Element"));
}
}
return makeDocumentation(sb);
} else if (descriptor instanceof RngElementDescriptor) {
final RngElementDescriptor d = (RngElementDescriptor) descriptor;
final PsiElement declaration = d.getDeclaration();
if (declaration instanceof XmlTag) {
return makeDocumentation(getDocumentationFromTag((XmlTag) declaration, xmlElement.getLocalName(), "Element"));
}
}
} else if (c instanceof XmlAttribute) {
final XmlAttribute attribute = (XmlAttribute) c;
final XmlAttributeDescriptor descriptor = attribute.getDescriptor();
if (descriptor instanceof RngXmlAttributeDescriptor) {
final RngXmlAttributeDescriptor d = (RngXmlAttributeDescriptor) descriptor;
final StringBuilder sb = new StringBuilder();
final Collection<PsiElement> declaration = ContainerUtil.newIdentityTroveSet(d.getDeclarations());
for (PsiElement psiElement : declaration) {
if (psiElement instanceof XmlTag) {
if (sb.length() > 0) {
sb.append("<hr>");
}
sb.append(getDocumentationFromTag((XmlTag) psiElement, d.getName(), "Attribute"));
}
}
return makeDocumentation(sb);
}
} else if (element instanceof XmlTag) {
return makeDocumentation(getDocumentationFromTag((XmlTag) element, ((XmlTag) element).getLocalName(), "Element"));
}
return null;
}
use of com.intellij.psi.xml.XmlElement in project intellij-community by JetBrains.
the class XsltExtractFunctionAction method getSettings.
protected RefactoringOptions getSettings(XPathExpression expression, Set<XPathExpression> matchingExpressions) {
final String name = Messages.showInputDialog(expression.getProject(), "Function Name: ", getRefactoringName(), Messages.getQuestionIcon());
final boolean[] b = new boolean[] { false };
if (name != null) {
final String[] parts = name.split(":", 2);
if (parts.length < 2) {
Messages.showMessageDialog(expression.getProject(), "Custom functions require a prefixed name", "Error", Messages.getErrorIcon());
b[0] = true;
}
final XmlElement context = PsiTreeUtil.getContextOfType(expression, XmlElement.class);
final NamespaceContext namespaceContext = expression.getXPathContext().getNamespaceContext();
if (namespaceContext != null && context != null && namespaceContext.resolve(parts[0], context) == null) {
Messages.showMessageDialog(expression.getProject(), "Prefix '" + parts[0] + "' is not defined", "Error", Messages.getErrorIcon());
b[0] = true;
}
}
return new RefactoringOptions() {
@Override
public boolean isCanceled() {
return b[0];
}
@Override
public String getName() {
return name;
}
};
}
use of com.intellij.psi.xml.XmlElement in project intellij-community by JetBrains.
the class DomAnchorImpl method createAnchor.
public static <T extends DomElement> DomAnchor<T> createAnchor(@NotNull T t, boolean usePsi) {
DomInvocationHandler handler = DomManagerImpl.getNotNullHandler(t);
if (handler.getStub() != null) {
return new StubAnchor<>(handler);
}
if (usePsi) {
final XmlElement element = t.getXmlElement();
if (element != null) {
return new PsiBasedDomAnchor<>(PsiAnchor.create(element), element.getProject());
}
}
final DomElement parent = t.getParent();
if (parent == null) {
LOG.error("Parent null: " + t);
}
if (parent instanceof DomFileElementImpl) {
final DomFileElementImpl fileElement = (DomFileElementImpl) parent;
//noinspection unchecked
return new RootAnchor<>(fileElement.getFile(), fileElement.getRootElementClass());
}
final DomAnchor<DomElement> parentAnchor = createAnchor(parent);
final String name = t.getGenericInfo().getElementName(t);
final AbstractDomChildrenDescription description = t.getChildDescription();
final List<? extends DomElement> values = description.getValues(parent);
if (name != null) {
int i = 0;
for (DomElement value : values) {
if (value.equals(t)) {
return new NamedAnchor<>(parentAnchor, description, name, i);
}
if (name.equals(value.getGenericInfo().getElementName(value))) {
i++;
}
}
}
final int index = values.indexOf(t);
if (index < 0) {
diagnoseNegativeIndex2(t, parent, description, values);
}
return new IndexedAnchor<>(parentAnchor, description, index);
}
Aggregations