Skip to content

Commit

Permalink
helix-term: introduce cdf / change-directory-to-current-file
Browse files Browse the repository at this point in the history
This will set the current working directory to that of the current
file's parent.
  • Loading branch information
cole-h committed Oct 6, 2022
1 parent e38edae commit 87c9de5
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 23 deletions.
1 change: 1 addition & 0 deletions book/src/generated/typable-cmd.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
| `:primary-clipboard-paste-replace` | Replace selections with content of system primary clipboard. |
| `:show-clipboard-provider` | Show clipboard provider name in status bar. |
| `:change-current-directory`, `:cd` | Change the current working directory. |
| `:change-directory-to-current-file`, `:cdf` | Change the current working directory to the parent of the current document if provided no arguments. |
| `:show-directory`, `:pwd` | Show the current working directory. |
| `:encoding` | Set encoding. Based on `https://encoding.spec.whatwg.org`. |
| `:reload` | Discard changes and reload from the source file. |
Expand Down
72 changes: 49 additions & 23 deletions helix-term/src/commands/typed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -929,41 +929,60 @@ fn show_clipboard_provider(
Ok(())
}

fn change_current_directory(
fn change_directory_impl(cx: &mut compositor::Context, dir: &Path) -> anyhow::Result<()> {
if let Err(e) = std::env::set_current_dir(dir) {
bail!("Couldn't change the current working directory: {}", e);
}

let cwd = std::env::current_dir().context("Couldn't get the new working directory")?;
cx.editor.set_status(format!(
"Current working directory is now {}",
cwd.display()
));

Ok(())
}

fn change_directory_to_current_file(
cx: &mut compositor::Context,
args: &[Cow<str>],
_args: &[Cow<str>],
event: PromptEvent,
) -> anyhow::Result<()> {
if event != PromptEvent::Validate {
return Ok(());
}

let dir = match args.first() {
Some(dir) => helix_core::path::expand_tilde(dir.as_ref().as_ref()),
let (_, doc) = current_ref!(cx.editor);
let dir = match doc.path() {
Some(path) => path
.parent()
.map(ToOwned::to_owned)
.context("Couldn't get parent of current document")?,
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");
}
}
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);
change_directory_impl(cx, &dir)
}

fn change_current_directory(
cx: &mut compositor::Context,
args: &[Cow<str>],
event: PromptEvent,
) -> anyhow::Result<()> {
if event != PromptEvent::Validate {
return Ok(());
}

let cwd = std::env::current_dir().context("Couldn't get the new working directory")?;
cx.editor.set_status(format!(
"Current working directory is now {}",
cwd.display()
));
Ok(())
let dir = helix_core::path::expand_tilde(
args.first()
.context("target directory not provided")?
.as_ref()
.as_ref(),
);

change_directory_impl(cx, &dir)
}

fn show_current_directory(
Expand Down Expand Up @@ -1902,10 +1921,17 @@ pub const TYPABLE_COMMAND_LIST: &[TypableCommand] = &[
TypableCommand {
name: "change-current-directory",
aliases: &["cd"],
doc: "Change the current working directory. Uses the parent of the current document if provided no arguments.",
doc: "Change the current working directory.",
fun: change_current_directory,
completer: Some(completers::directory),
},
TypableCommand {
name: "change-directory-to-current-file",
aliases: &["cdf"],
doc: "Change the current working directory to the parent of the current document if provided no arguments.",
fun: change_directory_to_current_file,
completer: Some(completers::directory),
},
TypableCommand {
name: "show-directory",
aliases: &["pwd"],
Expand Down

0 comments on commit 87c9de5

Please sign in to comment.