From f9e40316f41a2fb7488d465a29af5413127c85f4 Mon Sep 17 00:00:00 2001 From: = Date: Wed, 15 Jun 2022 01:33:31 +0200 Subject: [PATCH 1/4] adds --vsplit and --hsplit arguments --- contrib/completion/hx.bash | 2 +- contrib/completion/hx.fish | 3 ++- contrib/completion/hx.zsh | 2 ++ helix-term/src/application.rs | 21 +++++++++++++++++++-- helix-term/src/args.rs | 4 ++++ helix-term/src/main.rs | 2 ++ 6 files changed, 30 insertions(+), 4 deletions(-) diff --git a/contrib/completion/hx.bash b/contrib/completion/hx.bash index 6371bedb67bb..87c340284f6e 100644 --- a/contrib/completion/hx.bash +++ b/contrib/completion/hx.bash @@ -16,7 +16,7 @@ _hx() { 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 } && complete -F _hx hx diff --git a/contrib/completion/hx.fish b/contrib/completion/hx.fish index 4ec690d8b0b8..df2fb500c935 100644 --- a/contrib/completion/hx.fish +++ b/contrib/completion/hx.fish @@ -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 v -o vv -o vvv -d "Increases logging verbosity" 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" diff --git a/contrib/completion/hx.zsh b/contrib/completion/hx.zsh index 16631519bc46..f9d58d3c6569 100644 --- a/contrib/completion/hx.zsh +++ b/contrib/completion/hx.zsh @@ -14,6 +14,8 @@ _hx() { "--health[Checks for errors in editor setup]:language:->health" \ "-g[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" case "$state" in diff --git a/helix-term/src/application.rs b/helix-term/src/application.rs index 2dfccf0435ec..e4a2faa595ec 100644 --- a/helix-term/src/application.rs +++ b/helix-term/src/application.rs @@ -148,14 +148,31 @@ impl Application { } else { let nr_of_files = args.files.len(); editor.open(first.to_path_buf(), 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() { return Err(anyhow::anyhow!( "expected a path to file, found a directory. (to open a directory pass it as first argument)" )); } else { - let doc_id = editor.open(file, Action::Load)?; + // 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 = if args.vsplit { + Action::VerticalSplit + } else if args.hsplit { + Action::HorizontalSplit + } else { + Action::Load // neither vsplit nor hsplit were passed in, so just open files normally + }; + let doc_id = editor.open(file, action)?; // 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 doc = editor.document_mut(doc_id).unwrap(); let pos = Selection::point(pos_at_coords(doc.text().slice(..), pos, true)); diff --git a/helix-term/src/args.rs b/helix-term/src/args.rs index b99c7d1a2768..6d81c0526883 100644 --- a/helix-term/src/args.rs +++ b/helix-term/src/args.rs @@ -11,6 +11,8 @@ pub struct Args { pub load_tutor: bool, pub fetch_grammars: bool, pub build_grammars: bool, + pub vsplit: bool, + pub hsplit: bool, pub verbosity: u64, pub files: Vec<(PathBuf, Position)>, } @@ -28,6 +30,8 @@ impl Args { "--version" => args.display_version = true, "--help" => args.display_help = true, "--tutor" => args.load_tutor = true, + "--vsplit" => args.vsplit = true, + "--hsplit" => args.hsplit = true, "--health" => { args.health = true; args.health_arg = argv.next_if(|opt| !opt.starts_with('-')); diff --git a/helix-term/src/main.rs b/helix-term/src/main.rs index 58a901316d7f..f4f1c0b3b482 100644 --- a/helix-term/src/main.rs +++ b/helix-term/src/main.rs @@ -65,6 +65,8 @@ FLAGS: -v Increases logging verbosity each use for up to 3 times (default file: {}) -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!("VERSION_AND_GIT_HASH"), From 5df2b16ccee4f1aeba34c257de073a1e59ecb429 Mon Sep 17 00:00:00 2001 From: plexom <48958093+plexom@users.noreply.github.com> Date: Wed, 15 Jun 2022 12:18:19 +0200 Subject: [PATCH 2/4] moved comment --- helix-term/src/application.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/helix-term/src/application.rs b/helix-term/src/application.rs index e4a2faa595ec..b56572adbac6 100644 --- a/helix-term/src/application.rs +++ b/helix-term/src/application.rs @@ -166,7 +166,8 @@ impl Application { } else if args.hsplit { Action::HorizontalSplit } else { - Action::Load // neither vsplit nor hsplit were passed in, so just open files normally + // neither vsplit nor hsplit were passed in, so just open files normally + Action::Load }; let doc_id = editor.open(file, action)?; // with Action::Load all documents have the same view From 6dc8e51dd25b3c7bdc2948df12ea0662c26ccf10 Mon Sep 17 00:00:00 2001 From: plexom <48958093+plexom@users.noreply.github.com> Date: Wed, 15 Jun 2022 17:41:32 +0200 Subject: [PATCH 3/4] fixed lint (third time's a charm) --- helix-term/src/application.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/helix-term/src/application.rs b/helix-term/src/application.rs index b56572adbac6..8267f28ea4b2 100644 --- a/helix-term/src/application.rs +++ b/helix-term/src/application.rs @@ -167,7 +167,7 @@ impl Application { Action::HorizontalSplit } else { // neither vsplit nor hsplit were passed in, so just open files normally - Action::Load + Action::Load }; let doc_id = editor.open(file, action)?; // with Action::Load all documents have the same view From 645609b5044669b568a73c6bf2e7ada8ce653f3e Mon Sep 17 00:00:00 2001 From: = Date: Fri, 24 Jun 2022 20:51:30 +0200 Subject: [PATCH 4/4] changed vsplit and hsplit from two separate bools to type Option, and some cleanup --- helix-term/src/application.rs | 15 ++++++--------- helix-term/src/args.rs | 8 ++++---- 2 files changed, 10 insertions(+), 13 deletions(-) diff --git a/helix-term/src/application.rs b/helix-term/src/application.rs index 499b487a6fc6..5e310e1ccbaf 100644 --- a/helix-term/src/application.rs +++ b/helix-term/src/application.rs @@ -5,7 +5,7 @@ use helix_core::{ pos_at_coords, syntax, Selection, }; 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 crate::{ @@ -171,19 +171,16 @@ impl Application { // 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 = if args.vsplit { - Action::VerticalSplit - } else if args.hsplit { - Action::HorizontalSplit - } else { - // neither vsplit nor hsplit were passed in, so just open files normally - Action::Load + let action = match args.split { + Some(Layout::Vertical) => Action::VerticalSplit, + Some(Layout::Horizontal) => Action::HorizontalSplit, + None => Action::Load, }; let doc_id = editor .open(&file, action) .context(format!("open '{}'", file.to_string_lossy()))?; // with Action::Load all documents have the same view - // NOTE: this isn't necessarily true anymore, if + // 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; diff --git a/helix-term/src/args.rs b/helix-term/src/args.rs index 6d81c0526883..c3019ea7c875 100644 --- a/helix-term/src/args.rs +++ b/helix-term/src/args.rs @@ -1,5 +1,6 @@ use anyhow::Result; use helix_core::Position; +use helix_view::tree::Layout; use std::path::{Path, PathBuf}; #[derive(Default)] @@ -11,8 +12,7 @@ pub struct Args { pub load_tutor: bool, pub fetch_grammars: bool, pub build_grammars: bool, - pub vsplit: bool, - pub hsplit: bool, + pub split: Option, pub verbosity: u64, pub files: Vec<(PathBuf, Position)>, } @@ -30,8 +30,8 @@ impl Args { "--version" => args.display_version = true, "--help" => args.display_help = true, "--tutor" => args.load_tutor = true, - "--vsplit" => args.vsplit = true, - "--hsplit" => args.hsplit = true, + "--vsplit" => args.split = Some(Layout::Vertical), + "--hsplit" => args.split = Some(Layout::Horizontal), "--health" => { args.health = true; args.health_arg = argv.next_if(|opt| !opt.starts_with('-'));