From 736e7a3320b05695be1b475af794970ec34676bc Mon Sep 17 00:00:00 2001
From: Bjorn Ove Hay Andersen <bjrnove@gmail.com>
Date: Thu, 12 Oct 2023 10:35:43 +0200
Subject: [PATCH] Add +N CLI argument to jump to first file's line number
 (#8521)

* Accept +num flag for opening at line number

* Update +N argument feature according to feedback in original PR #5603

* Only override the line number of the first file if +N is specified

---------

Co-authored-by: Nachum Barcohen <38861757+nabaco@users.noreply.github.com>
---
 helix-term/src/args.rs | 14 ++++++++++++++
 helix-term/src/main.rs |  1 +
 2 files changed, 15 insertions(+)

diff --git a/helix-term/src/args.rs b/helix-term/src/args.rs
index f782539caa90..6a49889b678a 100644
--- a/helix-term/src/args.rs
+++ b/helix-term/src/args.rs
@@ -24,6 +24,7 @@ impl Args {
     pub fn parse_args() -> Result<Args> {
         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
 
@@ -88,6 +89,13 @@ impl Args {
                         }
                     }
                 }
+                arg if arg.starts_with('+') => {
+                    let arg = &arg[1..];
+                    line_number = match arg.parse::<usize>() {
+                        Ok(n) => n.saturating_sub(1),
+                        _ => anyhow::bail!("bad line number after +"),
+                    };
+                }
                 arg => args.files.push(parse_file(arg)),
             }
         }
@@ -97,6 +105,12 @@ impl Args {
             args.files.push(parse_file(&arg));
         }
 
+        if let Some(file) = args.files.first_mut() {
+            if line_number != 0 {
+                file.1.row = line_number;
+            }
+        }
+
         Ok(args)
     }
 }
diff --git a/helix-term/src/main.rs b/helix-term/src/main.rs
index ed3478ac9bd6..8db5f3100a01 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 <path>       Specify an initial working directory
+    +N                             Open the first given file at line number N
 ",
         env!("CARGO_PKG_NAME"),
         VERSION_AND_GIT_HASH,