diff --git a/example/cmd/_test/nushell.nu b/example/cmd/_test/nushell.nu index 8343897d9..fffdb4d71 100644 --- a/example/cmd/_test/nushell.nu +++ b/example/cmd/_test/nushell.nu @@ -1,3 +1,14 @@ -let example_completer = {|spans| +let example_completer = {|spans| + # if the current command is an alias, get it's expansion + let expanded_alias = (scope aliases | where name == $spans.0 | $in.0?.expansion?) + + # overwrite + let spans = (if $expanded_alias != null { + # put the first word of the expanded alias first in the span + $spans | skip 1 | prepend ($expanded_alias | split row " " | take 1) + } else { + $spans | skip 1 | prepend ($spans.0) + }) + example _carapace nushell ...$spans | from json } diff --git a/internal/shell/nushell/snippet.go b/internal/shell/nushell/snippet.go index 617e93c31..88734643f 100644 --- a/internal/shell/nushell/snippet.go +++ b/internal/shell/nushell/snippet.go @@ -10,7 +10,18 @@ import ( // Snippet creates the nushell completion script. func Snippet(cmd *cobra.Command) string { - return fmt.Sprintf(`let %v_completer = {|spans| + return fmt.Sprintf(`let %v_completer = {|spans| + # if the current command is an alias, get it's expansion + let expanded_alias = (scope aliases | where name == $spans.0 | $in.0?.expansion?) + + # overwrite + let spans = (if $expanded_alias != null { + # put the first word of the expanded alias first in the span + $spans | skip 1 | prepend ($expanded_alias | split row " " | take 1) + } else { + $spans | skip 1 | prepend ($spans.0) + }) + %v _carapace nushell ...$spans | from json }`, cmd.Name(), uid.Executable()) }