use of org.intellij.lang.xpath.XPathFile in project intellij-community by JetBrains.
the class BaseIntroduceAction method actionPerformedImpl.
protected boolean actionPerformedImpl(PsiFile file, Editor editor, XmlAttribute context, int offset) {
if (!(file instanceof XPathFile))
return false;
// pattern attribute may not reference variables
if (XsltSupport.isPatternAttribute(context))
return false;
final SelectionModel selectionModel = editor.getSelectionModel();
final boolean hasSelection = selectionModel.hasSelection();
final int start = selectionModel.getSelectionStart();
final int end = selectionModel.getSelectionEnd();
if (hasSelection) {
final PsiElement xpathElement = file.findElementAt(start);
if (xpathElement != null) {
XPathExpression expression = PsiTreeUtil.getParentOfType(xpathElement, XPathExpression.class);
while (expression != null) {
if (expression.getTextRange().getStartOffset() == start) {
final int diff = expression.getTextRange().getEndOffset() - end;
if (diff == 0) {
extractFromExpression(editor, expression);
return true;
} else if (diff > 0) {
break;
}
}
expression = PsiTreeUtil.getParentOfType(expression, XPathExpression.class);
}
}
} else {
final XPathExpression expression = PsiTreeUtil.getChildOfType(file, XPathExpression.class);
if (expression != null) {
final PsiFile containingFile = expression.getContainingFile();
assert containingFile != null;
final TextRange range = expression.getTextRange();
editor.getSelectionModel().setSelection(range.getStartOffset(), range.getEndOffset());
extractFromExpression(editor, expression);
return true;
}
}
return false;
}
use of org.intellij.lang.xpath.XPathFile in project intellij-community by JetBrains.
the class CompletionLists method addNameCompletions.
private static void addNameCompletions(ContextProvider contextProvider, final XPathNodeTest element, final Set<Lookup> list) {
final PrefixedName prefixedName = element.getQName();
final XPathNodeTest.PrincipalType principalType = element.getPrincipalType();
final Set<PsiFile> files = new HashSet<>();
final XPathFile file = (XPathFile) element.getContainingFile();
files.add(file);
ContainerUtil.addAll(files, contextProvider.getRelatedFiles(file));
for (PsiFile xpathFile : files) {
xpathFile.accept(new PsiRecursiveElementVisitor() {
public void visitElement(PsiElement e) {
if (e instanceof XPathNodeTest) {
final XPathNodeTest nodeTest = (XPathNodeTest) e;
final XPathNodeTest.PrincipalType _principalType = nodeTest.getPrincipalType();
if (_principalType == principalType) {
final PrefixedName _prefixedName = nodeTest.getQName();
if (_prefixedName != null && prefixedName != null) {
final String localName = _prefixedName.getLocalName();
if (!"*".equals(localName) && !localName.contains(INTELLIJ_IDEA_RULEZ)) {
if (Comparing.equal(_prefixedName.getPrefix(), prefixedName.getPrefix())) {
list.add(new NodeLookup(localName, _principalType));
} else if (prefixedName.getPrefix() == null) {
list.add(new NodeLookup(_prefixedName.toString(), _principalType));
}
}
}
}
}
super.visitElement(e);
}
});
}
}
use of org.intellij.lang.xpath.XPathFile in project intellij-community by JetBrains.
the class XPathChangeUtil method createExpression.
@NotNull
public static XPathExpression createExpression(PsiElement context, String text) {
final XPathFile file = createXPathFile(context, text);
final XPathExpression child = PsiTreeUtil.getChildOfType(file, XPathExpression.class);
assert child != null;
return child;
}
use of org.intellij.lang.xpath.XPathFile in project intellij-community by JetBrains.
the class XsltXmlAnnotator method visitXmlAttributeValue.
@Override
public void visitXmlAttributeValue(final XmlAttributeValue value) {
final PsiElement parent = value.getParent();
if (parent instanceof XmlAttribute) {
if (!XsltSupport.isXsltFile(parent.getContainingFile())) {
return;
}
final String s = value.getValue();
if (s == null || s.length() == 0) {
if (XsltSupport.isXPathAttribute((XmlAttribute) parent)) {
InjectedLanguageUtil.enumerate(value, new PsiLanguageInjectionHost.InjectedPsiVisitor() {
@Override
public void visit(@NotNull PsiFile injectedPsi, @NotNull List<PsiLanguageInjectionHost.Shred> places) {
if (injectedPsi instanceof XPathFile) {
if (injectedPsi.getTextLength() == 0) {
myHolder.createErrorAnnotation(value, "Empty XPath expression");
}
}
}
});
}
} else if (XsltSupport.mayBeAVT((XmlAttribute) parent)) {
final List<Integer> singleBraces = collectClosingBraceOffsets(s);
if (singleBraces != null) {
boolean enumerated = InjectedLanguageUtil.enumerate(value, new PsiLanguageInjectionHost.InjectedPsiVisitor() {
@Override
public void visit(@NotNull PsiFile injectedPsi, @NotNull List<PsiLanguageInjectionHost.Shred> places) {
if (injectedPsi instanceof XPathFile) {
for (PsiLanguageInjectionHost.Shred place : places) {
final TextRange range = place.getRangeInsideHost();
for (Iterator<Integer> iterator = singleBraces.iterator(); iterator.hasNext(); ) {
final Integer brace = iterator.next();
if (range.contains(brace)) {
iterator.remove();
}
}
}
}
}
});
if (enumerated) {
for (Integer brace : singleBraces) {
myHolder.createErrorAnnotation(TextRange.from(value.getTextOffset() + brace, 1), "Invalid single closing brace. Escape as '}}'");
}
}
}
}
}
super.visitXmlAttributeValue(value);
}
use of org.intellij.lang.xpath.XPathFile in project intellij-community by JetBrains.
the class CompletionLists method getFunctionCompletions.
public static Collection<Lookup> getFunctionCompletions(XPathElement element) {
final XPathFile xpathFile = (XPathFile) element.getContainingFile();
final ContextProvider contextProvider = ContextProvider.getContextProvider(xpathFile);
final NamespaceContext nsContext = contextProvider.getNamespaceContext();
String uri;
PrefixedName qn = null;
if (element instanceof QNameElement) {
qn = ((QNameElement) element).getQName();
if (qn != null) {
QName qName = contextProvider.getQName(qn, element);
if (qn.getPrefix() != null) {
if (qName != null) {
uri = qName.getNamespaceURI();
} else {
return Collections.emptySet();
}
} else {
uri = null;
}
} else {
uri = null;
}
} else {
uri = null;
}
final Map<Pair<QName, Integer>, ? extends Function> functions = contextProvider.getFunctionContext().getFunctions();
final List<Lookup> lookups = new ArrayList<>(functions.size());
for (Map.Entry<Pair<QName, Integer>, ? extends Function> entry : functions.entrySet()) {
final Function functionDecl = entry.getValue();
final QName f = entry.getKey().first;
final String p;
if (nsContext != null) {
String namespaceURI = f.getNamespaceURI();
if (uri != null && !namespaceURI.equals(uri)) {
continue;
}
final String prefixForURI = nsContext.getPrefixForURI(namespaceURI, PsiTreeUtil.getContextOfType(element, XmlElement.class, true));
if (prefixForURI == null && namespaceURI.length() > 0) {
continue;
}
p = qn == null || qn.getPrefix() == null ? makePrefix(prefixForURI) : "";
} else {
p = "";
}
lookups.add(FunctionLookup.newFunctionLookup(p + f.getLocalPart(), functionDecl));
}
return lookups;
}
Aggregations