Class TextDocument

java.lang.Object
org.eclipse.lsp4j.TextDocumentItem
org.eclipse.lemminx.commons.TextDocument
Direct Known Subclasses:
ModelTextDocument

public class TextDocument extends org.eclipse.lsp4j.TextDocumentItem
Text document extends LSP4j TextDocumentItem to provide methods to retrieve position.
  • Constructor Summary

    Constructors
    Constructor
    Description
     
    TextDocument(org.eclipse.lsp4j.TextDocumentItem document)
     
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    Dispose this document, releasing all retained memory (text content and line tracker) for garbage collection.
    Deprecated.
    Use getTextSequence() instead to avoid allocating a full String copy of the document text.
    getText(int start, int end)
    Returns a String from the text content between the given start and end offsets.
    Returns the text content as a CharSequence, avoiding the allocation of a full String copy when the document is backed by a StringBuilder.
    org.eclipse.lsp4j.Range
    getWordRangeAt(int textOffset, Predicate<Character> isValidChar)
    Returns the Range of the word surrounding the given offset, where word boundaries are determined by the provided character predicate.
    org.eclipse.lsp4j.Range
    getWordRangeAt(int textOffset, Pattern wordDefinition)
    Returns the Range of the word at the given offset, where word boundaries are determined by the provided regex pattern.
    boolean
    Returns true if this document has been disposed and false otherwise.
    boolean
     
    int
    lineAt(int position)
    Returns the line number the character at the given offset belongs to.
    lineDelimiter(int lineNumber)
     
    int
    lineOffsetAt(int position)
     
    lineText(int lineNumber)
     
    int
    offsetAt(org.eclipse.lsp4j.Position position)
     
    org.eclipse.lsp4j.Position
    positionAt(int position)
     
    void
    setIncremental(boolean incremental)
     
    void
     
    void
    update(List<org.eclipse.lsp4j.TextDocumentContentChangeEvent> changes)
    Update text of the document by using the changes and according the incremental support.

    Methods inherited from class org.eclipse.lsp4j.TextDocumentItem

    equals, getLanguageId, getUri, getVersion, hashCode, setLanguageId, setUri, setVersion, toString

    Methods inherited from class java.lang.Object

    clone, finalize, getClass, notify, notifyAll, wait, wait, wait
  • Constructor Details

    • TextDocument

      public TextDocument(org.eclipse.lsp4j.TextDocumentItem document)
    • TextDocument

      public TextDocument(String text, String uri)
  • Method Details

    • setText

      public void setText(String text)
      Overrides:
      setText in class org.eclipse.lsp4j.TextDocumentItem
    • getText

      @Deprecated public String getText()
      Deprecated.
      Use getTextSequence() instead to avoid allocating a full String copy of the document text.
      Returns the text content as a String. Prefer getTextSequence() which avoids costly string materialization when the document is backed by a StringBuilder.
      Overrides:
      getText in class org.eclipse.lsp4j.TextDocumentItem
    • getTextSequence

      public CharSequence getTextSequence()
      Returns the text content as a CharSequence, avoiding the allocation of a full String copy when the document is backed by a StringBuilder.

      Thread-safety contract: The returned CharSequence references the live internal buffer and is valid until the next call to update(List). Callers must not retain or read the returned sequence across an update. In practice, the LSP protocol guarantees that textDocument/didChange (which calls update(List)) cancels any in-progress parse via CancelChecker before modifying the buffer, so concurrent reads cannot occur.

      Returns:
      the text content as a CharSequence.
    • getText

      public String getText(int start, int end)
      Returns a String from the text content between the given start and end offsets.
      Parameters:
      start - the start offset.
      end - the end offset.
      Returns:
      a String from the text content between the given start and end offsets.
    • setIncremental

      public void setIncremental(boolean incremental)
    • isIncremental

      public boolean isIncremental()
    • positionAt

      public org.eclipse.lsp4j.Position positionAt(int position) throws BadLocationException
      Throws:
      BadLocationException
    • offsetAt

      public int offsetAt(org.eclipse.lsp4j.Position position) throws BadLocationException
      Throws:
      BadLocationException
    • lineText

      public String lineText(int lineNumber) throws BadLocationException
      Throws:
      BadLocationException
    • lineOffsetAt

      public int lineOffsetAt(int position) throws BadLocationException
      Throws:
      BadLocationException
    • lineAt

      public int lineAt(int position) throws BadLocationException
      Returns the line number the character at the given offset belongs to.
      Parameters:
      position - the offset whose line number to be determined
      Returns:
      the number of the line the offset is on
      Throws:
      BadLocationException - if the offset is invalid in this tracker
    • lineDelimiter

      public String lineDelimiter(int lineNumber) throws BadLocationException
      Throws:
      BadLocationException
    • getWordRangeAt

      public org.eclipse.lsp4j.Range getWordRangeAt(int textOffset, Predicate<Character> isValidChar)
      Returns the Range of the word surrounding the given offset, where word boundaries are determined by the provided character predicate.

      Scans left from textOffset to find the start of the word, and right to find the end. Characters for which isValidChar returns true are considered part of the word.

      Use cases:

      • RNC hover/rename: finding the identifier under the cursor (letters, digits, _, -, .)
      • Any language service that needs a word range with custom word boundary rules
      Parameters:
      textOffset - the offset within the document text.
      isValidChar - predicate that returns true for characters that belong to the word.
      Returns:
      the range of the word, or null if the offset is invalid.
    • getWordRangeAt

      public org.eclipse.lsp4j.Range getWordRangeAt(int textOffset, Pattern wordDefinition)
      Returns the Range of the word at the given offset, where word boundaries are determined by the provided regex pattern.

      Extracts the line text up to the cursor, then uses the regex to find the last match that ends exactly at the cursor position. This variant is useful when word boundaries follow a regex pattern rather than a simple character predicate.

      Use cases:

      • XML element name detection: using ELEMENT_NAME_REGEX to match qualified names including namespace prefixes
      Parameters:
      textOffset - the offset within the document text.
      wordDefinition - regex pattern that matches valid words.
      Returns:
      the range of the matched word, or null if the offset is invalid.
    • dispose

      public void dispose()
      Dispose this document, releasing all retained memory (text content and line tracker) for garbage collection.
    • isDisposed

      public boolean isDisposed()
      Returns true if this document has been disposed and false otherwise.
      Returns:
      true if this document has been disposed and false otherwise.
    • update

      public void update(List<org.eclipse.lsp4j.TextDocumentContentChangeEvent> changes)
      Update text of the document by using the changes and according the incremental support.
      Parameters:
      changes - the text document changes.