Skip to content

Commit f3e3741

Browse files
committed
Move regex pattern compilation out of method into initializer for performance
1 parent acfe8d6 commit f3e3741

File tree

1 file changed

+8
-5
lines changed

1 file changed

+8
-5
lines changed

cmd2/pt_utils.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@
2222
from .cmd2 import Cmd
2323

2424

25+
BASE_DELIMITERS = " \t\n" + "".join(constants.QUOTES) + "".join(constants.REDIRECTION_CHARS)
26+
27+
2528
class Cmd2Completer(Completer):
2629
"""Completer that delegates to cmd2's completion logic."""
2730

@@ -30,17 +33,17 @@ def __init__(self, cmd_app: 'Cmd', custom_settings: utils.CustomCompletionSettin
3033
self.cmd_app = cmd_app
3134
self.custom_settings = custom_settings
3235

33-
def get_completions(self, document: Document, _complete_event: object) -> Iterable[Completion]:
34-
"""Get completions for the current input."""
3536
# Define delimiters for completion to match cmd2/readline behavior
36-
delimiters = " \t\n" + "".join(constants.QUOTES) + "".join(constants.REDIRECTION_CHARS)
37+
delimiters = BASE_DELIMITERS
3738
if hasattr(self.cmd_app, 'statement_parser'):
3839
delimiters += "".join(self.cmd_app.statement_parser.terminators)
3940

4041
# Regex pattern for a word: one or more characters that are NOT delimiters
41-
pattern = re.compile(f"[^{re.escape(delimiters)}]+")
42+
self.word_pattern = re.compile(f"[^{re.escape(delimiters)}]+")
4243

43-
text = document.get_word_before_cursor(pattern=pattern)
44+
def get_completions(self, document: Document, _complete_event: object) -> Iterable[Completion]:
45+
"""Get completions for the current input."""
46+
text = document.get_word_before_cursor(pattern=self.word_pattern)
4447

4548
# We need the full line and indexes for cmd2
4649
line = document.text

0 commit comments

Comments
 (0)