java.lang.Object
org.eclipse.lemminx.extensions.relaxng.grammar.rnc.utils.RNCPositionUtils

public class RNCPositionUtils extends Object
Utility methods for computing positions and ranges in RNC documents.

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 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 null if not found.
      Throws:
      BadLocationException - if offset conversion fails.
    • getEndOfDocumentPosition

      public static org.eclipse.lsp4j.Position getEndOfDocumentPosition(CharSequence source)
      Computes the LSP Position at 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

      public static boolean endsWithNewline(CharSequence source)
      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:
      true if the last character is '\n'.
    • getLineSeparator

      public static String getLineSeparator(TextDocument textDocument)
      Returns the line separator used in the document, falling back to System.lineSeparator() if the document is empty.
    • selectUri

      public static org.eclipse.lsp4j.Range selectUri(TextDocument document, RNCFileReferenceNode node)
      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 null if conversion fails.
    • createRange

      public static org.eclipse.lsp4j.Range createRange(TextDocument document, int startOffset, int endOffset)
      Creates an LSP Range from character offsets, or null if 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 null if conversion fails.
    • createRange

      public static org.eclipse.lsp4j.Range createRange(RNCRange range)
      Creates an LSP Range from an RNCRange (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 null if conversion fails.
    • getWordAt

      public static String getWordAt(CharSequence text, int offset)
      Returns the identifier word at the given offset, or null if 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 null if 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 null if 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:
      true if the character can appear in an RNC identifier.
    • dedent

      public static String dedent(String text)
      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.