Skip to content

Commit 840778e

Browse files
committed
fix: completion of bdd prefixes optimized
- If you press CTRL+SPACE after a bdd prefix the completion list is shown without typing any other key. - if the cursor is at the bdd prefix, other bdd prefix are on the top of the completion list and if you select a bdd prefix only the old prefix is overwritten
1 parent 051627b commit 840778e

File tree

2 files changed

+34
-13
lines changed

2 files changed

+34
-13
lines changed

packages/language_server/src/robotcode/language_server/robotframework/parts/completion.py

Lines changed: 32 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -578,7 +578,9 @@ async def create_testcase_settings_completion_items(self, range: Optional[Range]
578578
for setting in settings
579579
]
580580

581-
async def create_bdd_prefix_completion_items(self, range: Optional[Range]) -> List[CompletionItem]:
581+
async def create_bdd_prefix_completion_items(
582+
self, range: Optional[Range], at_top: bool = False, with_space: bool = True
583+
) -> List[CompletionItem]:
582584
prefixes = {"Given", "When", "Then", "And", "But"}
583585

584586
if self.namespace.languages is not None:
@@ -589,9 +591,11 @@ async def create_bdd_prefix_completion_items(self, range: Optional[Range]) -> Li
589591
label=prefix,
590592
kind=CompletionItemKind.UNIT,
591593
detail="BDD Prefix",
592-
sort_text=f"080_{prefix}",
594+
sort_text=f"000_{prefix}" if at_top else f"080_{prefix}",
593595
insert_text_format=InsertTextFormat.PLAIN_TEXT,
594-
text_edit=TextEdit(range=range, new_text=f"{prefix} ") if range is not None else None,
596+
text_edit=TextEdit(range=range, new_text=prefix + (" " if with_space else ""))
597+
if range is not None
598+
else None,
595599
)
596600
for prefix in prefixes
597601
]
@@ -674,18 +678,29 @@ async def create_keyword_completion_items(
674678

675679
has_bdd = False
676680
bdd_token = None
681+
only_bdd = False
677682

678683
if token is not None:
679684
old_token = token
680685
bdd_token, token = self.split_bdd_prefix(self.namespace, token)
681686

687+
if (
688+
bdd_token is None
689+
and token is not None
690+
and self.is_bdd_token(self.namespace, token)
691+
and position.character > range_from_token(token).end.character
692+
):
693+
bdd_token = token
694+
token = None
695+
only_bdd = True
696+
682697
if token is not None and token.value == "":
683698
token = None
684699

685700
if bdd_token is not None and position.character > range_from_token(bdd_token).end.character:
686701
has_bdd = True
687702

688-
if not has_bdd and token is None:
703+
if not has_bdd and token is not None:
689704
token = old_token
690705

691706
if token is not None:
@@ -877,12 +892,20 @@ def enumerate_indexes(s: str, c: str) -> Iterator[int]:
877892
)
878893
)
879894

880-
if add_bdd_prefixes and not has_bdd:
881-
result += await self.create_bdd_prefix_completion_items(
882-
range_from_token(token) if token is not None else None
883-
)
895+
if add_bdd_prefixes and not (has_bdd or only_bdd):
896+
bdd_range = r
897+
at_top = False
898+
with_space = True
899+
if bdd_token is not None and (
900+
position in range_from_token(bdd_token) or position == range_from_token(bdd_token).end.character
901+
):
902+
at_top = True
903+
with_space = False
904+
bdd_range = range_from_token(bdd_token)
905+
906+
result += await self.create_bdd_prefix_completion_items(bdd_range, at_top, with_space)
884907

885-
if add_reserverd:
908+
if add_reserverd and not (has_bdd or only_bdd):
886909
for k in get_reserved_keywords():
887910
result.append(
888911
CompletionItem(

packages/language_server/src/robotcode/language_server/robotframework/parts/model_helper.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -613,11 +613,9 @@ def is_bdd_token(cls, namespace: Namespace, token: Token) -> bool:
613613
return bool(bdd_match)
614614

615615
parts = token.value.split()
616-
if len(parts) < 2:
617-
return False
618616

619-
for index in range(1, len(parts)):
620-
prefix = " ".join(parts[:index]).title()
617+
for index in range(0, len(parts)):
618+
prefix = " ".join(parts[: index + 1]).title()
621619

622620
if prefix.title() in (
623621
namespace.languages.bdd_prefixes if namespace.languages is not None else DEFAULT_BDD_PREFIXES

0 commit comments

Comments
 (0)