From 2660d37ad81e17989eb7bb678c0837a6ab710541 Mon Sep 17 00:00:00 2001 From: Antonio Scandurra Date: Thu, 13 Jan 2022 18:24:00 +0100 Subject: [PATCH] Return `Outline` from `MultiBuffer::outline` Co-Authored-By: Max Brunsfeld --- crates/editor/src/multi_buffer.rs | 24 ++++++++++++++++++++---- crates/language/src/buffer.rs | 4 ++-- crates/language/src/outline.rs | 12 ++++++------ crates/outline/src/outline.rs | 6 +++--- 4 files changed, 31 insertions(+), 15 deletions(-) diff --git a/crates/editor/src/multi_buffer.rs b/crates/editor/src/multi_buffer.rs index 54a44d2aa77555251458c731cdf266c97869d1d8..128cefd49e9a76cf6cee9fef0876ca93397caed3 100644 --- a/crates/editor/src/multi_buffer.rs +++ b/crates/editor/src/multi_buffer.rs @@ -6,8 +6,8 @@ use clock::ReplicaId; use collections::{HashMap, HashSet}; use gpui::{AppContext, Entity, ModelContext, ModelHandle, Task}; use language::{ - Buffer, BufferChunks, BufferSnapshot, Chunk, DiagnosticEntry, Event, File, Language, Selection, - ToOffset as _, ToPoint as _, TransactionId, Outline, + Buffer, BufferChunks, BufferSnapshot, Chunk, DiagnosticEntry, Event, File, Language, Outline, + OutlineItem, Selection, ToOffset as _, ToPoint as _, TransactionId, }; use std::{ cell::{Ref, RefCell}, @@ -1698,8 +1698,24 @@ impl MultiBufferSnapshot { }) } - pub fn outline(&self) -> Option { - self.as_singleton().and_then(move |buffer| buffer.outline()) + pub fn outline(&self) -> Option> { + let buffer = self.as_singleton()?; + let outline = buffer.outline()?; + let excerpt_id = &self.excerpts.iter().next().unwrap().id; + Some(Outline::new( + outline + .items + .into_iter() + .map(|item| OutlineItem { + id: item.id, + depth: item.depth, + range: self.anchor_in_excerpt(excerpt_id.clone(), item.range.start) + ..self.anchor_in_excerpt(excerpt_id.clone(), item.range.end), + text: item.text, + name_range_in_text: item.name_range_in_text, + }) + .collect(), + )) } fn buffer_snapshot_for_excerpt<'a>( diff --git a/crates/language/src/buffer.rs b/crates/language/src/buffer.rs index 03f7552e69e56451b7e26b27c837e5253583c1c4..40f83e9ba0d8175f7536a111adf3806cb22a5461 100644 --- a/crates/language/src/buffer.rs +++ b/crates/language/src/buffer.rs @@ -1835,7 +1835,7 @@ impl BufferSnapshot { } } - pub fn outline(&self) -> Option { + pub fn outline(&self) -> Option> { let tree = self.tree.as_ref()?; let grammar = self .language @@ -1910,7 +1910,7 @@ impl BufferSnapshot { Some(OutlineItem { id, depth: stack.len() - 1, - range, + range: self.anchor_after(range.start)..self.anchor_before(range.end), text, name_range_in_text, }) diff --git a/crates/language/src/outline.rs b/crates/language/src/outline.rs index 55472f9a1c6ab7f3bd4226e55644ebdfcf77fe4e..7d2e47d964cae10b3f5b98b81a96b5d6826df8b1 100644 --- a/crates/language/src/outline.rs +++ b/crates/language/src/outline.rs @@ -4,22 +4,22 @@ use fuzzy::{StringMatch, StringMatchCandidate}; use gpui::AppContext; #[derive(Debug)] -pub struct Outline { - pub items: Vec, +pub struct Outline { + pub items: Vec>, candidates: Vec, } #[derive(Clone, Debug)] -pub struct OutlineItem { +pub struct OutlineItem { pub id: usize, pub depth: usize, - pub range: Range, + pub range: Range, pub text: String, pub name_range_in_text: Range, } -impl Outline { - pub fn new(items: Vec) -> Self { +impl Outline { + pub fn new(items: Vec>) -> Self { Self { candidates: items .iter() diff --git a/crates/outline/src/outline.rs b/crates/outline/src/outline.rs index 7a47c1a01786c5fcc42ca1f15d581f8651cb59c0..9b182f0caeb8d93275969f40bc55c300001488f7 100644 --- a/crates/outline/src/outline.rs +++ b/crates/outline/src/outline.rs @@ -1,4 +1,4 @@ -use editor::{Editor, EditorSettings}; +use editor::{Anchor, Editor, EditorSettings}; use fuzzy::StringMatch; use gpui::{ action, @@ -34,7 +34,7 @@ pub fn init(cx: &mut MutableAppContext) { struct OutlineView { handle: WeakViewHandle, - outline: Outline, + outline: Outline, selected_match_index: usize, matches: Vec, query_editor: ViewHandle, @@ -90,7 +90,7 @@ impl View for OutlineView { impl OutlineView { fn new( - outline: Outline, + outline: Outline, settings: watch::Receiver, cx: &mut ViewContext, ) -> Self {