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 (Union[TextDocumentContentChangeEvent_Type1, TextDocumentContentChangeEvent_Type2]) –

Return type:

None

offset_at_position(client_position)

Return the character offset pointed at by the given client_position.

Parameters:

client_position (Position) –

Return type:

int

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 (Optional[str]) – If given, return the notebook document with the given uri.

  • cell_uri (Optional[str]) – 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 (Optional[str]) – If set, indicates that this text document represents a cell in a notebook document