mirror of
https://github.com/zed-industries/zed.git
synced 2026-12-02 21:40:41 +01:00
agent: Overridable max_ancestor_lines and context_lines for grep_tool (#64954)
## Summary Promote `MAX_ANCESTOR_LINES` and `CONTEXT_LINES` from const to tool_input variable with default value. Let session understand grep_tool tree-sitter node render mode and its fallback -- simple context render mode. ## Testing Sync update to exsiting unit tests. ## Self-review - [x] I've reviewed my diff for quality, security, reliability, and performance. - [x] UI changes follow the [checklist](https://zed.dev/docs/development/ui-checklist). - [x] Tests cover the new or changed behavior. Release Notes: - N/A --------- Co-authored-by: Kirill Bulatov <kirill@zed.dev>
This commit is contained in:
@@ -2,6 +2,7 @@ use crate::tools::edit_file_tool::*;
|
||||
use crate::{
|
||||
AgentTool, ContextServerRegistry, EditFileTool, GrepTool, GrepToolInput, ReadFileTool,
|
||||
ReadFileToolInput, Template, Templates, Thread, ToolCallEventStream, ToolInput,
|
||||
default_context_lines, default_max_ancestor_lines,
|
||||
};
|
||||
use Role::*;
|
||||
use anyhow::{Context as _, Result};
|
||||
@@ -954,6 +955,8 @@ fn eval_disable_cursor_blinking() {
|
||||
include_pattern: None,
|
||||
offset: 0,
|
||||
case_sensitive: false,
|
||||
max_ancestor_lines: default_max_ancestor_lines(),
|
||||
context_lines: default_context_lines(),
|
||||
},
|
||||
)],
|
||||
),
|
||||
@@ -1037,6 +1040,8 @@ fn eval_from_pixels_constructor() {
|
||||
include_pattern: Some("font-kit/src/canvas.rs".into()),
|
||||
offset: 0,
|
||||
case_sensitive: false,
|
||||
max_ancestor_lines: default_max_ancestor_lines(),
|
||||
context_lines: default_context_lines(),
|
||||
},
|
||||
)],
|
||||
),
|
||||
@@ -1054,6 +1059,8 @@ fn eval_from_pixels_constructor() {
|
||||
include_pattern: Some("font-kit/src/**/*.rs".into()),
|
||||
offset: 0,
|
||||
case_sensitive: false,
|
||||
max_ancestor_lines: default_max_ancestor_lines(),
|
||||
context_lines: default_context_lines(),
|
||||
},
|
||||
)],
|
||||
),
|
||||
@@ -1071,6 +1078,8 @@ fn eval_from_pixels_constructor() {
|
||||
include_pattern: Some("font-kit/src/**/*.rs".into()),
|
||||
offset: 0,
|
||||
case_sensitive: false,
|
||||
max_ancestor_lines: default_max_ancestor_lines(),
|
||||
context_lines: default_context_lines(),
|
||||
},
|
||||
)],
|
||||
),
|
||||
|
||||
@@ -17,6 +17,13 @@ use util::RangeExt;
|
||||
use util::markdown::{MarkdownCodeBlock, MarkdownInlineCode};
|
||||
use util::paths::PathMatcher;
|
||||
|
||||
pub const fn default_max_ancestor_lines() -> u32 {
|
||||
10
|
||||
}
|
||||
pub const fn default_context_lines() -> u32 {
|
||||
2
|
||||
}
|
||||
|
||||
/// Searches the contents of files in the project with a regular expression
|
||||
///
|
||||
/// - Prefer this tool to path search when searching for symbols in the project, because you won't need to guess what path it's in.
|
||||
@@ -56,6 +63,17 @@ pub struct GrepToolInput {
|
||||
/// Whether the regex is case-sensitive. Defaults to false (case-insensitive).
|
||||
#[serde(default)]
|
||||
pub case_sensitive: bool,
|
||||
/// The syntax-ancestor prefix includes its first line and up to this many following lines.
|
||||
/// Defaults to 10.
|
||||
/// 0 permits only the first line.
|
||||
/// If this prefix does not contain all matched lines, or no syntax ancestor is available, use context_lines instead.
|
||||
#[serde(default = "default_max_ancestor_lines")]
|
||||
pub max_ancestor_lines: u32,
|
||||
/// The number of lines to include before and after the matched lines in fallback context.
|
||||
/// Defaults to 2.
|
||||
/// Used only when no syntax ancestor is available or its capped prefix does not contain all matched lines.
|
||||
#[serde(default = "default_context_lines")]
|
||||
pub context_lines: u32,
|
||||
}
|
||||
|
||||
impl GrepToolInput {
|
||||
@@ -119,9 +137,6 @@ impl AgentTool for GrepTool {
|
||||
event_stream: ToolCallEventStream,
|
||||
cx: &mut App,
|
||||
) -> Task<Result<Self::Output, Self::Output>> {
|
||||
const CONTEXT_LINES: u32 = 2;
|
||||
const MAX_ANCESTOR_LINES: u32 = 10;
|
||||
|
||||
let project = self.project.clone();
|
||||
cx.spawn(async move |cx| {
|
||||
let input = input
|
||||
@@ -255,7 +270,7 @@ impl AgentTool for GrepTool {
|
||||
|
||||
if let Some(ancestor_node) = snapshot.syntax_ancestor(full_lines.clone()) {
|
||||
let full_ancestor_range = ancestor_node.byte_range().to_point(&snapshot);
|
||||
let end_row = full_ancestor_range.end.row.min(full_ancestor_range.start.row + MAX_ANCESTOR_LINES);
|
||||
let end_row = full_ancestor_range.end.row.min(full_ancestor_range.start.row.saturating_add(input.max_ancestor_lines));
|
||||
let end_col = snapshot.line_len(end_row);
|
||||
let capped_ancestor_range = Point::new(full_ancestor_range.start.row, 0)..Point::new(end_row, end_col);
|
||||
|
||||
@@ -267,10 +282,10 @@ impl AgentTool for GrepTool {
|
||||
let mut matched = matched;
|
||||
matched.start.column = 0;
|
||||
matched.start.row =
|
||||
matched.start.row.saturating_sub(CONTEXT_LINES);
|
||||
matched.start.row.saturating_sub(input.context_lines);
|
||||
matched.end.row = cmp::min(
|
||||
snapshot.max_point().row,
|
||||
matched.end.row + CONTEXT_LINES,
|
||||
matched.end.row.saturating_add(input.context_lines),
|
||||
);
|
||||
matched.end.column = snapshot.line_len(matched.end.row);
|
||||
|
||||
@@ -433,6 +448,8 @@ mod tests {
|
||||
include_pattern: Some("root/**/*.rs".to_string()),
|
||||
offset: 0,
|
||||
case_sensitive: false,
|
||||
max_ancestor_lines: default_max_ancestor_lines(),
|
||||
context_lines: default_context_lines(),
|
||||
};
|
||||
|
||||
let result = run_grep_tool(input, project.clone(), cx).await;
|
||||
@@ -452,6 +469,8 @@ mod tests {
|
||||
include_pattern: Some("root/**/src/**".to_string()),
|
||||
offset: 0,
|
||||
case_sensitive: false,
|
||||
max_ancestor_lines: default_max_ancestor_lines(),
|
||||
context_lines: default_context_lines(),
|
||||
};
|
||||
|
||||
let result = run_grep_tool(input, project.clone(), cx).await;
|
||||
@@ -474,6 +493,8 @@ mod tests {
|
||||
include_pattern: None,
|
||||
offset: 0,
|
||||
case_sensitive: false,
|
||||
max_ancestor_lines: default_max_ancestor_lines(),
|
||||
context_lines: default_context_lines(),
|
||||
};
|
||||
|
||||
let result = run_grep_tool(input, project.clone(), cx).await;
|
||||
@@ -510,6 +531,8 @@ mod tests {
|
||||
include_pattern: Some("**/*.txt".to_string()),
|
||||
offset: 0,
|
||||
case_sensitive: false,
|
||||
max_ancestor_lines: default_max_ancestor_lines(),
|
||||
context_lines: default_context_lines(),
|
||||
};
|
||||
|
||||
let result = run_grep_tool(input, project.clone(), cx).await;
|
||||
@@ -524,6 +547,8 @@ mod tests {
|
||||
include_pattern: Some("**/*.txt".to_string()),
|
||||
offset: 0,
|
||||
case_sensitive: true,
|
||||
max_ancestor_lines: default_max_ancestor_lines(),
|
||||
context_lines: default_context_lines(),
|
||||
};
|
||||
|
||||
let result = run_grep_tool(input, project.clone(), cx).await;
|
||||
@@ -538,6 +563,8 @@ mod tests {
|
||||
include_pattern: Some("**/*.txt".to_string()),
|
||||
offset: 0,
|
||||
case_sensitive: true,
|
||||
max_ancestor_lines: default_max_ancestor_lines(),
|
||||
context_lines: default_context_lines(),
|
||||
};
|
||||
|
||||
let result = run_grep_tool(input, project.clone(), cx).await;
|
||||
@@ -553,6 +580,8 @@ mod tests {
|
||||
include_pattern: Some("**/*.txt".to_string()),
|
||||
offset: 0,
|
||||
case_sensitive: true,
|
||||
max_ancestor_lines: default_max_ancestor_lines(),
|
||||
context_lines: default_context_lines(),
|
||||
};
|
||||
|
||||
let result = run_grep_tool(input, project.clone(), cx).await;
|
||||
@@ -591,6 +620,8 @@ mod tests {
|
||||
include_pattern: None,
|
||||
offset: 0,
|
||||
case_sensitive: false,
|
||||
max_ancestor_lines: default_max_ancestor_lines(),
|
||||
context_lines: default_context_lines(),
|
||||
};
|
||||
let task = cx.update(|cx| tool.run(ToolInput::resolved(input), event_stream, cx));
|
||||
let output = task.await.expect("grep tool should succeed");
|
||||
@@ -693,6 +724,8 @@ mod tests {
|
||||
include_pattern: None,
|
||||
offset: 0,
|
||||
case_sensitive: false,
|
||||
max_ancestor_lines: default_max_ancestor_lines(),
|
||||
context_lines: default_context_lines(),
|
||||
};
|
||||
let task = cx.update(|cx| tool.run(ToolInput::resolved(input), event_stream, cx));
|
||||
task.await.expect("grep tool should succeed");
|
||||
@@ -815,6 +848,8 @@ mod tests {
|
||||
include_pattern: Some("**/*.rs".to_string()),
|
||||
offset: 0,
|
||||
case_sensitive: false,
|
||||
max_ancestor_lines: default_max_ancestor_lines(),
|
||||
context_lines: default_context_lines(),
|
||||
};
|
||||
|
||||
let result = run_grep_tool(input, project.clone(), cx).await;
|
||||
@@ -844,6 +879,8 @@ mod tests {
|
||||
include_pattern: Some("**/*.rs".to_string()),
|
||||
offset: 0,
|
||||
case_sensitive: false,
|
||||
max_ancestor_lines: default_max_ancestor_lines(),
|
||||
context_lines: default_context_lines(),
|
||||
};
|
||||
|
||||
let result = run_grep_tool(input, project.clone(), cx).await;
|
||||
@@ -875,6 +912,8 @@ mod tests {
|
||||
include_pattern: Some("**/*.rs".to_string()),
|
||||
offset: 0,
|
||||
case_sensitive: false,
|
||||
max_ancestor_lines: default_max_ancestor_lines(),
|
||||
context_lines: default_context_lines(),
|
||||
};
|
||||
|
||||
let result = run_grep_tool(input, project.clone(), cx).await;
|
||||
@@ -910,6 +949,8 @@ mod tests {
|
||||
include_pattern: Some("**/*.rs".to_string()),
|
||||
offset: 0,
|
||||
case_sensitive: false,
|
||||
max_ancestor_lines: default_max_ancestor_lines(),
|
||||
context_lines: default_context_lines(),
|
||||
};
|
||||
|
||||
let result = run_grep_tool(input, project.clone(), cx).await;
|
||||
@@ -940,6 +981,8 @@ mod tests {
|
||||
include_pattern: Some("**/*.rs".to_string()),
|
||||
offset: 0,
|
||||
case_sensitive: false,
|
||||
max_ancestor_lines: default_max_ancestor_lines(),
|
||||
context_lines: default_context_lines(),
|
||||
};
|
||||
|
||||
let result = run_grep_tool(input, project.clone(), cx).await;
|
||||
@@ -980,6 +1023,8 @@ mod tests {
|
||||
include_pattern: Some("**/*.rs".to_string()),
|
||||
offset: 0,
|
||||
case_sensitive: false,
|
||||
max_ancestor_lines: default_max_ancestor_lines(),
|
||||
context_lines: default_context_lines(),
|
||||
};
|
||||
|
||||
let result = run_grep_tool(input, project.clone(), cx).await;
|
||||
@@ -1001,6 +1046,67 @@ mod tests {
|
||||
assert_eq!(result, expected);
|
||||
}
|
||||
|
||||
#[gpui::test]
|
||||
async fn test_grep_context_options(cx: &mut TestAppContext) {
|
||||
init_test(cx);
|
||||
cx.executor().allow_parking();
|
||||
|
||||
let text = "fn sample() {\n needle();\n}";
|
||||
let fs = FakeFs::new(cx.executor());
|
||||
fs.insert_tree(path!("/root"), json!({ "main.rs": text }))
|
||||
.await;
|
||||
let project = Project::test(fs, [path!("/root").as_ref()], cx).await;
|
||||
project.update(cx, |project, _cx| {
|
||||
project.languages().add(language::rust_lang())
|
||||
});
|
||||
|
||||
for (regex, max_ancestor_lines, context_lines, line_label, snippet, remaining_lines) in [
|
||||
("sample", 0, 1, "L1", "fn sample() {", Some(2)),
|
||||
(
|
||||
"sample",
|
||||
1,
|
||||
0,
|
||||
"L1-2",
|
||||
"fn sample() {\n needle();",
|
||||
Some(1),
|
||||
),
|
||||
(
|
||||
r"sample\(\) \{\n needle",
|
||||
0,
|
||||
0,
|
||||
"L1-2",
|
||||
"fn sample() {\n needle();",
|
||||
None,
|
||||
),
|
||||
("needle", 0, 0, "L2", " needle();", None),
|
||||
("needle", 0, 1, "L1-3", text, None),
|
||||
("needle", u32::MAX, 0, "L1-3", text, None),
|
||||
("needle", 0, u32::MAX, "L1-3", text, None),
|
||||
] {
|
||||
let input = serde_json::from_value::<GrepToolInput>(json!({
|
||||
"regex": regex,
|
||||
"max_ancestor_lines": max_ancestor_lines,
|
||||
"context_lines": context_lines,
|
||||
}))
|
||||
.expect("valid grep input");
|
||||
let result = run_grep_tool(input, project.clone(), cx).await;
|
||||
let mut expected = format!(
|
||||
"Found 1 matches:\n\n## Matches in root/main.rs\n\n### fn sample › {line_label}\n```\n{snippet}\n```\n"
|
||||
);
|
||||
if let Some(remaining_lines) = remaining_lines {
|
||||
writeln!(
|
||||
expected,
|
||||
"\n{remaining_lines} lines remaining in ancestor node. Read the file to see all."
|
||||
)
|
||||
.expect("write to string");
|
||||
}
|
||||
assert_eq!(
|
||||
result, expected,
|
||||
"regex: {regex}, max_ancestor_lines: {max_ancestor_lines}, context_lines: {context_lines}"
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
async fn run_grep_tool(
|
||||
input: GrepToolInput,
|
||||
project: Entity<Project>,
|
||||
@@ -1093,6 +1199,8 @@ mod tests {
|
||||
include_pattern: None,
|
||||
offset: 0,
|
||||
case_sensitive: false,
|
||||
max_ancestor_lines: default_max_ancestor_lines(),
|
||||
context_lines: default_context_lines(),
|
||||
},
|
||||
project.clone(),
|
||||
cx,
|
||||
@@ -1111,6 +1219,8 @@ mod tests {
|
||||
include_pattern: None,
|
||||
offset: 0,
|
||||
case_sensitive: false,
|
||||
max_ancestor_lines: default_max_ancestor_lines(),
|
||||
context_lines: default_context_lines(),
|
||||
},
|
||||
project.clone(),
|
||||
cx,
|
||||
@@ -1129,6 +1239,8 @@ mod tests {
|
||||
include_pattern: None,
|
||||
offset: 0,
|
||||
case_sensitive: false,
|
||||
max_ancestor_lines: default_max_ancestor_lines(),
|
||||
context_lines: default_context_lines(),
|
||||
},
|
||||
project.clone(),
|
||||
cx,
|
||||
@@ -1146,6 +1258,8 @@ mod tests {
|
||||
include_pattern: None,
|
||||
offset: 0,
|
||||
case_sensitive: false,
|
||||
max_ancestor_lines: default_max_ancestor_lines(),
|
||||
context_lines: default_context_lines(),
|
||||
},
|
||||
project.clone(),
|
||||
cx,
|
||||
@@ -1164,6 +1278,8 @@ mod tests {
|
||||
include_pattern: None,
|
||||
offset: 0,
|
||||
case_sensitive: false,
|
||||
max_ancestor_lines: default_max_ancestor_lines(),
|
||||
context_lines: default_context_lines(),
|
||||
},
|
||||
project.clone(),
|
||||
cx,
|
||||
@@ -1181,6 +1297,8 @@ mod tests {
|
||||
include_pattern: None,
|
||||
offset: 0,
|
||||
case_sensitive: false,
|
||||
max_ancestor_lines: default_max_ancestor_lines(),
|
||||
context_lines: default_context_lines(),
|
||||
},
|
||||
project.clone(),
|
||||
cx,
|
||||
@@ -1199,6 +1317,8 @@ mod tests {
|
||||
include_pattern: None,
|
||||
offset: 0,
|
||||
case_sensitive: false,
|
||||
max_ancestor_lines: default_max_ancestor_lines(),
|
||||
context_lines: default_context_lines(),
|
||||
},
|
||||
project.clone(),
|
||||
cx,
|
||||
@@ -1217,6 +1337,8 @@ mod tests {
|
||||
include_pattern: None,
|
||||
offset: 0,
|
||||
case_sensitive: false,
|
||||
max_ancestor_lines: default_max_ancestor_lines(),
|
||||
context_lines: default_context_lines(),
|
||||
},
|
||||
project.clone(),
|
||||
cx,
|
||||
@@ -1235,6 +1357,8 @@ mod tests {
|
||||
include_pattern: Some("../outside_project/**/*.rs".to_string()),
|
||||
offset: 0,
|
||||
case_sensitive: false,
|
||||
max_ancestor_lines: default_max_ancestor_lines(),
|
||||
context_lines: default_context_lines(),
|
||||
},
|
||||
project.clone(),
|
||||
cx,
|
||||
@@ -1330,6 +1454,8 @@ mod tests {
|
||||
include_pattern: None,
|
||||
offset: 0,
|
||||
case_sensitive: false,
|
||||
max_ancestor_lines: default_max_ancestor_lines(),
|
||||
context_lines: default_context_lines(),
|
||||
},
|
||||
project.clone(),
|
||||
cx,
|
||||
@@ -1384,6 +1510,8 @@ mod tests {
|
||||
include_pattern: Some("worktree1/**/*.rs".to_string()),
|
||||
offset: 0,
|
||||
case_sensitive: false,
|
||||
max_ancestor_lines: default_max_ancestor_lines(),
|
||||
context_lines: default_context_lines(),
|
||||
},
|
||||
project.clone(),
|
||||
cx,
|
||||
|
||||
Reference in New Issue
Block a user