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.

Incremental versus Full synchronization

Even if a server accepts Incremantal SyncKinds, clients may request a Full SyncKind. 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 from Full content 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.

Parameters:

offset (int)

Return type:

Position

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) ]

Parameters:

client_position (Position)

Return type:

int

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:

int

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:

Position

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:

Range

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.

Parameters:

range (Range)

Return type:

str

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:

str

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_word on the line up until position.character.

The second half is found by taking position.character up until the last match of re_end_word on 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:

str

class pygls.workspace.Workspace(root_uri, sync_kind=TextDocumentSyncKind.Incremental, workspace_folders=None, position_encoding=PositionEncodingKind.Utf16)
Parameters:
get_notebook_document(*, notebook_uri=None, cell_uri=None)

Return the notebook corresponding with the given uri.

If both notebook_uri and cell_uri are given, notebook_uri takes precedence.

Parameters:
  • notebook_uri (str | None) – If given, return the notebook document with the given uri.

  • cell_uri (str | None) – If given, return the notebook document which contains a cell with the given uri

Returns:

The requested notebook document if found, None otherwise.

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:

TextDocument

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