Search in sources :

Example 1 with XPathNumberImpl

use of org.intellij.lang.xpath.psi.impl.XPathNumberImpl in project intellij-community by JetBrains.

the class XPathAnnotator method visitXPathNumber.

@Override
public void visitXPathNumber(XPathNumber number) {
    if (number.getXPathVersion() == XPathVersion.V2) {
        final PsiElement leaf = PsiTreeUtil.nextLeaf(number);
        if (leaf != null) {
            final IElementType elementType = leaf.getNode().getElementType();
            if (elementType != XPathTokenTypes.STAR && XPath2TokenTypes.KEYWORDS.contains(elementType)) {
                final TextRange range = TextRange.create(number.getTextRange().getStartOffset(), leaf.getTextRange().getEndOffset());
                final Annotation annotation = myHolder.createErrorAnnotation(range, "Number literal must be followed by whitespace in XPath 2");
                final XPathBinaryExpression expression = PsiTreeUtil.getParentOfType(number, XPathBinaryExpression.class, true);
                if (expression != null) {
                    final XPathExpression lOperand = expression.getLOperand();
                    if (number == lOperand) {
                        final XPathExpression rOperand = expression.getROperand();
                        if (rOperand != null) {
                            final String display = number.getText() + " " + expression.getOperationSign();
                            final String replacement = display + " " + rOperand.getText();
                            assert PsiEquivalenceUtil.areElementsEquivalent(expression, XPathChangeUtil.createExpression(expression, replacement));
                            annotation.registerFix(new ExpressionReplacementFix(replacement, display, expression));
                        }
                    } else if (number == expression.getROperand()) {
                        final PsiElement next = PsiTreeUtil.getParentOfType(PsiTreeUtil.nextLeaf(expression), XPathExpression.class, true);
                        if (next instanceof XPathBinaryExpression) {
                            final XPathBinaryExpression left = (XPathBinaryExpression) next;
                            final XPathExpression rOperand = left.getROperand();
                            if (rOperand != null && lOperand != null) {
                                final String display = number.getText() + " " + left.getOperationSign();
                                final String replacement = lOperand.getText() + " " + expression.getOperationSign() + " " + display + " " + rOperand.getText();
                                assert PsiEquivalenceUtil.areElementsEquivalent(next, XPathChangeUtil.createExpression(next, replacement));
                                annotation.registerFix(new ExpressionReplacementFix(replacement, display, (XPathExpression) next));
                            }
                        }
                    }
                }
            }
        }
    } else {
        if (((XPathNumberImpl) number).isScientificNotation()) {
            myHolder.createErrorAnnotation(number, "Number literals in scientific notation are not allowed in XPath 1.0");
        }
    }
    super.visitXPathNumber(number);
}
Also used : IElementType(com.intellij.psi.tree.IElementType) XPathNumberImpl(org.intellij.lang.xpath.psi.impl.XPathNumberImpl) TextRange(com.intellij.openapi.util.TextRange) PsiElement(com.intellij.psi.PsiElement) Annotation(com.intellij.lang.annotation.Annotation)

Aggregations

Annotation (com.intellij.lang.annotation.Annotation)1 TextRange (com.intellij.openapi.util.TextRange)1 PsiElement (com.intellij.psi.PsiElement)1 IElementType (com.intellij.psi.tree.IElementType)1 XPathNumberImpl (org.intellij.lang.xpath.psi.impl.XPathNumberImpl)1