Search in sources :

Example 1 with UnwrapDescriptor

use of com.intellij.codeInsight.unwrap.UnwrapDescriptor in project intellij-community by JetBrains.

the class XmlUnwrapDescriptor method collectUnwrappers.

@Override
public List<Pair<PsiElement, Unwrapper>> collectUnwrappers(Project project, Editor editor, PsiFile file) {
    int offset = editor.getCaretModel().getOffset();
    PsiElement e1 = file.findElementAt(offset);
    if (e1 != null) {
        Language language = e1.getParent().getLanguage();
        if (language != file.getLanguage()) {
            UnwrapDescriptor unwrapDescriptor = LanguageUnwrappers.INSTANCE.forLanguage(language);
            if (unwrapDescriptor != null && !(unwrapDescriptor instanceof XmlUnwrapDescriptor)) {
                return unwrapDescriptor.collectUnwrappers(project, editor, file);
            }
        }
    }
    List<Pair<PsiElement, Unwrapper>> result = new ArrayList<>();
    FileViewProvider viewProvider = file.getViewProvider();
    for (Language language : viewProvider.getLanguages()) {
        UnwrapDescriptor unwrapDescriptor = LanguageUnwrappers.INSTANCE.forLanguage(language);
        if (unwrapDescriptor instanceof XmlUnwrapDescriptor) {
            PsiElement e = viewProvider.findElementAt(offset, language);
            PsiElement tag = PsiTreeUtil.getParentOfType(e, XmlTag.class);
            while (tag != null) {
                if (XmlChildRole.START_TAG_NAME_FINDER.findChild(tag.getNode()) != null) {
                    // Exclude implicit tags suck as 'jsp:root'
                    result.add(new Pair<>(tag, new XmlEnclosingTagUnwrapper()));
                }
                tag = PsiTreeUtil.getParentOfType(tag, XmlTag.class);
            }
        }
    }
    Collections.sort(result, (o1, o2) -> o2.first.getTextOffset() - o1.first.getTextOffset());
    return result;
}
Also used : Language(com.intellij.lang.Language) FileViewProvider(com.intellij.psi.FileViewProvider) ArrayList(java.util.ArrayList) PsiElement(com.intellij.psi.PsiElement) UnwrapDescriptor(com.intellij.codeInsight.unwrap.UnwrapDescriptor) Pair(com.intellij.openapi.util.Pair) XmlTag(com.intellij.psi.xml.XmlTag)

Aggregations

UnwrapDescriptor (com.intellij.codeInsight.unwrap.UnwrapDescriptor)1 Language (com.intellij.lang.Language)1 Pair (com.intellij.openapi.util.Pair)1 FileViewProvider (com.intellij.psi.FileViewProvider)1 PsiElement (com.intellij.psi.PsiElement)1 XmlTag (com.intellij.psi.xml.XmlTag)1 ArrayList (java.util.ArrayList)1