fix(editor): handle editor/visual/configured editor with arguments (#1587)

* fix(editor): handle editor/visual/configured editor with arguments

* style(fmt): rustfmt
This commit is contained in:
Aram Drevekenin 2022-07-14 15:14:25 +02:00 committed by GitHub
parent 72ed0e98cf
commit 50dd47c2e0
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -162,6 +162,24 @@ fn handle_terminal(
}
}
// this is a utility method to separate the arguments from a pathbuf before we turn it into a
// Command. eg. "/usr/bin/vim -e" ==> "/usr/bin/vim" + "-e" (the latter will be pushed to args)
fn separate_command_arguments(command: &mut PathBuf, args: &mut Vec<String>) {
if let Some(file_name) = command
.file_name()
.and_then(|f_n| f_n.to_str())
.map(|f_n| f_n.to_string())
{
let mut file_name_parts = file_name.split_ascii_whitespace();
if let Some(first_part) = file_name_parts.next() {
command.set_file_name(first_part);
for part in file_name_parts {
args.push(String::from(part));
}
}
}
}
/// If a [`TerminalAction::OpenFile(file)`] is given, the text editor specified by environment variable `EDITOR`
/// (or `VISUAL`, if `EDITOR` is not set) will be started in the new terminal, with the given
/// file open.
@ -191,11 +209,16 @@ pub fn spawn_terminal(
"No Editor found, consider setting a path to one in $EDITOR or $VISUAL",
);
}
let command = default_editor.unwrap_or_else(|| {
let mut command = default_editor.unwrap_or_else(|| {
PathBuf::from(env::var("EDITOR").unwrap_or_else(|_| env::var("VISUAL").unwrap()))
});
let mut args = vec![];
if !command.is_dir() {
separate_command_arguments(&mut command, &mut args);
}
let file_to_open = file_to_open
.into_os_string()
.into_string()