Skip to content

Commit 8768878

Browse files
committed
Added more setters for fields edited during completion post-processing.
1 parent 8c77f75 commit 8768878

2 files changed

Lines changed: 21 additions & 15 deletions

File tree

cmd2/argparse_completer.py

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
"""
55

66
import argparse
7-
import dataclasses
87
import inspect
98
from collections import deque
109
from collections.abc import (
@@ -493,10 +492,7 @@ def _handle_last_token(
493492
if completions:
494493
if not completions.hint:
495494
# Add a hint even though there are results in case Cmd.always_show_hint is True.
496-
completions = dataclasses.replace(
497-
completions,
498-
hint=_build_hint(self._parser, flag_arg_state.action),
499-
)
495+
completions._set_hint(_build_hint(self._parser, flag_arg_state.action))
500496

501497
return completions
502498

@@ -521,10 +517,7 @@ def _handle_last_token(
521517
if completions:
522518
if not completions.hint:
523519
# Add a hint even though there are results in case Cmd.always_show_hint is True.
524-
completions = dataclasses.replace(
525-
completions,
526-
hint=_build_hint(self._parser, pos_arg_state.action),
527-
)
520+
completions._set_hint(_build_hint(self._parser, pos_arg_state.action))
528521
return completions
529522

530523
# Otherwise, print a hint if text isn't possibly the start of a flag
@@ -660,10 +653,8 @@ def _build_completion_table(self, arg_state: _ArgumentState, completions: Comple
660653
for item in completions:
661654
table.add_row(Text.from_ansi(item.display), *item.table_data)
662655

663-
return dataclasses.replace(
664-
completions,
665-
table=table,
666-
)
656+
completions._set_table(table)
657+
return completions
667658

668659
def complete_subcommand_help(self, text: str, line: str, begidx: int, endidx: int, tokens: Sequence[str]) -> Completions:
669660
"""Supports cmd2's help command in the completion of subcommand names.

cmd2/completion.py

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,10 @@ class CompletionItem:
6464
table_data: Sequence[Any] = field(default_factory=tuple)
6565

6666
def _set_text(self, text: str) -> None:
67-
"""Update the completion string.
67+
"""Set the completion string.
6868
69-
Used internally by cmd2 to prepare the value for the command line.
69+
Used internally by cmd2 to prepare the text for insertion on
70+
the command line (e.g., restoring shortcuts).
7071
"""
7172
object.__setattr__(self, "text", text)
7273

@@ -285,6 +286,20 @@ class Completions(CompletionResultsBase):
285286
# The quote character to use if adding an opening or closing quote to the matches.
286287
_quote_char: str = field(default="", init=False)
287288

289+
def _set_hint(self, hint: str) -> None:
290+
"""Set the completion hint.
291+
292+
Used internally by cmd2 to add context to completion results.
293+
"""
294+
object.__setattr__(self, "hint", hint)
295+
296+
def _set_table(self, table: Table | None) -> None:
297+
"""Set the completion table.
298+
299+
Used internally by cmd2 to add context to completion results.
300+
"""
301+
object.__setattr__(self, "table", table)
302+
288303
def _set_add_opening_quote(self, value: bool) -> None:
289304
"""Set whether to add an opening quote.
290305

0 commit comments

Comments
 (0)