Skip to content

Commit 69d52b3

Browse files
authored
Merge pull request #3 from google-gemini/main
chore: add proper pluralization handling for match in grep tool (google-gemini#2344)
2 parents bc00fb0 + 9665928 commit 69d52b3

File tree

3 files changed

+16
-13
lines changed

3 files changed

+16
-13
lines changed

docs/tools/file-system.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ The Gemini CLI provides a comprehensive suite of tools for interacting with the
159159
- Returns a list of matching lines, each prefixed with its file path (relative to the search directory) and line number.
160160
- **Output (`llmContent`):** A formatted string of matches, e.g.:
161161
```
162-
Found 3 match(es) for pattern "myFunction" in path "." (filter: "*.ts"):
162+
Found 3 matches for pattern "myFunction" in path "." (filter: "*.ts"):
163163
---
164164
File: src/utils.ts
165165
L15: export function myFunction() {

packages/core/src/tools/grep.test.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -115,38 +115,38 @@ describe('GrepTool', () => {
115115
const params: GrepToolParams = { pattern: 'world' };
116116
const result = await grepTool.execute(params, abortSignal);
117117
expect(result.llmContent).toContain(
118-
'Found 3 match(es) for pattern "world" in path "."',
118+
'Found 3 matches for pattern "world" in path "."',
119119
);
120120
expect(result.llmContent).toContain('File: fileA.txt');
121121
expect(result.llmContent).toContain('L1: hello world');
122122
expect(result.llmContent).toContain('L2: second line with world');
123123
expect(result.llmContent).toContain('File: sub/fileC.txt');
124124
expect(result.llmContent).toContain('L1: another world in sub dir');
125-
expect(result.returnDisplay).toBe('Found 3 matche(s)');
125+
expect(result.returnDisplay).toBe('Found 3 matches');
126126
});
127127

128128
it('should find matches in a specific path', async () => {
129129
const params: GrepToolParams = { pattern: 'world', path: 'sub' };
130130
const result = await grepTool.execute(params, abortSignal);
131131
expect(result.llmContent).toContain(
132-
'Found 1 match(es) for pattern "world" in path "sub"',
132+
'Found 1 match for pattern "world" in path "sub"',
133133
);
134134
expect(result.llmContent).toContain('File: fileC.txt'); // Path relative to 'sub'
135135
expect(result.llmContent).toContain('L1: another world in sub dir');
136-
expect(result.returnDisplay).toBe('Found 1 matche(s)');
136+
expect(result.returnDisplay).toBe('Found 1 match');
137137
});
138138

139139
it('should find matches with an include glob', async () => {
140140
const params: GrepToolParams = { pattern: 'hello', include: '*.js' };
141141
const result = await grepTool.execute(params, abortSignal);
142142
expect(result.llmContent).toContain(
143-
'Found 1 match(es) for pattern "hello" in path "." (filter: "*.js")',
143+
'Found 1 match for pattern "hello" in path "." (filter: "*.js")',
144144
);
145145
expect(result.llmContent).toContain('File: fileB.js');
146146
expect(result.llmContent).toContain(
147147
'L2: function baz() { return "hello"; }',
148148
);
149-
expect(result.returnDisplay).toBe('Found 1 matche(s)');
149+
expect(result.returnDisplay).toBe('Found 1 match');
150150
});
151151

152152
it('should find matches with an include glob and path', async () => {
@@ -161,11 +161,11 @@ describe('GrepTool', () => {
161161
};
162162
const result = await grepTool.execute(params, abortSignal);
163163
expect(result.llmContent).toContain(
164-
'Found 1 match(es) for pattern "hello" in path "sub" (filter: "*.js")',
164+
'Found 1 match for pattern "hello" in path "sub" (filter: "*.js")',
165165
);
166166
expect(result.llmContent).toContain('File: another.js');
167167
expect(result.llmContent).toContain('L1: const greeting = "hello";');
168-
expect(result.returnDisplay).toBe('Found 1 matche(s)');
168+
expect(result.returnDisplay).toBe('Found 1 match');
169169
});
170170

171171
it('should return "No matches found" when pattern does not exist', async () => {
@@ -181,7 +181,7 @@ describe('GrepTool', () => {
181181
const params: GrepToolParams = { pattern: 'foo.*bar' }; // Matches 'const foo = "bar";'
182182
const result = await grepTool.execute(params, abortSignal);
183183
expect(result.llmContent).toContain(
184-
'Found 1 match(es) for pattern "foo.*bar" in path "."',
184+
'Found 1 match for pattern "foo.*bar" in path "."',
185185
);
186186
expect(result.llmContent).toContain('File: fileB.js');
187187
expect(result.llmContent).toContain('L1: const foo = "bar";');
@@ -191,7 +191,7 @@ describe('GrepTool', () => {
191191
const params: GrepToolParams = { pattern: 'HELLO' };
192192
const result = await grepTool.execute(params, abortSignal);
193193
expect(result.llmContent).toContain(
194-
'Found 2 match(es) for pattern "HELLO" in path "."',
194+
'Found 2 matches for pattern "HELLO" in path "."',
195195
);
196196
expect(result.llmContent).toContain('File: fileA.txt');
197197
expect(result.llmContent).toContain('L1: hello world');

packages/core/src/tools/grep.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,10 @@ export class GrepTool extends BaseTool<GrepToolParams, ToolResult> {
213213
{} as Record<string, GrepMatch[]>,
214214
);
215215

216-
let llmContent = `Found ${matches.length} match(es) for pattern "${params.pattern}" in path "${searchDirDisplay}"${params.include ? ` (filter: "${params.include}")` : ''}:\n---\n`;
216+
const matchCount = matches.length;
217+
const matchTerm = matchCount === 1 ? 'match' : 'matches';
218+
219+
let llmContent = `Found ${matchCount} ${matchTerm} for pattern "${params.pattern}" in path "${searchDirDisplay}"${params.include ? ` (filter: "${params.include}")` : ''}:\n---\n`;
217220

218221
for (const filePath in matchesByFile) {
219222
llmContent += `File: ${filePath}\n`;
@@ -226,7 +229,7 @@ export class GrepTool extends BaseTool<GrepToolParams, ToolResult> {
226229

227230
return {
228231
llmContent: llmContent.trim(),
229-
returnDisplay: `Found ${matches.length} matche(s)`,
232+
returnDisplay: `Found ${matchCount} ${matchTerm}`,
230233
};
231234
} catch (error) {
232235
console.error(`Error during GrepLogic execution: ${error}`);

0 commit comments

Comments
 (0)