Class TextDocument
- Direct Known Subclasses:
ModelTextDocument
TextDocumentItem to provide methods to
retrieve position.-
Constructor Summary
ConstructorsConstructorDescriptionTextDocument(String text, String uri) TextDocument(org.eclipse.lsp4j.TextDocumentItem document) -
Method Summary
Modifier and TypeMethodDescriptionvoiddispose()Dispose this document, releasing all retained memory (text content and line tracker) for garbage collection.getText()Deprecated.getText(int start, int end) Returns the text content as aCharSequence, avoiding the allocation of a fullStringcopy when the document is backed by aStringBuilder.org.eclipse.lsp4j.RangegetWordRangeAt(int textOffset, Predicate<Character> isValidChar) Returns theRangeof the word surrounding the given offset, where word boundaries are determined by the provided character predicate.org.eclipse.lsp4j.RangegetWordRangeAt(int textOffset, Pattern wordDefinition) Returns theRangeof the word at the given offset, where word boundaries are determined by the provided regex pattern.booleanReturns true if this document has been disposed and false otherwise.booleanintlineAt(int position) Returns the line number the character at the given offset belongs to.lineDelimiter(int lineNumber) intlineOffsetAt(int position) lineText(int lineNumber) intoffsetAt(org.eclipse.lsp4j.Position position) org.eclipse.lsp4j.PositionpositionAt(int position) voidsetIncremental(boolean incremental) voidvoidUpdate 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
-
Constructor Details
-
TextDocument
public TextDocument(org.eclipse.lsp4j.TextDocumentItem document) -
TextDocument
-
-
Method Details
-
setText
- Overrides:
setTextin classorg.eclipse.lsp4j.TextDocumentItem
-
getText
Deprecated.UsegetTextSequence()instead to avoid allocating a full String copy of the document text.Returns the text content as aString. PrefergetTextSequence()which avoids costly string materialization when the document is backed by aStringBuilder.- Overrides:
getTextin classorg.eclipse.lsp4j.TextDocumentItem
-
getTextSequence
Returns the text content as aCharSequence, avoiding the allocation of a fullStringcopy when the document is backed by aStringBuilder.Thread-safety contract: The returned
CharSequencereferences the live internal buffer and is valid until the next call toupdate(List). Callers must not retain or read the returned sequence across an update. In practice, the LSP protocol guarantees thattextDocument/didChange(which callsupdate(List)) cancels any in-progress parse viaCancelCheckerbefore modifying the buffer, so concurrent reads cannot occur.- Returns:
- the text content as a
CharSequence.
-
getText
- Parameters:
start- the start offset.end- the end offset.- Returns:
- a
Stringfrom the text content between the givenstartandendoffsets.
-
setIncremental
public void setIncremental(boolean incremental) -
isIncremental
public boolean isIncremental() -
positionAt
- Throws:
BadLocationException
-
offsetAt
- Throws:
BadLocationException
-
lineText
- Throws:
BadLocationException
-
lineOffsetAt
- Throws:
BadLocationException
-
lineAt
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
- Throws:
BadLocationException
-
getWordRangeAt
Returns theRangeof the word surrounding the given offset, where word boundaries are determined by the provided character predicate.Scans left from
textOffsetto find the start of the word, and right to find the end. Characters for whichisValidCharreturnstrueare 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 returnstruefor characters that belong to the word.- Returns:
- the range of the word, or
nullif the offset is invalid.
- RNC hover/rename: finding the identifier under the cursor
(letters, digits,
-
getWordRangeAt
Returns theRangeof 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_REGEXto 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
nullif the offset is invalid.
- XML element name detection: using
-
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
Update text of the document by using the changes and according the incremental support.- Parameters:
changes- the text document changes.
-
getTextSequence()instead to avoid allocating a full String copy of the document text.