Skip to content

Commit e544406

Browse files
committed
make commands unique for language server instances
1 parent 831b560 commit e544406

File tree

2 files changed

+29
-19
lines changed

2 files changed

+29
-19
lines changed

robotcode/language_server/common/parts/commands.py

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
from __future__ import annotations
22

3+
import uuid
4+
from dataclasses import dataclass
35
from typing import TYPE_CHECKING, Any, Awaitable, Callable, Dict, List, Optional
46

57
from ....jsonrpc2.protocol import JsonRPCErrorException, rpc_method
@@ -23,22 +25,38 @@
2325
_FUNC_TYPE = Callable[..., Awaitable[Optional[LSPAny]]]
2426

2527

28+
@dataclass
29+
class CommandEntry:
30+
name: str
31+
callback: _FUNC_TYPE
32+
33+
2634
class CommandsProtocolPart(LanguageServerProtocolPart, HasExtendCapabilities):
2735

2836
_logger = LoggingDescriptor()
2937

38+
PREFIX = f"{uuid.uuid4()}"
39+
3040
def __init__(self, parent: LanguageServerProtocol) -> None:
3141
super().__init__(parent)
32-
self.commands: Dict[str, _FUNC_TYPE] = {}
42+
self.commands: Dict[str, CommandEntry] = {}
3343

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}"
3648

3749
if command in self.commands:
3850
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)
4058

41-
self.commands[command] = callback
59+
return f"{self.PREFIX}.{name}"
4260

4361
def extend_capabilities(self, capabilities: ServerCapabilities) -> None:
4462

@@ -51,8 +69,8 @@ async def _workspace_execute_command(
5169
) -> Optional[LSPAny]:
5270
self._logger.info(f"execute command {command}")
5371

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:
5674
raise JsonRPCErrorException(ErrorCodes.INVALID_PARAMS, f"Command '{command}' unknown.")
5775

58-
return await callback(*(arguments or ()))
76+
return await entry.callback(*(arguments or ()))

robotcode/language_server/robotframework/parts/code_action.py

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,7 @@
1717
from ....utils.logging import LoggingDescriptor
1818
from ....utils.net import find_free_port
1919
from ....utils.uri import Uri
20-
from ...common.decorators import (
21-
code_action_kinds,
22-
command,
23-
get_command_name,
24-
language_id,
25-
)
20+
from ...common.decorators import code_action_kinds, command, language_id
2621
from ...common.lsp_types import (
2722
AnnotatedTextEdit,
2823
ChangeAnnotation,
@@ -292,7 +287,7 @@ async def collect(
292287
kind=CodeActionKinds.SOURCE + ".openDocumentation",
293288
command=Command(
294289
f"Translate Suite to {lang}",
295-
get_command_name(self.translate_suite),
290+
self.parent.commands.get_command_name(self.translate_suite),
296291
[document.document_uri, lang],
297292
),
298293
)
@@ -336,10 +331,7 @@ async def collect(
336331
CodeAction(
337332
"Create Keyword",
338333
kind=CodeActionKinds.QUICKFIX + ".createKeyword",
339-
command=Command(
340-
"Create Keyword",
341-
"robotcode.commingSoon",
342-
),
334+
command=Command("Create Keyword", self.parent.commands.get_command_name(self.comming_soon)),
343335
)
344336
]
345337

0 commit comments

Comments
 (0)