Skip to content

Commit

Permalink
feat: relative file harps
Browse files Browse the repository at this point in the history
  • Loading branch information
Axlefublr committed Nov 6, 2024
1 parent 2c9bda6 commit 706e8b0
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 5 deletions.
30 changes: 26 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -233,15 +233,37 @@ With a project file harp, I can use `c` again! Matter of fact, I can use `c` in

When deciding between a file harp and a project file harp, ask yourself this question: "when I want to access this file, what will my current working directory generally be?".

### Relative file harps

```
harp_relative_file_set
harp_relative_file_get
```

`harp_relative_file_set` takes current buffer's path, relative to cwd, and stores it in a harp in the `harp_relative_files` section. \
`harp_relative_file_get` takes the stored path, and opens it (relative to cwd).

Project structure tends to repeat: rust projects will have a `Cargo.toml`, `src/main.rs` or `src/lib.rs`; *every* project will have a `README.md`, maybe a `CONTRIBUTING.MD`; almost every git repo will have a `.gitignore`, and you may also use `.git/info/exclude`.

As you use project file harps for these, you'll quickly notice that doing so is inefficient. \
Project file harps make the most sense for files that are "special" to a specific project, while all of these are fairly regular.

*Relative* file harps let you store just the *relative* part of a file in a harp. \
If your current buffer is `~/prog/dotfiles/README.md`, and your current working directory is `~/prog/dotfiles`, \
A *relative* file harp will store just `README.md`, while a normal file harp would store the *entire* path: `~/prog/dotfiles/README.md`.

What this lets you do is lovely: you can store a file just once, and then continue reusing the harp for it for all of your projects. \
Instead of having to type in `:e .gitignore` to get to it, you just use the relative file harp `g` (for example) and immediately jump to the gitignore file of *the current* project.

### Cwd harps

```
harp_cwd_set
harp_cwd_get
```

`set` takes your current working directory (like from `:pwd`), and stores it in a harp in the `harp_dirs` section. \
`get` takes the stored directory, and `:cd`s into it.
`harp_cwd_set` takes your current working directory (like from `:pwd`), and stores it in a harp in the `harp_dirs` section. \
`harp_cwd_get` takes the stored directory, and `:cd`s into it.

Development is pretty projectual, and jumping through a bunch of commonly visited directories can be a chore. Closing and reopening helix just to fuzzy search some file somewhere is a bit too much effort.

Expand Down Expand Up @@ -279,8 +301,8 @@ harp_search_set
harp_search_get
```

`set` gets your latest search (stored in the `/` register) and stores it in a harp in the `harp_searches` section. \
`get` puts the stored search into your `/` register, effectively "making a search".
`harp_search_set` gets your latest search (stored in the `/` register) and stores it in a harp in the `harp_searches` section. \
`harp_search_get` puts the stored search into your `/` register, effectively "making a search".

A really obvious thing to want to do in an editor is to trim trailing whitespace. \
In helix it's a bit of a hassle: `%s[ \t]+$<CR>d`, where `<CR>` means <kbd>Enter</kbd>. \
Expand Down
2 changes: 1 addition & 1 deletion book/src/generated/typable-cmd.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,5 +92,5 @@
| `:read`, `:r` | Load a file into buffer |
| `:random`, `:rng`, `:rnd` | Randomize your selections |
| `:echo`, `:c` | Print to the messages line |
| `:echopy`, `:cc` | Put string into selected register (`+` by default) |
| `:echopy`, `:cc` | Put string into clipboard |
| `:reload-history` | Reload history files for persistent state |
2 changes: 2 additions & 0 deletions helix-term/src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -578,6 +578,8 @@ impl MappableCommand {
//-----------------------------------------------fork-----------------------------------------------
harp_file_get, "Open a file harp",
harp_file_set, "Set a file harp to the current buffer",
harp_relative_file_get, "Open a relative file harp",
harp_relative_file_set, "Set a relative file harp to the current buffer",
harp_project_file_get, "Open a relative to cwd file harp",
harp_project_file_set, "Set a relative to cwd file harp to the current buffer",
harp_cwd_get, ":cd into a cwd harp",
Expand Down
70 changes: 70 additions & 0 deletions helix-term/src/commands/forkcommands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,76 @@ pub fn harp_file_set(cx: &mut Context) {
)
}

pub fn harp_relative_file_get(cx: &mut Context) {
ui::prompt(
cx,
"harp relative file get:".into(),
None,
ui::completers::none,
move |cx, input: &str, event: PromptEvent| {
if event != PromptEvent::Validate {
return;
}
if input.is_empty() {
return;
}

let values = match HarpOutput::build("harp_relative_files", input, HarpContract::path())
{
Ok(values) => values,
Err(msg) => {
cx.editor.set_error(msg);
return;
}
};

cx.editor
.open(&values.path.unwrap(), Action::Replace)
.unwrap();
},
)
}

pub fn harp_relative_file_set(cx: &mut Context) {
ui::prompt(
cx,
"harp relative file set:".into(),
None,
ui::completers::none,
move |cx, input: &str, event: PromptEvent| {
if event != PromptEvent::Validate {
return;
}
if input.is_empty() {
return;
}

let (_, doc) = current!(cx.editor);
let Some(path) = &doc.path else {
cx.editor
.set_error("harp: current buffer doesn't have a path");
return;
};

let path = path
.strip_prefix(helix_stdx::env::current_working_dir())
.unwrap_or(path);
if let Err(msg) = harp_update(
"harp_relative_files",
input,
HarpInput {
path: Some(path.display().to_string()),
..Default::default()
},
) {
cx.editor.set_error(msg);
} else {
cx.editor.set_status("harp: set success");
};
},
)
}

pub fn harp_project_file_get(cx: &mut Context) {
ui::prompt(
cx,
Expand Down

0 comments on commit 706e8b0

Please sign in to comment.