Skip to content

Commit

Permalink
Allow specifying file start position
Browse files Browse the repository at this point in the history
Like helix-term/src/commands.rs:3426:15
  • Loading branch information
pickfire committed Jul 20, 2021
1 parent 585d6f8 commit 5f1abac
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 14 deletions.
27 changes: 22 additions & 5 deletions helix-term/src/application.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
use helix_core::syntax;
use helix_core::{syntax, Selection};
use helix_lsp::{lsp, util::lsp_pos_to_pos, LspProgressMap};
use helix_view::{theme, Editor};

use crate::{args::Args, compositor::Compositor, config::Config, job::Jobs, ui};
use crate::{
args::Args,
commands::{align_view, Align},
compositor::Compositor,
config::Config,
job::Jobs,
ui,
};

use log::error;

Expand Down Expand Up @@ -78,20 +85,30 @@ impl Application {
compositor.push(editor_view);

if !args.files.is_empty() {
let first = &args.files[0]; // we know it's not empty
let first = &args.files[0].0; // we know it's not empty
if first.is_dir() {
editor.new_file(Action::VerticalSplit);
compositor.push(Box::new(ui::file_picker(first.clone())));
} else {
let nr_of_files = args.files.len();
editor.open(first.to_path_buf(), Action::VerticalSplit)?;
for file in args.files {
for (file, row, col) in args.files {
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)?;
editor.open(file, Action::Load)?;
// let docid = editor.open(file, Action::Load)?;
// editor.document_mut(docid).unwrap().set_selection();
// FIXME: current is incorrect since it will only work for current
// document, other files loaded in background will result in panic
let (view, doc) = current!(editor);
let pos = Selection::point(doc.text().line_to_char(row) + col);
doc.set_selection(view.id, pos.clone());
if pos != Selection::point(0) {
align_view(doc, view, Align::Center);
}
}
}
editor.set_status(format!("Loaded {} files.", nr_of_files));
Expand Down
24 changes: 20 additions & 4 deletions helix-term/src/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ pub struct Args {
pub display_help: bool,
pub display_version: bool,
pub verbosity: u64,
pub files: Vec<PathBuf>,
pub files: Vec<(PathBuf, usize, usize)>,
}

impl Args {
Expand Down Expand Up @@ -39,15 +39,31 @@ impl Args {
}
}
}
arg => args.files.push(PathBuf::from(arg)),
arg => args.files.push(parse_file(arg)),
}
}

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

Ok(args)
}
}

/// Parse arg into [`PathBuf`] and position.
pub(crate) fn parse_file(s: &str) -> (PathBuf, usize, usize) {
split_path_pos(s).unwrap_or_else(|| (PathBuf::from(s), 0, 0))
}

/// Split file.rs:10:2 into [`PathBuf`] and position.
///
/// Does not validate if file.rs is a file or directory.
fn split_path_pos(s: &str) -> Option<(PathBuf, usize, usize)> {
let mut s = s.rsplitn(3, ':');
let col: usize = s.next()?.parse().ok()?;
let row: usize = s.next()?.parse().ok()?;
let path = s.next()?.into();
Some((path, row.saturating_sub(1), col.saturating_sub(1)))
}
16 changes: 12 additions & 4 deletions helix-term/src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ use insert::*;
use movement::Movement;

use crate::{
args,
compositor::{self, Component, Compositor},
ui::{self, Picker, Popup, Prompt, PromptEvent},
};
Expand Down Expand Up @@ -117,13 +118,13 @@ impl<'a> Context<'a> {
}
}

enum Align {
pub(crate) enum Align {
Top,
Center,
Bottom,
}

fn align_view(doc: &Document, view: &mut View, align: Align) {
pub(crate) fn align_view(doc: &Document, view: &mut View, align: Align) {
let pos = doc.selection(view.id).cursor();
let line = doc.text().char_to_line(pos);

Expand Down Expand Up @@ -1251,8 +1252,15 @@ mod cmd {
args: &[&str],
_event: PromptEvent,
) -> anyhow::Result<()> {
let path = args.get(0).context("wrong argument count")?;
let _ = cx.editor.open(path.into(), Action::Replace)?;
let arg = args.get(0).context("wrong argument count")?;
let (path, row, col) = args::parse_file(arg);
let _ = cx.editor.open(path, Action::Replace)?;
let (view, doc) = current!(cx.editor);
let pos = Selection::point(doc.text().line_to_char(row) + col);
doc.set_selection(view.id, pos.clone());
if pos != Selection::point(0) {
align_view(doc, view, Align::Center);
}
Ok(())
}

Expand Down
2 changes: 1 addition & 1 deletion helix-term/src/ui/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ pub fn file_picker(root: PathBuf) -> Picker<PathBuf> {
},
move |editor: &mut Editor, path: &PathBuf, action| {
editor
.open(path.into(), action)
.open(path.clone(), action)
.expect("editor.open failed");
},
)
Expand Down

0 comments on commit 5f1abac

Please sign in to comment.