From 70ed9990b8c08cb7d7707013db130451fab96310 Mon Sep 17 00:00:00 2001 From: EbbDrop Date: Tue, 9 Nov 2021 15:13:33 +0100 Subject: [PATCH] Added workspace_symbol_picker --- book/src/keymap.md | 1 + helix-term/src/commands.rs | 62 ++++++++++++++++++++++++++++++++++++++ helix-term/src/keymap.rs | 1 + 3 files changed, 64 insertions(+) diff --git a/book/src/keymap.md b/book/src/keymap.md index 6ae1e8d476fc7..eb1e4fec4f213 100644 --- a/book/src/keymap.md +++ b/book/src/keymap.md @@ -206,6 +206,7 @@ This layer is a kludge of mappings, mostly pickers. | `f` | Open file picker | `file_picker` | | `b` | Open buffer picker | `buffer_picker` | | `s` | Open symbol picker (current document) | `symbol_picker` | +| `t` | Open symbol picker (all documents in workspace) | `workspace_symbol_picker` | | `a` | Apply code action | `code_action` | | `'` | Open last fuzzy picker | `last_picker` | | `w` | Enter [window mode](#window-mode) | N/A | diff --git a/helix-term/src/commands.rs b/helix-term/src/commands.rs index 42163b8e0ddb7..bd450c689f304 100644 --- a/helix-term/src/commands.rs +++ b/helix-term/src/commands.rs @@ -237,6 +237,7 @@ impl Command { code_action, "Perform code action", buffer_picker, "Open buffer picker", symbol_picker, "Open symbol picker", + workspace_symbol_picker, "Open workspace symbol picker", last_picker, "Open last picker", prepend_to_line, "Insert at start of line", append_to_line, "Insert at end of line", @@ -2715,6 +2716,67 @@ fn symbol_picker(cx: &mut Context) { ) } +fn workspace_symbol_picker(cx: &mut Context) { + let (_, doc) = current!(cx.editor); + + let language_server = match doc.language_server() { + Some(language_server) => language_server, + None => return, + }; + let offset_encoding = language_server.offset_encoding(); + + let future = language_server.workspace_symbols("".to_string()); + + let current_path = doc_mut!(cx.editor).path().cloned(); + cx.callback( + future, + move |_editor: &mut Editor, + compositor: &mut Compositor, + response: Option>| { + if let Some(symbols) = response { + let picker = FilePicker::new( + symbols, + move |symbol| { + let path = symbol.location.uri.to_file_path().unwrap(); + if current_path.as_ref().map(|p| p == &path).unwrap_or(false) { + (&symbol.name).into() + } else { + let relative_path = helix_core::path::get_relative_path(path.as_path()) + .to_str() + .unwrap() + .to_owned(); + format!("{} ({})", &symbol.name, relative_path).into() + } + }, + move |editor: &mut Editor, symbol, action| { + let path = symbol.location.uri.to_file_path().unwrap(); + editor.open(path, action).expect("editor.open faild"); + let (view, doc) = current!(editor); + + if let Some(range) = + lsp_range_to_range(doc.text(), symbol.location.range, offset_encoding) + { + // we flip the range so that the cursor sits on the start of the symbol + // (for example start of the function). + doc.set_selection(view.id, Selection::single(range.head, range.anchor)); + align_view(doc, view, Align::Center); + } + }, + move |_editor, symbol| { + let path = symbol.location.uri.to_file_path().unwrap(); + let line = Some(( + symbol.location.range.start.line as usize, + symbol.location.range.end.line as usize, + )); + Some((path, line)) + }, + ); + compositor.push(Box::new(picker)) + } + }, + ) +} + pub fn code_action(cx: &mut Context) { let (view, doc) = current!(cx.editor); diff --git a/helix-term/src/keymap.rs b/helix-term/src/keymap.rs index d497401f99f9e..d6807e317467c 100644 --- a/helix-term/src/keymap.rs +++ b/helix-term/src/keymap.rs @@ -569,6 +569,7 @@ impl Default for Keymaps { "f" => file_picker, "b" => buffer_picker, "s" => symbol_picker, + "t" => workspace_symbol_picker, "a" => code_action, "'" => last_picker, "w" => { "Window"