Include highlighting runs in Outline

This commit is contained in:
Max Brunsfeld 2022-01-13 14:46:15 -08:00
parent 3e1c559b2d
commit 7913a1ea22
5 changed files with 36 additions and 10 deletions

View file

@ -1835,7 +1835,7 @@ impl BufferSnapshot {
}
}
pub fn outline(&self) -> Option<Outline<Anchor>> {
pub fn outline(&self, theme: Option<&SyntaxTheme>) -> Option<Outline<Anchor>> {
let tree = self.tree.as_ref()?;
let grammar = self
.language
@ -1849,6 +1849,8 @@ impl BufferSnapshot {
TextProvider(self.as_rope()),
);
let mut chunks = self.chunks(0..self.len(), theme);
let item_capture_ix = grammar.outline_query.capture_index_for_name("item")?;
let name_capture_ix = grammar.outline_query.capture_index_for_name("name")?;
let context_capture_ix = grammar
@ -1863,6 +1865,7 @@ impl BufferSnapshot {
let range = item_node.start_byte()..item_node.end_byte();
let mut text = String::new();
let mut name_ranges = Vec::new();
let mut text_runs = Vec::new();
for capture in mat.captures {
let node_is_name;
@ -1890,7 +1893,22 @@ impl BufferSnapshot {
name_ranges.push(start..end);
}
text.extend(self.text_for_range(range));
let mut offset = range.start;
chunks.seek(offset);
while let Some(mut chunk) = chunks.next() {
if chunk.text.len() > range.end - offset {
chunk.text = &chunk.text[0..(range.end - offset)];
offset = range.end;
} else {
offset += chunk.text.len();
}
text_runs.push((chunk.text.len(), chunk.highlight_style));
text.push_str(chunk.text);
if offset >= range.end {
break;
}
}
}
while stack.last().map_or(false, |prev_range| {
@ -1905,6 +1923,7 @@ impl BufferSnapshot {
range: self.anchor_after(range.start)..self.anchor_before(range.end),
text,
name_ranges: name_ranges.into_boxed_slice(),
text_runs,
})
})
.collect::<Vec<_>>();

View file

@ -1,5 +1,5 @@
use fuzzy::{StringMatch, StringMatchCandidate};
use gpui::executor::Background;
use gpui::{executor::Background, fonts::HighlightStyle};
use std::{ops::Range, sync::Arc};
#[derive(Debug)]
@ -14,6 +14,7 @@ pub struct OutlineItem<T> {
pub range: Range<T>,
pub text: String,
pub name_ranges: Box<[Range<u32>]>,
pub text_runs: Vec<(usize, Option<HighlightStyle>)>,
}
impl<T> Outline<T> {

View file

@ -332,7 +332,7 @@ async fn test_outline(mut cx: gpui::TestAppContext) {
let buffer = cx.add_model(|cx| Buffer::new(0, text, cx).with_language(language, None, cx));
let outline = buffer
.read_with(&cx, |buffer, _| buffer.snapshot().outline())
.read_with(&cx, |buffer, _| buffer.snapshot().outline(None))
.unwrap();
assert_eq!(