|
16 | 16 | from typing import Any |
17 | 17 |
|
18 | 18 | from prompt_toolkit import PromptSession |
| 19 | +from prompt_toolkit.completion import Completer, Completion, PathCompleter |
| 20 | +from prompt_toolkit.document import Document |
19 | 21 | from prompt_toolkit.formatted_text import FormattedText |
20 | 22 | from prompt_toolkit.shortcuts import print_formatted_text |
21 | 23 | from prompt_toolkit.styles import Style |
@@ -186,10 +188,46 @@ def _bottom_toolbar(session: ChatSession) -> FormattedText: |
186 | 188 | ) |
187 | 189 |
|
188 | 190 |
|
| 191 | +_SLASH_COMMANDS = [ |
| 192 | + "/exit", "/quit", "/help", "/clear", "/save", |
| 193 | + "/status", "/list", "/lint", "/add", |
| 194 | +] |
| 195 | + |
| 196 | + |
| 197 | +class _ChatCompleter(Completer): |
| 198 | + """Complete slash commands and file paths after /add.""" |
| 199 | + |
| 200 | + def __init__(self) -> None: |
| 201 | + self._path_completer = PathCompleter(expanduser=True) |
| 202 | + |
| 203 | + def get_completions(self, document: Document, complete_event: Any) -> Any: |
| 204 | + text = document.text_before_cursor |
| 205 | + |
| 206 | + # After "/add ", complete file paths |
| 207 | + if text.lstrip().lower().startswith("/add "): |
| 208 | + # Extract the path portion after "/add " |
| 209 | + prefix = text.lstrip() |
| 210 | + add_pos = prefix.lower().index("/add ") + 5 |
| 211 | + path_text = prefix[add_pos:] |
| 212 | + # Strip surrounding quotes if the user started one |
| 213 | + if path_text and path_text[0] in ("'", '"'): |
| 214 | + path_text = path_text[1:] |
| 215 | + path_doc = Document(path_text, len(path_text)) |
| 216 | + yield from self._path_completer.get_completions(path_doc, complete_event) |
| 217 | + return |
| 218 | + |
| 219 | + # Complete slash commands |
| 220 | + if text.startswith("/"): |
| 221 | + for cmd in _SLASH_COMMANDS: |
| 222 | + if cmd.startswith(text.lower()): |
| 223 | + yield Completion(cmd, start_position=-len(text)) |
| 224 | + |
| 225 | + |
189 | 226 | def _make_prompt_session(session: ChatSession, style: Style, use_color: bool) -> PromptSession: |
190 | 227 | return PromptSession( |
191 | 228 | message=FormattedText([("class:prompt", ">>> ")]), |
192 | 229 | style=style, |
| 230 | + completer=_ChatCompleter(), |
193 | 231 | bottom_toolbar=(lambda: _bottom_toolbar(session)) if use_color else None, |
194 | 232 | ) |
195 | 233 |
|
|
0 commit comments