on master help sign in
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>

commit4f9390a435a44578bcedf1da63ea1fad92185afc authorBob <QiBaobin@users.noreply.github.com> dateMon 29 Nov 2021, 09:53 (5 years ago) parent6f1a7b1
3 files changed+54
book/src/keymap.md+3 helix-term/src/commands.rs+46 helix-term/src/keymap.rs+5
book/src/keymap.md+3
@@ -160,6 +160,7 @@ Jumps to various locations.
160160| ----- | ----------- | ------- |
161161| `g` | Go to the start of the file | `goto_file_start` |
162162| `e` | Go to the end of the file | `goto_last_line` |
163+| `f` | Go to files in the selection | `goto_file` |
163164| `h` | Go to the start of the line | `goto_line_start` |
164165| `l` | Go to the end of the line | `goto_line_end` |
165166| `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.
202203| `v`, `Ctrl-v` | Vertical right split | `vsplit` |
203204| `s`, `Ctrl-s` | Horizontal bottom split | `hsplit` |
204205| `h`, `Ctrl-h`, `left` | Move to left split | `jump_view_left` |
206+| `f` | Go to files in the selection in horizontal splits | `goto_file` |
207+| `F` | Go to files in the selection in vertical splits | `goto_file` |
205208| `j`, `Ctrl-j`, `down` | Move to split below | `jump_view_down` |
206209| `k`, `Ctrl-k`, `up` | Move to split above | `jump_view_up` |
207210| `l`, `Ctrl-l`, `right` | Move to right split | `jump_view_right` |
helix-term/src/commands.rs+46
@@ -259,6 +259,9 @@ impl Command {
259259 goto_implementation, "Goto implementation",
260260 goto_file_start, "Goto file start/line",
261261 goto_file_end, "Goto file end",
262+ goto_file, "Goto files in the selection",
263+ goto_file_hsplit, "Goto files in the selection in horizontal splits",
264+ goto_file_vsplit, "Goto files in the selection in vertical splits",
262265 goto_reference, "Goto references",
263266 goto_window_top, "Goto window top",
264267 goto_window_middle, "Goto window middle",
@@ -835,6 +838,49 @@ fn goto_file_end(cx: &mut Context) {
835838 doc.set_selection(view.id, selection);
836839}
837840
841+fn goto_file(cx: &mut Context) {
842+ goto_file_impl(cx, Action::Replace);
843+}
844+
845+fn goto_file_hsplit(cx: &mut Context) {
846+ goto_file_impl(cx, Action::HorizontalSplit);
847+}
848+
849+fn goto_file_vsplit(cx: &mut Context) {
850+ goto_file_impl(cx, Action::VerticalSplit);
851+}
852+
853+fn goto_file_impl(cx: &mut Context, action: Action) {
854+ let (view, doc) = current_ref!(cx.editor);
855+ let text = doc.text();
856+ let selections = doc.selection(view.id);
857+ let mut paths: Vec<_> = selections
858+ .iter()
859+ .map(|r| text.slice(r.from()..r.to()).to_string())
860+ .collect();
861+ let primary = selections.primary();
862+ if selections.len() == 1 && primary.to() - primary.from() == 1 {
863+ let current_word = movement::move_next_long_word_start(
864+ text.slice(..),
865+ movement::move_prev_long_word_start(text.slice(..), primary, 1),
866+ 1,
867+ );
868+ paths.clear();
869+ paths.push(
870+ text.slice(current_word.from()..current_word.to())
871+ .to_string(),
872+ );
873+ }
874+ for sel in paths {
875+ let p = sel.trim();
876+ if !p.is_empty() {
877+ if let Err(e) = cx.editor.open(PathBuf::from(p), action) {
878+ cx.editor.set_error(format!("Open file failed: {:?}", e));
879+ }
880+ }
881+ }
882+}
883+
838884fn extend_word_impl<F>(cx: &mut Context, extend_fn: F)
839885where
840886 F: Fn(RopeSlice, Range, usize) -> Range,
helix-term/src/keymap.rs+5
@@ -512,6 +512,7 @@ impl Default for Keymaps {
512512 "g" => { "Goto"
513513 "g" => goto_file_start,
514514 "e" => goto_last_line,
515+ "f" => goto_file,
515516 "h" => goto_line_start,
516517 "l" => goto_line_end,
517518 "s" => goto_first_nonwhitespace,
@@ -622,6 +623,8 @@ impl Default for Keymaps {
622623 "C-w" | "w" => rotate_view,
623624 "C-s" | "s" => hsplit,
624625 "C-v" | "v" => vsplit,
626+ "f" => goto_file_hsplit,
627+ "F" => goto_file_vsplit,
625628 "C-q" | "q" => wclose,
626629 "C-o" | "o" => wonly,
627630 "C-h" | "h" | "left" => jump_view_left,
@@ -650,6 +653,8 @@ impl Default for Keymaps {
650653 "C-w" | "w" => rotate_view,
651654 "C-s" | "s" => hsplit,
652655 "C-v" | "v" => vsplit,
656+ "f" => goto_file_hsplit,
657+ "F" => goto_file_vsplit,
653658 "C-q" | "q" => wclose,
654659 "C-o" | "o" => wonly,
655660 "C-h" | "h" | "left" => jump_view_left,