Skip to content

Simplify external completer #1049

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
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
41 changes: 25 additions & 16 deletions cookbook/external_completers.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,18 +68,20 @@ Sometimes, a single external completer is not flexible enough. Luckily, as many

```nu
let multiple_completers = {|spans|
match $spans.0
{
ls: $ls_completer
git: $git_completer
} | get -i $spans.0 | default $default_completer | do $in $spans
ls => $ls_completer
git => $git_completer
_ => $default_completer
} | do $in $spans
}
```

> **Note**
> In the example above, `$spans.0` is the command being run at the time. The completer will try to `get` the desired completer in the record, and fallback to `$default_completer`.
> In the example above, `$spans.0` is the command being run at the time. The completer will match the desired completer, and fallback to `$default_completer`.
>
> - If we try to autocomplete `git <tab>`, `spans` will be `[git ""]`. `{ ... } | get -i git` will return the `$git_completer`
> - If we try to autocomplete `other_command <tab>`, `spans` will be `[other_command ""]`. `{ ... } | get -i other_command` will return null, and `default` will return the default completer.
> - If we try to autocomplete `git <tab>`, `spans` will be `[git ""]`. `match $spans.0 { ... }` will return the `$git_completer`.
> - If we try to autocomplete `other_command <tab>`, `spans` will be `[other_command ""]`. The match will fallback to the default case (`_`) and return the `$default_completer`.

## Troubleshooting

Expand Down Expand Up @@ -134,20 +136,27 @@ let carapace_completer = {|spans: list<string>|

# This completer will use carapace by default
let external_completer = {|spans|
let expanded_alias = (scope aliases | where name == $spans.0 | get -i 0 | get -i expansion)
let spans = (if $expanded_alias != null {
$spans | skip 1 | prepend ($expanded_alias | split words)
} else { $spans })
let expanded_alias = scope alias-completions
| where name == $spans.0
| get -i 0.expansion

let spans = if $expanded_alias != null {
$spans
| skip 1
| prepend ($expanded_alias | split row ' ')
} else {
$spans
}

{
match {
Copy link
Contributor

Choose a reason for hiding this comment

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

What is match matching on here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oops, that's a typo. It should be match $spans.0 :)

I'll create a followup PR soon when I have time

# carapace completions are incorrect for nu
nu: $fish_completer
nu => $fish_completer
# fish completes commits and branch names in a nicer way
git: $fish_completer
git => $fish_completer
# carapace doesn't have completions for asdf
asdf: $fish_completer
} | get -i $spans.0 | default $carapace_completer | do $in $spans

asdf => $fish_completer
_ => $carapace_completer
} | do $in $spans
}

$env.config = {
Expand Down