Code at the speed of thought – Zed is a high-performance, multiplayer code editor from the creators of Atom and Tree-sitter.
Remove unnecessary `Option` from functions querying buffer outline (#37935)
Michael Sloan441a934d8433
diff --git a/crates/agent/src/context.rs b/crates/agent/src/context.rsindex a94a933d864..71fa8176a01 100644--- a/crates/agent/src/context.rs+++ b/crates/agent/src/context.rs@@ -201,10 +201,9 @@ impl FileContextHandle {parse_status.changed().await.log_err();}- if let Ok(snapshot) = buffer.read_with(cx, |buffer, _| buffer.snapshot())- && let Some(outline) = snapshot.outline(None)- {- let items = outline+ if let Ok(snapshot) = buffer.read_with(cx, |buffer, _| buffer.snapshot()) {+ let items = snapshot+ .outline(None).items.into_iter().map(|item| item.to_point(&snapshot));diff --git a/crates/agent2/src/tools/grep_tool.rs b/crates/agent2/src/tools/grep_tool.rsindex e76a16bcc7f..2bcfbd711bd 100644--- a/crates/agent2/src/tools/grep_tool.rs+++ b/crates/agent2/src/tools/grep_tool.rs@@ -261,10 +261,8 @@ impl AgentTool for GrepTool {let end_row = range.end.row;output.push_str("\n### ");- if let Some(parent_symbols) = &parent_symbols {- for symbol in parent_symbols {- write!(output, "{} › ", symbol.text)?;- }+ for symbol in parent_symbols {+ write!(output, "{} › ", symbol.text)?;}if range.start.row == end_row {diff --git a/crates/agent_ui/src/inline_assistant.rs b/crates/agent_ui/src/inline_assistant.rsindex 7beb2f8ff80..4ac88e6daa3 100644--- a/crates/agent_ui/src/inline_assistant.rs+++ b/crates/agent_ui/src/inline_assistant.rs@@ -1813,16 +1813,13 @@ impl CodeActionProvider for AssistantCodeActionProvider {has_diagnostics = true;}if has_diagnostics {- if let Some(symbols_containing_start) = snapshot.symbols_containing(range.start, None)- && let Some(symbol) = symbols_containing_start.last()- {+ let symbols_containing_start = snapshot.symbols_containing(range.start, None);+ if let Some(symbol) = symbols_containing_start.last() {range.start = cmp::min(range.start, symbol.range.start.to_point(&snapshot));range.end = cmp::max(range.end, symbol.range.end.to_point(&snapshot));}-- if let Some(symbols_containing_end) = snapshot.symbols_containing(range.end, None)- && let Some(symbol) = symbols_containing_end.last()- {+ let symbols_containing_end = snapshot.symbols_containing(range.end, None);+ if let Some(symbol) = symbols_containing_end.last() {range.start = cmp::min(range.start, symbol.range.start.to_point(&snapshot));range.end = cmp::max(range.end, symbol.range.end.to_point(&snapshot));}diff --git a/crates/assistant_slash_commands/src/symbols_command.rs b/crates/assistant_slash_commands/src/symbols_command.rsindex 30287091444..c7003198007 100644--- a/crates/assistant_slash_commands/src/symbols_command.rs+++ b/crates/assistant_slash_commands/src/symbols_command.rs@@ -1,4 +1,4 @@-use anyhow::{Context as _, Result, anyhow};+use anyhow::{Result, anyhow};use assistant_slash_command::{ArgumentCompletion, SlashCommand, SlashCommandOutput, SlashCommandOutputSection,SlashCommandResult,@@ -70,9 +70,7 @@ impl SlashCommand for OutlineSlashCommand {let path = snapshot.resolve_file_path(cx, true);cx.background_spawn(async move {- let outline = snapshot- .outline(None)- .context("no symbols for active tab")?;+ let outline = snapshot.outline(None);let path = path.as_deref().unwrap_or(Path::new("untitled"));let mut outline_text = format!("Symbols for {}:\n", path.display());diff --git a/crates/assistant_tool/src/outline.rs b/crates/assistant_tool/src/outline.rsindex 4f8bde54560..d9bf64cf059 100644--- a/crates/assistant_tool/src/outline.rs+++ b/crates/assistant_tool/src/outline.rs@@ -41,9 +41,7 @@ pub async fn file_outline(}let snapshot = buffer.read_with(cx, |buffer, _| buffer.snapshot())?;- let outline = snapshot- .outline(None)- .context("No outline information available for this file at path {path}")?;+ let outline = snapshot.outline(None);render_outline(outlinediff --git a/crates/assistant_tools/src/grep_tool.rs b/crates/assistant_tools/src/grep_tool.rsindex 41dde5bbfe4..e43a54661ca 100644--- a/crates/assistant_tools/src/grep_tool.rs+++ b/crates/assistant_tools/src/grep_tool.rs@@ -267,10 +267,8 @@ impl Tool for GrepTool {let end_row = range.end.row;output.push_str("\n### ");- if let Some(parent_symbols) = &parent_symbols {- for symbol in parent_symbols {- write!(output, "{} › ", symbol.text)?;- }+ for symbol in parent_symbols {+ write!(output, "{} › ", symbol.text)?;}if range.start.row == end_row {diff --git a/crates/language/src/buffer.rs b/crates/language/src/buffer.rsindex 363040e2fc6..0a8d62845aa 100644--- a/crates/language/src/buffer.rs+++ b/crates/language/src/buffer.rs@@ -3701,9 +3701,8 @@ impl BufferSnapshot {////// This method allows passing an optional [`SyntaxTheme`] to/// syntax-highlight the returned symbols.- pub fn outline(&self, theme: Option<&SyntaxTheme>) -> Option<Outline<Anchor>> {- self.outline_items_containing(0..self.len(), true, theme)- .map(Outline::new)+ pub fn outline(&self, theme: Option<&SyntaxTheme>) -> Outline<Anchor> {+ Outline::new(self.outline_items_containing(0..self.len(), true, theme))}/// Returns all the symbols that contain the given position.@@ -3714,20 +3713,20 @@ impl BufferSnapshot {&self,position: T,theme: Option<&SyntaxTheme>,- ) -> Option<Vec<OutlineItem<Anchor>>> {+ ) -> Vec<OutlineItem<Anchor>> {let position = position.to_offset(self);let mut items = self.outline_items_containing(position.saturating_sub(1)..self.len().min(position + 1),false,theme,- )?;+ );let mut prev_depth = None;items.retain(|item| {let result = prev_depth.is_none_or(|prev_depth| item.depth > prev_depth);prev_depth = Some(item.depth);result});- Some(items)+ items}pub fn outline_range_containing<T: ToOffset>(&self, range: Range<T>) -> Option<Range<Point>> {@@ -3777,21 +3776,19 @@ impl BufferSnapshot {range: Range<T>,include_extra_context: bool,theme: Option<&SyntaxTheme>,- ) -> Option<Vec<OutlineItem<Anchor>>> {+ ) -> Vec<OutlineItem<Anchor>> {let range = range.to_offset(self);let mut matches = self.syntax.matches(range.clone(), &self.text, |grammar| {grammar.outline_config.as_ref().map(|c| &c.query)});- let configs = matches- .grammars()- .iter()- .map(|g| g.outline_config.as_ref().unwrap())- .collect::<Vec<_>>();let mut items = Vec::new();let mut annotation_row_ranges: Vec<Range<u32>> = Vec::new();while let Some(mat) = matches.peek() {- let config = &configs[mat.grammar_index];+ let config = matches.grammars()[mat.grammar_index]+ .outline_config+ .as_ref()+ .unwrap();if let Some(item) =self.next_outline_item(config, &mat, &range, include_extra_context, theme){@@ -3870,7 +3867,7 @@ impl BufferSnapshot {item_ends_stack.push(item.range.end);}- Some(anchor_items)+ anchor_items}fn next_outline_item(diff --git a/crates/language/src/buffer_tests.rs b/crates/language/src/buffer_tests.rsindex 050ec457dfe..8f3f15cacd4 100644--- a/crates/language/src/buffer_tests.rs+++ b/crates/language/src/buffer_tests.rs@@ -779,9 +779,7 @@ async fn test_outline(cx: &mut gpui::TestAppContext) {.unindent();let buffer = cx.new(|cx| Buffer::local(text, cx).with_language(Arc::new(rust_lang()), cx));- let outline = buffer- .update(cx, |buffer, _| buffer.snapshot().outline(None))- .unwrap();+ let outline = buffer.update(cx, |buffer, _| buffer.snapshot().outline(None));assert_eq!(outline@@ -863,9 +861,7 @@ async fn test_outline_nodes_with_newlines(cx: &mut gpui::TestAppContext) {.unindent();let buffer = cx.new(|cx| Buffer::local(text, cx).with_language(Arc::new(rust_lang()), cx));- let outline = buffer- .update(cx, |buffer, _| buffer.snapshot().outline(None))- .unwrap();+ let outline = buffer.update(cx, |buffer, _| buffer.snapshot().outline(None));assert_eq!(outline@@ -902,7 +898,7 @@ async fn test_outline_with_extra_context(cx: &mut gpui::TestAppContext) {let snapshot = buffer.update(cx, |buffer, _| buffer.snapshot());// extra context nodes are included in the outline.- let outline = snapshot.outline(None).unwrap();+ let outline = snapshot.outline(None);assert_eq!(outline.items@@ -913,7 +909,7 @@ async fn test_outline_with_extra_context(cx: &mut gpui::TestAppContext) {);// extra context nodes do not appear in breadcrumbs.- let symbols = snapshot.symbols_containing(3, None).unwrap();+ let symbols = snapshot.symbols_containing(3, None);assert_eq!(symbols.iter()@@ -945,9 +941,7 @@ fn test_outline_annotations(cx: &mut App) {.unindent();let buffer = cx.new(|cx| Buffer::local(text, cx).with_language(Arc::new(rust_lang()), cx));- let outline = buffer- .update(cx, |buffer, _| buffer.snapshot().outline(None))- .unwrap();+ let outline = buffer.update(cx, |buffer, _| buffer.snapshot().outline(None));assert_eq!(outline@@ -1051,7 +1045,6 @@ async fn test_symbols_containing(cx: &mut gpui::TestAppContext) {) -> Vec<(String, Range<Point>)> {snapshot.symbols_containing(position, None)- .unwrap().into_iter().map(|item| {(diff --git a/crates/languages/src/css.rs b/crates/languages/src/css.rsindex 5cea35084d5..79e3a62a342 100644--- a/crates/languages/src/css.rs+++ b/crates/languages/src/css.rs@@ -237,7 +237,7 @@ mod tests {.unindent();let buffer = cx.new(|cx| language::Buffer::local(text, cx).with_language(language, cx));- let outline = buffer.read_with(cx, |buffer, _| buffer.snapshot().outline(None).unwrap());+ let outline = buffer.read_with(cx, |buffer, _| buffer.snapshot().outline(None));assert_eq!(outline.itemsdiff --git a/crates/languages/src/typescript.rs b/crates/languages/src/typescript.rsindex 15adea6070a..d8f18f5f045 100644--- a/crates/languages/src/typescript.rs+++ b/crates/languages/src/typescript.rs@@ -1030,7 +1030,7 @@ mod tests {.unindent();let buffer = cx.new(|cx| language::Buffer::local(text, cx).with_language(language, cx));- let outline = buffer.read_with(cx, |buffer, _| buffer.snapshot().outline(None).unwrap());+ let outline = buffer.read_with(cx, |buffer, _| buffer.snapshot().outline(None));assert_eq!(outline.items@@ -1084,7 +1084,7 @@ mod tests {.unindent();let buffer = cx.new(|cx| language::Buffer::local(text, cx).with_language(language, cx));- let outline = buffer.read_with(cx, |buffer, _| buffer.snapshot().outline(None).unwrap());+ let outline = buffer.read_with(cx, |buffer, _| buffer.snapshot().outline(None));assert_eq!(outline.itemsdiff --git a/crates/multi_buffer/src/multi_buffer.rs b/crates/multi_buffer/src/multi_buffer.rsindex f5955ec1768..0bcfa2f4d3e 100644--- a/crates/multi_buffer/src/multi_buffer.rs+++ b/crates/multi_buffer/src/multi_buffer.rs@@ -6119,7 +6119,7 @@ impl MultiBufferSnapshot {pub fn outline(&self, theme: Option<&SyntaxTheme>) -> Option<Outline<Anchor>> {let (excerpt_id, _, buffer) = self.as_singleton()?;- let outline = buffer.outline(theme)?;+ let outline = buffer.outline(theme);Some(Outline::new(outline.items@@ -6164,7 +6164,6 @@ impl MultiBufferSnapshot {.buffer.symbols_containing(anchor.text_anchor, theme).into_iter()- .flatten().flat_map(|item| {Some(OutlineItem {depth: item.depth,diff --git a/crates/outline_panel/src/outline_panel.rs b/crates/outline_panel/src/outline_panel.rsindex 2491c65a7f6..a8485248dbe 100644--- a/crates/outline_panel/src/outline_panel.rs+++ b/crates/outline_panel/src/outline_panel.rs@@ -3350,13 +3350,11 @@ impl OutlinePanel {let buffer_language = buffer_snapshot.language().cloned();let fetched_outlines = cx.background_spawn(async move {- let mut outlines = buffer_snapshot- .outline_items_containing(- excerpt_range.context,- false,- Some(&syntax_theme),- )- .unwrap_or_default();+ let mut outlines = buffer_snapshot.outline_items_containing(+ excerpt_range.context,+ false,+ Some(&syntax_theme),+ );outlines.retain(|outline| {buffer_language.is_none()|| buffer_language.as_ref()diff --git a/crates/project/src/task_inventory.rs b/crates/project/src/task_inventory.rsindex 15e6024808e..20dce218f4a 100644--- a/crates/project/src/task_inventory.rs+++ b/crates/project/src/task_inventory.rs@@ -986,7 +986,7 @@ impl ContextProvider for BasicContextProvider {let buffer = location.buffer.read(cx);let buffer_snapshot = buffer.snapshot();let symbols = buffer_snapshot.symbols_containing(location.range.start, None);- let symbol = symbols.unwrap_or_default().last().map(|symbol| {+ let symbol = symbols.last().map(|symbol| {let range = symbol.name_ranges.last()