From 099250c6915a3d81fa8e5781b6fdfba8b2c84880 Mon Sep 17 00:00:00 2001 From: Antonio Scandurra Date: Tue, 29 Mar 2022 13:49:55 +0200 Subject: [PATCH] Introduce `MultiBuffer::symbols_containing` --- crates/editor/src/multi_buffer.rs | 27 +++++++++++++++++++++++++++ crates/text/src/anchor.rs | 2 +- 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/crates/editor/src/multi_buffer.rs b/crates/editor/src/multi_buffer.rs index af98f3d5896d955872193a29730696aa9a80ed17..ad3fc3202d94cfeef9f6ace31ccac8068b0c9b10 100644 --- a/crates/editor/src/multi_buffer.rs +++ b/crates/editor/src/multi_buffer.rs @@ -2264,6 +2264,33 @@ impl MultiBufferSnapshot { )) } + pub fn symbols_containing( + &self, + offset: T, + theme: Option<&SyntaxTheme>, + ) -> Option<(BufferSnapshot, Vec>)> { + let anchor = self.anchor_before(offset); + let excerpt_id = anchor.excerpt_id(); + let excerpt = self.excerpt(excerpt_id)?; + Some(( + excerpt.buffer.clone(), + excerpt + .buffer + .symbols_containing(anchor.text_anchor, theme) + .into_iter() + .flatten() + .map(|item| OutlineItem { + 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, + highlight_ranges: item.highlight_ranges, + name_ranges: item.name_ranges, + }) + .collect(), + )) + } + fn excerpt<'a>(&'a self, excerpt_id: &'a ExcerptId) -> Option<&'a Excerpt> { let mut cursor = self.excerpts.cursor::>(); cursor.seek(&Some(excerpt_id), Bias::Left, &()); diff --git a/crates/text/src/anchor.rs b/crates/text/src/anchor.rs index e642aa45d3ff06ffd326dcf633df1985ee5d7ef0..00ec288168c45468f6f51c97dd5e3c3445d23064 100644 --- a/crates/text/src/anchor.rs +++ b/crates/text/src/anchor.rs @@ -4,7 +4,7 @@ use anyhow::Result; use std::{cmp::Ordering, fmt::Debug, ops::Range}; use sum_tree::Bias; -#[derive(Clone, Eq, PartialEq, Debug, Hash)] +#[derive(Copy, Clone, Eq, PartialEq, Debug, Hash)] pub struct Anchor { pub timestamp: clock::Local, pub offset: usize,