gf as goto_file (#1102)

* goto_file

* support goto_file under current cursor

* add C-w f/F

* sync space w with window mode

* Update helix-term/src/commands.rs

Co-authored-by: Blaž Hrastnik <blaz@mxxn.io>
This commit is contained in:
Bob 2021-11-29 09:53:29 +08:00 committed by GitHub
parent 6f1a7b1220
commit 4f9390a435
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 54 additions and 0 deletions

View file

@ -160,6 +160,7 @@ Jumps to various locations.
| ----- | ----------- | ------- |
| `g` | Go to the start of the file | `goto_file_start` |
| `e` | Go to the end of the file | `goto_last_line` |
| `f` | Go to files in the selection | `goto_file` |
| `h` | Go to the start of the line | `goto_line_start` |
| `l` | Go to the end of the line | `goto_line_end` |
| `s` | Go to first non-whitespace character of the line | `goto_first_nonwhitespace` |
@ -202,6 +203,8 @@ This layer is similar to vim keybindings as kakoune does not support window.
| `v`, `Ctrl-v` | Vertical right split | `vsplit` |
| `s`, `Ctrl-s` | Horizontal bottom split | `hsplit` |
| `h`, `Ctrl-h`, `left` | Move to left split | `jump_view_left` |
| `f` | Go to files in the selection in horizontal splits | `goto_file` |
| `F` | Go to files in the selection in vertical splits | `goto_file` |
| `j`, `Ctrl-j`, `down` | Move to split below | `jump_view_down` |
| `k`, `Ctrl-k`, `up` | Move to split above | `jump_view_up` |
| `l`, `Ctrl-l`, `right` | Move to right split | `jump_view_right` |

View file

@ -259,6 +259,9 @@ impl Command {
goto_implementation, "Goto implementation",
goto_file_start, "Goto file start/line",
goto_file_end, "Goto file end",
goto_file, "Goto files in the selection",
goto_file_hsplit, "Goto files in the selection in horizontal splits",
goto_file_vsplit, "Goto files in the selection in vertical splits",
goto_reference, "Goto references",
goto_window_top, "Goto window top",
goto_window_middle, "Goto window middle",
@ -835,6 +838,49 @@ fn goto_file_end(cx: &mut Context) {
doc.set_selection(view.id, selection);
}
fn goto_file(cx: &mut Context) {
goto_file_impl(cx, Action::Replace);
}
fn goto_file_hsplit(cx: &mut Context) {
goto_file_impl(cx, Action::HorizontalSplit);
}
fn goto_file_vsplit(cx: &mut Context) {
goto_file_impl(cx, Action::VerticalSplit);
}
fn goto_file_impl(cx: &mut Context, action: Action) {
let (view, doc) = current_ref!(cx.editor);
let text = doc.text();
let selections = doc.selection(view.id);
let mut paths: Vec<_> = selections
.iter()
.map(|r| text.slice(r.from()..r.to()).to_string())
.collect();
let primary = selections.primary();
if selections.len() == 1 && primary.to() - primary.from() == 1 {
let current_word = movement::move_next_long_word_start(
text.slice(..),
movement::move_prev_long_word_start(text.slice(..), primary, 1),
1,
);
paths.clear();
paths.push(
text.slice(current_word.from()..current_word.to())
.to_string(),
);
}
for sel in paths {
let p = sel.trim();
if !p.is_empty() {
if let Err(e) = cx.editor.open(PathBuf::from(p), action) {
cx.editor.set_error(format!("Open file failed: {:?}", e));
}
}
}
}
fn extend_word_impl<F>(cx: &mut Context, extend_fn: F)
where
F: Fn(RopeSlice, Range, usize) -> Range,

View file

@ -512,6 +512,7 @@ impl Default for Keymaps {
"g" => { "Goto"
"g" => goto_file_start,
"e" => goto_last_line,
"f" => goto_file,
"h" => goto_line_start,
"l" => goto_line_end,
"s" => goto_first_nonwhitespace,
@ -622,6 +623,8 @@ impl Default for Keymaps {
"C-w" | "w" => rotate_view,
"C-s" | "s" => hsplit,
"C-v" | "v" => vsplit,
"f" => goto_file_hsplit,
"F" => goto_file_vsplit,
"C-q" | "q" => wclose,
"C-o" | "o" => wonly,
"C-h" | "h" | "left" => jump_view_left,
@ -650,6 +653,8 @@ impl Default for Keymaps {
"C-w" | "w" => rotate_view,
"C-s" | "s" => hsplit,
"C-v" | "v" => vsplit,
"f" => goto_file_hsplit,
"F" => goto_file_vsplit,
"C-q" | "q" => wclose,
"C-o" | "o" => wonly,
"C-h" | "h" | "left" => jump_view_left,