Skip to content
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

Improve auto completion for shell commands #12883

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion helix-term/src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6245,7 +6245,7 @@ fn shell_prompt(cx: &mut Context, prompt: Cow<'static, str>, behavior: ShellBeha
cx,
prompt,
Some('|'),
ui::completers::filename,
ui::completers::shell,
move |cx, input: &str, event: PromptEvent| {
if event != PromptEvent::Validate {
return;
Expand Down
17 changes: 12 additions & 5 deletions helix-term/src/commands/typed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,13 @@ impl CommandSignature {
var_args: completer,
}
}

const fn hybrid(completers: &'static [Completer], fallback: Completer) -> Self {
Self {
positional_args: completers,
var_args: fallback,
}
}
}

fn quit(cx: &mut compositor::Context, args: &[Cow<str>], event: PromptEvent) -> anyhow::Result<()> {
Expand Down Expand Up @@ -3156,35 +3163,35 @@ pub const TYPABLE_COMMAND_LIST: &[TypableCommand] = &[
aliases: &[],
doc: "Run shell command, inserting output before each selection.",
fun: insert_output,
signature: CommandSignature::none(),
signature: CommandSignature::hybrid(&[completers::program], completers::filename)
},
TypableCommand {
name: "append-output",
aliases: &[],
doc: "Run shell command, appending output after each selection.",
fun: append_output,
signature: CommandSignature::none(),
signature: CommandSignature::hybrid(&[completers::program], completers::filename)
},
TypableCommand {
name: "pipe",
aliases: &[],
doc: "Pipe each selection to the shell command.",
fun: pipe,
signature: CommandSignature::none(),
signature: CommandSignature::hybrid(&[completers::program], completers::filename)
},
TypableCommand {
name: "pipe-to",
aliases: &[],
doc: "Pipe each selection to the shell command, ignoring output.",
fun: pipe_to,
signature: CommandSignature::none(),
signature: CommandSignature::hybrid(&[completers::program], completers::filename)
},
TypableCommand {
name: "run-shell-command",
aliases: &["sh"],
doc: "Run a shell command",
fun: run_shell_command,
signature: CommandSignature::all(completers::filename)
signature: CommandSignature::hybrid(&[completers::program], completers::filename)
},
TypableCommand {
name: "reset-diff-change",
Expand Down
45 changes: 45 additions & 0 deletions helix-term/src/ui/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,7 @@ pub mod completers {
use super::Utf8PathBuf;
use crate::ui::prompt::Completion;
use helix_core::fuzzy::fuzzy_match;
use helix_core::shellwords::Shellwords;
use helix_core::syntax::LanguageServerFeature;
use helix_view::document::SCRATCH_BUFFER_NAME;
use helix_view::theme;
Expand Down Expand Up @@ -640,4 +641,48 @@ pub mod completers {
.map(|(name, _)| ((0..), name.into()))
.collect()
}

pub fn program(_editor: &Editor, input: &str) -> Vec<Completion> {
static PROGRAMS_IN_PATH: Lazy<Vec<String>> = Lazy::new(|| {
// Go through the entire PATH and read all files into a vec.
let mut vec = std::env::var("PATH")
.unwrap_or("".to_owned())
.split(":")
.flat_map(|s| {
std::fs::read_dir(s)
.map_or_else(|_| vec![], |res| res.into_iter().collect::<Vec<_>>())
})
.filter_map(|it| it.ok())
.map(|f| f.path())
.filter(|p| !p.is_dir())
.filter_map(|p| p.file_name().and_then(|s| s.to_str().map(|s| s.to_owned())))
.collect::<Vec<_>>();

// Paths can share programs like /bin and /usr/bin sometimes contain the same.
vec.dedup();

vec
});

fuzzy_match(input, PROGRAMS_IN_PATH.iter(), false)
.into_iter()
.map(|(name, _)| ((0..), name.clone().into()))
.collect()
}

pub fn shell(editor: &Editor, input: &str) -> Vec<Completion> {
let shellwords = Shellwords::from(input);
let words = shellwords.words();

if words.len() > 1 || shellwords.ends_with_whitespace() {
let offset = words[0].len() + 1;
// Theoretically one could now parse bash completion scripts, but filename should suffice for now.
let mut completions = filename(editor, &input[offset..]);
for completion in completions.iter_mut() {
completion.0.start += offset;
}
return completions;
}
program(editor, input)
}
}