|
| 1 | +use helix_core::{tree_sitter::Query, Selection, Uri}; |
| 2 | +use helix_view::{align_view, Align, DocumentId}; |
| 3 | + |
| 4 | +use crate::ui::{overlay::overlaid, picker::PathOrId, Picker, PickerColumn}; |
| 5 | + |
| 6 | +use super::Context; |
| 7 | + |
| 8 | +#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)] |
| 9 | +enum SymbolKind { |
| 10 | + Function, |
| 11 | + Macro, |
| 12 | + Module, |
| 13 | + Constant, |
| 14 | + Struct, |
| 15 | + Interface, |
| 16 | + Type, |
| 17 | + Class, |
| 18 | +} |
| 19 | + |
| 20 | +impl SymbolKind { |
| 21 | + fn as_str(&self) -> &'static str { |
| 22 | + match self { |
| 23 | + Self::Function => "function", |
| 24 | + Self::Macro => "macro", |
| 25 | + Self::Module => "module", |
| 26 | + Self::Constant => "constant", |
| 27 | + Self::Struct => "struct", |
| 28 | + Self::Interface => "interface", |
| 29 | + Self::Type => "type", |
| 30 | + Self::Class => "class", |
| 31 | + } |
| 32 | + } |
| 33 | +} |
| 34 | + |
| 35 | +fn definition_symbol_kind_for_capture(symbols: &Query, capture_index: usize) -> Option<SymbolKind> { |
| 36 | + match symbols.capture_names()[capture_index] { |
| 37 | + "definition.function" => Some(SymbolKind::Function), |
| 38 | + "definition.macro" => Some(SymbolKind::Macro), |
| 39 | + "definition.module" => Some(SymbolKind::Module), |
| 40 | + "definition.constant" => Some(SymbolKind::Constant), |
| 41 | + "definition.struct" => Some(SymbolKind::Struct), |
| 42 | + "definition.interface" => Some(SymbolKind::Interface), |
| 43 | + "definition.type" => Some(SymbolKind::Type), |
| 44 | + "definition.class" => Some(SymbolKind::Class), |
| 45 | + _ => None, |
| 46 | + } |
| 47 | +} |
| 48 | + |
| 49 | +// NOTE: Uri is cheap to clone and DocumentId is Copy |
| 50 | +#[derive(Debug, Clone)] |
| 51 | +enum UriOrDocumentId { |
| 52 | + // TODO: the workspace symbol picker will take advantage of this. |
| 53 | + #[allow(dead_code)] |
| 54 | + Uri(Uri), |
| 55 | + Id(DocumentId), |
| 56 | +} |
| 57 | + |
| 58 | +impl UriOrDocumentId { |
| 59 | + fn path_or_id(&self) -> Option<PathOrId<'_>> { |
| 60 | + match self { |
| 61 | + Self::Id(id) => Some(PathOrId::Id(*id)), |
| 62 | + Self::Uri(uri) => uri.as_path().map(PathOrId::Path), |
| 63 | + } |
| 64 | + } |
| 65 | +} |
| 66 | + |
| 67 | +#[derive(Debug)] |
| 68 | +struct Symbol { |
| 69 | + kind: SymbolKind, |
| 70 | + name: String, |
| 71 | + start: usize, |
| 72 | + end: usize, |
| 73 | + start_line: usize, |
| 74 | + end_line: usize, |
| 75 | + doc: UriOrDocumentId, |
| 76 | +} |
| 77 | + |
| 78 | +pub fn syntax_symbol_picker(cx: &mut Context) { |
| 79 | + let doc = doc!(cx.editor); |
| 80 | + let Some((syntax, lang_config)) = doc.syntax().zip(doc.language_config()) else { |
| 81 | + cx.editor |
| 82 | + .set_error("Syntax tree is not available on this buffer"); |
| 83 | + return; |
| 84 | + }; |
| 85 | + let Some(symbols_query) = lang_config.symbols_query() else { |
| 86 | + cx.editor |
| 87 | + .set_error("Syntax-based symbols information not available for this language"); |
| 88 | + return; |
| 89 | + }; |
| 90 | + |
| 91 | + let doc_id = doc.id(); |
| 92 | + let text = doc.text(); |
| 93 | + |
| 94 | + let columns = vec![ |
| 95 | + PickerColumn::new("kind", |symbol: &Symbol, _| symbol.kind.as_str().into()), |
| 96 | + PickerColumn::new("name", |symbol: &Symbol, _| symbol.name.as_str().into()), |
| 97 | + ]; |
| 98 | + |
| 99 | + let symbols = syntax |
| 100 | + .captures(symbols_query, text.slice(..), None) |
| 101 | + .filter_map(move |(match_, capture_index)| { |
| 102 | + let capture = match_.captures[capture_index]; |
| 103 | + let kind = definition_symbol_kind_for_capture(symbols_query, capture.index as usize)?; |
| 104 | + let node = capture.node; |
| 105 | + let start = text.byte_to_char(node.start_byte()); |
| 106 | + let end = text.byte_to_char(node.end_byte()); |
| 107 | + |
| 108 | + Some(Symbol { |
| 109 | + kind, |
| 110 | + name: text.slice(start..end).to_string(), |
| 111 | + start, |
| 112 | + end, |
| 113 | + |
| 114 | + start_line: text.char_to_line(start), |
| 115 | + end_line: text.char_to_line(end), |
| 116 | + doc: UriOrDocumentId::Id(doc_id), |
| 117 | + }) |
| 118 | + }); |
| 119 | + |
| 120 | + let picker = Picker::new( |
| 121 | + columns, |
| 122 | + 1, // name |
| 123 | + symbols, |
| 124 | + (), |
| 125 | + move |cx, symbol, action| { |
| 126 | + cx.editor.switch(doc_id, action); |
| 127 | + let view = view_mut!(cx.editor); |
| 128 | + let doc = doc_mut!(cx.editor, &doc_id); |
| 129 | + doc.set_selection(view.id, Selection::single(symbol.start, symbol.end)); |
| 130 | + if action.align_view(view, doc.id()) { |
| 131 | + align_view(doc, view, Align::Center) |
| 132 | + } |
| 133 | + }, |
| 134 | + ) |
| 135 | + .with_preview(|_editor, symbol| { |
| 136 | + Some(( |
| 137 | + symbol.doc.path_or_id()?, |
| 138 | + Some((symbol.start_line, symbol.end_line)), |
| 139 | + )) |
| 140 | + }) |
| 141 | + .truncate_start(false); |
| 142 | + |
| 143 | + cx.push_layer(Box::new(overlaid(picker))); |
| 144 | +} |
0 commit comments