From ca1459450597e7e76cc340bad682653de404d7d8 Mon Sep 17 00:00:00 2001 From: Florian Wetschoreck Date: Tue, 4 Apr 2023 11:41:45 +0200 Subject: [PATCH 1/3] add comments to POIs --- pylsp/python_lsp.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/pylsp/python_lsp.py b/pylsp/python_lsp.py index 43f886cc..4e825ff6 100644 --- a/pylsp/python_lsp.py +++ b/pylsp/python_lsp.py @@ -411,6 +411,7 @@ def m_text_document__did_open(self, textDocument=None, **_kwargs): self.lint(textDocument['uri'], is_saved=True) def m_text_document__did_change(self, contentChanges=None, textDocument=None, **_kwargs): + # called for textDocument/didChange workspace = self._match_uri_to_workspace(textDocument['uri']) for change in contentChanges: workspace.update_document( @@ -418,6 +419,7 @@ def m_text_document__did_change(self, contentChanges=None, textDocument=None, ** change, version=textDocument.get('version') ) + # lint triggers a diagnostics message in a debounced fashion self.lint(textDocument['uri'], is_saved=False) def m_text_document__did_save(self, textDocument=None, **_kwargs): From 3a7de7a40cef37e8c1bcdb96d78cb60f7ef33fbc Mon Sep 17 00:00:00 2001 From: Florian Wetschoreck Date: Tue, 4 Apr 2023 12:09:11 +0200 Subject: [PATCH 2/3] add POIs --- pylsp/hookspecs.py | 1 + pylsp/plugins/hover.py | 1 + pylsp/python_lsp.py | 7 +++++++ pylsp/workspace.py | 4 +++- 4 files changed, 12 insertions(+), 1 deletion(-) diff --git a/pylsp/hookspecs.py b/pylsp/hookspecs.py index d1a2458e..7765b534 100644 --- a/pylsp/hookspecs.py +++ b/pylsp/hookspecs.py @@ -49,6 +49,7 @@ def pylsp_document_did_open(config, workspace, document): pass +# the method can be hooked into by plugins which will receive the args @hookspec def pylsp_document_did_save(config, workspace, document): pass diff --git a/pylsp/plugins/hover.py b/pylsp/plugins/hover.py index 6f8b3b76..6fa78dc1 100644 --- a/pylsp/plugins/hover.py +++ b/pylsp/plugins/hover.py @@ -11,6 +11,7 @@ @hookimpl def pylsp_hover(config, document, position): code_position = _utils.position_to_jedi_linecolumn(document, position) + # uses jedi to determine code positions definitions = document.jedi_script(use_document_path=True).infer(**code_position) word = document.word_at_position(position) diff --git a/pylsp/python_lsp.py b/pylsp/python_lsp.py index 4e825ff6..e4a821ac 100644 --- a/pylsp/python_lsp.py +++ b/pylsp/python_lsp.py @@ -145,6 +145,10 @@ async def run_server(): asyncio.run(run_server()) +# calls methods for a given message type e.g. m_text_document__did_change for textDocument/didChange +# the logic lives within pylsp_jsonrpc.MethodDispatcher + +# in general, a lot of the calling logic is contained within pylsp_jsonrpc e.g. Endpoint, JsonRpcStreamReader, JsonRpcStreamWriter class PythonLSPServer(MethodDispatcher): """ Implementation of the Microsoft VSCode Language Server Protocol https://github.com/Microsoft/language-server-protocol/blob/master/versions/protocol-1-x.md @@ -232,6 +236,7 @@ def _hook(self, hook_name, doc_uri=None, **kwargs): workspace = self._match_uri_to_workspace(doc_uri) doc = workspace.get_document(doc_uri) if doc_uri else None hook_handlers = self.config.plugin_manager.subset_hook_caller(hook_name, self.config.disabled_plugins) + # point where config, workspace, document are passed to hook handlerss return hook_handlers(config=self.config, workspace=workspace, document=doc, **kwargs) def capabilities(self): @@ -369,6 +374,7 @@ def highlight(self, doc_uri, position): return flatten(self._hook('pylsp_document_highlight', doc_uri, position=position)) or None def hover(self, doc_uri, position): + # might be involved in go to definition return self._hook('pylsp_hover', doc_uri, position=position) or {'contents': ''} @_utils.debounce(LINT_DEBOUNCE_S, keyed_by='doc_uri') @@ -382,6 +388,7 @@ def lint(self, doc_uri, is_saved): ) def references(self, doc_uri, position, exclude_declaration): + # might derive references for go to definition return flatten(self._hook( 'pylsp_references', doc_uri, position=position, exclude_declaration=exclude_declaration diff --git a/pylsp/workspace.py b/pylsp/workspace.py index ea7f55e8..ae026eab 100644 --- a/pylsp/workspace.py +++ b/pylsp/workspace.py @@ -32,7 +32,7 @@ def wrapper(self, *args, **kwargs): return method(self, *args, **kwargs) return wrapper - +# object that is available for plugins class Workspace: M_PUBLISH_DIAGNOSTICS = 'textDocument/publishDiagnostics' @@ -263,6 +263,7 @@ def close(self): self.__rope_autoimport.close() +# object that is available for plugins class Document: def __init__(self, uri, workspace, source=None, version=None, local=True, extra_sys_path=None, @@ -380,6 +381,7 @@ def jedi_names(self, all_scopes=False, definitions=True, references=False): @lock def jedi_script(self, position=None, use_document_path=False): + # get jedi.Script for Document which might be involved for go to definition extra_paths = [] environment_path = None env_vars = None From dcf1525cfbda8672c1a31a64ed530a7a647876bf Mon Sep 17 00:00:00 2001 From: Florian Wetschoreck Date: Tue, 4 Apr 2023 12:11:15 +0200 Subject: [PATCH 3/3] refactor --- pylsp/python_lsp.py | 2 +- pylsp/workspace.py | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/pylsp/python_lsp.py b/pylsp/python_lsp.py index e4a821ac..48dac945 100644 --- a/pylsp/python_lsp.py +++ b/pylsp/python_lsp.py @@ -236,7 +236,7 @@ def _hook(self, hook_name, doc_uri=None, **kwargs): workspace = self._match_uri_to_workspace(doc_uri) doc = workspace.get_document(doc_uri) if doc_uri else None hook_handlers = self.config.plugin_manager.subset_hook_caller(hook_name, self.config.disabled_plugins) - # point where config, workspace, document are passed to hook handlerss + # point where config, workspace, document are passed to hook handlers return hook_handlers(config=self.config, workspace=workspace, document=doc, **kwargs) def capabilities(self): diff --git a/pylsp/workspace.py b/pylsp/workspace.py index ae026eab..c42446c6 100644 --- a/pylsp/workspace.py +++ b/pylsp/workspace.py @@ -32,6 +32,7 @@ def wrapper(self, *args, **kwargs): return method(self, *args, **kwargs) return wrapper + # object that is available for plugins class Workspace: