Search in sources :

Example 1 with XsltElement

use of org.intellij.lang.xpath.xslt.psi.XsltElement in project intellij-community by JetBrains.

the class VariableInlineHandler method invoke.

public static void invoke(@NotNull final XPathVariable variable, Editor editor) {
    final String type = LanguageFindUsages.INSTANCE.forLanguage(variable.getLanguage()).getType(variable);
    final Project project = variable.getProject();
    final XmlTag tag = ((XsltElement) variable).getTag();
    final String expression = tag.getAttributeValue("select");
    if (expression == null) {
        CommonRefactoringUtil.showErrorHint(project, editor, MessageFormat.format("{0} ''{1}'' has no value.", StringUtil.capitalize(type), variable.getName()), TITLE, null);
        return;
    }
    final Collection<PsiReference> references = ReferencesSearch.search(variable, new LocalSearchScope(tag.getParentTag()), false).findAll();
    if (references.size() == 0) {
        CommonRefactoringUtil.showErrorHint(project, editor, MessageFormat.format("{0} ''{1}'' is never used.", variable.getName()), TITLE, null);
        return;
    }
    boolean hasExternalRefs = false;
    if (XsltSupport.isTopLevelElement(tag)) {
        final Query<PsiReference> query = ReferencesSearch.search(variable, GlobalSearchScope.allScope(project), false);
        hasExternalRefs = !query.forEach(new Processor<PsiReference>() {

            int allRefs = 0;

            public boolean process(PsiReference psiReference) {
                if (++allRefs > references.size()) {
                    return false;
                } else if (!references.contains(psiReference)) {
                    return false;
                }
                return true;
            }
        });
    }
    final HighlightManager highlighter = HighlightManager.getInstance(project);
    final ArrayList<RangeHighlighter> highlighters = new ArrayList<>();
    final PsiReference[] psiReferences = references.toArray(new PsiReference[references.size()]);
    TextRange[] ranges = ContainerUtil.map2Array(psiReferences, TextRange.class, s -> {
        final PsiElement psiElement = s.getElement();
        final XmlAttributeValue context = PsiTreeUtil.getContextOfType(psiElement, XmlAttributeValue.class, true);
        if (psiElement instanceof XPathElement && context != null) {
            return XsltCodeInsightUtil.getRangeInsideHostingFile((XPathElement) psiElement).cutOut(s.getRangeInElement());
        }
        return psiElement.getTextRange().cutOut(s.getRangeInElement());
    });
    final Editor e = editor instanceof EditorWindow ? ((EditorWindow) editor).getDelegate() : editor;
    for (TextRange range : ranges) {
        final TextAttributes textAttributes = EditorColors.SEARCH_RESULT_ATTRIBUTES.getDefaultAttributes();
        final Color color = getScrollmarkColor(textAttributes);
        highlighter.addOccurrenceHighlight(e, range.getStartOffset(), range.getEndOffset(), textAttributes, HighlightManagerImpl.HIDE_BY_ESCAPE, highlighters, color);
    }
    highlighter.addOccurrenceHighlights(e, new PsiElement[] { ((XsltVariable) variable).getNameIdentifier() }, EditorColors.WRITE_SEARCH_RESULT_ATTRIBUTES.getDefaultAttributes(), false, highlighters);
    if (!hasExternalRefs) {
        if (!ApplicationManager.getApplication().isUnitTestMode() && Messages.showYesNoDialog(MessageFormat.format("Inline {0} ''{1}''? ({2} occurrence{3})", type, variable.getName(), String.valueOf(references.size()), references.size() > 1 ? "s" : ""), TITLE, Messages.getQuestionIcon()) != Messages.YES) {
            return;
        }
    } else {
        if (!ApplicationManager.getApplication().isUnitTestMode() && Messages.showYesNoDialog(MessageFormat.format("Inline {0} ''{1}''? ({2} local occurrence{3})\n" + "\nWarning: It is being used in external files. Its declaration will not be removed.", type, variable.getName(), String.valueOf(references.size()), references.size() > 1 ? "s" : ""), TITLE, Messages.getWarningIcon()) != Messages.YES) {
            return;
        }
    }
    final boolean hasRefs = hasExternalRefs;
    new WriteCommandAction.Simple(project, "XSLT.Inline", tag.getContainingFile()) {

        @Override
        protected void run() throws Throwable {
            try {
                for (PsiReference psiReference : references) {
                    final PsiElement element = psiReference.getElement();
                    if (element instanceof XPathElement) {
                        final XPathElement newExpr = XPathChangeUtil.createExpression(element, expression);
                        element.replace(newExpr);
                    } else {
                        assert false;
                    }
                }
                if (!hasRefs) {
                    tag.delete();
                }
            } catch (IncorrectOperationException e) {
                Logger.getInstance(VariableInlineHandler.class.getName()).error(e);
            }
        }
    }.execute();
}
Also used : WriteCommandAction(com.intellij.openapi.command.WriteCommandAction) XsltElement(org.intellij.lang.xpath.xslt.psi.XsltElement) ArrayList(java.util.ArrayList) RangeHighlighter(com.intellij.openapi.editor.markup.RangeHighlighter) TextAttributes(com.intellij.openapi.editor.markup.TextAttributes) PsiElement(com.intellij.psi.PsiElement) LocalSearchScope(com.intellij.psi.search.LocalSearchScope) HighlightManager(com.intellij.codeInsight.highlighting.HighlightManager) PsiReference(com.intellij.psi.PsiReference) TextRange(com.intellij.openapi.util.TextRange) XmlAttributeValue(com.intellij.psi.xml.XmlAttributeValue) XPathElement(org.intellij.lang.xpath.psi.XPathElement) EditorWindow(com.intellij.injected.editor.EditorWindow) Project(com.intellij.openapi.project.Project) IncorrectOperationException(com.intellij.util.IncorrectOperationException) Editor(com.intellij.openapi.editor.Editor) XmlTag(com.intellij.psi.xml.XmlTag)

Example 2 with XsltElement

use of org.intellij.lang.xpath.xslt.psi.XsltElement in project intellij-community by JetBrains.

the class XsltDocumentationProvider method getUrlFor.

@Nullable
public List<String> getUrlFor(PsiElement psiElement, PsiElement psiElement1) {
    if (psiElement instanceof XsltElement)
        return null;
    final String category;
    final String name;
    final String tagName = getTagName(psiElement);
    if (tagName != null) {
        name = tagName;
        category = "element";
    } else if (psiElement instanceof XPathFunction) {
        name = ((XPathFunction) psiElement).getName();
        category = "function";
    } else if (psiElement instanceof DocElement) {
        name = ((DocElement) psiElement).getName();
        category = ((DocElement) psiElement).getCategory();
    } else {
        return null;
    }
    try {
        final Document document = getDocumentationDocument();
        final XPath xPath = XPath.newInstance("//x:" + category + "[@name = '" + name + "']");
        xPath.addNamespace("x", document.getRootElement().getNamespaceURI());
        final Element e = (Element) xPath.selectSingleNode(document);
        if (e != null) {
            return Collections.singletonList(e.getParentElement().getAttributeValue("base") + e.getAttributeValue("href"));
        }
    } catch (Exception e) {
        Logger.getInstance(getClass().getName()).error(e);
    }
    return null;
}
Also used : XPath(org.jdom.xpath.XPath) XPathFunction(org.intellij.lang.xpath.psi.XPathFunction) XsltElement(org.intellij.lang.xpath.xslt.psi.XsltElement) XsltElement(org.intellij.lang.xpath.xslt.psi.XsltElement) LightElement(com.intellij.psi.impl.light.LightElement) Element(org.jdom.Element) Document(org.jdom.Document) JDOMException(org.jdom.JDOMException) IncorrectOperationException(com.intellij.util.IncorrectOperationException) TransformerConfigurationException(javax.xml.transform.TransformerConfigurationException) IOException(java.io.IOException) Nullable(org.jetbrains.annotations.Nullable)

Example 3 with XsltElement

use of org.intellij.lang.xpath.xslt.psi.XsltElement in project intellij-community by JetBrains.

the class XsltElementFactoryImpl method wrapElement.

@Override
public <T extends XsltElement> T wrapElement(XmlTag target, Class<T> clazz) {
    assert target.isValid();
    final Pair<ASTNode, XsltElement> wrapper = target.getUserData(WRAPPER);
    final ASTNode tagNode = target.getNode();
    final ASTNode nameNode = tagNode != null ? XmlChildRole.START_TAG_NAME_FINDER.findChild(tagNode) : null;
    if (wrapper != null) {
        if (wrapper.getFirst() == nameNode) {
            if (wrapper.second.isValid() && clazz.isAssignableFrom(wrapper.second.getClass())) {
                return clazz.cast(wrapper.second);
            }
        }
    }
    assert XsltSupport.isXsltTag(target) : "Not an XSLT tag: {" + target.getNamespace() + "}" + target.getName();
    final XsltElement element;
    if (XsltSupport.isTemplate(target, false)) {
        element = new XsltTemplateImpl(target);
    } else if (XsltSupport.isVariable(target)) {
        element = new XsltVariableImpl(target);
    } else if (XsltSupport.isParam(target)) {
        element = new XsltParameterImpl(target);
    } else if (XsltSupport.isTemplateCall(target)) {
        element = new XsltCallTemplateImpl(target);
    } else if (XsltSupport.isApplyTemplates(target)) {
        element = new XsltApplyTemplatesImpl(target);
    } else if ("with-param".equals(target.getLocalName())) {
        element = new XsltWithParamImpl(target);
    } else if (XsltSupport.isXsltRootTag(target)) {
        element = new XsltStylesheetImpl(target);
    } else if (XsltSupport.isFunction(target)) {
        element = new XsltFunctionImpl(target);
    } else {
        element = new DummyElementImpl(target);
    }
    if (!(element instanceof DummyElementImpl)) {
        target.putUserData(WRAPPER, Pair.create(nameNode, element));
    }
    return clazz.cast(element);
}
Also used : XsltElement(org.intellij.lang.xpath.xslt.psi.XsltElement) ASTNode(com.intellij.lang.ASTNode)

Aggregations

XsltElement (org.intellij.lang.xpath.xslt.psi.XsltElement)3 IncorrectOperationException (com.intellij.util.IncorrectOperationException)2 HighlightManager (com.intellij.codeInsight.highlighting.HighlightManager)1 EditorWindow (com.intellij.injected.editor.EditorWindow)1 ASTNode (com.intellij.lang.ASTNode)1 WriteCommandAction (com.intellij.openapi.command.WriteCommandAction)1 Editor (com.intellij.openapi.editor.Editor)1 RangeHighlighter (com.intellij.openapi.editor.markup.RangeHighlighter)1 TextAttributes (com.intellij.openapi.editor.markup.TextAttributes)1 Project (com.intellij.openapi.project.Project)1 TextRange (com.intellij.openapi.util.TextRange)1 PsiElement (com.intellij.psi.PsiElement)1 PsiReference (com.intellij.psi.PsiReference)1 LightElement (com.intellij.psi.impl.light.LightElement)1 LocalSearchScope (com.intellij.psi.search.LocalSearchScope)1 XmlAttributeValue (com.intellij.psi.xml.XmlAttributeValue)1 XmlTag (com.intellij.psi.xml.XmlTag)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 TransformerConfigurationException (javax.xml.transform.TransformerConfigurationException)1