Search in sources :

Example 41 with TextRange

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

the class DomHardCoreTestCase method assertReference.

protected PsiReference assertReference(final GenericDomValue value, PsiElement resolveTo) {
    final XmlTagValue tagValue = value.getXmlTag().getValue();
    final TextRange textRange = tagValue.getTextRange();
    final String s = value.getStringValue();
    assertNotNull(s);
    final int i = tagValue.getText().indexOf(s);
    return assertReference(value, resolveTo, textRange.getStartOffset() + i + s.length());
}
Also used : XmlTagValue(com.intellij.psi.xml.XmlTagValue) TextRange(com.intellij.openapi.util.TextRange)

Example 42 with TextRange

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

the class XmlAttributeImpl method getReferences.

@NotNull
@Override
public PsiReference[] getReferences(@NotNull PsiReferenceService.Hints hints) {
    if (hints.offsetInElement != null) {
        XmlElement nameElement = getNameElement();
        if (nameElement == null || hints.offsetInElement > nameElement.getStartOffsetInParent() + nameElement.getTextLength()) {
            return PsiReference.EMPTY_ARRAY;
        }
    }
    final PsiReference[] referencesFromProviders = ReferenceProvidersRegistry.getReferencesFromProviders(this);
    PsiReference[] refs;
    if (isNamespaceDeclaration()) {
        refs = new PsiReference[referencesFromProviders.length + 1];
        final String localName = getLocalName();
        final String prefix = XmlUtil.findPrefixByQualifiedName(getName());
        final TextRange range = prefix.isEmpty() ? TextRange.from(getName().length(), 0) : TextRange.from(prefix.length() + 1, localName.length());
        refs[0] = new SchemaPrefixReference(this, range, localName, null);
    } else {
        final String prefix = getNamespacePrefix();
        if (!prefix.isEmpty() && !getLocalName().isEmpty()) {
            refs = new PsiReference[referencesFromProviders.length + 2];
            refs[0] = new SchemaPrefixReference(this, TextRange.from(0, prefix.length()), prefix, null);
            refs[1] = new XmlAttributeReference(this);
        } else {
            refs = new PsiReference[referencesFromProviders.length + 1];
            refs[0] = new XmlAttributeReference(this);
        }
    }
    System.arraycopy(referencesFromProviders, 0, refs, refs.length - referencesFromProviders.length, referencesFromProviders.length);
    return refs;
}
Also used : TextRange(com.intellij.openapi.util.TextRange) NotNull(org.jetbrains.annotations.NotNull)

Example 43 with TextRange

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

the class XmlAttributeImpl method getDisplayValue.

@Override
public String getDisplayValue() {
    String displayText = myDisplayText;
    if (displayText != null)
        return displayText;
    XmlAttributeValue value = getValueElement();
    if (value == null)
        return null;
    PsiElement firstChild = value.getFirstChild();
    if (firstChild == null)
        return null;
    ASTNode child = firstChild.getNode();
    TextRange valueTextRange = new TextRange(0, value.getTextLength());
    if (child != null && child.getElementType() == XmlTokenType.XML_ATTRIBUTE_VALUE_START_DELIMITER) {
        valueTextRange = new TextRange(child.getTextLength(), valueTextRange.getEndOffset());
        child = child.getTreeNext();
    }
    final TIntArrayList gapsStarts = new TIntArrayList();
    final TIntArrayList gapsShifts = new TIntArrayList();
    StringBuilder buffer = new StringBuilder(getTextLength());
    while (child != null) {
        final int start = buffer.length();
        IElementType elementType = child.getElementType();
        if (elementType == XmlTokenType.XML_ATTRIBUTE_VALUE_END_DELIMITER) {
            valueTextRange = new TextRange(valueTextRange.getStartOffset(), child.getTextRange().getStartOffset() - value.getTextRange().getStartOffset());
            break;
        }
        if (elementType == XmlTokenType.XML_CHAR_ENTITY_REF) {
            buffer.append(XmlUtil.getCharFromEntityRef(child.getText()));
        } else if (elementType == XmlElementType.XML_ENTITY_REF) {
            buffer.append(XmlUtil.getEntityValue((XmlEntityRef) child));
        } else {
            appendChildToDisplayValue(buffer, child);
        }
        int end = buffer.length();
        int originalLength = child.getTextLength();
        if (end - start != originalLength) {
            gapsStarts.add(start);
            gapsShifts.add(originalLength - (end - start));
        }
        child = child.getTreeNext();
    }
    int[] gapDisplayStarts = ArrayUtil.newIntArray(gapsShifts.size());
    int[] gapPhysicalStarts = ArrayUtil.newIntArray(gapsShifts.size());
    int currentGapsSum = 0;
    for (int i = 0; i < gapDisplayStarts.length; i++) {
        currentGapsSum += gapsShifts.get(i);
        gapDisplayStarts[i] = gapsStarts.get(i);
        gapPhysicalStarts[i] = gapDisplayStarts[i] + currentGapsSum;
    }
    myGapDisplayStarts = gapDisplayStarts;
    myGapPhysicalStarts = gapPhysicalStarts;
    myValueTextRange = valueTextRange;
    return myDisplayText = buffer.toString();
}
Also used : IElementType(com.intellij.psi.tree.IElementType) ASTNode(com.intellij.lang.ASTNode) TextRange(com.intellij.openapi.util.TextRange) TIntArrayList(gnu.trove.TIntArrayList)

Example 44 with TextRange

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

the class URLReference method handleElementRename.

@Override
public PsiElement handleElementRename(String newElementName) throws IncorrectOperationException {
    final TextRange textRangeInElement = getRangeInElement();
    final PsiElement elementToChange = myElement.findElementAt(textRangeInElement.getStartOffset());
    assert elementToChange != null;
    final ElementManipulator<PsiElement> manipulator = ElementManipulators.getManipulator(elementToChange);
    assert manipulator != null;
    final int offsetFromStart = myElement.getTextRange().getStartOffset() + textRangeInElement.getStartOffset() - elementToChange.getTextOffset();
    manipulator.handleContentChange(elementToChange, new TextRange(offsetFromStart, offsetFromStart + textRangeInElement.getLength()), newElementName);
    return myElement;
}
Also used : TextRange(com.intellij.openapi.util.TextRange)

Example 45 with TextRange

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

the class XmlAttributeLiteralEscaper method decode.

@Override
public boolean decode(@NotNull final TextRange rangeInsideHost, @NotNull StringBuilder outChars) {
    TextRange valueTextRange = myXmlAttribute.getValueTextRange();
    int startInDecoded = myXmlAttribute.physicalToDisplay(rangeInsideHost.getStartOffset() - valueTextRange.getStartOffset());
    int endInDecoded = myXmlAttribute.physicalToDisplay(rangeInsideHost.getEndOffset() - valueTextRange.getStartOffset());
    String displayValue = myXmlAttribute.getDisplayValue();
    //todo investigate IIOB http://www.jetbrains.net/jira/browse/IDEADEV-16796
    startInDecoded = startInDecoded < 0 ? 0 : startInDecoded > displayValue.length() ? displayValue.length() : startInDecoded;
    endInDecoded = endInDecoded < 0 ? 0 : endInDecoded > displayValue.length() ? displayValue.length() : endInDecoded;
    if (startInDecoded > endInDecoded)
        endInDecoded = startInDecoded;
    outChars.append(displayValue, startInDecoded, endInDecoded);
    return true;
}
Also used : 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