-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
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
Provide some way to create a keymap that pipes the selection to a specific shell command #1943
Comments
The helper for the all the shell commands is here: helix/helix-term/src/commands.rs Lines 4248 to 4309 in 8b91ecd
But it also has the code for displaying the prompt interleaved with it, so the code for execution of a given command and replacing the buffer contents will have to extracted out, for example: fn shell(cx: &mut Context, cmd: &str, behavior: ShellBehavior) {
let pipe = match behavior {
ShellBehavior::Replace | ShellBehavior::Ignore => true,
ShellBehavior::Insert | ShellBehavior::Append => false,
};
let config = cx.editor.config();
let shell = &config.shell;
let (view, doc) = current!(cx.editor);
let selection = doc.selection(view.id);
let mut changes = Vec::with_capacity(selection.len());
// ....
}
fn shell_prompt(cx: &mut Context, prompt: Cow<'static, str>, behavior: ShellBehavior) {
ui::prompt(
cx,
prompt,
Some('|'),
ui::completers::none,
move |cx, input: &str, event: PromptEvent| {
if event != PromptEvent::Validate {
return;
}
if input.is_empty() {
return;
}
shell(cx, input, behavior)
})
} You might have to replace |
I found myself needing this as well. And since it didn't seem too hard for a first PR, I took a stab at it #1972 |
@DeviousStoat This looks amazing! The problems I ran into using external formatters in Neovim were:
It would be great if these two problems could be avoided in Helix. I expect number 2 was just a Vim thing, but can your PR recognise an error and abort with a message? I am really hoping to get Prettier https://prettier.io/docs/en/cli.html working in Helix for all the different file types it can act on, that would be amazing! We would need a way to turn off LSP formatting on a server by server basis. For example, the TypeScript Language Server formatting sucks, but you could use Prettier instead. PS https://dprint.dev/ is written in Rust and copies a lot of Prettier functionality. |
Hey @David-Else! Another improvement might be to print the stderr content to the status bar though because for now I think it is only logged and it just prints |
@DeviousStoat |
# in <config_dir>/helix/languages.toml (same directory as config.toml)
[[language]]
name = "rust"
auto-format = false |
Closed by #1972. Example: # <config_dir>/helix/config.toml
[keys.normal]
"=" = ":pipe fmt -w 80" is my stop-gap until the reflow command lands. |
Can pipe type-able commands be used for Prettier somehow? https://prettier.io/docs/en/cli.html |
The only trouble with prettier would be specifying the language since it supports multiple. If I wanted to bind a key to format json: [keys.normal]
"=" = ":pipe prettier --parser json" Then I can say But there isn't a way to pass in the buffer's language currently. I believe in vim and kakoune you can do it with something like environment variables. |
@the-mikedavis did you find a workaround for this or are you currently using language specific keybinds? |
There isn't a workaround for this. If I use prettier I call out to it in a separate shell. |
I can map a key to
shell_pipe
like this:but I cannot specify any arguments to
shell_pipe
. I think there are many ways to solve this problem. One solution would be #1383, but the easiest would probably be to add a new typable command:pipe
that I can then map:The use case is piping the content of the file to a formatter (and then saving it). So:
The text was updated successfully, but these errors were encountered: