diff --git a/lapce-app/src/doc.rs b/lapce-app/src/doc.rs index 85bc76c7..9d03b761 100644 --- a/lapce-app/src/doc.rs +++ b/lapce-app/src/doc.rs @@ -989,7 +989,7 @@ pub fn get_document_symbol(&self) { }) .collect(), }; - let symbol_new = Some(SymbolData { items, path }); + let symbol_new = Some(SymbolData::new(items, path, cx)); doc.document_symbol_data.update(|symbol| { *symbol = symbol_new; }); diff --git a/lapce-app/src/panel/document_symbol.rs b/lapce-app/src/panel/document_symbol.rs index 8eca90be..83565f20 100644 --- a/lapce-app/src/panel/document_symbol.rs +++ b/lapce-app/src/panel/document_symbol.rs @@ -10,7 +10,7 @@ }, View, }; -use lsp_types::DocumentSymbol; +use lsp_types::{DocumentSymbol, SymbolKind}; use super::position::PanelPosition; use crate::{ @@ -22,11 +22,42 @@ #[derive(Clone, Debug)] pub struct SymbolData { - pub items: Vec>, pub path: PathBuf, + pub file: RwSignal, } impl SymbolData { + pub fn new( + items: Vec>, + path: PathBuf, + cx: Scope, + ) -> Self { + let name = path + .file_name() + .and_then(|x| x.to_str()) + .map(|x| x.to_string()) + .unwrap_or_default(); + #[allow(deprecated)] + let file_ds = DocumentSymbol { + name: name.clone(), + detail: None, + kind: SymbolKind::FILE, + tags: None, + deprecated: None, + range: Default::default(), + selection_range: Default::default(), + children: None, + }; + let file = cx.create_rw_signal(SymbolInformationItemData { + id: Id::next(), + name, + detail: None, + item: file_ds, + open: cx.create_rw_signal(true), + children: items, + }); + Self { path, file } + } fn get_children( &self, min: usize, @@ -37,19 +68,10 @@ fn get_children( Rc, RwSignal, )> { - let mut children = Vec::new(); let path = Rc::new(self.path.clone()); let level: usize = 0; let mut next = 0; - for item in &self.items { - if next >= max { - return children; - } - let child_children = - get_children(*item, &mut next, min, max, level, path.clone()); - children.extend(child_children); - } - children + get_children(self.file, &mut next, min, max, level, path.clone()) } } @@ -149,11 +171,7 @@ pub fn new(root: Option>>) -> Self { { fn total_len(&self) -> usize { if let Some(root) = self.root.as_ref().and_then(|x| x.get()) { - let len = root.items.iter().fold(0, |mut x, item| { - x += item.get_untracked().child_count(); - x - }); - len + root.file.get_untracked().child_count() } else { 0 }