1
1
from __future__ import annotations
2
2
3
+ import uuid
4
+ from dataclasses import dataclass
3
5
from typing import TYPE_CHECKING , Any , Awaitable , Callable , Dict , List , Optional
4
6
5
7
from ....jsonrpc2 .protocol import JsonRPCErrorException , rpc_method
23
25
_FUNC_TYPE = Callable [..., Awaitable [Optional [LSPAny ]]]
24
26
25
27
28
+ @dataclass
29
+ class CommandEntry :
30
+ name : str
31
+ callback : _FUNC_TYPE
32
+
33
+
26
34
class CommandsProtocolPart (LanguageServerProtocolPart , HasExtendCapabilities ):
27
35
28
36
_logger = LoggingDescriptor ()
29
37
38
+ PREFIX = f"{ uuid .uuid4 ()} "
39
+
30
40
def __init__ (self , parent : LanguageServerProtocol ) -> None :
31
41
super ().__init__ (parent )
32
- self .commands : Dict [str , _FUNC_TYPE ] = {}
42
+ self .commands : Dict [str , CommandEntry ] = {}
33
43
34
- def register (self , callback : _FUNC_TYPE , name : Optional [str ] = None ) -> None :
35
- command = name or get_command_name (callback )
44
+ def register (self , callback : _FUNC_TYPE , name : Optional [str ] = None ) -> CommandEntry :
45
+ name = name or get_command_name (callback )
46
+
47
+ command = f"{ self .PREFIX } .{ name } "
36
48
37
49
if command in self .commands :
38
50
self ._logger .critical (f"command '{ command } ' already registered." )
39
- return
51
+ else :
52
+ self .commands [command ] = CommandEntry (name , callback )
53
+
54
+ return self .commands [command ]
55
+
56
+ def get_command_name (self , callback : _FUNC_TYPE , name : Optional [str ] = None ) -> str :
57
+ name = name or get_command_name (callback )
40
58
41
- self .commands [ command ] = callback
59
+ return f" { self .PREFIX } . { name } "
42
60
43
61
def extend_capabilities (self , capabilities : ServerCapabilities ) -> None :
44
62
@@ -51,8 +69,8 @@ async def _workspace_execute_command(
51
69
) -> Optional [LSPAny ]:
52
70
self ._logger .info (f"execute command { command } " )
53
71
54
- callback = self .commands .get (command , None )
55
- if callback is None :
72
+ entry = self .commands .get (command , None )
73
+ if entry is None or entry . callback is None :
56
74
raise JsonRPCErrorException (ErrorCodes .INVALID_PARAMS , f"Command '{ command } ' unknown." )
57
75
58
- return await callback (* (arguments or ()))
76
+ return await entry . callback (* (arguments or ()))
0 commit comments