Skip to content

Commit

Permalink
helix-term: cd to the parent of the current document when provided no…
Browse files Browse the repository at this point in the history
… arguments
  • Loading branch information
cole-h committed Aug 23, 2022
1 parent e4c9d40 commit bee1dec
Showing 1 changed file with 16 additions and 7 deletions.
23 changes: 16 additions & 7 deletions helix-term/src/commands/typed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -911,12 +911,21 @@ fn change_current_directory(
return Ok(());
}

let dir = helix_core::path::expand_tilde(
args.first()
.context("target directory not provided")?
.as_ref()
.as_ref(),
);
let dir = match args.first() {
Some(dir) => helix_core::path::expand_tilde(dir.as_ref().as_ref()),
None => {
let (_, doc) = current_ref!(cx.editor);
match doc.path() {
Some(path) => path
.parent()
.map(ToOwned::to_owned)
.context("Couldn't get parent of current document")?,
None => {
bail!("Current document has no path");
}
}
}
};

if let Err(e) = std::env::set_current_dir(dir) {
bail!("Couldn't change the current working directory: {}", e);
Expand Down Expand Up @@ -1802,7 +1811,7 @@ pub const TYPABLE_COMMAND_LIST: &[TypableCommand] = &[
TypableCommand {
name: "change-current-directory",
aliases: &["cd"],
doc: "Change the current working directory.",
doc: "Change the current working directory. Uses the parent of the current document if provided no arguments.",
fun: change_current_directory,
completer: Some(completers::directory),
},
Expand Down

0 comments on commit bee1dec

Please sign in to comment.