adds --vsplit and --hsplit arguments (#2773)

* adds --vsplit and --hsplit arguments

* moved comment

* fixed lint (third time's a charm)

* changed vsplit and hsplit from two separate bools to type Option<Layout>, and some cleanup
This commit is contained in:
plexom 2022-07-01 11:27:32 +02:00 committed by GitHub
parent 15d96c843a
commit f10b6f6ee2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 29 additions and 5 deletions

View File

@ -16,7 +16,7 @@ _hx() {
COMPREPLY=($(compgen -W "$languages" -- $2)) COMPREPLY=($(compgen -W "$languages" -- $2))
;; ;;
*) *)
COMPREPLY=($(compgen -fd -W "-h --help --tutor -V --version -v -vv -vvv --health -g --grammar" -- $2)) COMPREPLY=($(compgen -fd -W "-h --help --tutor -V --version -v -vv -vvv --health -g --grammar --vsplit --hsplit" -- $2))
;; ;;
esac esac
} && complete -F _hx hx } && complete -F _hx hx

View File

@ -9,4 +9,5 @@ complete -c hx -l health -x -a "$langs" -d "Checks for errors in editor setup"
complete -c hx -s g -l grammar -x -a "fetch build" -d "Fetches or builds tree-sitter grammars" complete -c hx -s g -l grammar -x -a "fetch build" -d "Fetches or builds tree-sitter grammars"
complete -c hx -s v -o vv -o vvv -d "Increases logging verbosity" complete -c hx -s v -o vv -o vvv -d "Increases logging verbosity"
complete -c hx -s V -l version -d "Prints version information" complete -c hx -s V -l version -d "Prints version information"
complete -c hx -l vsplit -d "Splits all given files vertically into different windows"
complete -c hx -l hsplit -d "Splits all given files horizontally into different windows"

View File

@ -14,6 +14,8 @@ _hx() {
"--health[Checks for errors in editor setup]:language:->health" \ "--health[Checks for errors in editor setup]:language:->health" \
"-g[Fetches or builds tree-sitter grammars]:action:->grammar" \ "-g[Fetches or builds tree-sitter grammars]:action:->grammar" \
"--grammar[Fetches or builds tree-sitter grammars]:action:->grammar" \ "--grammar[Fetches or builds tree-sitter grammars]:action:->grammar" \
"--vsplit[Splits all given files vertically into different windows]" \
"--hsplit[Splits all given files horizontally into different windows]" \
"*:file:_files" "*:file:_files"
case "$state" in case "$state" in

View File

@ -5,7 +5,7 @@
pos_at_coords, syntax, Selection, pos_at_coords, syntax, Selection,
}; };
use helix_lsp::{lsp, util::lsp_pos_to_pos, LspProgressMap}; use helix_lsp::{lsp, util::lsp_pos_to_pos, LspProgressMap};
use helix_view::{align_view, editor::ConfigEvent, theme, Align, Editor}; use helix_view::{align_view, editor::ConfigEvent, theme, tree::Layout, Align, Editor};
use serde_json::json; use serde_json::json;
use crate::{ use crate::{
@ -158,16 +158,31 @@ pub fn new(args: Args, config: Config) -> Result<Self, Error> {
} else { } else {
let nr_of_files = args.files.len(); let nr_of_files = args.files.len();
editor.open(first, Action::VerticalSplit)?; editor.open(first, Action::VerticalSplit)?;
for (file, pos) in args.files { // Because the line above already opens the first file, we can
// simply skip opening it a second time by using .skip(1) here.
for (file, pos) in args.files.into_iter().skip(1) {
if file.is_dir() { if file.is_dir() {
return Err(anyhow::anyhow!( return Err(anyhow::anyhow!(
"expected a path to file, found a directory. (to open a directory pass it as first argument)" "expected a path to file, found a directory. (to open a directory pass it as first argument)"
)); ));
} else { } else {
// If the user passes in either `--vsplit` or
// `--hsplit` as a command line argument, all the given
// files will be opened according to the selected
// option. If neither of those two arguments are passed
// in, just load the files normally.
let action = match args.split {
Some(Layout::Vertical) => Action::VerticalSplit,
Some(Layout::Horizontal) => Action::HorizontalSplit,
None => Action::Load,
};
let doc_id = editor let doc_id = editor
.open(&file, Action::Load) .open(&file, action)
.context(format!("open '{}'", file.to_string_lossy()))?; .context(format!("open '{}'", file.to_string_lossy()))?;
// with Action::Load all documents have the same view // with Action::Load all documents have the same view
// NOTE: this isn't necessarily true anymore. If
// `--vsplit` or `--hsplit` are used, the file which is
// opened last is focused on.
let view_id = editor.tree.focus; let view_id = editor.tree.focus;
let doc = editor.document_mut(doc_id).unwrap(); let doc = editor.document_mut(doc_id).unwrap();
let pos = Selection::point(pos_at_coords(doc.text().slice(..), pos, true)); let pos = Selection::point(pos_at_coords(doc.text().slice(..), pos, true));

View File

@ -1,5 +1,6 @@
use anyhow::Result; use anyhow::Result;
use helix_core::Position; use helix_core::Position;
use helix_view::tree::Layout;
use std::path::{Path, PathBuf}; use std::path::{Path, PathBuf};
#[derive(Default)] #[derive(Default)]
@ -11,6 +12,7 @@ pub struct Args {
pub load_tutor: bool, pub load_tutor: bool,
pub fetch_grammars: bool, pub fetch_grammars: bool,
pub build_grammars: bool, pub build_grammars: bool,
pub split: Option<Layout>,
pub verbosity: u64, pub verbosity: u64,
pub files: Vec<(PathBuf, Position)>, pub files: Vec<(PathBuf, Position)>,
} }
@ -28,6 +30,8 @@ pub fn parse_args() -> Result<Args> {
"--version" => args.display_version = true, "--version" => args.display_version = true,
"--help" => args.display_help = true, "--help" => args.display_help = true,
"--tutor" => args.load_tutor = true, "--tutor" => args.load_tutor = true,
"--vsplit" => args.split = Some(Layout::Vertical),
"--hsplit" => args.split = Some(Layout::Horizontal),
"--health" => { "--health" => {
args.health = true; args.health = true;
args.health_arg = argv.next_if(|opt| !opt.starts_with('-')); args.health_arg = argv.next_if(|opt| !opt.starts_with('-'));

View File

@ -67,6 +67,8 @@ async fn main_impl() -> Result<i32> {
-v Increases logging verbosity each use for up to 3 times -v Increases logging verbosity each use for up to 3 times
(default file: {}) (default file: {})
-V, --version Prints version information -V, --version Prints version information
--vsplit Splits all given files vertically into different windows
--hsplit Splits all given files horizontally into different windows
", ",
env!("CARGO_PKG_NAME"), env!("CARGO_PKG_NAME"),
env!("VERSION_AND_GIT_HASH"), env!("VERSION_AND_GIT_HASH"),