Remove unnecessary `Option` from functions querying buffer outline (#37935)

Michael Sloan created

`None` case wasn't being used

Release Notes:

- N/A

Change summary

crates/agent/src/context.rs                            |  7 +-
crates/agent2/src/tools/grep_tool.rs                   |  6 -
crates/agent_ui/src/inline_assistant.rs                | 11 +---
crates/assistant_slash_commands/src/symbols_command.rs |  6 -
crates/assistant_tool/src/outline.rs                   |  4 -
crates/assistant_tools/src/grep_tool.rs                |  6 -
crates/language/src/buffer.rs                          | 25 +++++------
crates/language/src/buffer_tests.rs                    | 17 ++-----
crates/languages/src/css.rs                            |  2 
crates/languages/src/typescript.rs                     |  4 
crates/multi_buffer/src/multi_buffer.rs                |  3 
crates/outline_panel/src/outline_panel.rs              | 12 ++---
crates/project/src/task_inventory.rs                   |  2 
13 files changed, 40 insertions(+), 65 deletions(-)

Detailed changes

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));

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 {

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));
             }

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());

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(
         outline

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 {

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(

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| {
                 (

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
                 .items

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
                 .items

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,

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()

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()