Skip to content

Commit

Permalink
Added workspace_symbol_picker
Browse files Browse the repository at this point in the history
  • Loading branch information
EbbDrop committed Nov 9, 2021
1 parent 7c9f620 commit 70ed999
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 0 deletions.
1 change: 1 addition & 0 deletions book/src/keymap.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 |
Expand Down
62 changes: 62 additions & 0 deletions helix-term/src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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<Vec<lsp::SymbolInformation>>| {
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);

Expand Down
1 change: 1 addition & 0 deletions helix-term/src/keymap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit 70ed999

Please sign in to comment.