diff --git a/crates/agent/src/context.rs b/crates/agent/src/context.rs index a94a933d864d204037b3d575cbdc4d85870aeddb..71fa8176a012569373df927eb37145208d6a105d 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.rs index e76a16bcc7f57b035fb1e2b09243d22230b52085..2bcfbd711bd7507be235481197e16cf76b391b6b 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.rs index 7beb2f8ff80627399e7ebe774d8849d038621aef..4ac88e6daa3d3623580e206c2759f27b218d1bac 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.rs index 30287091444db391056a77664890229df95a1d46..c700319800769e3a6e45234355c850e746231200 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.rs index 4f8bde5456073912185fe160d48363eac7601ef5..d9bf64cf059a33f1cc4e6d2833e77a0554b82d93 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( outline diff --git a/crates/assistant_tools/src/grep_tool.rs b/crates/assistant_tools/src/grep_tool.rs index 41dde5bbfe49ad5bd82fdb5072c14f5728c518c5..e43a54661ca146902a49fa1d975e44d486e18587 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.rs index 363040e2fc6e72098ab3b4176b226fb02e72d411..0a8d62845aa26a5504a8c86e897cafa21c57f0f8 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> { - self.outline_items_containing(0..self.len(), true, theme) - .map(Outline::new) + pub fn outline(&self, theme: Option<&SyntaxTheme>) -> Outline { + 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> { 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(&self, range: Range) -> Option> { @@ -3777,21 +3776,19 @@ impl BufferSnapshot { range: Range, include_extra_context: bool, theme: Option<&SyntaxTheme>, - ) -> Option>> { + ) -> Vec> { 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::>(); let mut items = Vec::new(); let mut annotation_row_ranges: Vec> = 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.rs index 050ec457dfe6e83d420206b381d5524b9c583441..8f3f15cacd4be19e375d46d480b3e41f4edd779e 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)> { snapshot .symbols_containing(position, None) - .unwrap() .into_iter() .map(|item| { ( diff --git a/crates/languages/src/css.rs b/crates/languages/src/css.rs index 5cea35084d54f86c6a2b47385bce628b73aa29c7..79e3a62a342092a6203361e5611b9ff81481e984 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 .items diff --git a/crates/languages/src/typescript.rs b/crates/languages/src/typescript.rs index 15adea6070ada89e21de7d7d347f183c1aa010ab..d8f18f5f045f67e658a5458ee5db443f5c85d92e 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 .items diff --git a/crates/multi_buffer/src/multi_buffer.rs b/crates/multi_buffer/src/multi_buffer.rs index f5955ec176805433e5c10fec723fa3974baf7a70..0bcfa2f4d3eb605f0beefb0f2a53817059597ce2 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> { 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.rs index 2491c65a7f6d8aed6224b76401328770c8e7bc50..a8485248dbe2e544c80d59b3ad549c54c49e6e51 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.rs index 15e6024808e0075c853985b6c73ea388c9fea411..20dce218f4a4bd7c45a85d56110ec2b69c84391a 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()