Skip to content

Commit 28a1f8a

Browse files
committed
feat: library doc now generates a more RFW-like signature for arguments and argument defaults like ${SPACE}, ${EMPTY}, ${True}, etc. for the respective values
1 parent ffce34d commit 28a1f8a

File tree

107 files changed

+2152
-10
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

107 files changed

+2152
-10
lines changed

packages/language_server/src/robotcode/language_server/robotframework/diagnostics/imports_manager.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -571,6 +571,7 @@ def clear_cache(self) -> None:
571571

572572
@_logger.call
573573
async def get_command_line_variables(self) -> List[VariableDefinition]:
574+
from robot.errors import DataError
574575
from robot.utils.text import split_args_from_name_or_path
575576

576577
async with self._command_line_variables_lock:
@@ -634,6 +635,8 @@ async def get_command_line_variables(self) -> List[VariableDefinition]:
634635

635636
except (SystemExit, KeyboardInterrupt, asyncio.CancelledError):
636637
raise
638+
except DataError:
639+
pass
637640
except BaseException as e:
638641
# TODO add diagnostics
639642
self._logger.exception(e)

packages/language_server/src/robotcode/language_server/robotframework/diagnostics/library_doc.py

Lines changed: 45 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,29 @@ class KeywordArgumentKind(Enum):
234234
VAR_NAMED = "VAR_NAMED"
235235

236236

237+
def robot_arg_repr(arg: Any) -> Optional[str]:
238+
from robot.running.arguments.argumentspec import ArgInfo
239+
240+
robot_arg = cast(ArgInfo, arg)
241+
242+
if robot_arg.default is ArgInfo.NOTSET:
243+
return None
244+
245+
if robot_arg.default is None:
246+
return "${None}"
247+
248+
if isinstance(robot_arg.default, (int, float, bool)):
249+
return f"${{{robot_arg.default!r}}}"
250+
251+
if isinstance(robot_arg.default, str) and robot_arg.default == "":
252+
return "${EMPTY}"
253+
254+
if isinstance(robot_arg.default, str) and robot_arg.default == "\\ \\":
255+
return "${SPACE}"
256+
257+
return str(robot_arg.default_repr)
258+
259+
237260
@dataclass
238261
class KeywordArgumentDoc:
239262
name: str
@@ -251,15 +274,26 @@ def from_robot(arg: Any) -> KeywordArgumentDoc:
251274

252275
return KeywordArgumentDoc(
253276
name=robot_arg.name,
254-
default_value=robot_arg.default_repr,
277+
default_value=robot_arg_repr(robot_arg),
255278
str_repr=str(arg),
256279
types=robot_arg.types_reprs,
257280
kind=KeywordArgumentKind[robot_arg.kind],
258281
required=robot_arg.required,
259282
)
260283

261284
def __str__(self) -> str:
262-
return self.str_repr
285+
prefix = ""
286+
if self.kind == KeywordArgumentKind.VAR_POSITIONAL:
287+
prefix = "*"
288+
elif self.kind == KeywordArgumentKind.VAR_NAMED:
289+
prefix = "**"
290+
291+
return (
292+
f"{prefix}{self.name!s}"
293+
f"{(': ' + (' | '.join(f'{s}' for s in self.types))) if self.types else ''}"
294+
f"{' =' if self.default_value is not None else ''}"
295+
f"{f' {self.default_value!s}' if self.default_value else ''}"
296+
)
263297

264298
def __hash__(self) -> int:
265299
return id(self)
@@ -431,7 +465,7 @@ def to_markdown(self, add_signature: bool = True, header_level: int = 2, add_typ
431465
if result:
432466
result += "\n\n"
433467

434-
result += f"##{'#'*header_level} Documentation:\n\n"
468+
result += f"##{'#'*header_level} Documentation:\n"
435469

436470
if self.doc_format == ROBOT_DOC_FORMAT:
437471
result += MarkDownFormatter().format(self.doc)
@@ -452,10 +486,12 @@ def _get_signature(self, header_level: int, add_type: bool = True) -> str:
452486
result = ""
453487

454488
if self.args:
455-
result += f"\n##{'#'*header_level} Arguments: \n\n"
489+
result += f"\n##{'#'*header_level} Arguments: \n"
456490

457491
result += "\n| | | | |"
458-
result += "\n|:--- | --:|:--|:---|"
492+
result += "\n|:--|:--|:--|:--|"
493+
494+
escaped_pipe = " \\| "
459495

460496
for a in self.args:
461497
prefix = ""
@@ -466,9 +502,10 @@ def _get_signature(self, header_level: int, add_type: bool = True) -> str:
466502

467503
result += (
468504
f"\n| `{prefix}{a.name!s}`"
469-
f"| {'=' if a.default_value is not None else ''}"
470-
f"| {f'`{a.default_value!s}`' if a.default_value else ''}"
471-
f"| {' or '.join(f'`<{s}>`' for s in a.types) if a.types is not None else ''} |"
505+
f'{": " if a.types else " "}'
506+
f"| {escaped_pipe.join(f'`{s}`' for s in a.types) if a.types else ''} "
507+
f"| {'=' if a.default_value is not None else ''} "
508+
f"| {f'`{a.default_value!s}`' if a.default_value else ''} |"
472509
)
473510

474511
if self.tags:
@@ -705,7 +742,6 @@ def write_lines(*args: str) -> None:
705742
write_lines(
706743
f"#{'#'*header_level} {(self.type.capitalize()) if self.type else 'Unknown'} *{self.name}*",
707744
"",
708-
"",
709745
)
710746

711747
if self.version or self.scope:

tests/robotcode/language_server/robotframework/parts/_regtest_outputs/rf41/test_references.test[references.robot-001-016-a_builtin_library].out

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,3 +48,12 @@ result:
4848
character: 16
4949
line: 1
5050
uri: tests/references.robot
51+
- !Location
52+
range:
53+
end:
54+
character: 22
55+
line: 1
56+
start:
57+
character: 11
58+
line: 1
59+
uri: tests/signature_help.robot

tests/robotcode/language_server/robotframework/parts/_regtest_outputs/rf41/test_references.test[references.robot-001-021-a_builtin_library].out

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,3 +48,12 @@ result:
4848
character: 16
4949
line: 1
5050
uri: tests/references.robot
51+
- !Location
52+
range:
53+
end:
54+
character: 22
55+
line: 1
56+
start:
57+
character: 11
58+
line: 1
59+
uri: tests/signature_help.robot

tests/robotcode/language_server/robotframework/parts/_regtest_outputs/rf41/test_references.test[references.robot-001-026-a_builtin_library].out

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,3 +48,12 @@ result:
4848
character: 16
4949
line: 1
5050
uri: tests/references.robot
51+
- !Location
52+
range:
53+
end:
54+
character: 22
55+
line: 1
56+
start:
57+
character: 11
58+
line: 1
59+
uri: tests/signature_help.robot
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
data: !GeneratedTestData
2+
character: 26
3+
line: 3
4+
name: library signature
5+
result: !SignatureHelp
6+
active_parameter: null
7+
active_signature: 0
8+
signatures:
9+
- activeParameter: 0
10+
documentation:
11+
kind: !MarkupKind 'MARKDOWN'
12+
value: '#### Documentation:
13+
14+
This is the constructor of the class.
15+
16+
17+
'
18+
label: '(l: List[int] | None = ${None}, s: str = Hello World!, i: int = ${0},
19+
b: bool = ${False})'
20+
parameters:
21+
- documentation: null
22+
label: 'l: List[int] | None = ${None}'
23+
- documentation: null
24+
label: 's: str = Hello World!'
25+
- documentation: null
26+
label: 'i: int = ${0}'
27+
- documentation: null
28+
label: 'b: bool = ${False}'
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
data: !GeneratedTestData
2+
character: 28
3+
line: 3
4+
name: library signature
5+
result: !SignatureHelp
6+
active_parameter: null
7+
active_signature: 0
8+
signatures:
9+
- activeParameter: 0
10+
documentation:
11+
kind: !MarkupKind 'MARKDOWN'
12+
value: '#### Documentation:
13+
14+
This is the constructor of the class.
15+
16+
17+
'
18+
label: '(l: List[int] | None = ${None}, s: str = Hello World!, i: int = ${0},
19+
b: bool = ${False})'
20+
parameters:
21+
- documentation: null
22+
label: 'l: List[int] | None = ${None}'
23+
- documentation: null
24+
label: 's: str = Hello World!'
25+
- documentation: null
26+
label: 'i: int = ${0}'
27+
- documentation: null
28+
label: 'b: bool = ${False}'
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
data: !GeneratedTestData
2+
character: 30
3+
line: 3
4+
name: library signature
5+
result: !SignatureHelp
6+
active_parameter: null
7+
active_signature: 0
8+
signatures:
9+
- activeParameter: 0
10+
documentation:
11+
kind: !MarkupKind 'MARKDOWN'
12+
value: '#### Documentation:
13+
14+
This is the constructor of the class.
15+
16+
17+
'
18+
label: '(l: List[int] | None = ${None}, s: str = Hello World!, i: int = ${0},
19+
b: bool = ${False})'
20+
parameters:
21+
- documentation: null
22+
label: 'l: List[int] | None = ${None}'
23+
- documentation: null
24+
label: 's: str = Hello World!'
25+
- documentation: null
26+
label: 'i: int = ${0}'
27+
- documentation: null
28+
label: 'b: bool = ${False}'
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
data: !GeneratedTestData
2+
character: 31
3+
line: 6
4+
name: Suite Setup first param
5+
result: !SignatureHelp
6+
active_parameter: 0
7+
active_signature: 0
8+
signatures:
9+
- activeParameter: 0
10+
documentation:
11+
kind: !MarkupKind 'MARKDOWN'
12+
value: '#### Documentation:
13+
14+
This is a method of the class.
15+
16+
17+
'
18+
label: '(i: int, b: bool, l: List[int] | None = ${None})'
19+
parameters:
20+
- documentation: null
21+
label: 'i: int'
22+
- documentation: null
23+
label: 'b: bool'
24+
- documentation: null
25+
label: 'l: List[int] | None = ${None}'
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
data: !GeneratedTestData
2+
character: 36
3+
line: 6
4+
name: Suite Setup second param
5+
result: !SignatureHelp
6+
active_parameter: 1
7+
active_signature: 0
8+
signatures:
9+
- activeParameter: 1
10+
documentation:
11+
kind: !MarkupKind 'MARKDOWN'
12+
value: '#### Documentation:
13+
14+
This is a method of the class.
15+
16+
17+
'
18+
label: '(i: int, b: bool, l: List[int] | None = ${None})'
19+
parameters:
20+
- documentation: null
21+
label: 'i: int'
22+
- documentation: null
23+
label: 'b: bool'
24+
- documentation: null
25+
label: 'l: List[int] | None = ${None}'

0 commit comments

Comments
 (0)