Skip to content

Commit 4fdeccb

Browse files
committed
#382: classic mode to auto complete relative paths outside of project root
1 parent fcfdaab commit 4fdeccb

4 files changed

Lines changed: 23 additions & 16 deletions

File tree

cecli/commands/core.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -165,12 +165,12 @@ def get_raw_completions(self, cmd):
165165
raw_completer = getattr(self, f"completions_raw_{cmd}", None)
166166
return raw_completer
167167

168-
def get_completions(self, cmd):
168+
def get_completions(self, cmd, args=""):
169169
assert cmd.startswith("/")
170170
cmd = cmd[1:]
171171
command_class = CommandRegistry.get_command(cmd)
172172
if command_class:
173-
return command_class.get_completions(self.io, self.coder, "")
173+
return command_class.get_completions(self.io, self.coder, args)
174174
return []
175175

176176
def get_commands(self):

cecli/commands/read_only.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -215,9 +215,14 @@ def get_completions(cls, io, coder, args) -> List[str]:
215215
if "/" in args:
216216
# Has directory component
217217
dir_part, file_part = args.rsplit("/", 1)
218-
search_dir = root / dir_part
218+
if dir_part == "":
219+
search_dir = Path("/")
220+
path_prefix = "/"
221+
else:
222+
# Use os.path.expanduser for ~ support if needed, but Path handles it mostly
223+
search_dir = (root / dir_part).resolve()
224+
path_prefix = dir_part + "/"
219225
search_prefix = file_part.lower()
220-
path_prefix = dir_part + "/"
221226
else:
222227
search_dir = root
223228
search_prefix = args.lower()
@@ -228,8 +233,9 @@ def get_completions(cls, io, coder, args) -> List[str]:
228233
if search_dir.exists() and search_dir.is_dir():
229234
for entry in search_dir.iterdir():
230235
name = entry.name
231-
if search_prefix and search_prefix not in name.lower():
236+
if search_prefix and not name.lower().startswith(search_prefix):
232237
continue
238+
233239
# Add trailing slash for directories
234240
if entry.is_dir():
235241
completions.append(path_prefix + name + "/")
@@ -238,6 +244,7 @@ def get_completions(cls, io, coder, args) -> List[str]:
238244
except (PermissionError, OSError):
239245
pass
240246

247+
# Also include files already in the chat that match
241248
add_completions = coder.commands.get_completions("/add")
242249
for c in add_completions:
243250
if args.lower() in str(c).lower() and str(c) not in completions:

cecli/commands/read_only_stub.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ def _add_read_only_directory(
206206

207207
@classmethod
208208
def get_completions(cls, io, coder, args) -> List[str]:
209-
"""Get completion options for read-only command."""
209+
"""Get completion options for read-only-stub command."""
210210
from pathlib import Path
211211

212212
root = Path(coder.root) if hasattr(coder, "root") else Path.cwd()
@@ -215,9 +215,13 @@ def get_completions(cls, io, coder, args) -> List[str]:
215215
if "/" in args:
216216
# Has directory component
217217
dir_part, file_part = args.rsplit("/", 1)
218-
search_dir = root / dir_part
218+
if dir_part == "":
219+
search_dir = Path("/")
220+
path_prefix = "/"
221+
else:
222+
search_dir = (root / dir_part).resolve()
223+
path_prefix = dir_part + "/"
219224
search_prefix = file_part.lower()
220-
path_prefix = dir_part + "/"
221225
else:
222226
search_dir = root
223227
search_prefix = args.lower()
@@ -228,8 +232,9 @@ def get_completions(cls, io, coder, args) -> List[str]:
228232
if search_dir.exists() and search_dir.is_dir():
229233
for entry in search_dir.iterdir():
230234
name = entry.name
231-
if search_prefix and search_prefix not in name.lower():
235+
if search_prefix and not name.lower().startswith(search_prefix):
232236
continue
237+
233238
# Add trailing slash for directories
234239
if entry.is_dir():
235240
completions.append(path_prefix + name + "/")
@@ -238,6 +243,7 @@ def get_completions(cls, io, coder, args) -> List[str]:
238243
except (PermissionError, OSError):
239244
pass
240245

246+
# Also include files already in the chat that match
241247
add_completions = coder.commands.get_completions("/add")
242248
for c in add_completions:
243249
if args.lower() in str(c).lower() and str(c) not in completions:

cecli/io.py

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -236,16 +236,10 @@ def get_command_completions(self, document, complete_event, text, words):
236236
yield from raw_completer(document, complete_event)
237237
return
238238

239-
if cmd not in self.command_completions:
240-
candidates = self.commands.get_completions(cmd)
241-
self.command_completions[cmd] = candidates
242-
else:
243-
candidates = self.command_completions[cmd]
239+
candidates = self.commands.get_completions(cmd, partial)
244240

245241
if candidates is None:
246242
return
247-
248-
candidates = [word for word in candidates if partial in word.lower()]
249243
for candidate in sorted(candidates):
250244
yield Completion(candidate, start_position=-len(words[-1]))
251245

0 commit comments

Comments
 (0)