11from __future__ import annotations
2- from .._util import weak_method
2+ from ..api_wrapper_interface import ApiNotificationHandler
3+ from ..api_wrapper_interface import ApiRequestHandler
34from ..api_wrapper_interface import ApiWrapperInterface
45from ..server_resource_interface import ServerStatus
56from .api_decorator import register_decorated_handlers
67from .interface import ClientHandlerInterface
8+ from abc import ABCMeta
79from functools import partial
810from LSP .plugin import AbstractPlugin
911from LSP .plugin import ClientConfig
1618from LSP .plugin import WorkspaceFolder
1719from LSP .plugin .core .rpc import method2attr
1820from os import path
19- from typing import Any , Callable , Dict , List , Optional , Tuple , TypedDict
20- from weakref import ref
21+ from typing import Any , Callable
22+ from typing_extensions import override
23+ from weakref import WeakMethod , ref
2124import sublime
2225
2326__all__ = ['ClientHandler' ]
2427
25- LanguagesDict = TypedDict ('LanguagesDict' , {
26- 'document_selector' : Optional [str ],
27- 'languageId' : Optional [str ],
28- 'scopes' : Optional [List [str ]],
29- 'syntaxes' : Optional [List [str ]],
30- }, total = False )
31- ApiNotificationHandler = Callable [[Any ], None ]
32- ApiRequestHandler = Callable [[Any , Callable [[Any ], None ]], None ]
33-
3428
3529class ApiWrapper (ApiWrapperInterface ):
3630 def __init__ (self , plugin : 'ref[AbstractPlugin]' ):
3731 self .__plugin = plugin
3832
39- def __session (self ) -> Optional [ Session ] :
33+ def __session (self ) -> Session | None :
4034 plugin = self .__plugin ()
4135 return plugin .weaksession () if plugin else None
4236
4337 # --- ApiWrapperInterface -----------------------------------------------------------------------------------------
4438
39+ @override
4540 def on_notification (self , method : str , handler : ApiNotificationHandler ) -> None :
46- def handle_notification (weak_handler : ApiNotificationHandler , params : Any ) -> None :
47- weak_handler (params )
41+ def handle_notification (weak_handler : WeakMethod [ApiNotificationHandler ], params : Any ) -> None :
42+ if handler := weak_handler ():
43+ handler (params )
4844
4945 plugin = self .__plugin ()
5046 if plugin :
51- setattr (plugin , method2attr (method ), partial (handle_notification , weak_method (handler )))
47+ setattr (plugin , method2attr (method ), partial (handle_notification , WeakMethod (handler )))
5248
49+ @override
5350 def on_request (self , method : str , handler : ApiRequestHandler ) -> None :
5451 def send_response (request_id : Any , result : Any ) -> None :
5552 session = self .__session ()
5653 if session :
5754 session .send_response (Response (request_id , result ))
5855
59- def on_response (weak_handler : ApiRequestHandler , params : Any , request_id : Any ) -> None :
60- weak_handler (params , lambda result : send_response (request_id , result ))
56+ def on_response (weak_handler : WeakMethod [ApiRequestHandler ], params : Any , request_id : Any ) -> None :
57+ if handler := weak_handler ():
58+ handler (params , lambda result : send_response (request_id , result ))
6159
6260 plugin = self .__plugin ()
6361 if plugin :
64- setattr (plugin , method2attr (method ), partial (on_response , weak_method (handler )))
62+ setattr (plugin , method2attr (method ), partial (on_response , WeakMethod (handler )))
6563
64+ @override
6665 def send_notification (self , method : str , params : Any ) -> None :
6766 session = self .__session ()
6867 if session :
6968 session .send_notification (Notification (method , params ))
7069
70+ @override
7171 def send_request (self , method : str , params : Any , handler : Callable [[Any , bool ], None ]) -> None :
7272 session = self .__session ()
7373 if session :
@@ -77,41 +77,47 @@ def send_request(self, method: str, params: Any, handler: Callable[[Any, bool],
7777 handler (None , True )
7878
7979
80- class ClientHandler (AbstractPlugin , ClientHandlerInterface ):
80+ class ClientHandler (AbstractPlugin , ClientHandlerInterface , metaclass = ABCMeta ):
8181 """
8282 The base class for creating an LSP plugin.
8383 """
8484
8585 # --- AbstractPlugin handlers -------------------------------------------------------------------------------------
8686
8787 @classmethod
88+ @override
8889 def name (cls ) -> str :
8990 return cls .get_displayed_name ()
9091
9192 @classmethod
92- def configuration (cls ) -> Tuple [sublime .Settings , str ]:
93+ @override
94+ def configuration (cls ) -> tuple [sublime .Settings , str ]:
9395 return cls .read_settings ()
9496
9597 @classmethod
96- def additional_variables (cls ) -> Dict [str , str ]:
98+ @override
99+ def additional_variables (cls ) -> dict [str , str ]:
97100 return cls .get_additional_variables ()
98101
99102 @classmethod
103+ @override
100104 def needs_update_or_installation (cls ) -> bool :
101105 if cls .manages_server ():
102106 server = cls .get_server ()
103107 return bool (server and server .needs_installation ())
104108 return False
105109
106110 @classmethod
111+ @override
107112 def install_or_update (cls ) -> None :
108113 server = cls .get_server ()
109114 if server :
110115 server .install_or_update ()
111116
112117 @classmethod
118+ @override
113119 def can_start (cls , window : sublime .Window , initiating_view : sublime .View ,
114- workspace_folders : List [WorkspaceFolder ], configuration : ClientConfig ) -> Optional [ str ] :
120+ workspace_folders : list [WorkspaceFolder ], configuration : ClientConfig ) -> str | None :
115121 if cls .manages_server ():
116122 server = cls .get_server ()
117123 if not server or server .get_status () == ServerStatus .ERROR :
@@ -127,8 +133,9 @@ def can_start(cls, window: sublime.Window, initiating_view: sublime.View,
127133 return None
128134
129135 @classmethod
136+ @override
130137 def on_pre_start (cls , window : sublime .Window , initiating_view : sublime .View ,
131- workspace_folders : List [WorkspaceFolder ], configuration : ClientConfig ) -> Optional [ str ] :
138+ workspace_folders : list [WorkspaceFolder ], configuration : ClientConfig ) -> str | None :
132139 extra_paths = cls .get_additional_paths ()
133140 if extra_paths :
134141 original_path_raw = configuration .env .get ('PATH' ) or ''
@@ -147,10 +154,12 @@ def on_pre_start(cls, window: sublime.Window, initiating_view: sublime.View,
147154 # --- ClientHandlerInterface --------------------------------------------------------------------------------------
148155
149156 @classmethod
157+ @override
150158 def setup (cls ) -> None :
151159 register_plugin (cls )
152160
153161 @classmethod
162+ @override
154163 def cleanup (cls ) -> None :
155164 unregister_plugin (cls )
156165
0 commit comments