Package org.eclipse.lemminx.utils
Class XMLPositionUtility
java.lang.Object
org.eclipse.lemminx.utils.XMLPositionUtility
XML position utility.
-
Nested Class Summary
Nested Classes -
Method Summary
Modifier and TypeMethodDescriptionstatic booleancovers(org.eclipse.lsp4j.Range range, org.eclipse.lsp4j.Position position) static org.eclipse.lsp4j.DocumentLinkcreateDocumentLink(DOMRange target, String location, boolean adjust) Create a document linkstatic org.eclipse.lsp4j.LocationcreateLocation(DOMRange target) Returns the location for the giventargetnode.static org.eclipse.lsp4j.LocationLinkcreateLocationLink(DOMRange origin, DOMRange target) Returns the location link for the givenoriginandtargetnodes.static org.eclipse.lsp4j.LocationLinkcreateLocationLink(org.eclipse.lsp4j.Range origin, DOMRange target) Returns the location link for the givenoriginandtargetnodes.static org.eclipse.lsp4j.LocationLinkcreateLocationLink(org.eclipse.lsp4j.Range origin, TargetRange target) Returns the location link for the givenoriginandtargetnodes.static org.eclipse.lsp4j.RangecreateRange(int startOffset, int endOffset, DOMDocument document) static org.eclipse.lsp4j.RangecreateRange(DOMRange range) Returns the range for the givennode.static org.eclipse.lsp4j.RangecreateSelectionRange(DOMRange origin) Returns the selection range for the givenoriginnode.static booleandoesTagCoverPosition(org.eclipse.lsp4j.Range startTagRange, org.eclipse.lsp4j.Range endTagRange, org.eclipse.lsp4j.Position position) static DOMElementfindUnclosedParentElement(DOMNode node, int offset) Finds the nearest parent element that is not closed at the given offset.static org.eclipse.lsp4j.RangegetElementDeclMissingContentOrCategory(int offset, DOMDocument document) Returns the range of the last content parameter in a<!ELEMENT>declaration.static intgetEntityReferenceEndOffset(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.static intgetEntityReferenceStartOffset(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.static org.eclipse.lsp4j.RangegetLastDTDDeclParameter(int offset, DOMDocument document) Will give the range for the last DTD Decl parameter at 'offset'.static org.eclipse.lsp4j.RangegetLastValidDTDDeclParameter(int offset, DOMDocument document) static org.eclipse.lsp4j.RangegetLastValidDTDDeclParameter(int offset, DOMDocument document, boolean selectWholeParameter) Will give the range for the last VALID DTD Decl parameter at 'offset'.static org.eclipse.lsp4j.RangegetLastValidDTDDeclParameterOrUnrecognized(int offset, DOMDocument document) Will give the range for the last VALID DTD Decl parameter at 'offset'.static org.eclipse.lsp4j.RangegetLastValidDTDDeclParameterOrUnrecognized(int offset, DOMDocument document, boolean selectWholeParameter) static org.eclipse.lsp4j.PositiongetMatchingTagPosition(DOMDocument xmlDocument, org.eclipse.lsp4j.Position position) static org.eclipse.lsp4j.RangegetTagNameRange(TokenType tokenType, int startOffset, DOMDocument xmlDocument) static booleanisBeforeOrEqual(org.eclipse.lsp4j.Position pos1, org.eclipse.lsp4j.Position pos2) static booleanisInAttributeValue(DOMDocument xmlDocument, org.eclipse.lsp4j.Position position) Returns true if the given position is within an attribute value, and false otherwisestatic org.eclipse.lsp4j.RangeselectAllAttributes(int offset, DOMDocument document) static org.eclipse.lsp4j.RangeselectAttlistAttributeAt(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.static org.eclipse.lsp4j.RangeselectAttlistAttributeValueAt(int offset, DOMDocument document) Returns the range of the attribute value (e.g.static org.eclipse.lsp4j.RangeselectAttributeFromGivenNameAt(String attrName, int offset, DOMDocument document) static org.eclipse.lsp4j.RangeselectAttributeLocalPartFromGivenNameAt(String attrName, int offset, DOMDocument document) static org.eclipse.lsp4j.RangeselectAttributeName(DOMAttr attr) Returns the attribute name range and null otherwise.static org.eclipse.lsp4j.RangeselectAttributeNameAt(int offset, DOMDocument document) static org.eclipse.lsp4j.RangeselectAttributeNameFromGivenNameAt(String attrName, int offset, DOMDocument document) static org.eclipse.lsp4j.RangeselectAttributePrefixFromGivenNameAt(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 returnedstatic org.eclipse.lsp4j.RangeselectAttributeValue(DOMAttr attr) Returns the attribute value range and null otherwise.static org.eclipse.lsp4j.RangeselectAttributeValue(DOMAttr attr, boolean withoutQuote) Returns the attribute value range and null otherwise.static org.eclipse.lsp4j.RangeselectAttributeValueAt(String attrName, int offset, boolean withouQuote, DOMDocument document) static org.eclipse.lsp4j.RangeselectAttributeValueAt(String attrName, int offset, DOMDocument document) static org.eclipse.lsp4j.RangeselectAttributeValueByGivenValueAt(String attrValue, int offset, DOMDocument document) static org.eclipse.lsp4j.RangeselectAttributeValueFromGivenValue(String attrValue, int offset, DOMDocument document) static org.eclipse.lsp4j.RangeselectAttributeValueTokenAt(String attrName, String token, int offset, DOMDocument document) Returns the range of a specific token within a space-separated attribute value.static org.eclipse.lsp4j.RangeselectChildEndTag(String childTag, int offset, DOMDocument document) static org.eclipse.lsp4j.RangeselectChildNodeAttributeValueFromGivenNameAt(String childNodeName, String attrName, int offset, DOMDocument document) Returns the range of the attribute value of a specific child node, if it existsstatic org.eclipse.lsp4j.RangeselectContent(int offset, DOMDocument document) static intselectCurrentTagOffset(int offset, DOMDocument document) static org.eclipse.lsp4j.RangeselectDTDDeclNameAt(int offset, DOMDocument document) Returns the range of the declared name (e.g.static org.eclipse.lsp4j.RangeselectDTDDeclTagNameAt(int offset, DOMDocument document) static org.eclipse.lsp4j.RangeselectDTDElementDeclAt(int offset, DOMDocument document) static org.eclipse.lsp4j.RangeselectDuplicateEnumTokenAt(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.static org.eclipse.lsp4j.RangeselectDuplicateMixedContentType(String typeName, DTDElementDecl elementDecl, DOMDocument document) Returns the range of the second (duplicate) occurrence oftypeNamein a mixed content model, including the preceding|separator.static org.eclipse.lsp4j.RangeselectEndTagLocalName(DOMElement element) Returns the range of the end tag of the given LOCALelementname and null otherwise.static org.eclipse.lsp4j.RangeselectEndTagName(int offset, DOMDocument document) static org.eclipse.lsp4j.RangeselectEndTagName(DOMElement element) Returns the range of the end tag of the givenelementname and null otherwise.static org.eclipse.lsp4j.RangeselectEndTagName(DOMElement element, boolean localNameOnly) Returns the range of the end tag of the givenelementand null otherwise.selectEntityReference(int offset, DOMDocument document) Returns the range of the entity reference in a text node (ex : &) and null otherwise.selectEntityReference(int offset, DOMDocument document, boolean endsWithSemicolon) Returns the range of the entity reference in a text node (ex : &) and null otherwise.static org.eclipse.lsp4j.RangeselectFirstChild(int offset, DOMDocument document) Returns the range covering the first child of the node located at offset.static org.eclipse.lsp4j.RangeselectFirstNonWhitespaceText(int offset, DOMDocument document) static org.eclipse.lsp4j.RangeselectParameterNameFromGivenName(String parameterName, DTDDeclNode declNode) static org.eclipse.lsp4j.RangeselectRootAttributeValue(String attrName, DOMDocument document) Finds the root element of the given document and returns the attribute valueRangefor the attributeattrName.static org.eclipse.lsp4j.RangeselectRootStartTag(DOMDocument document) Returns the range of the root start tag (excludes the 'invalid input: '<'') of the givendocumentand null otherwise.static org.eclipse.lsp4j.RangeselectStartTagLocalName(DOMNode element) Returns the range of a tag's local name.static org.eclipse.lsp4j.RangeselectStartTagName(int offset, DOMDocument document) static org.eclipse.lsp4j.RangeselectStartTagName(DOMNode element) Returns the range of the start tag name (excludes the 'invalid input: '<'') of the givenelementand null otherwise.static org.eclipse.lsp4j.RangeselectText(DOMText text) Returns the text content range and null otherwise.static org.eclipse.lsp4j.RangeselectTrimmedText(int offset, DOMDocument document) Returns the range covering the trimmed text belonging to the node located at offset.static org.eclipse.lsp4j.RangeSelect the value from the start/end node without quote.static org.eclipse.lsp4j.RangeselectWholeTag(int offset, DOMDocument document) static org.eclipse.lsp4j.LocationtoLocation(org.eclipse.lsp4j.LocationLink locationLink)
-
Method Details
-
selectAttributeName
Returns the attribute name range and null otherwise.- Parameters:
attr- the attribute.- Returns:
- the attribute name range and null otherwise.
-
selectAttributeNameAt
-
selectAttributeValue
Returns the attribute value range and null otherwise.- Parameters:
attr- the attribute.- Returns:
- the attribute value range and null otherwise.
-
selectAttributeValue
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 justa.- 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/tagattrName- the attribute nameoffset- text offset from beginning of documentdocument- the DOM document.- Returns:
- the child node attribute value range and null otherwise.
-
selectAllAttributes
-
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 documentposition- 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 valueRangefor the attributeattrName. IfattrNameis not declared then null is returned.- Parameters:
attrName- The name of the attribute to find the range of the value fordocument- The document to use the root element of- Returns:
- The range in
documentwhere the declared value of attributeattrNameresides (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
Finds the nearest parent element that is not closed at the given offset.- Parameters:
node- the starting nodeoffset- the current cursor offset- Returns:
- the unclosed parent element, or null if none found
-
selectRootStartTag
Returns the range of the root start tag (excludes the 'invalid input: '<'') of the givendocumentand null otherwise.- Parameters:
document- the DOM document.- Returns:
- the range of the root start tag (excludes the 'invalid input: '<'') of the given
documentand null otherwise.
-
selectStartTagName
-
selectStartTagName
Returns the range of the start tag name (excludes the 'invalid input: '<'') of the givenelementand null otherwise.- Parameters:
element- the DOM element- Returns:
- the range of the start tag of the given
elementand null otherwise.
-
selectStartTagLocalName
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
-
selectEndTagName
-
selectEndTagName
Returns the range of the end tag of the givenelementname and null otherwise.- Parameters:
element- the DOM element- Returns:
- the range of the end tag of the given
elementand null otherwise.
-
selectEndTagLocalName
Returns the range of the end tag of the given LOCALelementname and null otherwise.- Parameters:
element- the DOM element- Returns:
- the range of the end tag of the given
elementand null otherwise.
-
selectEndTagName
Returns the range of the end tag of the givenelementand null otherwise.- Parameters:
element- the DOM element- Returns:
- the range of the end tag of the given
elementand 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 offsetdocument- 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 offsetdocument- the documentendsWithSemicolon- 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
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
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
Returns the text content range and null otherwise.- Parameters:
text- the DOM text node..- Returns:
- the text content range and null otherwise.
-
selectContent
-
selectTrimmedText
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
-
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
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
-
selectDTDDeclNameAt
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 oftypeNamein 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
|typeNamefor the second occurrence, ornullif 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
Returns the range for the givennode.- 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
Returns the location link for the givenoriginandtargetnodes.- Parameters:
origin- the origin node.target- the target node.- Returns:
- the location link for the given
originandtargetnodes.
-
createSelectionRange
Returns the selection range for the givenoriginnode.- Parameters:
origin- the origin node.- Returns:
- the selection range for the given
originnode.
-
createLocationLink
public static org.eclipse.lsp4j.LocationLink createLocationLink(org.eclipse.lsp4j.Range origin, DOMRange target) Returns the location link for the givenoriginandtargetnodes.- Parameters:
origin- the origin node.target- the target node.- Returns:
- the location link for the given
originandtargetnodes.
-
createLocationLink
public static org.eclipse.lsp4j.LocationLink createLocationLink(org.eclipse.lsp4j.Range origin, TargetRange target) Returns the location link for the givenoriginandtargetnodes.- Parameters:
origin- the origin node.target- the target node.- Returns:
- the location link for the given
originandtargetnodes.
-
createLocation
Returns the location for the giventargetnode.- Parameters:
target- the target node.- Returns:
- the location for the given
targetnode.
-
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 linklocation- URI where the link should pointadjust-truemeans the first and last character oftargetwill not be a part of the link.- Returns:
- A DocumentLink over the range
targetthat points tolocation. - Throws:
BadLocationException- iftargetis out of the bounds of the document
-
selectFirstChild
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
-
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
Select the value from the start/end node without quote. For the given attr value:
it will returnrange without ". - Parameters:
node- the DOM node.- Returns:
- the value from the start/end node without quote.
-