Workspace¶
- class pygls.workspace.TextDocument(uri, source=None, version=None, language_id=None, local=True, sync_kind=TextDocumentSyncKind.Incremental, position_codec=None)¶
- Parameters:
- apply_change(change)¶
Apply a text change to a document, considering TextDocumentSyncKind
Performs either
Incremental,Full, or no synchronization based on both the client request and server capabilities.IncrementalversusFullsynchronizationEven if a server accepts
IncremantalSyncKinds, clients may request aFullSyncKind. In LSP 3.x, clients make this request by omitting both Range and RangeLength from their request. Consequently, the attributes “range” and “rangeLength” will be missing fromFullcontent update client requests in the pygls Python library.- Parameters:
change (TextDocumentContentChangePartial | TextDocumentContentChangeWholeDocument)
- Return type:
None
- client_position_at_offset(offset)¶
Convert a numeric character offset (index into self.source) into a line-column position in client units.
- offset_at_position(client_position)¶
Convert client_position to an index into self.source.
The index is the number of code points preceding the client_position in self.source.
Example in a code action request handler: selected_string = document.source[
document.offset_at_position(params.range.start) : document.offset_at_position(params.range.end) ]
- offset_at_server_position(server_position)¶
Convert server_position to an index into self.source.
The index is the number of code points preceding the client_position in self.source.
- Parameters:
server_position (ServerTextPosition)
- Return type:
- position_from_client_units(position)¶
Convert a position from client units into code points, suitable for indexing into
self.lines.- Parameters:
position (Position)
- Return type:
ServerTextPosition
- position_to_client_units(position)¶
Convert a position from code points into client units, suitable for sending to the client.
- Parameters:
position (ServerTextPosition)
- Return type:
- range_from_client_units(range)¶
Convert a range from client units into code points, suitable for indexing into
self.lines.- Parameters:
range (Range)
- Return type:
ServerTextRange
- range_to_client_units(range)¶
Convert a range from code points into client units, suitable for sending to the client.
- Parameters:
range (ServerTextRange)
- Return type:
- server_position_at_offset(offset)¶
Convert a numeric character offset (index into self.source) into a line-column position.
- Parameters:
offset (int)
- Return type:
ServerTextPosition
- text_in_client_range(range)¶
Given a range in client units, return the text in this range in this document.
- text_in_server_range(range)¶
Given a range in server units, return the text in this range in this document.
- Parameters:
range (ServerTextRange)
- Return type:
- word_at_position(client_position, re_start_word=re.compile('[A-Za-z_0-9]*$'), re_end_word=re.compile('^[A-Za-z_0-9]*'))¶
Return the word at position.
The word is constructed in two halves, the first half is found by taking the first match of
re_start_wordon the line up untilposition.character.The second half is found by taking
position.characterup until the last match ofre_end_wordon the line.re.findall()is used to find the matches.- Parameters:
position – The line and character offset.
re_start_word (Pattern[str]) – The regular expression for extracting the word backward from position. The default pattern is
[A-Za-z_0-9]*$.re_end_word (Pattern[str]) – The regular expression for extracting the word forward from position. The default pattern is
^[A-Za-z_0-9]*.client_position (Position)
- Returns:
The word (obtained by concatenating the two matches) at position.
- Return type:
- class pygls.workspace.Workspace(root_uri, sync_kind=TextDocumentSyncKind.Incremental, workspace_folders=None, position_encoding=PositionEncodingKind.Utf16)¶
- Parameters:
root_uri (str | None)
sync_kind (TextDocumentSyncKind)
workspace_folders (Sequence[WorkspaceFolder] | None)
position_encoding (PositionEncodingKind | str | None)
- get_notebook_document(*, notebook_uri=None, cell_uri=None)¶
Return the notebook corresponding with the given uri.
If both
notebook_uriandcell_uriare given,notebook_uritakes precedence.- Parameters:
- Returns:
The requested notebook document if found,
Noneotherwise.- Return type:
Optional[NotebookDocument]
- get_text_document(doc_uri)¶
Return a managed document if-present, else create one pointing at disk.
See https://github.com/Microsoft/language-server-protocol/issues/177
- Parameters:
doc_uri (str)
- Return type:
- put_text_document(text_document, notebook_uri=None)¶
Add a text document to the workspace.
- Parameters:
text_document (TextDocumentItem) – The text document to add
notebook_uri (str | None) – If set, indicates that this text document represents a cell in a notebook document