1use anyhow::Result;
2use gpui::{AsyncApp, Entity};
3use language::{Buffer, OutlineItem};
4use regex::Regex;
5use std::fmt::Write;
6use text::Point;
7
8/// For files over this size, instead of reading them (or including them in context),
9/// we automatically provide the file's symbol outline instead, with line numbers.
10pub const AUTO_OUTLINE_SIZE: usize = 16384;
11
12/// Result of getting buffer content, which can be either full content or an outline.
13pub struct BufferContent {
14 /// The actual content (either full text or outline)
15 pub text: String,
16 /// Whether this is an outline (true) or full content (false)
17 pub is_outline: bool,
18}
19
20/// Returns either the full content of a buffer or its outline, depending on size.
21/// For files larger than AUTO_OUTLINE_SIZE, returns an outline with a header.
22/// For smaller files, returns the full content.
23pub async fn get_buffer_content_or_outline(
24 buffer: Entity<Buffer>,
25 path: Option<&str>,
26 cx: &AsyncApp,
27) -> Result<BufferContent> {
28 let file_size = buffer.read_with(cx, |buffer, _| buffer.text().len())?;
29
30 if file_size > AUTO_OUTLINE_SIZE {
31 // For large files, use outline instead of full content
32 // Wait until the buffer has been fully parsed, so we can read its outline
33 buffer
34 .read_with(cx, |buffer, _| buffer.parsing_idle())?
35 .await;
36
37 let outline_items = buffer.read_with(cx, |buffer, _| {
38 let snapshot = buffer.snapshot();
39 snapshot
40 .outline(None)
41 .items
42 .into_iter()
43 .map(|item| item.to_point(&snapshot))
44 .collect::<Vec<_>>()
45 })?;
46
47 // If no outline exists, fall back to first 1KB so the agent has some context
48 if outline_items.is_empty() {
49 let text = buffer.read_with(cx, |buffer, _| {
50 let snapshot = buffer.snapshot();
51 let len = snapshot.len().min(1024);
52 let content = snapshot.text_for_range(0..len).collect::<String>();
53 if let Some(path) = path {
54 format!("# First 1KB of {path} (file too large to show full content, and no outline available)\n\n{content}")
55 } else {
56 format!("# First 1KB of file (file too large to show full content, and no outline available)\n\n{content}")
57 }
58 })?;
59
60 return Ok(BufferContent {
61 text,
62 is_outline: false,
63 });
64 }
65
66 let outline_text = render_outline(outline_items, None, 0, usize::MAX).await?;
67
68 let text = if let Some(path) = path {
69 format!(
70 "# File outline for {path} (file too large to show full content)\n\n{outline_text}",
71 )
72 } else {
73 format!("# File outline (file too large to show full content)\n\n{outline_text}",)
74 };
75 Ok(BufferContent {
76 text,
77 is_outline: true,
78 })
79 } else {
80 // File is small enough, return full content
81 let text = buffer.read_with(cx, |buffer, _| buffer.text())?;
82 Ok(BufferContent {
83 text,
84 is_outline: false,
85 })
86 }
87}
88
89async fn render_outline(
90 items: impl IntoIterator<Item = OutlineItem<Point>>,
91 regex: Option<Regex>,
92 offset: usize,
93 results_per_page: usize,
94) -> Result<String> {
95 let mut items = items.into_iter().skip(offset);
96
97 let entries = items
98 .by_ref()
99 .filter(|item| {
100 regex
101 .as_ref()
102 .is_none_or(|regex| regex.is_match(&item.text))
103 })
104 .take(results_per_page)
105 .collect::<Vec<_>>();
106 let has_more = items.next().is_some();
107
108 let mut output = String::new();
109 let entries_rendered = render_entries(&mut output, entries);
110
111 // Calculate pagination information
112 let page_start = offset + 1;
113 let page_end = offset + entries_rendered;
114 let total_symbols = if has_more {
115 format!("more than {}", page_end)
116 } else {
117 page_end.to_string()
118 };
119
120 // Add pagination information
121 if has_more {
122 writeln!(&mut output, "\nShowing symbols {page_start}-{page_end} (there were more symbols found; use offset: {page_end} to see next page)",
123 )
124 } else {
125 writeln!(
126 &mut output,
127 "\nShowing symbols {page_start}-{page_end} (total symbols: {total_symbols})",
128 )
129 }
130 .ok();
131
132 Ok(output)
133}
134
135fn render_entries(
136 output: &mut String,
137 items: impl IntoIterator<Item = OutlineItem<Point>>,
138) -> usize {
139 let mut entries_rendered = 0;
140
141 for item in items {
142 // Indent based on depth ("" for level 0, " " for level 1, etc.)
143 for _ in 0..item.depth {
144 output.push(' ');
145 }
146 output.push_str(&item.text);
147
148 // Add position information - convert to 1-based line numbers for display
149 let start_line = item.range.start.row + 1;
150 let end_line = item.range.end.row + 1;
151
152 if start_line == end_line {
153 writeln!(output, " [L{}]", start_line).ok();
154 } else {
155 writeln!(output, " [L{}-{}]", start_line, end_line).ok();
156 }
157 entries_rendered += 1;
158 }
159
160 entries_rendered
161}
162
163#[cfg(test)]
164mod tests {
165 use super::*;
166 use fs::FakeFs;
167 use gpui::TestAppContext;
168 use project::Project;
169 use settings::SettingsStore;
170
171 #[gpui::test]
172 async fn test_large_file_fallback_to_subset(cx: &mut TestAppContext) {
173 cx.update(|cx| {
174 let settings = SettingsStore::test(cx);
175 cx.set_global(settings);
176 });
177
178 let fs = FakeFs::new(cx.executor());
179 let project = Project::test(fs, [], cx).await;
180
181 let content = "A".repeat(100 * 1024); // 100KB
182 let content_len = content.len();
183 let buffer = project
184 .update(cx, |project, cx| project.create_buffer(true, cx))
185 .await
186 .expect("failed to create buffer");
187
188 buffer.update(cx, |buffer, cx| buffer.set_text(content, cx));
189
190 let result = cx
191 .spawn(|cx| async move { get_buffer_content_or_outline(buffer, None, &cx).await })
192 .await
193 .unwrap();
194
195 // Should contain some of the actual file content
196 assert!(
197 result.text.contains("AAAAAAAAAA"),
198 "Result did not contain content subset"
199 );
200
201 // Should be marked as not an outline (it's truncated content)
202 assert!(
203 !result.is_outline,
204 "Large file without outline should not be marked as outline"
205 );
206
207 // Should be reasonably sized (much smaller than original)
208 assert!(
209 result.text.len() < 50 * 1024,
210 "Result size {} should be smaller than 50KB",
211 result.text.len()
212 );
213
214 // Should be significantly smaller than the original content
215 assert!(
216 result.text.len() < content_len / 10,
217 "Result should be much smaller than original content"
218 );
219 }
220}