Search in sources :

Example 86 with TextRange

use of com.intellij.openapi.util.TextRange 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 87 with TextRange

use of com.intellij.openapi.util.TextRange in project intellij-community by JetBrains.

the class HtmlSelectioner method getMinimalTextRangeLength.

@Override
public int getMinimalTextRangeLength(@NotNull PsiElement element, @NotNull CharSequence text, int cursorOffset) {
    if (WebEditorOptions.getInstance().isSelectWholeCssIdentifierOnDoubleClick()) {
        final XmlAttribute attribute = PsiTreeUtil.getParentOfType(element, XmlAttribute.class);
        final XmlAttributeValue attributeValue = PsiTreeUtil.getParentOfType(element, XmlAttributeValue.class);
        if (attribute != null && attributeValue != null) {
            if (HtmlUtil.CLASS_ATTRIBUTE_NAME.equalsIgnoreCase(attribute.getName())) {
                final TextRange valueTextRange = attributeValue.getValueTextRange();
                if (!valueTextRange.isEmpty()) {
                    int start = cursorOffset;
                    int end = cursorOffset;
                    while (start > valueTextRange.getStartOffset()) {
                        if (!JAVA_IDENTIFIER_AND_HYPHEN_CONDITION.value(text.charAt(start - 1))) {
                            break;
                        }
                        start--;
                    }
                    while (end < valueTextRange.getEndOffset()) {
                        if (!JAVA_IDENTIFIER_AND_HYPHEN_CONDITION.value(text.charAt(end + 1))) {
                            break;
                        }
                        end++;
                    }
                    return end - start;
                }
            }
        }
    }
    return super.getMinimalTextRangeLength(element, text, cursorOffset);
}
Also used : TextRange(com.intellij.openapi.util.TextRange) UnfairTextRange(com.intellij.openapi.util.UnfairTextRange)

Example 88 with TextRange

use of com.intellij.openapi.util.TextRange in project intellij-community by JetBrains.

the class HtmlSelectioner method select.

@Override
public List<TextRange> select(PsiElement e, @NotNull CharSequence editorText, int cursorOffset, @NotNull Editor editor) {
    List<TextRange> result;
    if (!(e instanceof XmlToken) || XmlTokenSelectioner.shouldSelectToken((XmlToken) e) || ((XmlToken) e).getTokenType() == XmlTokenType.XML_DATA_CHARACTERS) {
        result = super.select(e, editorText, cursorOffset, editor);
    } else {
        result = ContainerUtil.newArrayList();
    }
    final PsiElement parent = e.getParent();
    if (parent instanceof XmlComment) {
        result.addAll(expandToWholeLine(editorText, parent.getTextRange(), true));
    }
    PsiFile psiFile = e.getContainingFile();
    addAttributeSelection(result, editor, cursorOffset, editorText, e);
    final FileViewProvider fileViewProvider = psiFile.getViewProvider();
    for (Language lang : fileViewProvider.getLanguages()) {
        final PsiFile langFile = fileViewProvider.getPsi(lang);
        if (langFile != psiFile)
            addAttributeSelection(result, editor, cursorOffset, editorText, fileViewProvider.findElementAt(cursorOffset, lang));
    }
    EditorHighlighter highlighter = HighlighterFactory.createHighlighter(e.getProject(), psiFile.getVirtualFile());
    highlighter.setText(editorText);
    addTagSelection2(e, result);
    return result;
}
Also used : FileViewProvider(com.intellij.psi.FileViewProvider) Language(com.intellij.lang.Language) TextRange(com.intellij.openapi.util.TextRange) UnfairTextRange(com.intellij.openapi.util.UnfairTextRange) PsiFile(com.intellij.psi.PsiFile) PsiElement(com.intellij.psi.PsiElement) EditorHighlighter(com.intellij.openapi.editor.highlighter.EditorHighlighter)

Example 89 with TextRange

use of com.intellij.openapi.util.TextRange in project intellij-community by JetBrains.

the class HtmlSelectioner method addAttributeSelection.

private static void addAttributeSelection(@NotNull List<TextRange> result, @NotNull Editor editor, int cursorOffset, @NotNull CharSequence editorText, @Nullable PsiElement e) {
    final XmlAttribute attribute = PsiTreeUtil.getParentOfType(e, XmlAttribute.class);
    if (attribute != null) {
        result.add(attribute.getTextRange());
        final XmlAttributeValue value = attribute.getValueElement();
        if (value != null) {
            if (HtmlUtil.CLASS_ATTRIBUTE_NAME.equalsIgnoreCase(attribute.getName())) {
                addClassAttributeRanges(result, editor, cursorOffset, editorText, value);
            }
            final TextRange range = value.getTextRange();
            result.add(range);
            if (value.getFirstChild() != null && value.getFirstChild().getNode().getElementType() == XmlTokenType.XML_ATTRIBUTE_VALUE_START_DELIMITER) {
                result.add(new TextRange(range.getStartOffset() + 1, range.getEndOffset() - 1));
            }
        }
    }
}
Also used : TextRange(com.intellij.openapi.util.TextRange) UnfairTextRange(com.intellij.openapi.util.UnfairTextRange)

Example 90 with TextRange

use of com.intellij.openapi.util.TextRange in project intellij-community by JetBrains.

the class XmlEncodingReferenceProvider method extractFromContentAttribute.

public static PsiReference[] extractFromContentAttribute(final XmlAttributeValue value) {
    String text = value.getValue();
    int start = text.indexOf(CHARSET_PREFIX);
    if (start != -1) {
        start += CHARSET_PREFIX.length();
        int end = text.indexOf(';', start);
        if (end == -1)
            end = text.length();
        String charsetName = text.substring(start, end);
        TextRange textRange = new TextRange(start, end).shiftRight(xmlAttributeValueRange(value).getStartOffset());
        return new PsiReference[] { new XmlEncodingReference(value, charsetName, textRange, 0) };
    }
    return PsiReference.EMPTY_ARRAY;
}
Also used : PsiReference(com.intellij.psi.PsiReference) TextRange(com.intellij.openapi.util.TextRange)

Aggregations

TextRange (com.intellij.openapi.util.TextRange)1314 PsiElement (com.intellij.psi.PsiElement)261 NotNull (org.jetbrains.annotations.NotNull)237 Nullable (org.jetbrains.annotations.Nullable)167 Document (com.intellij.openapi.editor.Document)132 ArrayList (java.util.ArrayList)117 Project (com.intellij.openapi.project.Project)96 PsiFile (com.intellij.psi.PsiFile)96 ASTNode (com.intellij.lang.ASTNode)95 Editor (com.intellij.openapi.editor.Editor)75 PsiReference (com.intellij.psi.PsiReference)54 VirtualFile (com.intellij.openapi.vfs.VirtualFile)51 IElementType (com.intellij.psi.tree.IElementType)48 IncorrectOperationException (com.intellij.util.IncorrectOperationException)48 Pair (com.intellij.openapi.util.Pair)47 HighlightInfo (com.intellij.codeInsight.daemon.impl.HighlightInfo)33 ProperTextRange (com.intellij.openapi.util.ProperTextRange)32 FoldingDescriptor (com.intellij.lang.folding.FoldingDescriptor)31 XmlTag (com.intellij.psi.xml.XmlTag)31 List (java.util.List)31