Skip to content

Commit dd6202a

Browse files
committed
fix: Source actions are missing in the context menu for versions #129
1 parent 9f14ce0 commit dd6202a

File tree

2 files changed

+76
-20
lines changed

2 files changed

+76
-20
lines changed

packages/language_server/robotcode/language_server/robotframework/parts/code_action_documentation.py

Lines changed: 73 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -21,22 +21,39 @@
2121
from robotcode.core.utils.net import find_free_port
2222
from robotcode.jsonrpc2.protocol import rpc_method
2323
from robotcode.language_server.common.decorators import code_action_kinds, language_id
24-
from robotcode.language_server.common.lsp_types import CodeAction, CodeActionContext, CodeActionKind, Command, Range
24+
from robotcode.language_server.common.lsp_types import (
25+
CodeAction,
26+
CodeActionContext,
27+
CodeActionKind,
28+
Command,
29+
Range,
30+
)
2531
from robotcode.language_server.common.text_document import TextDocument
26-
from robotcode.language_server.robotframework.configuration import DocumentationServerConfig
32+
from robotcode.language_server.robotframework.configuration import (
33+
DocumentationServerConfig,
34+
)
2735
from robotcode.language_server.robotframework.diagnostics.library_doc import (
2836
get_library_doc,
2937
get_robot_library_html_doc_str,
3038
resolve_robot_variables,
3139
)
32-
from robotcode.language_server.robotframework.diagnostics.namespace import LibraryEntry, Namespace
33-
from robotcode.language_server.robotframework.utils.ast_utils import Token, get_node_at_position, range_from_token
40+
from robotcode.language_server.robotframework.diagnostics.namespace import (
41+
LibraryEntry,
42+
Namespace,
43+
)
44+
from robotcode.language_server.robotframework.utils.ast_utils import (
45+
Token,
46+
get_node_at_position,
47+
range_from_token,
48+
)
3449

3550
from .model_helper import ModelHelperMixin
3651
from .protocol_part import RobotLanguageServerProtocolPart
3752

3853
if TYPE_CHECKING:
39-
from robotcode.language_server.robotframework.protocol import RobotLanguageServerProtocol # pragma: no cover
54+
from robotcode.language_server.robotframework.protocol import (
55+
RobotLanguageServerProtocol,
56+
)
4057

4158

4259
@dataclass(repr=False)
@@ -110,7 +127,9 @@ def do_GET(self) -> None: # noqa: N802
110127
try:
111128
if type_ in ["md", "markdown"]:
112129
libdoc = get_library_doc(
113-
name, tuple(args.split("::") if args else ()), base_dir=basedir if basedir else "."
130+
name,
131+
tuple(args.split("::") if args else ()),
132+
base_dir=basedir if basedir else ".",
114133
)
115134

116135
def calc_md() -> str:
@@ -149,7 +168,9 @@ def calc_md() -> str:
149168
self.wfile.write(
150169
bytes(
151170
HTML_ERROR_TEMPLATE.substitute(
152-
type=type(e).__qualname__, message=str(e), stacktrace="".join(traceback.format_exc())
171+
type=type(e).__qualname__,
172+
message=str(e),
173+
stacktrace="".join(traceback.format_exc()),
153174
),
154175
"utf-8",
155176
)
@@ -167,7 +188,7 @@ def server_bind(self) -> None:
167188
return super().server_bind()
168189

169190

170-
CODEACTIONKINDS_SOURCE_OPENDOCUMENTATION = f"{CodeActionKind.SOURCE}.openDocumentation"
191+
CODEACTIONKINDS_SOURCE_OPENDOCUMENTATION = f"{CodeActionKind.SOURCE.value}.openDocumentation"
171192

172193

173194
class RobotCodeActionDocumentationProtocolPart(RobotLanguageServerProtocolPart, ModelHelperMixin):
@@ -229,7 +250,11 @@ async def _ensure_http_server_started(self) -> None:
229250
)
230251
@_logger.call
231252
async def collect(
232-
self, sender: Any, document: TextDocument, range: Range, context: CodeActionContext
253+
self,
254+
sender: Any,
255+
document: TextDocument,
256+
range: Range,
257+
context: CodeActionContext,
233258
) -> Optional[List[Union[Command, CodeAction]]]:
234259
from robot.parsing.lexer import Token as RobotToken
235260
from robot.parsing.model.statements import (
@@ -248,9 +273,14 @@ async def collect(
248273
node = await get_node_at_position(model, range.start)
249274

250275
if context.only and isinstance(node, (LibraryImport, ResourceImport)):
251-
if CodeActionKind.SOURCE in context.only and range in range_from_token(node.get_token(RobotToken.NAME)):
276+
if CodeActionKind.SOURCE.value in context.only and range in range_from_token(
277+
node.get_token(RobotToken.NAME)
278+
):
252279
url = await self.build_url(
253-
node.name, node.args if isinstance(node, LibraryImport) else (), document, namespace
280+
node.name,
281+
node.args if isinstance(node, LibraryImport) else (),
282+
document,
283+
namespace,
254284
)
255285

256286
return [self.open_documentation_code_action(url)]
@@ -264,7 +294,10 @@ async def collect(
264294
else node.keyword
265295
if isinstance(node, KeywordCall)
266296
else node.name,
267-
cast(Token, node.get_token(RobotToken.KEYWORD if isinstance(node, KeywordCall) else RobotToken.NAME)),
297+
cast(
298+
Token,
299+
node.get_token(RobotToken.KEYWORD if isinstance(node, KeywordCall) else RobotToken.NAME),
300+
),
268301
[cast(Token, t) for t in node.get_tokens(RobotToken.ARGUMENT)],
269302
namespace,
270303
range.start,
@@ -277,7 +310,7 @@ async def collect(
277310
kw_doc, _ = result
278311

279312
if kw_doc is not None:
280-
if context.only and CodeActionKind.SOURCE in context.only:
313+
if context.only and CodeActionKind.SOURCE.value in context.only:
281314
entry: Optional[LibraryEntry] = None
282315

283316
if kw_doc.libtype == "LIBRARY":
@@ -302,19 +335,35 @@ async def collect(
302335

303336
self_libdoc = await namespace.get_library_doc()
304337
if entry is None and self_libdoc.digest == kw_doc.parent:
305-
entry = LibraryEntry(self_libdoc.name, str(document.uri.to_path().name), self_libdoc)
338+
entry = LibraryEntry(
339+
self_libdoc.name,
340+
str(document.uri.to_path().name),
341+
self_libdoc,
342+
)
306343

307344
if entry is None:
308345
return None
309346

310-
url = await self.build_url(entry.import_name, entry.args, document, namespace, kw_doc.name)
347+
url = await self.build_url(
348+
entry.import_name,
349+
entry.args,
350+
document,
351+
namespace,
352+
kw_doc.name,
353+
)
311354

312355
return [self.open_documentation_code_action(url)]
313356

314357
if isinstance(node, KeywordName):
315358
name_token = node.get_token(RobotToken.KEYWORD_NAME)
316359
if name_token is not None and range in range_from_token(name_token):
317-
url = await self.build_url(str(document.uri.to_path().name), (), document, namespace, name_token.value)
360+
url = await self.build_url(
361+
str(document.uri.to_path().name),
362+
(),
363+
document,
364+
namespace,
365+
name_token.value,
366+
)
318367

319368
return [self.open_documentation_code_action(url)]
320369

@@ -366,7 +415,14 @@ async def build_url(
366415
url_args = "::".join(args) if args else ""
367416

368417
base_url = f"http://localhost:{self._documentation_server_port}"
369-
params = urllib.parse.urlencode({"name": name, "args": url_args, "basedir": str(base_dir), "theme": "${theme}"})
418+
params = urllib.parse.urlencode(
419+
{
420+
"name": name,
421+
"args": url_args,
422+
"basedir": str(base_dir),
423+
"theme": "${theme}",
424+
}
425+
)
370426

371427
return f"{base_url}/?&{params}{f'#{target}' if target else ''}"
372428

packages/language_server/robotcode/language_server/robotframework/parts/code_action_fixes.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
from robotcode.language_server.robotframework.protocol import RobotLanguageServerProtocol # pragma: no cover
3333

3434

35-
CODEACTIONKINDS_QUICKFIX_CREATEKEYWORD = f"{CodeActionKind.QUICK_FIX}.createKeyword"
35+
CODEACTIONKINDS_QUICKFIX_CREATEKEYWORD = f"{CodeActionKind.QUICK_FIX.value}.createKeyword"
3636

3737

3838
KEYWORD_WITH_ARGS_TEMPLATE = Template(
@@ -88,13 +88,13 @@ async def collect(
8888
kw_not_found_in_diagnostics = next((d for d in context.diagnostics if d.code == "KeywordNotFoundError"), None)
8989

9090
if kw_not_found_in_diagnostics and (
91-
(context.only and CodeActionKind.QUICK_FIX in context.only)
91+
(context.only and CodeActionKind.QUICK_FIX.value in context.only)
9292
or context.trigger_kind in [CodeActionTriggerKind.INVOKED, CodeActionTriggerKind.AUTOMATIC]
9393
):
9494
return [
9595
CodeAction(
9696
"Create Keyword",
97-
kind=CodeActionKind.QUICK_FIX + ".createKeyword",
97+
kind=CodeActionKind.QUICK_FIX.value + ".createKeyword",
9898
command=Command(
9999
"Create Keyword",
100100
self.parent.commands.get_command_name(self.create_keyword),

0 commit comments

Comments
 (0)