use of com.intellij.psi.xml.XmlElement 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.xml.XmlElement in project intellij-community by JetBrains.
the class CompletionLists method getNodeTestCompletions.
public static Collection<Lookup> getNodeTestCompletions(final XPathNodeTest element) {
if (!element.isNameTest()) {
return Collections.emptyList();
}
final PrefixedName prefixedName = element.getQName();
assert prefixedName != null;
final String canonicalText = prefixedName.toString();
final String suffix = canonicalText.substring(canonicalText.indexOf(INTELLIJ_IDEA_RULEZ));
final XPathAxisSpecifier axisSpecifier = element.getStep().getAxisSpecifier();
final ContextProvider contextProvider = ContextProvider.getContextProvider(element);
final XmlElement context = contextProvider.getContextElement();
final boolean insidePrefix = suffix.contains(INTELLIJ_IDEA_RULEZ + ":");
final Set<Lookup> list = new HashSet<>();
addNameCompletions(contextProvider, element, list);
final String namespacePrefix = prefixedName.getPrefix();
if (namespacePrefix == null || insidePrefix) {
addNamespaceCompletions(contextProvider.getNamespaceContext(), list, context);
}
final XPathNodeTest.PrincipalType principalType = addContextNames(element, contextProvider, prefixedName, list);
if (namespacePrefix == null && !insidePrefix) {
if (axisSpecifier == null || axisSpecifier.isDefaultAxis()) {
list.addAll(getAxisCompletions());
// wow, this code sux. find a better implementation
PsiElement sibling = element.getParent().getPrevSibling();
while (sibling instanceof PsiWhiteSpace) {
sibling = sibling.getPrevSibling();
}
boolean check = sibling != null;
if (!check) {
XPathLocationPath lp = null;
do {
lp = PsiTreeUtil.getParentOfType(lp == null ? element : lp, XPathLocationPath.class, true);
} while (lp != null && lp.getPrevSibling() == null);
check = lp == null || (sibling = lp.getPrevSibling()) != null;
}
if (check) {
if (sibling instanceof XPathToken && XPathTokenTypes.PATH_OPS.contains(((XPathToken) sibling).getTokenType())) {
// xx/yy<caret> : prevSibl = /
} else {
list.addAll(getFunctionCompletions(element));
list.addAll(getVariableCompletions(element));
}
}
}
if (principalType == XPathNodeTest.PrincipalType.ELEMENT && prefixedName.getPrefix() == null) {
list.addAll(getNodeTypeCompletions(element));
}
}
return list;
}
use of com.intellij.psi.xml.XmlElement in project intellij-community by JetBrains.
the class CompletionLists method addContextNames.
private static XPathNodeTest.PrincipalType addContextNames(XPathNodeTest element, ContextProvider contextProvider, PrefixedName prefixedName, Set<Lookup> list) {
final NamespaceContext namespaceContext = contextProvider.getNamespaceContext();
final XmlElement context = contextProvider.getContextElement();
final XPathNodeTest.PrincipalType principalType = element.getPrincipalType();
if (principalType == XPathNodeTest.PrincipalType.ELEMENT) {
final Set<QName> elementNames = contextProvider.getElements(false);
if (elementNames != null) {
for (QName pair : elementNames) {
if ("*".equals(pair.getLocalPart()))
continue;
if (namespaceMatches(prefixedName, pair.getNamespaceURI(), namespaceContext, context, true)) {
if (prefixedName.getPrefix() == null && namespaceContext != null) {
final String p = namespaceContext.getPrefixForURI(pair.getNamespaceURI(), context);
list.add(new NodeLookup(makePrefix(p) + pair.getLocalPart(), XPathNodeTest.PrincipalType.ELEMENT));
} else {
list.add(new NodeLookup(pair.getLocalPart(), XPathNodeTest.PrincipalType.ELEMENT));
}
}
}
}
} else if (principalType == XPathNodeTest.PrincipalType.ATTRIBUTE) {
final Set<QName> attributeNames = contextProvider.getAttributes(false);
if (attributeNames != null) {
for (QName pair : attributeNames) {
if ("*".equals(pair.getLocalPart()))
continue;
if (namespaceMatches(prefixedName, pair.getNamespaceURI(), namespaceContext, context, false)) {
if (prefixedName.getPrefix() == null && namespaceContext != null) {
final String p = namespaceContext.getPrefixForURI(pair.getNamespaceURI(), context);
list.add(new NodeLookup(makePrefix(p) + pair.getLocalPart(), XPathNodeTest.PrincipalType.ATTRIBUTE));
} else {
list.add(new NodeLookup(pair.getLocalPart(), XPathNodeTest.PrincipalType.ATTRIBUTE));
}
}
}
}
}
return principalType;
}
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 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));
}
Aggregations