Skip to content

Commit

Permalink
Allows open file at line and column with +<line>:<column> command line
Browse files Browse the repository at this point in the history
  • Loading branch information
Rational-Curiosity committed Nov 18, 2021
1 parent bd56dde commit d54db2b
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 2 deletions.
28 changes: 27 additions & 1 deletion helix-term/src/application.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ pub struct Application {

impl Application {
pub fn new(args: Args, mut config: Config) -> Result<Self, Error> {
use helix_view::document::Mode;
use helix_view::editor::Action;
let mut compositor = Compositor::new()?;
let size = compositor.size();
Expand Down Expand Up @@ -124,13 +125,38 @@ impl Application {
} else {
let nr_of_files = args.files.len();
editor.open(first.to_path_buf(), Action::VerticalSplit)?;
for file in args.files {
for (file, pos) in args.files.iter().zip(args.positions.iter()) {
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 {
editor.open(file.to_path_buf(), Action::Load)?;
if let Some((line, column)) = pos {
let (view, doc) = current!(editor);
let text = doc.text();
let max_line = if text.line(text.len_lines() - 1).len_chars() == 0 {
// If the last line is blank, don't jump to it.
text.len_lines().saturating_sub(2)
} else {
text.len_lines() - 1
};
let line_idx = std::cmp::min(line - 1, max_line);
let text = text.slice(..);
let mut pos = text.line_to_char(line_idx);
if let Some(col) = column {
let line_len_chars = text.line(line_idx).len_chars();
pos += if col > &line_len_chars {
&line_len_chars
} else {
col
} - 1;
}
let selection = doc.selection(view.id).clone().transform(|range| {
range.put_cursor(text, pos, doc.mode == Mode::Select)
});
doc.set_selection(view.id, selection);
}
}
}
editor.set_status(format!("Loaded {} files.", nr_of_files));
Expand Down
37 changes: 36 additions & 1 deletion helix-term/src/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ pub struct Args {
pub load_tutor: bool,
pub verbosity: u64,
pub files: Vec<PathBuf>,
pub positions: Vec<Option<(usize, Option<usize>)>>,
}

impl Args {
Expand All @@ -18,6 +19,7 @@ impl Args {

iter.next(); // skip the program, we don't care about that

let mut position: Option<(usize, Option<usize>)> = None;
for arg in &mut iter {
match arg.as_str() {
"--" => break, // stop parsing at this point treat the remaining as files
Expand All @@ -41,13 +43,46 @@ impl Args {
}
}
}
arg => args.files.push(PathBuf::from(arg)),
arg if arg.starts_with('+') => {
let mut line_col: Vec<usize> = vec![];
for number_str in arg[1..].split(":") {
match number_str.parse() {
Ok(number) => line_col.push(number),
Err(_) => {
return Err(Error::msg(format!(
"parsing {} expected number, actual {}",
arg, number_str
)));
}
}
}
match line_col.len() {
1 => {
position = Some((line_col[0], None));
}
2 => {
position = Some((line_col[0], Some(line_col[1])));
}
_ => {
return Err(Error::msg(format!(
"expected +<line>:<column>, actual {}",
arg
)))
}
}
}
arg => {
args.files.push(PathBuf::from(arg));
args.positions.push(position);
position = None;
}
}
}

// push the remaining args, if any to the files
for filename in iter {
args.files.push(PathBuf::from(filename));
args.positions.push(position);
}

Ok(args)
Expand Down

0 comments on commit d54db2b

Please sign in to comment.