Skip to content

Commit abcaa8e

Browse files
author
Eric Wheeler
committed
feat: enhance list_code_definition_names to support files
Fix 'cwd option must be a path to a directory' error. Add support for analyzing individual source files. Update tool description to clarify file and directory usage. Signed-off-by: Eric Wheeler <[email protected]>
1 parent 0fd399d commit abcaa8e

File tree

2 files changed

+35
-12
lines changed

2 files changed

+35
-12
lines changed

src/core/Cline.ts

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2505,10 +2505,10 @@ export class Cline extends EventEmitter<ClineEvents> {
25052505
}
25062506
}
25072507
case "list_code_definition_names": {
2508-
const relDirPath: string | undefined = block.params.path
2508+
const relPath: string | undefined = block.params.path
25092509
const sharedMessageProps: ClineSayTool = {
25102510
tool: "listCodeDefinitionNames",
2511-
path: getReadablePath(this.cwd, removeClosingTag("path", relDirPath)),
2511+
path: getReadablePath(this.cwd, removeClosingTag("path", relPath)),
25122512
}
25132513
try {
25142514
if (block.partial) {
@@ -2519,19 +2519,35 @@ export class Cline extends EventEmitter<ClineEvents> {
25192519
await this.ask("tool", partialMessage, block.partial).catch(() => {})
25202520
break
25212521
} else {
2522-
if (!relDirPath) {
2522+
if (!relPath) {
25232523
this.consecutiveMistakeCount++
25242524
pushToolResult(
25252525
await this.sayAndCreateMissingParamError("list_code_definition_names", "path"),
25262526
)
25272527
break
25282528
}
25292529
this.consecutiveMistakeCount = 0
2530-
const absolutePath = path.resolve(this.cwd, relDirPath)
2531-
const result = await parseSourceCodeForDefinitionsTopLevel(
2532-
absolutePath,
2533-
this.rooIgnoreController,
2534-
)
2530+
const absolutePath = path.resolve(this.cwd, relPath)
2531+
let result: string
2532+
try {
2533+
const stats = await fs.stat(absolutePath)
2534+
if (stats.isFile()) {
2535+
const fileResult = await parseSourceCodeDefinitionsForFile(
2536+
absolutePath,
2537+
this.rooIgnoreController,
2538+
)
2539+
result = fileResult ?? "No source code definitions found in this file."
2540+
} else if (stats.isDirectory()) {
2541+
result = await parseSourceCodeForDefinitionsTopLevel(
2542+
absolutePath,
2543+
this.rooIgnoreController,
2544+
)
2545+
} else {
2546+
result = "The specified path is neither a file nor a directory."
2547+
}
2548+
} catch {
2549+
result = `${absolutePath}: does not exist or cannot be accessed.`
2550+
}
25352551
const completeMessage = JSON.stringify({
25362552
...sharedMessageProps,
25372553
content: result,

src/core/prompts/tools/list-code-definition-names.ts

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,23 @@ import { ToolArgs } from "./types"
22

33
export function getListCodeDefinitionNamesDescription(args: ToolArgs): string {
44
return `## list_code_definition_names
5-
Description: Request to list definition names (classes, functions, methods, etc.) used in source code files at the top level of the specified directory. This tool provides insights into the codebase structure and important constructs, encapsulating high-level concepts and relationships that are crucial for understanding the overall architecture.
5+
Description: Request to list definition names (classes, functions, methods, etc.) from source code. This tool can analyze either a single file or all files at the top level of a specified directory. It provides insights into the codebase structure and important constructs, encapsulating high-level concepts and relationships that are crucial for understanding the overall architecture.
66
Parameters:
7-
- path: (required) The path of the directory (relative to the current working directory ${args.cwd}) to list top level source code definitions for.
7+
- path: (required) The path of the file or directory (relative to the current working directory ${args.cwd}) to analyze. When given a directory, it lists definitions from all top-level source files.
88
Usage:
99
<list_code_definition_names>
1010
<path>Directory path here</path>
1111
</list_code_definition_names>
1212
13-
Example: Requesting to list all top level source code definitions in the current directory
13+
Examples:
14+
15+
1. List definitions from a specific file:
16+
<list_code_definition_names>
17+
<path>src/main.ts</path>
18+
</list_code_definition_names>
19+
20+
2. List definitions from all files in a directory:
1421
<list_code_definition_names>
15-
<path>.</path>
22+
<path>src/</path>
1623
</list_code_definition_names>`
1724
}

0 commit comments

Comments
 (0)