Skip to content

Commit

Permalink
feat: Allow creating new files with given path
Browse files Browse the repository at this point in the history
  • Loading branch information
nkitsaini committed Aug 20, 2023
1 parent ebf1b52 commit bbb1a5d
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 12 deletions.
6 changes: 3 additions & 3 deletions helix-term/src/application.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ impl Application {
let first = &args.files[0].0; // we know it's not empty
if first.is_dir() {
helix_loader::set_current_working_dir(first.clone())?;
editor.new_file(Action::VerticalSplit);
editor.new_file(Action::VerticalSplit, None);
let picker = ui::file_picker(".".into(), &config.load().editor);
compositor.push(Box::new(overlaid(picker)));
} else {
Expand Down Expand Up @@ -211,11 +211,11 @@ impl Application {
align_view(doc, view, Align::Center);
}
} else if stdin().is_tty() || cfg!(feature = "integration") {
editor.new_file(Action::VerticalSplit);
editor.new_file(Action::VerticalSplit, None);
} else {
editor
.new_file_from_stdin(Action::VerticalSplit)
.unwrap_or_else(|_| editor.new_file(Action::VerticalSplit));
.unwrap_or_else(|_| editor.new_file(Action::VerticalSplit, None));
}

editor.set_theme(theme);
Expand Down
4 changes: 2 additions & 2 deletions helix-term/src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4846,15 +4846,15 @@ fn hsplit(cx: &mut Context) {
}

fn hsplit_new(cx: &mut Context) {
cx.editor.new_file(Action::HorizontalSplit);
cx.editor.new_file(Action::HorizontalSplit, None);
}

fn vsplit(cx: &mut Context) {
split(cx, Action::VerticalSplit);
}

fn vsplit_new(cx: &mut Context) {
cx.editor.new_file(Action::VerticalSplit);
cx.editor.new_file(Action::VerticalSplit, None);
}

fn wclose(cx: &mut Context) {
Expand Down
12 changes: 7 additions & 5 deletions helix-term/src/commands/typed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -421,7 +421,11 @@ fn new_file(
return Ok(());
}

cx.editor.new_file(Action::Replace);
// We have to create path_buf and path seperately otherwise the &Path will have no owner
let path_buf = _args.first().map(|x| PathBuf::from(x.as_ref()));
let path = path_buf.as_ref().map(|path| path.as_path());

cx.editor.new_file(Action::Replace, path);

Ok(())
}
Expand Down Expand Up @@ -1584,7 +1588,7 @@ fn vsplit_new(
return Ok(());
}

cx.editor.new_file(Action::VerticalSplit);
cx.editor.new_file(Action::VerticalSplit, None);

Ok(())
}
Expand All @@ -1598,7 +1602,7 @@ fn hsplit_new(
return Ok(());
}

cx.editor.new_file(Action::HorizontalSplit);
cx.editor.new_file(Action::HorizontalSplit, None);

Ok(())
}
Expand Down Expand Up @@ -2408,8 +2412,6 @@ pub const TYPABLE_COMMAND_LIST: &[TypableCommand] = &[
aliases: &["n"],
doc: "Create a new scratch buffer.",
fun: new_file,
// TODO: This seems to complete with a filename, but doesn't use that filename to
// set the path of the newly created buffer.
signature: CommandSignature::positional(&[completers::filename]),
},
TypableCommand {
Expand Down
7 changes: 5 additions & 2 deletions helix-view/src/editor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1411,8 +1411,11 @@ impl Editor {
id
}

pub fn new_file(&mut self, action: Action) -> DocumentId {
self.new_file_from_document(action, Document::default(self.config.clone()))
pub fn new_file(&mut self, action: Action, path: Option<&Path>) -> DocumentId {
let mut document = Document::default(self.config.clone());
document.set_path(path);

self.new_file_from_document(action, document)
}

pub fn new_file_from_stdin(&mut self, action: Action) -> Result<DocumentId, Error> {
Expand Down

0 comments on commit bbb1a5d

Please sign in to comment.