Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 25 additions & 7 deletions nnote/completions.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,33 @@
def complete_note_titles(ctx, param, incomplete):
try:
config = Config.load()
directory = ctx.params.get("directory")
root = config.notes_dir / directory if directory else config.notes_dir
root = config.notes_dir
if not root or not root.exists():
return []
return [
CompletionItem(p.name)
for p in sorted(root.iterdir())
if p.is_file() and p.name.startswith(incomplete)
]

# Path-style input ("dir1/note") — split on last slash
if "/" in incomplete:
dir_part, name_part = incomplete.rsplit("/", 1)
search_dir = root / dir_part
value_prefix = dir_part + "/"
else:
directory = ctx.params.get("directory")
search_dir = root / directory if directory else root
value_prefix = ""
name_part = incomplete

if not search_dir.exists():
return []

results = []
for p in sorted(search_dir.iterdir()):
if not p.name.startswith(name_part):
continue
if p.is_file():
results.append(CompletionItem(value_prefix + p.name))
elif p.is_dir():
results.append(CompletionItem(value_prefix + p.name + "/"))
return results
except Exception:
return []

Expand Down
26 changes: 16 additions & 10 deletions tests/test_completions.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,14 @@ def test_complete_note_titles_returns_matching_files(cfg):
assert "beta" not in names


def test_complete_note_titles_empty_prefix_returns_all(cfg):
_make_notes(cfg.notes_dir, ["x", "y"])
def test_complete_note_titles_empty_prefix_returns_files_and_dirs(cfg):
_make_notes(cfg.notes_dir, ["x", "y"], dirs=["subdir"])
with patch("nnote.completions.Config.load", return_value=cfg):
results = complete_note_titles(_FakeCtx(), None, "")
assert {r.value for r in results} == {"x", "y"}
assert {r.value for r in results} == {"x", "y", "subdir/"}


def test_complete_note_titles_respects_directory(cfg):
def test_complete_note_titles_respects_directory_flag(cfg):
subdir = cfg.notes_dir / "work"
_make_notes(subdir, ["report", "review"])
_make_notes(cfg.notes_dir, ["readme"])
Expand All @@ -60,13 +60,19 @@ def test_complete_note_titles_respects_directory(cfg):
assert "readme" not in names


def test_complete_note_titles_no_dirs_in_results(cfg):
_make_notes(cfg.notes_dir, ["note"], dirs=["subdir"])
def test_complete_note_titles_path_style(cfg):
subdir = cfg.notes_dir / "work"
_make_notes(subdir, ["report", "review"])
with patch("nnote.completions.Config.load", return_value=cfg):
results = complete_note_titles(_FakeCtx(), None, "")
names = [r.value for r in results]
assert "subdir" not in names
assert "note" in names
results = complete_note_titles(_FakeCtx(), None, "work/re")
assert {r.value for r in results} == {"work/report", "work/review"}


def test_complete_note_titles_path_style_dir_prefix(cfg):
_make_notes(cfg.notes_dir, [], dirs=["work", "personal"])
with patch("nnote.completions.Config.load", return_value=cfg):
results = complete_note_titles(_FakeCtx(), None, "w")
assert {r.value for r in results} == {"work/"}


def test_complete_directories_returns_subdirs(cfg):
Expand Down