Search in sources :

Example 1 with XMLLanguage

use of com.intellij.lang.xml.XMLLanguage in project intellij-community by JetBrains.

the class XmlAutoPopupHandler method isLanguageRelevant.

private static boolean isLanguageRelevant(final PsiElement element, final PsiFile file, final Ref<Boolean> isRelevantLanguage, final Ref<Boolean> isAnt) {
    Boolean isAntFile = isAnt.get();
    if (isAntFile == null) {
        isAntFile = XmlUtil.isAntFile(file);
        isAnt.set(isAntFile);
    }
    Boolean result = isRelevantLanguage.get();
    if (result == null) {
        Language language = element.getLanguage();
        PsiElement parent = element.getParent();
        if (element instanceof PsiWhiteSpace && parent != null) {
            language = parent.getLanguage();
        }
        result = language instanceof XMLLanguage || HtmlUtil.supportsXmlTypedHandlers(file) || isAntFile.booleanValue();
        isRelevantLanguage.set(result);
    }
    return result.booleanValue();
}
Also used : Language(com.intellij.lang.Language) XMLLanguage(com.intellij.lang.xml.XMLLanguage) XMLLanguage(com.intellij.lang.xml.XMLLanguage) PsiElement(com.intellij.psi.PsiElement) PsiWhiteSpace(com.intellij.psi.PsiWhiteSpace)

Example 2 with XMLLanguage

use of com.intellij.lang.xml.XMLLanguage in project intellij-community by JetBrains.

the class XmlSlashTypedHandler method charTyped.

@Override
public Result charTyped(final char c, final Project project, @NotNull final Editor editor, @NotNull final PsiFile editedFile) {
    if (!WebEditorOptions.getInstance().isAutoCloseTag())
        return Result.CONTINUE;
    if (c == '/' && XmlGtTypedHandler.fileContainsXmlLanguage(editedFile)) {
        PsiDocumentManager.getInstance(project).commitAllDocuments();
        PsiFile file = PsiDocumentManager.getInstance(project).getPsiFile(editor.getDocument());
        if (file == null)
            return Result.CONTINUE;
        FileViewProvider provider = file.getViewProvider();
        final int offset = editor.getCaretModel().getOffset();
        PsiElement element = provider.findElementAt(offset - 1, XMLLanguage.class);
        if (element == null)
            return Result.CONTINUE;
        if (!(element.getLanguage() instanceof XMLLanguage))
            return Result.CONTINUE;
        ASTNode prevLeaf = element.getNode();
        if (prevLeaf == null)
            return Result.CONTINUE;
        final String prevLeafText = prevLeaf.getText();
        if ("</".equals(prevLeafText) && prevLeaf.getElementType() == XmlTokenType.XML_END_TAG_START) {
            XmlTag tag = PsiTreeUtil.getParentOfType(element, XmlTag.class);
            if (tag != null && StringUtil.isNotEmpty(tag.getName()) && TreeUtil.findSibling(prevLeaf, XmlTokenType.XML_NAME) == null) {
                // check for template language like JSP
                if (provider instanceof MultiplePsiFilesPerDocumentFileViewProvider) {
                    PsiElement element1 = SingleRootFileViewProvider.findElementAt(file, offset - 1);
                    if (element1 != null) {
                        // case of top-level jsp tag
                        XmlTag tag1 = PsiTreeUtil.getParentOfType(element1, XmlTag.class);
                        if (shouldReplace(tag, tag1, offset)) {
                            tag = tag1;
                        } else {
                            // if we have enclosing jsp tag, actual tag to be completed will be previous sibling
                            tag1 = PsiTreeUtil.getPrevSiblingOfType(element1.getParent(), XmlTag.class);
                            if (shouldReplace(tag, tag1, offset)) {
                                tag = tag1;
                            }
                        }
                    }
                }
                EditorModificationUtil.insertStringAtCaret(editor, tag.getName() + ">", false);
                autoIndent(editor);
                return Result.STOP;
            }
        }
        if (!"/".equals(prevLeafText.trim()))
            return Result.CONTINUE;
        while ((prevLeaf = TreeUtil.prevLeaf(prevLeaf)) != null && prevLeaf.getElementType() == XmlTokenType.XML_WHITE_SPACE) ;
        if (prevLeaf instanceof OuterLanguageElement) {
            element = file.getViewProvider().findElementAt(offset - 1, file.getLanguage());
            prevLeaf = element != null ? element.getNode() : null;
            while ((prevLeaf = TreeUtil.prevLeaf(prevLeaf)) != null && prevLeaf.getElementType() == XmlTokenType.XML_WHITE_SPACE) ;
        }
        if (prevLeaf == null)
            return Result.CONTINUE;
        XmlTag tag = PsiTreeUtil.getParentOfType(prevLeaf.getPsi(), XmlTag.class);
        if (tag == null) {
            // prevLeaf maybe in one tree and element in another
            PsiElement element2 = provider.findElementAt(prevLeaf.getStartOffset(), XMLLanguage.class);
            tag = PsiTreeUtil.getParentOfType(element2, XmlTag.class);
            if (tag == null)
                return Result.CONTINUE;
        }
        final XmlToken startToken = XmlUtil.getTokenOfType(tag, XmlTokenType.XML_START_TAG_START);
        if (startToken == null || !startToken.getText().equals("<"))
            return Result.CONTINUE;
        if (XmlUtil.getTokenOfType(tag, XmlTokenType.XML_TAG_END) != null)
            return Result.CONTINUE;
        if (XmlUtil.getTokenOfType(tag, XmlTokenType.XML_EMPTY_ELEMENT_END) != null)
            return Result.CONTINUE;
        if (PsiTreeUtil.getParentOfType(element, XmlAttributeValue.class) != null)
            return Result.CONTINUE;
        EditorModificationUtil.insertStringAtCaret(editor, ">", false);
        return Result.STOP;
    }
    return Result.CONTINUE;
}
Also used : OuterLanguageElement(com.intellij.psi.templateLanguages.OuterLanguageElement) ASTNode(com.intellij.lang.ASTNode) XMLLanguage(com.intellij.lang.xml.XMLLanguage)

Example 3 with XMLLanguage

use of com.intellij.lang.xml.XMLLanguage in project intellij-community by JetBrains.

the class HtmlUtil method getIncludedPathsElements.

public static List<XmlAttributeValue> getIncludedPathsElements(@NotNull final XmlFile file) {
    final List<XmlAttributeValue> result = new ArrayList<>();
    file.acceptChildren(new XmlRecursiveElementWalkingVisitor() {

        @Override
        public void visitXmlTag(XmlTag tag) {
            XmlAttribute attribute = null;
            if ("link".equalsIgnoreCase(tag.getName())) {
                attribute = tag.getAttribute("href");
            } else if ("script".equalsIgnoreCase(tag.getName()) || "img".equalsIgnoreCase(tag.getName())) {
                attribute = tag.getAttribute("src");
            }
            if (attribute != null)
                result.add(attribute.getValueElement());
            super.visitXmlTag(tag);
        }

        @Override
        public void visitElement(PsiElement element) {
            if (element.getLanguage() instanceof XMLLanguage) {
                super.visitElement(element);
            }
        }
    });
    return result.isEmpty() ? Collections.emptyList() : result;
}
Also used : XMLLanguage(com.intellij.lang.xml.XMLLanguage) XmlRecursiveElementWalkingVisitor(com.intellij.psi.XmlRecursiveElementWalkingVisitor) PsiElement(com.intellij.psi.PsiElement)

Example 4 with XMLLanguage

use of com.intellij.lang.xml.XMLLanguage in project intellij-plugins by JetBrains.

the class DartPackageAwareFileIncludeProvider method getIncludeInfos.

private static FileIncludeInfo[] getIncludeInfos(@NotNull final XmlFile xmlFile) {
    final List<FileIncludeInfo> result = new ArrayList<>();
    xmlFile.acceptChildren(new XmlRecursiveElementVisitor() {

        @Override
        public void visitXmlTag(XmlTag tag) {
            final String path = "link".equalsIgnoreCase(tag.getName()) ? getPathRelativeToPackageRoot(tag.getAttributeValue("href")) : "script".equalsIgnoreCase(tag.getName()) ? getPathRelativeToPackageRoot(tag.getAttributeValue("src")) : null;
            if (!StringUtil.isEmptyOrSpaces(path)) {
                result.add(new FileIncludeInfo(path));
            }
            super.visitXmlTag(tag);
        }

        @Override
        public void visitElement(PsiElement element) {
            if (element.getLanguage() instanceof XMLLanguage) {
                super.visitElement(element);
            }
        }
    });
    return ContainerUtil.toArray(result, FileIncludeInfo.EMPTY);
}
Also used : FileIncludeInfo(com.intellij.psi.impl.include.FileIncludeInfo) XmlRecursiveElementVisitor(com.intellij.psi.XmlRecursiveElementVisitor) ArrayList(java.util.ArrayList) XMLLanguage(com.intellij.lang.xml.XMLLanguage) PsiElement(com.intellij.psi.PsiElement) XmlTag(com.intellij.psi.xml.XmlTag)

Example 5 with XMLLanguage

use of com.intellij.lang.xml.XMLLanguage in project intellij-community by JetBrains.

the class HtmlLineMarkerProvider method collectSlowLineMarkers.

@Override
public void collectSlowLineMarkers(@NotNull final List<PsiElement> elements, @NotNull final Collection<LineMarkerInfo> result) {
    Map<Language, LineMarkerProvider> localEmbeddedLanguagesLineMarkerProviders = null;
    Map<LineMarkerProvider, List<PsiElement>> embeddedLineMarkersWorkItems = null;
    for (PsiElement element : elements) {
        if (element instanceof PsiWhiteSpace)
            continue;
        final Language language = element.getLanguage();
        if (!(language instanceof XMLLanguage)) {
            if (localEmbeddedLanguagesLineMarkerProviders == null) {
                localEmbeddedLanguagesLineMarkerProviders = new THashMap<>();
            }
            final LineMarkerProvider lineMarkerProvider = getLineMarkerProviderFromLanguage(language, localEmbeddedLanguagesLineMarkerProviders);
            if (lineMarkerProvider != null) {
                if (embeddedLineMarkersWorkItems == null)
                    embeddedLineMarkersWorkItems = new THashMap<>();
                List<PsiElement> elementList = embeddedLineMarkersWorkItems.get(lineMarkerProvider);
                if (elementList == null) {
                    elementList = new ArrayList<>(5);
                    embeddedLineMarkersWorkItems.put(lineMarkerProvider, elementList);
                }
                elementList.add(element);
            }
        }
    }
    if (embeddedLineMarkersWorkItems != null) {
        for (Map.Entry<LineMarkerProvider, List<PsiElement>> entry : embeddedLineMarkersWorkItems.entrySet()) {
            entry.getKey().collectSlowLineMarkers(entry.getValue(), result);
        }
    }
}
Also used : LineMarkerProvider(com.intellij.codeInsight.daemon.LineMarkerProvider) Language(com.intellij.lang.Language) XMLLanguage(com.intellij.lang.xml.XMLLanguage) THashMap(gnu.trove.THashMap) ArrayList(java.util.ArrayList) List(java.util.List) XMLLanguage(com.intellij.lang.xml.XMLLanguage) THashMap(gnu.trove.THashMap) Map(java.util.Map) PsiElement(com.intellij.psi.PsiElement) PsiWhiteSpace(com.intellij.psi.PsiWhiteSpace)

Aggregations

XMLLanguage (com.intellij.lang.xml.XMLLanguage)7 PsiElement (com.intellij.psi.PsiElement)5 Language (com.intellij.lang.Language)3 PsiWhiteSpace (com.intellij.psi.PsiWhiteSpace)3 ArrayList (java.util.ArrayList)2 LineMarkerProvider (com.intellij.codeInsight.daemon.LineMarkerProvider)1 HtmlFileType (com.intellij.ide.highlighter.HtmlFileType)1 XHtmlFileType (com.intellij.ide.highlighter.XHtmlFileType)1 XmlFileType (com.intellij.ide.highlighter.XmlFileType)1 ASTNode (com.intellij.lang.ASTNode)1 FileType (com.intellij.openapi.fileTypes.FileType)1 LanguageFileType (com.intellij.openapi.fileTypes.LanguageFileType)1 PsiFile (com.intellij.psi.PsiFile)1 XmlRecursiveElementVisitor (com.intellij.psi.XmlRecursiveElementVisitor)1 XmlRecursiveElementWalkingVisitor (com.intellij.psi.XmlRecursiveElementWalkingVisitor)1 FileIncludeInfo (com.intellij.psi.impl.include.FileIncludeInfo)1 OuterLanguageElement (com.intellij.psi.templateLanguages.OuterLanguageElement)1 XmlFile (com.intellij.psi.xml.XmlFile)1 XmlTag (com.intellij.psi.xml.XmlTag)1 IncorrectOperationException (com.intellij.util.IncorrectOperationException)1