From 66fbb60001d34cdc1f2302068befb8fd3895474e Mon Sep 17 00:00:00 2001 From: Nachum Barcohen <38861757+nabaco@users.noreply.github.com> Date: Fri, 20 Jan 2023 04:30:05 +0200 Subject: [PATCH 1/3] Accept +num flag for opening at line number --- helix-term/src/args.rs | 15 +++++++++++++++ helix-term/src/main.rs | 1 + 2 files changed, 16 insertions(+) diff --git a/helix-term/src/args.rs b/helix-term/src/args.rs index f782539caa90..86fe69fce4c0 100644 --- a/helix-term/src/args.rs +++ b/helix-term/src/args.rs @@ -18,6 +18,7 @@ pub struct Args { pub config_file: Option, pub files: Vec<(PathBuf, Position)>, pub working_directory: Option, + pub line_number: usize, } impl Args { @@ -88,6 +89,16 @@ impl Args { } } } + arg if arg.starts_with('+') => { + let arg = arg.get(1..).unwrap(); + args.line_number = match arg.parse() { + Ok(n) => n, + _ => anyhow::bail!("bad line number after +"), + }; + if args.line_number > 0 { + args.line_number -= 1; + } + } arg => args.files.push(parse_file(arg)), } } @@ -97,6 +108,10 @@ impl Args { args.files.push(parse_file(&arg)); } + if let Some(file) = args.files.first_mut() { + file.1.row = args.line_number; + } + Ok(args) } } diff --git a/helix-term/src/main.rs b/helix-term/src/main.rs index ed3478ac9bd6..c9b35ffc9e29 100644 --- a/helix-term/src/main.rs +++ b/helix-term/src/main.rs @@ -67,6 +67,7 @@ FLAGS: --vsplit Splits all given files vertically into different windows --hsplit Splits all given files horizontally into different windows -w, --working-dir Specify an initial working directory + +N Goto line number N ", env!("CARGO_PKG_NAME"), VERSION_AND_GIT_HASH, From 6410e6507069a5ad9341ccd4bbeba83874e99ae7 Mon Sep 17 00:00:00 2001 From: Bjorn Ove Hay Andersen Date: Wed, 11 Oct 2023 19:34:34 +0200 Subject: [PATCH 2/3] Update +N argument feature according to feedback in original PR #5603 --- helix-term/src/args.rs | 13 +++++-------- helix-term/src/main.rs | 2 +- 2 files changed, 6 insertions(+), 9 deletions(-) diff --git a/helix-term/src/args.rs b/helix-term/src/args.rs index 86fe69fce4c0..4219d989fd67 100644 --- a/helix-term/src/args.rs +++ b/helix-term/src/args.rs @@ -18,13 +18,13 @@ pub struct Args { pub config_file: Option, pub files: Vec<(PathBuf, Position)>, pub working_directory: Option, - pub line_number: usize, } impl Args { pub fn parse_args() -> Result { let mut args = Args::default(); let mut argv = std::env::args().peekable(); + let mut line_number = 0; argv.next(); // skip the program, we don't care about that @@ -90,14 +90,11 @@ impl Args { } } arg if arg.starts_with('+') => { - let arg = arg.get(1..).unwrap(); - args.line_number = match arg.parse() { - Ok(n) => n, + let arg = &arg[1..]; + line_number = match arg.parse::() { + Ok(n) => n.saturating_sub(1), _ => anyhow::bail!("bad line number after +"), }; - if args.line_number > 0 { - args.line_number -= 1; - } } arg => args.files.push(parse_file(arg)), } @@ -109,7 +106,7 @@ impl Args { } if let Some(file) = args.files.first_mut() { - file.1.row = args.line_number; + file.1.row = line_number; } Ok(args) diff --git a/helix-term/src/main.rs b/helix-term/src/main.rs index c9b35ffc9e29..8db5f3100a01 100644 --- a/helix-term/src/main.rs +++ b/helix-term/src/main.rs @@ -67,7 +67,7 @@ FLAGS: --vsplit Splits all given files vertically into different windows --hsplit Splits all given files horizontally into different windows -w, --working-dir Specify an initial working directory - +N Goto line number N + +N Open the first given file at line number N ", env!("CARGO_PKG_NAME"), VERSION_AND_GIT_HASH, From 72d712bb436ca2dbb3f86702b60a3eeba07d3a3c Mon Sep 17 00:00:00 2001 From: Bjorn Ove Hay Andersen Date: Wed, 11 Oct 2023 19:41:15 +0200 Subject: [PATCH 3/3] Only override the line number of the first file if +N is specified --- helix-term/src/args.rs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/helix-term/src/args.rs b/helix-term/src/args.rs index 4219d989fd67..6a49889b678a 100644 --- a/helix-term/src/args.rs +++ b/helix-term/src/args.rs @@ -106,7 +106,9 @@ impl Args { } if let Some(file) = args.files.first_mut() { - file.1.row = line_number; + if line_number != 0 { + file.1.row = line_number; + } } Ok(args)