Skip to content

Commit bf9c3a6

Browse files
committed
improved argument completion and fixed some minor bugs.
Signed-off-by: René <[email protected]>
1 parent 512850b commit bf9c3a6

File tree

11 files changed

+60
-39
lines changed

11 files changed

+60
-39
lines changed

RobotDebug/RobotDebug.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ def start_keyword(self, name, attrs):
4444

4545
path = attrs["source"]
4646
if path and Path(path).exists() and path not in self.source_files:
47-
self.source_files[path] = Path(path).open().readlines()
47+
self.source_files[path] = Path(path).open().readlines() # noqa: SIM115
4848
lineno = attrs["lineno"]
4949
self.library.current_source_path = path
5050
self.library.current_source_line = lineno

RobotDebug/cmdcompleter.py

Lines changed: 36 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
from robot.libraries.BuiltIn import BuiltIn
99
from robot.parsing.parser.parser import _tokens_to_statements
1010

11-
from .globals import IS_RF_7
11+
from .globals import IS_RF_7, KEYWORD_SEP
1212
from .lexer import get_robot_token, get_variable_token
1313
from .prompttoolkitcmd import PromptToolkitCmd
1414
from .robotkeyword import normalize_kw
@@ -69,9 +69,16 @@ def _find_statement_details_at_cursor(self):
6969

7070
def _find_token_at_cursor(self):
7171
for token in self.statement.tokens:
72-
if (
73-
token.type
74-
): # in ["KEYWORD", "IF", "FOR", "ELSE", "ELSE IF"]: TODO: Commented to get all tokens in debug. lets see the impact
72+
if token.type in [
73+
"KEYWORD",
74+
"IF",
75+
"FOR",
76+
"ELSE",
77+
"ELSE IF",
78+
"TRY",
79+
"WHILE",
80+
"VAR",
81+
]: # TODO: Commented to get all tokens in debug. lets see the impact
7582
self.statement_type = token.type
7683
if (
7784
token.lineno == self.cursor_row + 1
@@ -93,6 +100,12 @@ def __init__(self, libs, keywords, helps, cmd_repl: Optional[PromptToolkitCmd] =
93100
self.helps = helps
94101
self.libs = libs
95102
self.keywords = list(keywords)
103+
self.keywords_catalog = {}
104+
for keyword in self.keywords:
105+
self.keywords_catalog[normalize_kw(keyword.name)] = keyword
106+
self.keywords_catalog[
107+
f"{normalize_kw(keyword.parent.name)}.{normalize_kw(keyword.name)}"
108+
] = keyword
96109
self.current_statement = None
97110
for name, display, display_meta in self.get_commands():
98111
self.names.append(name)
@@ -163,28 +176,28 @@ def _get_resource_completions(self, text):
163176
)
164177

165178
def _get_argument_completer(self, text):
166-
for keyword in self.keywords:
167-
if normalize_kw(keyword.name) == normalize_kw(
168-
self.current_statement.statement.keyword
169-
) or f"{normalize_kw(keyword.parent.name)}.{normalize_kw(keyword.name)}" == normalize_kw(
170-
self.current_statement.statement.keyword
171-
):
172-
data_tokens = self.current_statement.statement.data_tokens
173-
args = keyword.args
174-
set_named_args, set_pos_args = self.get_set_args(args, data_tokens)
175-
if set_named_args:
176-
yield from self.get_named_arg_completion(args, set_named_args, set_pos_args)
177-
else:
178-
yield from self.get_pos_arg_completion(args, set_pos_args)
179+
keyword = self.keywords_catalog.get(
180+
normalize_kw(self.current_statement.statement.keyword), None
181+
)
182+
if keyword:
183+
data_tokens = self.current_statement.statement.data_tokens
184+
args = keyword.args
185+
set_named_args, set_pos_args = self.get_set_args(args, data_tokens)
186+
if set_named_args:
187+
yield from self.get_named_arg_completion(args, set_named_args, set_pos_args)
188+
else:
189+
yield from self.get_pos_arg_completion(args, set_pos_args)
179190

180-
def get_pos_arg_completion(self, args, set_pos_args):
191+
def get_pos_arg_completion(
192+
self, args, set_pos_args
193+
): # TODO: here is an issue. if more positional args are set, than existing, named_only will be removed from proposal
181194
for index, arg in enumerate([*args.positional_or_named, *args.named_only]):
182195
if index + 1 > len(set_pos_args):
183-
suffix = "=" if arg in [*args.positional_or_named, *args.named_only] else ""
196+
# suffix = "=" if arg in [*args.positional_or_named, *args.named_only] else ""
184197
yield Completion(
185-
f"{arg}",
198+
f"{arg}=",
186199
0,
187-
display=f"{arg}{suffix}",
200+
display=f"{arg}=",
188201
display_meta=str(args.defaults.get(arg, "")),
189202
)
190203

@@ -338,7 +351,7 @@ def _get_keyword_completions(
338351
):
339352
yield from [
340353
Completion(
341-
f"{var[:-1]}",
354+
var,
342355
-cursor_pos,
343356
display=var,
344357
display_meta=repr(val),
@@ -365,7 +378,7 @@ def __init__(self, completer: CmdCompleter):
365378
def get_suggestion(self, buffer: Buffer, document: Document) -> Union[Suggestion, None]:
366379
text = document.text
367380
completions = [compl.text for compl in self.completer.get_completions(document, None)]
368-
last_word = text.split(" ")[-1]
381+
last_word = KEYWORD_SEP.split(text)[-1]
369382
matches = [kw for kw in completions if kw.startswith(last_word)]
370383
matches.extend([kw for kw in completions if kw.lower().startswith(last_word.lower())])
371384
return Suggestion(matches[0][len(last_word) :] if matches else "")

RobotDebug/globals.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import re
12
from enum import Enum
23

34
from robot.version import get_version
@@ -25,3 +26,4 @@ class StepMode(str, Enum):
2526

2627

2728
IS_RF_7 = int(get_version().split(".", 1)[0]) >= 7 # noqa: PLR2004
29+
KEYWORD_SEP = re.compile(r"[ \t]{2,}|\t")

RobotDebug/history_app.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -136,10 +136,14 @@ def get_history_content(his, pure_commands: bool = True):
136136
[
137137
e
138138
for e in dict.fromkeys(
139-
reversed([re.sub(r" {2,}", " " * 4, v).strip() for v in his.get_strings()])
139+
reversed(
140+
[
141+
re.sub(r"(?:(?<![\n ])(?:[ \t]{2,}|\t))", " " * 4, v).strip()
142+
for v in his.get_strings()
143+
]
144+
)
140145
)
141-
if (not HEADER_MATCHER.match(e) and pure_commands)
142-
or (HEADER_MATCHER.match(e) and not pure_commands)
146+
if bool(HEADER_MATCHER.match(e)) != pure_commands
143147
]
144148
)
145149
)

RobotDebug/lexer.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,7 @@ def get_variable_token(token_list):
5050
var_tokens = list(token.tokenize_variables())
5151
except Exception:
5252
var_tokens = [token]
53-
for v_token in var_tokens:
54-
yield v_token
53+
yield from var_tokens
5554

5655

5756
class RobotFrameworkLocalLexer(Lexer):

RobotDebug/prompttoolkitcmd.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -304,7 +304,7 @@ def loop_once(self):
304304
if line is None:
305305
return None
306306

307-
if line == "exit" or line == "EOF":
307+
if line in ["exit", "EOF"]:
308308
line = "EOF"
309309
return True
310310

RobotDebug/robotkeyword.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import re
21
import tempfile
32
from pathlib import Path
43
from typing import Iterator, List, Tuple
@@ -14,10 +13,9 @@
1413
from robot.running import ResourceFile
1514
from robot.variables.search import is_variable
1615

16+
from .globals import KEYWORD_SEP
1717
from .robotlib import ImportedLibraryDocBuilder, ImportedResourceDocBuilder, get_libs
1818

19-
KEYWORD_SEP = re.compile(" +|\t")
20-
2119
_lib_keywords_cache = {}
2220
_resource_keywords_cache = {}
2321
temp_resources = []

RobotDebug/robotlib.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from copy import deepcopy
2+
23
from robot.libdocpkg.model import LibraryDoc
34
from robot.libdocpkg.robotbuilder import (
45
KeywordDocBuilder,

RobotDebug/sourcelines.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@ def print_source_lines(style, source_file, lineno, before_and_after=5):
1616
if not source_file or not lineno:
1717
return
1818

19-
Path(source_file).open().readlines()
19+
Path(
20+
source_file
21+
).open().readlines() # noqa: SIM115 TODO: not sure why i did this. Maybe to check if file is actually readable...
2022
prefixed_token = get_pygments_token_from_file(lineno, source_file)
2123
printable_token = filter_token_by_lineno(
2224
prefixed_token, lineno - before_and_after, lineno + before_and_after + 1
@@ -28,7 +30,9 @@ def print_test_case_lines(style, source_file, current_lineno):
2830
if not source_file or not current_lineno:
2931
return
3032

31-
Path(source_file).open().readlines()
33+
Path(
34+
source_file
35+
).open().readlines() # noqa: SIM115 TODO: not sure why i did this. Maybe to check if file is actually readable...
3236
prefixed_token = get_pygments_token_from_file(current_lineno, source_file)
3337
printable_token = filter_token_by_scope(prefixed_token, current_lineno)
3438
print_pygments_styles(printable_token, style)

RobotDebug/version.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
VERSION = "4.4.0"
1+
VERSION = "4.5.0"

0 commit comments

Comments
 (0)