-
-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
173 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
//! Complete commands within fish | ||
|
||
// For fish the behaviour should match the default as close as possible | ||
// 1. grouping shorthand completions i.e. -a<TAB> should show other shorthands to end up with | ||
// -ams... | ||
// 2. only complete options when one - is typed | ||
// Due to https://github.com/fish-shell/fish-shell/issues/7943 we need to implement this our self | ||
// | ||
|
||
use std::{ | ||
ffi::OsString, | ||
fmt::Display, | ||
io::{stdout, Write}, | ||
}; | ||
|
||
use clap::Args; | ||
|
||
use super::{complete, completions_for_arg, Completer, CompletionContext}; | ||
|
||
/// Dynamic completion for Fish | ||
pub struct Fish; | ||
|
||
impl Completer for Fish { | ||
type CompleteArgs = CompleteArgs; | ||
|
||
fn completion_script(cmd: &mut clap::Command) -> Vec<u8> { | ||
let name = cmd.get_bin_name().unwrap_or_else(|| cmd.get_name()); | ||
format!( | ||
r#"complete -x -c {name} -a "{name} complete --previous (commandline --current-process --tokenize --cut-at-cursor) --current (commandline --current-token)""# | ||
).into_bytes() | ||
} | ||
|
||
fn file_name(name: &str) -> String { | ||
format!("{name}.fish") | ||
} | ||
|
||
fn try_complete( | ||
CompleteArgs { | ||
commandline, | ||
current, | ||
}: &Self::CompleteArgs, | ||
cmd: &mut clap::Command, | ||
) -> clap::error::Result<()> { | ||
let CompletionContext { | ||
value_for, | ||
options, | ||
subcommands, | ||
} = complete(cmd, commandline)?; | ||
|
||
let mut completions = Vec::new(); | ||
let current = current.to_string_lossy(); | ||
|
||
// fish only shows flags, when the user currently types a flag | ||
if !options.is_empty() && current.starts_with("--") { | ||
for option in options { | ||
// TODO maybe only offer aliases when the user currently is typing one | ||
// This could easily inflate the number of completions when a lot of aliases are | ||
// used, on the other hand this can be achieved by using hidden aliases | ||
if let Some(longs) = option.get_long_and_visible_aliases() { | ||
for long in longs { | ||
add_completion(&mut completions, long, option.get_help())?; | ||
} | ||
} | ||
} | ||
// TODO display non option values, in case the arg allows values starting with `--` | ||
} else if !options.is_empty() && current.starts_with('-') { | ||
// TODO implement flag joining i.e. show `-er` when the user typed `-e` | ||
for option in options { | ||
if let Some(shorts) = option.get_short_and_visible_aliases() { | ||
for short in shorts { | ||
add_completion(&mut completions, short, option.get_help())?; | ||
} | ||
} | ||
} | ||
// TODO display non option values, in case the arg allows values starting with `-` | ||
} else if let Some(value_for) = value_for { | ||
for (item, help) in completions_for_arg(value_for)? { | ||
add_completion(&mut completions, item, help)?; | ||
} | ||
} else { | ||
for subcommand in subcommands { | ||
add_completion( | ||
&mut completions, | ||
subcommand.get_name(), | ||
subcommand.get_about(), | ||
)?; | ||
for alias in subcommand.get_visible_aliases() { | ||
add_completion(&mut completions, alias, subcommand.get_about())?; | ||
} | ||
} | ||
} | ||
|
||
stdout().write_all(&completions)?; | ||
Ok(()) | ||
} | ||
} | ||
|
||
fn add_completion( | ||
completions: &mut Vec<u8>, | ||
item: impl Display, | ||
help: Option<impl Display>, | ||
) -> clap::error::Result<()> { | ||
if let Some(help) = help { | ||
writeln!(completions, "{item}\t{help}")?; | ||
} else { | ||
writeln!(completions, "{item}")?; | ||
} | ||
Ok(()) | ||
} | ||
|
||
#[derive(Args, Clone, Debug)] | ||
/// Arguments for Fish Completion | ||
pub struct CompleteArgs { | ||
/// commandline tokens before the cursor | ||
commandline: Vec<OsString>, | ||
/// token containing the cursor | ||
current: OsString, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters