Class RNCPositionUtils
Use case: when Jing reports an error at a character offset, these methods locate the corresponding token (identifier, string literal, etc.) to produce a precise diagnostic range that highlights the relevant word rather than a single character.
Example: Jing reports "reference to undefined pattern" at offset 42.
selectWord(org.eclipse.lemminx.commons.TextDocument, java.lang.String, int, java.lang.String) finds the identifier boundaries around offset 42 and
returns a range covering "myPattern" (columns 8–17).
-
Field Summary
Fields -
Method Summary
Modifier and TypeMethodDescriptionstatic org.eclipse.lsp4j.RangecreateRange(TextDocument document, int startOffset, int endOffset) Creates an LSPRangefrom character offsets, ornullif the offsets are invalid.static org.eclipse.lsp4j.RangecreateRange(RNCRange range) Creates an LSPRangefrom anRNCRange(parser error range).static StringRemoves common leading whitespace from all lines after the first.static booleanendsWithNewline(CharSequence source) Returns whether the document ends with a newline character.static org.eclipse.lsp4j.PositiongetEndOfDocumentPosition(CharSequence source) Computes the LSPPositionat the very end of the document.static StringgetLineSeparator(TextDocument textDocument) Returns the line separator used in the document, falling back toSystem.lineSeparator()if the document is empty.static StringgetWordAt(CharSequence text, int offset) Returns the identifier word at the given offset, ornullif the offset is not on an identifier character.static org.eclipse.lsp4j.RangegetWordRange(TextDocument textDocument, int offset, String word) Returns the LSP range of the identifier word at the given offset.static booleanisIdentifierChar(int ch) Returns whether the character is a valid RNC identifier character (letter, digit, underscore, hyphen, or dot).static org.eclipse.lsp4j.RangeselectNameBeforeBraces(TextDocument document, String text, int offset) Selects the element/attribute name that precedes a{ }block ending at or near the given offset.static org.eclipse.lsp4j.RangeselectToken(TextDocument document, String text, int offset) Selects the token (identifier or string literal) at the given offset.static org.eclipse.lsp4j.RangeselectUri(TextDocument document, RNCFileReferenceNode node) Returns the range of the URI string content (without quotes) inside an include or external reference node.static org.eclipse.lsp4j.RangeselectWord(TextDocument document, String text, int offset, String word) Selects the range of a known word near the given offset.
-
Field Details
-
ZERO_RANGE
public static final org.eclipse.lsp4j.Range ZERO_RANGE
-
-
Method Details
-
selectWord
public static org.eclipse.lsp4j.Range selectWord(TextDocument document, String text, int offset, String word) throws BadLocationException Selects the range of a known word near the given offset.Use case: Jing reports an error mentioning a specific identifier (e.g., "undefined pattern \"foo\""). This method searches for
"foo"near the error offset and returns its exact range for the diagnostic highlight.- Parameters:
document- the text document for offset-to-position conversion.text- the document text.offset- the approximate character offset of the error.word- the word to search for near the offset.- Returns:
- the range covering the word, or a fallback token range.
- Throws:
BadLocationException- if offset conversion fails.
-
selectToken
public static org.eclipse.lsp4j.Range selectToken(TextDocument document, String text, int offset) throws BadLocationException Selects the token (identifier or string literal) at the given offset.Use case: when no specific word is known, this method finds whatever token is at the error offset — an identifier, a quoted string, or a zero-width range if the offset is on whitespace.
- Parameters:
document- the text document for offset-to-position conversion.text- the document text.offset- the character offset to inspect.- Returns:
- the range covering the token at the offset.
- Throws:
BadLocationException- if offset conversion fails.
-
selectNameBeforeBraces
public static org.eclipse.lsp4j.Range selectNameBeforeBraces(TextDocument document, String text, int offset) throws BadLocationException Selects the element/attribute name that precedes a{ }block ending at or near the given offset.Use case: Jing reports "missing-pattern" at the end of an empty block
element root { }. This method scans backwards past the braces to select"root"for the diagnostic range.- Parameters:
document- the text document for offset-to-position conversion.text- the document text.offset- the character offset at or after the closing brace.- Returns:
- the range covering the name, or
nullif not found. - Throws:
BadLocationException- if offset conversion fails.
-
getEndOfDocumentPosition
Computes the LSPPositionat the very end of the document.Use case: code actions that append content (e.g., "Create define") need the end-of-document position to insert text at the bottom of the file.
- Parameters:
source- the document text as a CharSequence (avoids toString).- Returns:
- the position after the last character in the document.
-
endsWithNewline
Returns whether the document ends with a newline character.Use case: code actions that append content need to know whether to prepend a newline before the inserted text.
- Parameters:
source- the document text.- Returns:
trueif the last character is'\n'.
-
getLineSeparator
Returns the line separator used in the document, falling back toSystem.lineSeparator()if the document is empty. -
selectUri
Returns the range of the URI string content (without quotes) inside an include or external reference node.Use case: document links and definition targets need the range of the URI value (excluding the surrounding quotes) for clickable navigation.
- Parameters:
document- the text document for offset-to-position conversion.node- the include or external reference node.- Returns:
- the range of the URI content, or
nullif conversion fails.
-
createRange
public static org.eclipse.lsp4j.Range createRange(TextDocument document, int startOffset, int endOffset) Creates an LSPRangefrom character offsets, ornullif the offsets are invalid.- Parameters:
document- the text document for offset-to-position conversion.startOffset- the inclusive start offset.endOffset- the exclusive end offset.- Returns:
- the range, or
nullif conversion fails.
-
createRange
Creates an LSPRangefrom anRNCRange(parser error range).- Parameters:
range- the parser-reported range with start/end offsets and a reference to the owner document.- Returns:
- the LSP range, or
nullif conversion fails.
-
getWordAt
Returns the identifier word at the given offset, ornullif the offset is not on an identifier character.Use case: hover, definition, and references need to extract the word under the cursor to look up defines and keywords.
- Parameters:
text- the document text.offset- the character offset to inspect.- Returns:
- the word at the offset, or
nullif none found.
-
getWordRange
public static org.eclipse.lsp4j.Range getWordRange(TextDocument textDocument, int offset, String word) Returns the LSP range of the identifier word at the given offset.Use case: document highlighting and rename need the word range to know which text span to highlight or replace.
- Parameters:
textDocument- the text document.offset- the character offset within the word.word- unused (kept for API consistency).- Returns:
- the range of the word, or
nullif not on an identifier.
-
isIdentifierChar
public static boolean isIdentifierChar(int ch) Returns whether the character is a valid RNC identifier character (letter, digit, underscore, hyphen, or dot).- Parameters:
ch- the character to test.- Returns:
trueif the character can appear in an RNC identifier.
-
dedent
Removes common leading whitespace from all lines after the first.Use case: when extracting a pattern that is indented inside a define body, this normalises the extracted text to column 0 so it can be used as a top-level define body.
-