Class XMLPositionUtility

java.lang.Object
org.eclipse.lemminx.utils.XMLPositionUtility

public class XMLPositionUtility extends Object
XML position utility.
  • Method Details

    • selectAttributeName

      public static org.eclipse.lsp4j.Range selectAttributeName(DOMAttr attr)
      Returns the attribute name range and null otherwise.
      Parameters:
      attr - the attribute.
      Returns:
      the attribute name range and null otherwise.
    • selectAttributeNameAt

      public static org.eclipse.lsp4j.Range selectAttributeNameAt(int offset, DOMDocument document)
    • selectAttributeValue

      public static org.eclipse.lsp4j.Range selectAttributeValue(DOMAttr attr)
      Returns the attribute value range and null otherwise.
      Parameters:
      attr - the attribute.
      Returns:
      the attribute value range and null otherwise.
    • selectAttributeValue

      public static org.eclipse.lsp4j.Range selectAttributeValue(DOMAttr attr, boolean withoutQuote)
      Returns the attribute value range and null otherwise.
      Parameters:
      attr - the attribute.
      withoutQuote - true if range must remove the quote and false otherwise.
      Returns:
      the attribute value range and null otherwise.
    • selectAttributeValueAt

      public static org.eclipse.lsp4j.Range selectAttributeValueAt(String attrName, int offset, DOMDocument document)
    • selectAttributeValueAt

      public static org.eclipse.lsp4j.Range selectAttributeValueAt(String attrName, int offset, boolean withouQuote, DOMDocument document)
    • selectAttributeValueFromGivenValue

      public static org.eclipse.lsp4j.Range selectAttributeValueFromGivenValue(String attrValue, int offset, DOMDocument document)
    • selectAttributeValueByGivenValueAt

      public static org.eclipse.lsp4j.Range selectAttributeValueByGivenValueAt(String attrValue, int offset, DOMDocument document)
    • selectAttributeValueTokenAt

      public static org.eclipse.lsp4j.Range selectAttributeValueTokenAt(String attrName, String token, int offset, DOMDocument document)
      Returns the range of a specific token within a space-separated attribute value. Falls back to the whole attribute value range if the token is not found.

      Example: refs="a b" with token "a" → highlights just a.

      Parameters:
      attrName - the attribute name to search.
      token - the token to find within the attribute value.
      offset - the offset within the element.
      document - the DOM document.
      Returns:
      the range of the token, or the whole attribute value range.
    • selectAttributeNameFromGivenNameAt

      public static org.eclipse.lsp4j.Range selectAttributeNameFromGivenNameAt(String attrName, int offset, DOMDocument document)
    • selectAttributePrefixFromGivenNameAt

      public static org.eclipse.lsp4j.Range selectAttributePrefixFromGivenNameAt(String attrName, int offset, DOMDocument document)
      Returns the range of the prefix of an attribute name For example, if attrName = "xsi:example", the range for "xsi" will be returned
    • selectAttributeLocalPartFromGivenNameAt

      public static org.eclipse.lsp4j.Range selectAttributeLocalPartFromGivenNameAt(String attrName, int offset, DOMDocument document)
    • selectAttributeFromGivenNameAt

      public static org.eclipse.lsp4j.Range selectAttributeFromGivenNameAt(String attrName, int offset, DOMDocument document)
    • selectChildNodeAttributeValueFromGivenNameAt

      public static org.eclipse.lsp4j.Range selectChildNodeAttributeValueFromGivenNameAt(String childNodeName, String attrName, int offset, DOMDocument document)
      Returns the range of the attribute value of a specific child node, if it exists
      Parameters:
      childNodeName - the tag name of the child node/tag
      attrName - the attribute name
      offset - text offset from beginning of document
      document - the DOM document.
      Returns:
      the child node attribute value range and null otherwise.
    • selectAllAttributes

      public static org.eclipse.lsp4j.Range selectAllAttributes(int offset, DOMDocument document)
    • isInAttributeValue

      public static boolean isInAttributeValue(DOMDocument xmlDocument, org.eclipse.lsp4j.Position position)
      Returns true if the given position is within an attribute value, and false otherwise
      Parameters:
      xmlDocument - the model of the document
      position - the position to check
      Returns:
      true if the given position is within an attribute value, and false otherwise
    • selectRootAttributeValue

      public static org.eclipse.lsp4j.Range selectRootAttributeValue(String attrName, DOMDocument document)
      Finds the root element of the given document and returns the attribute value Range for the attribute attrName. If attrName is not declared then null is returned.
      Parameters:
      attrName - The name of the attribute to find the range of the value for
      document - The document to use the root element of
      Returns:
      The range in document where the declared value of attribute attrName resides (including quotations), or null if the attriubte is not declared.
    • selectChildEndTag

      public static org.eclipse.lsp4j.Range selectChildEndTag(String childTag, int offset, DOMDocument document)
    • findUnclosedParentElement

      public static DOMElement findUnclosedParentElement(DOMNode node, int offset)
      Finds the nearest parent element that is not closed at the given offset.
      Parameters:
      node - the starting node
      offset - the current cursor offset
      Returns:
      the unclosed parent element, or null if none found
    • selectRootStartTag

      public static org.eclipse.lsp4j.Range selectRootStartTag(DOMDocument document)
      Returns the range of the root start tag (excludes the 'invalid input: '<'') of the given document and null otherwise.
      Parameters:
      document - the DOM document.
      Returns:
      the range of the root start tag (excludes the 'invalid input: '<'') of the given document and null otherwise.
    • selectStartTagName

      public static org.eclipse.lsp4j.Range selectStartTagName(int offset, DOMDocument document)
    • selectStartTagName

      public static org.eclipse.lsp4j.Range selectStartTagName(DOMNode element)
      Returns the range of the start tag name (excludes the 'invalid input: '<'') of the given element and null otherwise.
      Parameters:
      element - the DOM element
      Returns:
      the range of the start tag of the given element and null otherwise.
    • selectStartTagLocalName

      public static org.eclipse.lsp4j.Range selectStartTagLocalName(DOMNode element)
      Returns the range of a tag's local name. If the tag does not have a prefix, implying it doesn't have a local name, it will return null.
      Parameters:
      element -
      Returns:
    • selectCurrentTagOffset

      public static int selectCurrentTagOffset(int offset, DOMDocument document)
    • selectEndTagName

      public static org.eclipse.lsp4j.Range selectEndTagName(int offset, DOMDocument document)
    • selectEndTagName

      public static org.eclipse.lsp4j.Range selectEndTagName(DOMElement element)
      Returns the range of the end tag of the given element name and null otherwise.
      Parameters:
      element - the DOM element
      Returns:
      the range of the end tag of the given element and null otherwise.
    • selectEndTagLocalName

      public static org.eclipse.lsp4j.Range selectEndTagLocalName(DOMElement element)
      Returns the range of the end tag of the given LOCAL element name and null otherwise.
      Parameters:
      element - the DOM element
      Returns:
      the range of the end tag of the given element and null otherwise.
    • selectEndTagName

      public static org.eclipse.lsp4j.Range selectEndTagName(DOMElement element, boolean localNameOnly)
      Returns the range of the end tag of the given element and null otherwise.
      Parameters:
      element - the DOM element
      Returns:
      the range of the end tag of the given element and null otherwise.
    • selectEntityReference

      public static XMLPositionUtility.EntityReferenceRange selectEntityReference(int offset, DOMDocument document)
      Returns the range of the entity reference in a text node (ex : &) and null otherwise.
      Parameters:
      offset - the offset
      document - the document
      Returns:
      the range of the entity reference in a text node (ex : &) and null otherwise.
    • selectEntityReference

      public static XMLPositionUtility.EntityReferenceRange selectEntityReference(int offset, DOMDocument document, boolean endsWithSemicolon)
      Returns the range of the entity reference in a text node (ex : &) and null otherwise.
      Parameters:
      offset - the offset
      document - the document
      endsWithSemicolon - true if the entity reference must end with ';' and false otherwise.
      Returns:
      the range of the entity reference in a text node (ex : &) and null otherwise.
    • getEntityReferenceStartOffset

      public static int getEntityReferenceStartOffset(CharSequence text, int offset)
      Returns the start offset of the entity reference (ex : invalid input: '&am'|p;) from the left of the given offset and -1 if no entity reference.
      Parameters:
      text - the XML content.
      offset - the offset.
      Returns:
      the start offset of the entity reference (ex : invalid input: '&am'|p;) from the left of the given offset and -1 if no entity reference.
    • getEntityReferenceEndOffset

      public static int getEntityReferenceEndOffset(CharSequence text, int offset)
      Returns the end offset of the entity reference (ex : invalid input: '&am'|p;) from the right of the given offset and -1 if no entity reference.
      Parameters:
      text - the XML content.
      offset - the offset.
      Returns:
      the end offset of the entity reference (ex : invalid input: '&am'|p;) from the right of the given offset and -1 if no entity reference.
    • selectFirstNonWhitespaceText

      public static org.eclipse.lsp4j.Range selectFirstNonWhitespaceText(int offset, DOMDocument document)
    • selectText

      public static org.eclipse.lsp4j.Range selectText(DOMText text)
      Returns the text content range and null otherwise.
      Parameters:
      text - the DOM text node..
      Returns:
      the text content range and null otherwise.
    • selectContent

      public static org.eclipse.lsp4j.Range selectContent(int offset, DOMDocument document)
    • selectTrimmedText

      public static org.eclipse.lsp4j.Range selectTrimmedText(int offset, DOMDocument document)
      Returns the range covering the trimmed text belonging to the node located at offset. This method assumes that the node located at offset only contains text. For example, if the node located at offset is: hello the returned range will cover only "hello".
      Parameters:
      offset -
      document -
      Returns:
      range covering the trimmed text belonging to the node located at offset
    • selectDTDElementDeclAt

      public static org.eclipse.lsp4j.Range selectDTDElementDeclAt(int offset, DOMDocument document)
    • getLastValidDTDDeclParameter

      public static org.eclipse.lsp4j.Range getLastValidDTDDeclParameter(int offset, DOMDocument document, boolean selectWholeParameter)
      Will give the range for the last VALID DTD Decl parameter at 'offset'. An unrecognized Parameter is not considered VALID, eg: "invalid input: '<!'ELEMENT elementName (content) UNRECOGNIZED_CONTENT" will give the range of 1 character length, after '(content)'
    • getLastValidDTDDeclParameter

      public static org.eclipse.lsp4j.Range getLastValidDTDDeclParameter(int offset, DOMDocument document)
    • getLastValidDTDDeclParameterOrUnrecognized

      public static org.eclipse.lsp4j.Range getLastValidDTDDeclParameterOrUnrecognized(int offset, DOMDocument document)
      Will give the range for the last VALID DTD Decl parameter at 'offset'. An unrecognized Parameter is not considered VALID, eg: invalid input: '<!'ELEMENT elementName (content) UNRECOGNIZED_CONTENT will give the range 1 character after '(content)'
    • getLastValidDTDDeclParameterOrUnrecognized

      public static org.eclipse.lsp4j.Range getLastValidDTDDeclParameterOrUnrecognized(int offset, DOMDocument document, boolean selectWholeParameter)
    • getLastDTDDeclParameter

      public static org.eclipse.lsp4j.Range getLastDTDDeclParameter(int offset, DOMDocument document)
      Will give the range for the last DTD Decl parameter at 'offset'. An unrecognized Parameter is considered as well. eg: invalid input: '<!'ELEMENT elementName (content) UNRECOGNIZED_CONTENT will give the range 1 character after '(content)'
    • selectDTDDeclTagNameAt

      public static org.eclipse.lsp4j.Range selectDTDDeclTagNameAt(int offset, DOMDocument document)
    • selectDTDDeclNameAt

      public static org.eclipse.lsp4j.Range selectDTDDeclNameAt(int offset, DOMDocument document)
      Returns the range of the declared name (e.g. 'root' in <!ELEMENT root ...>) for the DTD declaration node at the given offset.
      Parameters:
      offset - the offset within the DTD declaration.
      document - the DOM document.
      Returns:
      the range of the declared name, or null if not found.
    • selectAttlistAttributeAt

      public static org.eclipse.lsp4j.Range selectAttlistAttributeAt(String attrName, int offset, DOMDocument document)
      Returns the range of the given attribute name within the ATTLIST declaration at the given offset, searching both the root declaration and its internal children.
      Parameters:
      attrName - the attribute name to find.
      offset - the offset within the ATTLIST declaration.
      document - the DOM document.
      Returns:
      the range of the matching attribute name, or null if not found.
    • selectAttlistAttributeValueAt

      public static org.eclipse.lsp4j.Range selectAttlistAttributeValueAt(int offset, DOMDocument document)
      Returns the range of the attribute value (e.g. "four") in the ATTLIST declaration at the given offset.

      Example: <!ATTLIST root status (one|two|three) "four"> → highlights "four".

      Parameters:
      offset - the offset within the ATTLIST declaration.
      document - the DOM document.
      Returns:
      the range of the attribute value, or null if not found.
    • selectDuplicateEnumTokenAt

      public static org.eclipse.lsp4j.Range selectDuplicateEnumTokenAt(String tokenName, int offset, DOMDocument document)
      Returns the range of the second (duplicate) occurrence of a token in an ATTLIST enumeration type, including the preceding | for clean removal.

      Example: (one|two|one) with tokenName "one" → highlights |one (the duplicate).

      Parameters:
      tokenName - the duplicate token name to find.
      offset - the offset within the ATTLIST declaration.
      document - the DOM document.
      Returns:
      the range of the duplicate token, or null if not found.
    • selectDuplicateMixedContentType

      public static org.eclipse.lsp4j.Range selectDuplicateMixedContentType(String typeName, DTDElementDecl elementDecl, DOMDocument document)
      Returns the range of the second (duplicate) occurrence of typeName in a mixed content model, including the preceding | separator.
       <!ELEMENT root (#PCDATA|item|item)*>
                                    |--| ← returned range (without preceding '|')
       
      Parameters:
      typeName - the duplicate element type name (e.g. "item").
      elementDecl - the <!ELEMENT> declaration containing the mixed content model.
      document - the DOM document.
      Returns:
      the range covering |typeName for the second occurrence, or null if not found.
    • getElementDeclMissingContentOrCategory

      public static org.eclipse.lsp4j.Range getElementDeclMissingContentOrCategory(int offset, DOMDocument document)
      Returns the range of the last content parameter in a <!ELEMENT> declaration. For single-parameter declarations, returns the element name; for multi-parameter declarations, returns the content model.

      Example: <!ELEMENT root (bar> → highlights (bar.

    • selectParameterNameFromGivenName

      public static org.eclipse.lsp4j.Range selectParameterNameFromGivenName(String parameterName, DTDDeclNode declNode)
    • createRange

      public static org.eclipse.lsp4j.Range createRange(DOMRange range)
      Returns the range for the given node.
      Parameters:
      node - the node
      Returns:
      the range for the given node.
    • createRange

      public static org.eclipse.lsp4j.Range createRange(int startOffset, int endOffset, DOMDocument document)
    • createLocationLink

      public static org.eclipse.lsp4j.LocationLink createLocationLink(DOMRange origin, DOMRange target)
      Returns the location link for the given origin and target nodes.
      Parameters:
      origin - the origin node.
      target - the target node.
      Returns:
      the location link for the given origin and target nodes.
    • createSelectionRange

      public static org.eclipse.lsp4j.Range createSelectionRange(DOMRange origin)
      Returns the selection range for the given origin node.
      Parameters:
      origin - the origin node.
      Returns:
      the selection range for the given origin node.
    • createLocationLink

      public static org.eclipse.lsp4j.LocationLink createLocationLink(org.eclipse.lsp4j.Range origin, DOMRange target)
      Returns the location link for the given origin and target nodes.
      Parameters:
      origin - the origin node.
      target - the target node.
      Returns:
      the location link for the given origin and target nodes.
    • createLocationLink

      public static org.eclipse.lsp4j.LocationLink createLocationLink(org.eclipse.lsp4j.Range origin, TargetRange target)
      Returns the location link for the given origin and target nodes.
      Parameters:
      origin - the origin node.
      target - the target node.
      Returns:
      the location link for the given origin and target nodes.
    • createLocation

      public static org.eclipse.lsp4j.Location createLocation(DOMRange target)
      Returns the location for the given target node.
      Parameters:
      target - the target node.
      Returns:
      the location for the given target node.
    • createDocumentLink

      public static org.eclipse.lsp4j.DocumentLink createDocumentLink(DOMRange target, String location, boolean adjust) throws BadLocationException
      Create a document link
      Parameters:
      target - The range in the document that should be the link
      location - URI where the link should point
      adjust - true means the first and last character of target will not be a part of the link.
      Returns:
      A DocumentLink over the range target that points to location.
      Throws:
      BadLocationException - if target is out of the bounds of the document
    • selectFirstChild

      public static org.eclipse.lsp4j.Range selectFirstChild(int offset, DOMDocument document)
      Returns the range covering the first child of the node located at offset. Returns null if node is not a DOMElement, or if a node does not exist at offset.
      Parameters:
      offset -
      document -
      Returns:
      range covering the first child of the node located at offset
    • selectWholeTag

      public static org.eclipse.lsp4j.Range selectWholeTag(int offset, DOMDocument document)
    • doesTagCoverPosition

      public static boolean doesTagCoverPosition(org.eclipse.lsp4j.Range startTagRange, org.eclipse.lsp4j.Range endTagRange, org.eclipse.lsp4j.Position position)
    • covers

      public static boolean covers(org.eclipse.lsp4j.Range range, org.eclipse.lsp4j.Position position)
    • isBeforeOrEqual

      public static boolean isBeforeOrEqual(org.eclipse.lsp4j.Position pos1, org.eclipse.lsp4j.Position pos2)
    • getTagNameRange

      public static org.eclipse.lsp4j.Range getTagNameRange(TokenType tokenType, int startOffset, DOMDocument xmlDocument)
    • toLocation

      public static org.eclipse.lsp4j.Location toLocation(org.eclipse.lsp4j.LocationLink locationLink)
    • getMatchingTagPosition

      public static org.eclipse.lsp4j.Position getMatchingTagPosition(DOMDocument xmlDocument, org.eclipse.lsp4j.Position position)
    • selectValueWithoutQuote

      public static org.eclipse.lsp4j.Range selectValueWithoutQuote(DOMRange node)
      Select the value from the start/end node without quote. For the given attr value:

      it will return range without ".
      Parameters:
      node - the DOM node.
      Returns:
      the value from the start/end node without quote.