Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,12 @@ editor = 'nano +{$line}'
editor = 'nvim +{$line} +"normal! zz"'
```

For further customization, `{$path}` is also available for the file path:

```toml
editor = 'code -g {$path}:{$line}'
```

Without `{$line}`, the editor opens at the top of the file.

## Extra Files
Expand Down
3 changes: 3 additions & 0 deletions config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ theme = "ocean"
# editor = 'nano +{$line}'
# editor = 'nvim +{$line} +"normal! zz"'
#
# Also, {$path} offers more customization options:
# editor = 'code -g {$path}:{$line}'
#
# Priority: --editor flag > LEAF_EDITOR > this setting > nano (notepad on Windows)
# editor = "nano"

Expand Down
2 changes: 1 addition & 1 deletion src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ fn open_config_in_editor(path: &Path) -> anyhow::Result<()> {
}

fn launch_editor(editor: &str, path: &Path) {
let expanded = crate::editor::expand_line_placeholder(editor, 1);
let expanded = crate::editor::expand_editor_placeholders(editor, 1, path);
if try_launch_editor(&expanded, path) {
return;
}
Expand Down
7 changes: 5 additions & 2 deletions src/editor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@ use std::path::{Path, PathBuf};
use std::process::{Command, Stdio};

const EDITOR_LINE_PLACEHOLDER: &str = "{$line}";
const EDITOR_PATH_PLACEHOLDER: &str = "{$path}";

pub(crate) fn expand_line_placeholder(editor_cmd: &str, line: usize) -> String {
editor_cmd.replace(EDITOR_LINE_PLACEHOLDER, &line.to_string())
pub(crate) fn expand_editor_placeholders(editor_cmd: &str, line: usize, path: &Path) -> String {
editor_cmd
.replace(EDITOR_LINE_PLACEHOLDER, &line.to_string())
.replace(EDITOR_PATH_PLACEHOLDER, &path.display().to_string())
}

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
Expand Down
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ const MAX_STDIN_BYTES: usize = 8 * 1024 * 1024;
pub(crate) use config::{config_path, LeafConfig};
#[cfg(test)]
pub(crate) use editor::{
binary_name, classify, expand_line_placeholder, resolve_editor, selection_modifier_label,
binary_name, classify, expand_editor_placeholders, resolve_editor, selection_modifier_label,
split_editor_cmd, try_new_tab_command, EditorKind, TerminalEmulator,
};
#[cfg(test)]
Expand Down
2 changes: 1 addition & 1 deletion src/runtime/mouse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ pub(super) fn handle_open_in_editor(
let editor_cmd = match app.editor_config() {
Some(e) => {
let visible_source_line = app.source_line_at(app.scroll());
editor::expand_line_placeholder(e, visible_source_line)
editor::expand_editor_placeholders(e, visible_source_line, &filepath)
}
None => {
app.set_editor_flash(EditorFlash::EditorNotFound("no editor configured".into()));
Expand Down
40 changes: 30 additions & 10 deletions src/tests/editor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -261,35 +261,55 @@ fn resolve_editor_config_takes_priority_over_fallback() {
}

#[test]
fn expand_line_placeholder_no_placeholder_returns_unchanged() {
let result = expand_line_placeholder("nvim", 42);
fn expand_editor_placeholders_no_placeholder_returns_unchanged() {
let result = expand_editor_placeholders("nvim", 42, Path::new(""));
assert_eq!(result, "nvim");
}

#[test]
fn expand_line_placeholder_substitutes_single_occurrence() {
let result = expand_line_placeholder("nvim +{$line}", 42);
fn expand_editor_placeholders_substitutes_single_occurrence() {
let result = expand_editor_placeholders("nvim +{$line}", 42, Path::new(""));
assert_eq!(result, "nvim +42");
}

#[test]
fn expand_line_placeholder_substitutes_all_occurrences() {
let result = expand_line_placeholder("code -g {$line}:{$line}", 7);
fn expand_editor_placeholders_substitutes_all_occurrences() {
let result = expand_editor_placeholders("code -g {$line}:{$line}", 7, Path::new(""));
assert_eq!(result, "code -g 7:7");
}

#[test]
fn expand_line_placeholder_preserves_surrounding_chars() {
let result = expand_line_placeholder(r#"nvim +{$line} +"normal! zz""#, 123);
fn expand_editor_placeholders_preserves_surrounding_chars() {
let result = expand_editor_placeholders(r#"nvim +{$line} +"normal! zz""#, 123, Path::new(""));
assert_eq!(result, r#"nvim +123 +"normal! zz""#);
}

#[test]
fn expand_line_placeholder_ignores_unsupported_variants() {
let result = expand_line_placeholder("nvim +${line} +{line} +{$LINE}", 5);
fn expand_editor_placeholders_ignores_unsupported_variants() {
let result = expand_editor_placeholders("nvim +${line} +{line} +{$LINE}", 5, Path::new(""));
assert_eq!(result, "nvim +${line} +{line} +{$LINE}");
}

#[test]
fn expand_editor_placeholders_line_and_path() {
let result = expand_editor_placeholders("code -g {$path}:{$line}", 0, Path::new("file.rs"));
assert_eq!(result, "code -g file.rs:0");
}

#[test]
fn expand_editor_placeholders_multiple_occurrences() {
let result =
expand_editor_placeholders("code {$path} && echo {$path}", 1, Path::new("test.md"));
assert_eq!(result, "code test.md && echo test.md");
}

#[test]
fn expand_editor_placeholders_full_path() {
let result =
expand_editor_placeholders("code -g {$path}:{$line}", 8, Path::new("/tmp/file.rs"));
assert_eq!(result, "code -g /tmp/file.rs:8");
}

#[test]
fn split_editor_cmd_quoted_arg_with_space() {
let (bin, args) = split_editor_cmd(r#"nvim +123 +"normal! zz""#);
Expand Down
Loading