@@ -38,16 +38,17 @@ def update_sys_path(path_to_add: str, strategy: str) -> None:
3838# pylint: disable=wrong-import-position,import-error
3939import lsp_jsonrpc as jsonrpc
4040import lsp_utils as utils
41- import lsprotocol .types as lsp
42- from pygls import server , uris , workspace
41+ from lsprotocol import types as lsp
42+ from pygls import uris , workspace
43+ from pygls .lsp .server import LanguageServer
4344
4445WORKSPACE_SETTINGS = {}
4546GLOBAL_SETTINGS = {}
4647RUNNER = pathlib .Path (__file__ ).parent / "lsp_runner.py"
4748
4849MAX_WORKERS = 5
4950# TODO: Update the language server name and version.
50- LSP_SERVER = server . LanguageServer (
51+ LSP_SERVER = LanguageServer (
5152 name = "<pytool-display-name>" , version = "<server version>" , max_workers = MAX_WORKERS
5253)
5354
@@ -91,28 +92,34 @@ def update_sys_path(path_to_add: str, strategy: str) -> None:
9192@LSP_SERVER .feature (lsp .TEXT_DOCUMENT_DID_OPEN )
9293def did_open (params : lsp .DidOpenTextDocumentParams ) -> None :
9394 """LSP handler for textDocument/didOpen request."""
94- document = LSP_SERVER .workspace .get_document (params .text_document .uri )
95+ document = LSP_SERVER .workspace .get_text_document (params .text_document .uri )
9596 diagnostics : list [lsp .Diagnostic ] = _linting_helper (document )
96- LSP_SERVER .publish_diagnostics (document .uri , diagnostics )
97+ LSP_SERVER .text_document_publish_diagnostics (
98+ lsp .PublishDiagnosticsParams (uri = document .uri , diagnostics = diagnostics )
99+ )
97100
98101
99102@LSP_SERVER .feature (lsp .TEXT_DOCUMENT_DID_SAVE )
100103def did_save (params : lsp .DidSaveTextDocumentParams ) -> None :
101104 """LSP handler for textDocument/didSave request."""
102- document = LSP_SERVER .workspace .get_document (params .text_document .uri )
105+ document = LSP_SERVER .workspace .get_text_document (params .text_document .uri )
103106 diagnostics : list [lsp .Diagnostic ] = _linting_helper (document )
104- LSP_SERVER .publish_diagnostics (document .uri , diagnostics )
107+ LSP_SERVER .text_document_publish_diagnostics (
108+ lsp .PublishDiagnosticsParams (uri = document .uri , diagnostics = diagnostics )
109+ )
105110
106111
107112@LSP_SERVER .feature (lsp .TEXT_DOCUMENT_DID_CLOSE )
108113def did_close (params : lsp .DidCloseTextDocumentParams ) -> None :
109114 """LSP handler for textDocument/didClose request."""
110- document = LSP_SERVER .workspace .get_document (params .text_document .uri )
115+ document = LSP_SERVER .workspace .get_text_document (params .text_document .uri )
111116 # Publishing empty diagnostics to clear the entries for this file.
112- LSP_SERVER .publish_diagnostics (document .uri , [])
117+ LSP_SERVER .text_document_publish_diagnostics (
118+ lsp .PublishDiagnosticsParams (uri = document .uri , diagnostics = [])
119+ )
113120
114121
115- def _linting_helper (document : workspace .Document ) -> list [lsp .Diagnostic ]:
122+ def _linting_helper (document : workspace .TextDocument ) -> list [lsp .Diagnostic ]:
116123 # TODO: Determine if your tool supports passing file content via stdin.
117124 # If you want to support linting on change then your tool will need to
118125 # support linting over stdin to be effective. Read, and update
@@ -200,7 +207,7 @@ def formatting(params: lsp.DocumentFormattingParams) -> list[lsp.TextEdit] | Non
200207 # formatting support on save. You have to return an array of lsp.TextEdit
201208 # objects, to provide your formatted results.
202209
203- document = LSP_SERVER .workspace .get_document (params .text_document .uri )
210+ document = LSP_SERVER .workspace .get_text_document (params .text_document .uri )
204211 edits = _formatting_helper (document )
205212 if edits :
206213 return edits
@@ -210,7 +217,7 @@ def formatting(params: lsp.DocumentFormattingParams) -> list[lsp.TextEdit] | Non
210217 return None
211218
212219
213- def _formatting_helper (document : workspace .Document ) -> list [lsp .TextEdit ] | None :
220+ def _formatting_helper (document : workspace .TextDocument ) -> list [lsp .TextEdit ] | None :
214221 # TODO: For formatting on save support the formatter you use must support
215222 # formatting via stdin.
216223 # Read, and update_run_tool_on_document and _run_tool functions as needed
@@ -240,7 +247,7 @@ def _get_line_endings(lines: list[str]) -> str:
240247 return None
241248
242249
243- def _match_line_endings (document : workspace .Document , text : str ) -> str :
250+ def _match_line_endings (document : workspace .TextDocument , text : str ) -> str :
244251 """Ensures that the edited text line endings matches the document line endings."""
245252 expected = _get_line_endings (document .source .splitlines (keepends = True ))
246253 actual = _get_line_endings (text .splitlines (keepends = True ))
@@ -289,7 +296,7 @@ def on_shutdown(_params: Optional[Any] = None) -> None:
289296 jsonrpc .shutdown_json_rpc ()
290297
291298
292- def get_cwd (settings : dict , document : Optional [workspace .Document ]) -> str :
299+ def get_cwd (settings : dict , document : Optional [workspace .TextDocument ]) -> str :
293300 """Returns the working directory for running the tool.
294301
295302 Resolves the following VS Code file-related variable substitutions when
@@ -389,7 +396,7 @@ def _get_settings_by_path(file_path: pathlib.Path):
389396 return setting_values [0 ]
390397
391398
392- def _get_document_key (document : workspace .Document ):
399+ def _get_document_key (document : workspace .TextDocument ):
393400 if WORKSPACE_SETTINGS :
394401 document_workspace = pathlib .Path (document .path )
395402 workspaces = {s ["workspaceFS" ] for s in WORKSPACE_SETTINGS .values ()}
@@ -403,7 +410,7 @@ def _get_document_key(document: workspace.Document):
403410 return None
404411
405412
406- def _get_settings_by_document (document : workspace .Document | None ):
413+ def _get_settings_by_document (document : workspace .TextDocument | None ):
407414 if document is None or document .path is None :
408415 return list (WORKSPACE_SETTINGS .values ())[0 ]
409416
@@ -425,7 +432,7 @@ def _get_settings_by_document(document: workspace.Document | None):
425432# Internal execution APIs.
426433# *****************************************************
427434def _run_tool_on_document (
428- document : workspace .Document ,
435+ document : workspace .TextDocument ,
429436 use_stdin : bool = False ,
430437 extra_args : Optional [Sequence [str ]] = None ,
431438) -> utils .RunResult | None :
@@ -635,25 +642,37 @@ def _run_tool(extra_args: Sequence[str]) -> utils.RunResult:
635642def log_to_output (
636643 message : str , msg_type : lsp .MessageType = lsp .MessageType .Log
637644) -> None :
638- LSP_SERVER .show_message_log ( message , msg_type )
645+ LSP_SERVER .window_log_message ( lsp . LogMessageParams ( type = msg_type , message = message ) )
639646
640647
641648def log_error (message : str ) -> None :
642- LSP_SERVER .show_message_log (message , lsp .MessageType .Error )
649+ LSP_SERVER .window_log_message (
650+ lsp .LogMessageParams (type = lsp .MessageType .Error , message = message )
651+ )
643652 if os .getenv ("LS_SHOW_NOTIFICATION" , "off" ) in ["onError" , "onWarning" , "always" ]:
644- LSP_SERVER .show_message (message , lsp .MessageType .Error )
653+ LSP_SERVER .window_show_message (
654+ lsp .ShowMessageParams (type = lsp .MessageType .Error , message = message )
655+ )
645656
646657
647658def log_warning (message : str ) -> None :
648- LSP_SERVER .show_message_log (message , lsp .MessageType .Warning )
659+ LSP_SERVER .window_log_message (
660+ lsp .LogMessageParams (type = lsp .MessageType .Warning , message = message )
661+ )
649662 if os .getenv ("LS_SHOW_NOTIFICATION" , "off" ) in ["onWarning" , "always" ]:
650- LSP_SERVER .show_message (message , lsp .MessageType .Warning )
663+ LSP_SERVER .window_show_message (
664+ lsp .ShowMessageParams (type = lsp .MessageType .Warning , message = message )
665+ )
651666
652667
653668def log_always (message : str ) -> None :
654- LSP_SERVER .show_message_log (message , lsp .MessageType .Info )
669+ LSP_SERVER .window_log_message (
670+ lsp .LogMessageParams (type = lsp .MessageType .Info , message = message )
671+ )
655672 if os .getenv ("LS_SHOW_NOTIFICATION" , "off" ) in ["always" ]:
656- LSP_SERVER .show_message (message , lsp .MessageType .Info )
673+ LSP_SERVER .window_show_message (
674+ lsp .ShowMessageParams (type = lsp .MessageType .Info , message = message )
675+ )
657676
658677
659678# *****************************************************
0 commit comments