Skip to content

feat(completion): improve @N<TAB> UX with grouped section menu#67

Merged
p0fi merged 4 commits into
mainfrom
feat/improve-completion-ux
Apr 24, 2026
Merged

feat(completion): improve @N<TAB> UX with grouped section menu#67
p0fi merged 4 commits into
mainfrom
feat/improve-completion-ux

Conversation

@p0fi
Copy link
Copy Markdown
Owner

@p0fi p0fi commented Apr 24, 2026

Summary

Fixes #59 — typing matter @1<TAB> previously showed nothing useful (or only sibling nodes sharing the @1 prefix). Users had to already know to type / for endpoints or for device commands.

This PR wires up a four-section completion menu when the typed token is an exact numeric node ID:

  • Devices — sibling nodes that prefix-match (@1, @11, …)
  • Endpoints — node's endpoints (@1/0, @1/1, …)
  • Device Commands — target-aware commands (tree, rename, decommission, …)
  • Cluster Commands — cluster shorthands (OnOff, LevelControl, …)
  • Tools — code, help, …

How it works

Go side (cli/completion/completer.go)

  • New TopLevelCommand struct + ExpandSeparator (+) constant.
  • TargetCompletionFunc gains a topLevelCommands func() parameter. When the typed token is a bare numeric @N that exactly matches a commissioned node (stage-1b), it emits:
    • @N/EP endpoint tokens (appear under "Endpoints" in zsh)
    • @N+<cmd> expansion tokens for each target-aware root command
  • RootCompletionFunc forwards the parameter through.

Helper (cli/cluster.go)

  • topLevelCommandsForCompletion() snapshots the visible root cobra commands and tags each as device/cluster/tool. Non-target-aware commands (commission, discover, fabric, use, …) are excluded so the menu stays relevant.

Bug fix (cli/root.go)

  • Execute() was stripping @1 from os.Args for __complete invocations, causing cobra to receive an empty argument list. Fixed with an isCompletionInvocation() guard.

Zsh script (cli/completion.go)

  • Splits legacy target_cmds into device_targets (@n) and endpoint_targets (@N/EP).
  • Parses @N+<cmd> tokens: looks up <cmd> in _matter_group_map, buckets into exp_device/cluster/tool arrays.
  • Emits expansion groups with compadd -U -p "@N " so the candidate displays as the bare command name but inserts as @N <cmd> on the command line.

Test plan

  • TestTargetCompletionFunc_Stage1b_EndpointsInExactMatch — exact @n emits endpoint tokens
  • TestTargetCompletionFunc_Stage1b_CommandsInExactMatch — expansion tokens emitted only for TargetAware commands
  • TestTargetCompletionFunc_Stage1b_NamedNoExpansion — alias prefix (@kitchen-light) does NOT trigger expansion
  • TestTargetCompletionFunc_Stage1b_NonExistentNodeNoExpansion@99 (no match) does NOT trigger expansion
  • All existing completer tests still pass (mise run test)
  • mise run lint passes clean

🤖 Generated with Claude Code

Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR improves the CLI’s @N<TAB> autocompletion experience by enriching exact numeric node matches with endpoint candidates and grouped “next action” suggestions (device/cluster/tool commands), primarily for the custom zsh completion script.

Changes:

  • Extend Go completion logic to emit endpoint candidates (e.g. @N/0, @N/1) and encoded expansion tokens for exact @N matches.
  • Add a root-command snapshot helper to provide target-aware top-level commands for completion expansion.
  • Update the zsh completion script to split targets into “Devices” vs “Endpoints” sections and to interpret encoded expansion tokens into grouped menus; adjust Execute() behavior for cobra completion invocations.

Reviewed changes

Copilot reviewed 8 out of 8 changed files in this pull request and generated 4 comments.

Show a summary per file
File Description
cli/root.go Wires root completion with the new top-level command snapshot; adds a guard around ExtractTargetFromArgs for cobra completion invocations.
cli/completion/completer.go Adds stage-1b exact-match enrichment (endpoints + encoded @N+<cmd> tokens) and updates completion function signatures.
cli/cluster.go Implements topLevelCommandsForCompletion() to snapshot visible root commands and tag target-awareness.
cli/completion.go Updates the generated zsh completion script to add “Devices/Endpoints” sections and parse @N+<cmd> tokens into grouped expansions.
cli/completion/completer_test.go Adds tests covering stage-1b endpoint enrichment and expansion-token emission rules.
cli/rename.go Updates call site for the new TargetCompletionFunc(nil) signature.
cli/decommission.go Updates call site for the new TargetCompletionFunc(nil) signature.
cli/fabric.go Updates call site for the new TargetCompletionFunc(nil) signature.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread cli/root.go
Comment thread cli/completion/completer.go
Comment thread cli/completion.go
Comment thread cli/completion/completer.go Outdated
p0fi and others added 3 commits April 24, 2026 15:05
Typing `matter @1<TAB>` previously offered only sibling nodes that
happened to share the @1 prefix. Users had to already know to type
space (for device commands) or slash (for endpoints) to see anything
useful.

This change wires up a four-section completion menu whenever the typed
token is an exact numeric node ID:

  Devices          – sibling nodes matching the prefix (@1, @11, ...)
  Endpoints        – node's endpoints (@1/0, @1/1, ...)
  Device Commands  – target-aware commands (tree, rename, ...)
  Cluster Commands – cluster shorthands (OnOff, LevelControl, ...)
  Tools            – code, help, ...

Implementation:

• cli/completion/completer.go
  - Add TopLevelCommand struct and ExpandSeparator constant.
  - TargetCompletionFunc gains a topLevelCommands func() parameter.
  - Stage-1b: when toComplete is a bare numeric @n that exactly matches
    a commissioned node, emit @N/EP endpoint tokens and @n+<cmd>
    expansion tokens for each target-aware top-level command.
  - RootCompletionFunc accepts the same parameter and forwards it.

• cli/cluster.go
  - Add topLevelCommandsForCompletion(), which snapshots the visible
    root cobra commands and labels each as device/cluster/tool. Non-
    TargetAware commands (commission, discover, fabric, …) are excluded.

• cli/root.go
  - Execute() skips @target extraction for __complete invocations so the
    target token reaches the completion function instead of being
    consumed by the normal arg-parsing path.

• cli/completion.go (zsh script)
  - Split legacy target_cmds bucket into device_targets (@n) and
    endpoint_targets (@N/EP).
  - Parse @n+<cmd> expansion tokens: look up <cmd> in _matter_group_map,
    bucket into exp_device/cluster/tool arrays.
  - Emit expansion groups with compadd -U -p "@n " so the candidate
    displays as the bare command name but inserts as "@n <cmd>".

• cli/completion/completer_test.go
  - Tests for stage-1b endpoint emission on exact match.
  - Tests for @n+<cmd> token generation with/without topLevelCommands.
  - Tests confirming alias-based and non-existent node prefixes do not
    trigger expansion.

Closes #59
- Extract @targets from committed completion args (not toComplete) so
  cobra can still resolve subcommands like "matter @1/1 OnOff <TAB>".
- Gate "@n+<cmd>" expansion tokens behind MATTER_COMPLETION_EXPAND=zsh
  so bash, fish, and PowerShell completions do not display the literal
  zsh-specific encoding.
- Route ungrouped expansion commands (e.g. "help") to the Tools bucket
  in the zsh script's case statement instead of silently dropping them.
- Clarify that TopLevelCommand.Group is metadata for shell-specific
  helpers; it is not encoded in the emitted "@n+<name>" token.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
Prevents backslash-escaping of spaces in "@n " so completions insert
as separate shell words, improving CLI ergonomics.
@p0fi p0fi force-pushed the feat/improve-completion-ux branch from a4f9156 to f1de03a Compare April 24, 2026 13:05
Stale cross-package staticcheck facts were causing phantom SA5011
diagnostics locally while CI (which starts with no lint cache) passed.
Cleaning the cache up-front costs ~10s but makes local results match CI.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
@p0fi p0fi self-assigned this Apr 24, 2026
@p0fi p0fi merged commit 182594c into main Apr 24, 2026
4 checks passed
@p0fi p0fi deleted the feat/improve-completion-ux branch April 24, 2026 13:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Improve Autocompletion UX

2 participants