Search in sources :

Example 6 with XmlToken

use of com.intellij.psi.xml.XmlToken in project intellij-community by JetBrains.

the class XmlTokenSelectioner method select.

@Override
public List<TextRange> select(PsiElement e, CharSequence editorText, int cursorOffset, Editor editor) {
    XmlToken token = (XmlToken) e;
    if (shouldSelectToken(token)) {
        List<TextRange> ranges = super.select(e, editorText, cursorOffset, editor);
        SelectWordUtil.addWordSelection(editor.getSettings().isCamelWords(), editorText, cursorOffset, ranges);
        return ranges;
    } else {
        List<TextRange> result = new ArrayList<>();
        SelectWordUtil.addWordSelection(editor.getSettings().isCamelWords(), editorText, cursorOffset, result);
        return result;
    }
}
Also used : ArrayList(java.util.ArrayList) TextRange(com.intellij.openapi.util.TextRange) XmlToken(com.intellij.psi.xml.XmlToken)

Example 7 with XmlToken

use of com.intellij.psi.xml.XmlToken in project intellij-community by JetBrains.

the class TagDeclarationRangeHandler method getDeclarationRange.

@Override
@NotNull
public TextRange getDeclarationRange(@NotNull final PsiElement container) {
    XmlTag xmlTag = (XmlTag) container;
    int endOffset = xmlTag.getTextRange().getStartOffset();
    for (PsiElement child = xmlTag.getFirstChild(); child != null; child = child.getNextSibling()) {
        endOffset = child.getTextRange().getEndOffset();
        if (child instanceof XmlToken) {
            XmlToken token = (XmlToken) child;
            IElementType tokenType = token.getTokenType();
            if (tokenType == XmlTokenType.XML_EMPTY_ELEMENT_END || tokenType == XmlTokenType.XML_TAG_END)
                break;
        }
    }
    return new TextRange(xmlTag.getTextRange().getStartOffset(), endOffset);
}
Also used : IElementType(com.intellij.psi.tree.IElementType) TextRange(com.intellij.openapi.util.TextRange) PsiElement(com.intellij.psi.PsiElement) XmlTag(com.intellij.psi.xml.XmlTag) XmlToken(com.intellij.psi.xml.XmlToken) NotNull(org.jetbrains.annotations.NotNull)

Example 8 with XmlToken

use of com.intellij.psi.xml.XmlToken in project intellij-community by JetBrains.

the class DtdSelectioner method select.

@Override
public List<TextRange> select(PsiElement e, CharSequence editorText, int cursorOffset, Editor editor) {
    PsiElement[] children = e.getChildren();
    PsiElement first = null;
    PsiElement last = null;
    for (PsiElement child : children) {
        if (child instanceof XmlToken) {
            XmlToken token = (XmlToken) child;
            if (token.getTokenType() == XmlTokenType.XML_TAG_END) {
                last = token;
                break;
            }
            if (token.getTokenType() == XmlTokenType.XML_ELEMENT_DECL_START || token.getTokenType() == XmlTokenType.XML_ATTLIST_DECL_START) {
                first = token;
            }
        }
    }
    List<TextRange> result = new ArrayList<>(1);
    if (first != null && last != null) {
        final int offset = last.getTextRange().getEndOffset() + 1;
        result.addAll(ExtendWordSelectionHandlerBase.expandToWholeLine(editorText, new TextRange(first.getTextRange().getStartOffset(), offset < editorText.length() ? offset : editorText.length()), false));
    }
    return result;
}
Also used : ArrayList(java.util.ArrayList) TextRange(com.intellij.openapi.util.TextRange) PsiElement(com.intellij.psi.PsiElement) XmlToken(com.intellij.psi.xml.XmlToken)

Example 9 with XmlToken

use of com.intellij.psi.xml.XmlToken in project intellij-community by JetBrains.

the class XmlSurroundDescriptor method getElementsToSurround.

@Override
@NotNull
public PsiElement[] getElementsToSurround(PsiFile file, int startOffset, int endOffset) {
    final Pair<XmlTagChild, XmlTagChild> childrenInRange = XmlUtil.findTagChildrenInRange(file, startOffset, endOffset);
    if (childrenInRange == null) {
        final PsiElement elementAt = file.findElementAt(startOffset);
        if (elementAt instanceof XmlToken && ((XmlToken) elementAt).getTokenType() == XmlTokenType.XML_DATA_CHARACTERS) {
            return new PsiElement[] { elementAt };
        }
        return PsiElement.EMPTY_ARRAY;
    }
    List<PsiElement> result = new ArrayList<>();
    PsiElement first = childrenInRange.getFirst();
    PsiElement last = childrenInRange.getSecond();
    while (true) {
        result.add(first);
        if (first == last)
            break;
        first = first.getNextSibling();
    }
    return PsiUtilCore.toPsiElementArray(result);
}
Also used : ArrayList(java.util.ArrayList) XmlTagChild(com.intellij.psi.xml.XmlTagChild) PsiElement(com.intellij.psi.PsiElement) XmlToken(com.intellij.psi.xml.XmlToken) NotNull(org.jetbrains.annotations.NotNull)

Example 10 with XmlToken

use of com.intellij.psi.xml.XmlToken in project intellij-community by JetBrains.

the class DefaultXmlTagNameProvider method getRootTagsVariants.

private static List<LookupElement> getRootTagsVariants(final XmlTag tag, final List<LookupElement> elements) {
    elements.add(LookupElementBuilder.create("?xml version=\"1.0\" encoding=\"\" ?>").withPresentableText("<?xml version=\"1.0\" encoding=\"\" ?>").withInsertHandler(new InsertHandler<LookupElement>() {

        @Override
        public void handleInsert(InsertionContext context, LookupElement item) {
            int offset = context.getEditor().getCaretModel().getOffset();
            context.getEditor().getCaretModel().moveToOffset(offset - 4);
            AutoPopupController.getInstance(context.getProject()).scheduleAutoPopup(context.getEditor());
        }
    }));
    final FileBasedIndex fbi = FileBasedIndex.getInstance();
    Collection<String> result = new ArrayList<>();
    Processor<String> processor = Processors.cancelableCollectProcessor(result);
    fbi.processAllKeys(XmlNamespaceIndex.NAME, processor, tag.getProject());
    final GlobalSearchScope scope = new EverythingGlobalScope();
    for (final String ns : result) {
        if (ns.startsWith("file://"))
            continue;
        fbi.processValues(XmlNamespaceIndex.NAME, ns, null, new FileBasedIndex.ValueProcessor<XsdNamespaceBuilder>() {

            @Override
            public boolean process(final VirtualFile file, XsdNamespaceBuilder value) {
                List<String> tags = value.getRootTags();
                for (String s : tags) {
                    elements.add(LookupElementBuilder.create(s).withTypeText(ns).withInsertHandler(new XmlTagInsertHandler() {

                        @Override
                        public void handleInsert(InsertionContext context, LookupElement item) {
                            final Editor editor = context.getEditor();
                            final Document document = context.getDocument();
                            final int caretOffset = editor.getCaretModel().getOffset();
                            final RangeMarker caretMarker = document.createRangeMarker(caretOffset, caretOffset);
                            caretMarker.setGreedyToRight(true);
                            XmlFile psiFile = (XmlFile) context.getFile();
                            boolean incomplete = XmlUtil.getTokenOfType(tag, XmlTokenType.XML_TAG_END) == null && XmlUtil.getTokenOfType(tag, XmlTokenType.XML_EMPTY_ELEMENT_END) == null;
                            XmlNamespaceHelper.getHelper(psiFile).insertNamespaceDeclaration(psiFile, editor, Collections.singleton(ns), null, null);
                            editor.getCaretModel().moveToOffset(caretMarker.getEndOffset());
                            XmlTag rootTag = psiFile.getRootTag();
                            if (incomplete) {
                                XmlToken token = XmlUtil.getTokenOfType(rootTag, XmlTokenType.XML_EMPTY_ELEMENT_END);
                                // remove tag end added by smart attribute adder :(
                                if (token != null)
                                    token.delete();
                                PsiDocumentManager.getInstance(context.getProject()).doPostponedOperationsAndUnblockDocument(document);
                                super.handleInsert(context, item);
                            }
                        }
                    }));
                }
                return true;
            }
        }, scope);
    }
    return elements;
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) XmlFile(com.intellij.psi.xml.XmlFile) EverythingGlobalScope(com.intellij.psi.search.EverythingGlobalScope) XsdNamespaceBuilder(com.intellij.xml.index.XsdNamespaceBuilder) RangeMarker(com.intellij.openapi.editor.RangeMarker) InsertionContext(com.intellij.codeInsight.completion.InsertionContext) LookupElement(com.intellij.codeInsight.lookup.LookupElement) PrioritizedLookupElement(com.intellij.codeInsight.completion.PrioritizedLookupElement) XmlTagInsertHandler(com.intellij.codeInsight.completion.XmlTagInsertHandler) Document(com.intellij.openapi.editor.Document) XmlToken(com.intellij.psi.xml.XmlToken) GlobalSearchScope(com.intellij.psi.search.GlobalSearchScope) InsertHandler(com.intellij.codeInsight.completion.InsertHandler) XmlTagInsertHandler(com.intellij.codeInsight.completion.XmlTagInsertHandler) Editor(com.intellij.openapi.editor.Editor) FileBasedIndex(com.intellij.util.indexing.FileBasedIndex) XmlTag(com.intellij.psi.xml.XmlTag)

Aggregations

XmlToken (com.intellij.psi.xml.XmlToken)29 PsiElement (com.intellij.psi.PsiElement)18 XmlTag (com.intellij.psi.xml.XmlTag)12 TextRange (com.intellij.openapi.util.TextRange)6 IElementType (com.intellij.psi.tree.IElementType)6 Nullable (org.jetbrains.annotations.Nullable)5 NotNull (org.jetbrains.annotations.NotNull)4 ASTNode (com.intellij.lang.ASTNode)3 Document (com.intellij.openapi.editor.Document)3 PsiErrorElement (com.intellij.psi.PsiErrorElement)3 XmlAttribute (com.intellij.psi.xml.XmlAttribute)3 ArrayList (java.util.ArrayList)3 ResourceUrl (com.android.ide.common.resources.ResourceUrl)2 ResourceType (com.android.resources.ResourceType)2 JSClass (com.intellij.lang.javascript.psi.ecmal4.JSClass)2 Editor (com.intellij.openapi.editor.Editor)2 Project (com.intellij.openapi.project.Project)2 VirtualFile (com.intellij.openapi.vfs.VirtualFile)2 PsiFile (com.intellij.psi.PsiFile)2 HtmlTag (com.intellij.psi.html.HtmlTag)2