From bb4dd228f3db020ae188d3447132074a9280cf65 Mon Sep 17 00:00:00 2001 From: Test Date: Tue, 27 Aug 2024 14:04:35 -0700 Subject: [PATCH] update tree-sitter queries --- .../providers/RepoMapContextProvider.ts | 75 +++++++++++++------ core/indexing/CodeSnippetsIndex.ts | 73 ++++++++++-------- .../tree-sitter/code-snippet-queries/c.scm | 2 +- .../tree-sitter/code-snippet-queries/go.scm | 23 ++++-- .../tree-sitter/code-snippet-queries/java.scm | 13 +++- .../code-snippet-queries/javascript.scm | 30 +++++--- .../code-snippet-queries/ocaml.scm | 6 +- .../tree-sitter/code-snippet-queries/php.scm | 6 +- .../code-snippet-queries/python.scm | 6 +- .../tree-sitter/code-snippet-queries/rust.scm | 9 ++- .../code-snippet-queries/typescript.scm | 39 ++++++---- 11 files changed, 182 insertions(+), 100 deletions(-) diff --git a/core/context/providers/RepoMapContextProvider.ts b/core/context/providers/RepoMapContextProvider.ts index 514839292..1d629e9f5 100644 --- a/core/context/providers/RepoMapContextProvider.ts +++ b/core/context/providers/RepoMapContextProvider.ts @@ -16,10 +16,13 @@ class RepoMapContextProvider extends BaseContextProvider { "this map contains the name of the file, and the signature for any " + "classes, methods, or functions in the file.\n\n"; + // The max percent of the context window we will take + REPO_MAX_CONTEXT_LENGTH_RATIO = 0.5; + static description: ContextProviderDescription = { title: "repo_map", displayTitle: "Repository Map", - description: "Overview of the repository structure", + description: "List of files and signatures", type: "normal", }; @@ -27,49 +30,79 @@ class RepoMapContextProvider extends BaseContextProvider { query: string, extras: ContextProviderExtras, ): Promise { - const repoMapPath = getRepoMapFilePath(); - - await this.generateRepoMap(extras); - - const content = fs.readFileSync(repoMapPath, "utf8"); - return [ { name: "Repository Map", description: "Overview of the repository structure", - content, + content: await this.generateRepoMap(extras), }, ]; } - private async generateRepoMap(extras: ContextProviderExtras): Promise { + private indentMultilineString(str: string) { + return str + .split("\n") + .map((line: any) => "\t" + line) + .join("\n"); + } + + private async generateRepoMap({ llm, ide }: ContextProviderExtras) { const repoMapPath = getRepoMapFilePath(); - const [workspaceDir] = await extras.ide.getWorkspaceDirs(); + const [workspaceDir] = await ide.getWorkspaceDirs(); + const maxRepoMapTokens = + llm.contextLength * this.REPO_MAX_CONTEXT_LENGTH_RATIO; if (fs.existsSync(repoMapPath)) { - console.log(`Overwriting existing repo map at ${repoMapPath}`); + console.debug(`Overwriting existing repo map at ${repoMapPath}`); } const writeStream = fs.createWriteStream(repoMapPath); writeStream.write(this.repoMapPreamble); - for await (const { - path: absolutePath, - signatures, - } of CodeSnippetsCodebaseIndex.getAllPathsAndSignatures(workspaceDir)) { - const relativePath = path.relative(workspaceDir, absolutePath); + let curTokens = llm.countTokens(this.repoMapPreamble); - writeStream.write(`${relativePath}:\n`); + for await (const pathsAndSignatures of CodeSnippetsCodebaseIndex.getAllPathsAndSignatures( + workspaceDir, + )) { + let content = ""; - for (const signature of signatures) { - writeStream.write(` ${signature}\n`); + for (const [absolutePath, signatures] of Object.entries( + pathsAndSignatures, + )) { + const relativePath = path.relative(workspaceDir, absolutePath); + content += `${relativePath}:\n`; + + for (const signature of signatures.slice(0, -1)) { + content += `${this.indentMultilineString(signature)}\n\t...\n`; + } + + // Don't add the trailing elipsis for the last entry + content += `${this.indentMultilineString( + signatures[signatures.length - 1], + )}\n\n`; } - writeStream.write("\n"); + curTokens += llm.countTokens(content); + + if (curTokens >= maxRepoMapTokens) { + break; + } + + writeStream.write(content); } writeStream.end(); - console.log(`Generated repo map at ${repoMapPath}`); + console.debug(`Generated repo map at ${repoMapPath}`); + + if (curTokens >= maxRepoMapTokens) { + console.debug( + "Full repo map was unable to be genereated due to context window limitations", + ); + } + + const repoMap = fs.readFileSync(repoMapPath, "utf8"); + + return repoMap; } } diff --git a/core/indexing/CodeSnippetsIndex.ts b/core/indexing/CodeSnippetsIndex.ts index 34e065b50..fff4ceba9 100644 --- a/core/indexing/CodeSnippetsIndex.ts +++ b/core/indexing/CodeSnippetsIndex.ts @@ -37,7 +37,7 @@ export class CodeSnippetsCodebaseIndex implements CodebaseIndex { cacheKey TEXT NOT NULL, content TEXT NOT NULL, title TEXT NOT NULL, - signature TEXT NOT NULL, + signature TEXT, startLine INTEGER NOT NULL, endLine INTEGER NOT NULL )`); @@ -107,47 +107,58 @@ export class CodeSnippetsCodebaseIndex implements CodebaseIndex { } private getSnippetsFromMatch(match: Parser.QueryMatch): SnippetChunk { + const bodyTypesToTreatAsSignatures = [ + "interface_declaration", // TypeScript, Java + "struct_item", // Rust + "type_spec", // Go + ]; + + const bodyCaptureGroupPrefixes = ["definition", "reference"]; + let title = "", content = "", signature = "", - parameters = "", - returnType = "", startLine = 0, - endLine = 0; - - const nodeTypesToTreatAsSignatures = ["interface_declaration"]; + endLine = 0, + hasSeenBody = false; + // This loop assumes that the ordering of the capture groups is represenatative + // of the structure of the language, e.g. for a TypeScript match on a function, + // `function myFunc(param: string): string`, the first capture would be the `myFunc` + // the second capture would be the `(param: string)`, etc for (const { name, node } of match.captures) { + // Assume we are capturing groups using a dot syntax for more precise groupings + // However, for this case, we only care about the first substring + const trimmedCaptureName = name.split(".")[0]; + const nodeText = node.text; const nodeType = node.type; - switch (name) { - case "name": + if (bodyCaptureGroupPrefixes.includes(trimmedCaptureName)) { + if (bodyTypesToTreatAsSignatures.includes(nodeType)) { + // Note we override whatever existing value there is here + signature = nodeText; + hasSeenBody = true; + } + + content = nodeText; + startLine = node.startPosition.row; + endLine = node.endPosition.row; + } else { + if (trimmedCaptureName === "name") { title = nodeText; - break; - case "body": - if (nodeTypesToTreatAsSignatures.includes(nodeType)) { - signature = nodeText; + } + + if (!hasSeenBody) { + signature += nodeText + " "; + + if (trimmedCaptureName === "comment") { + signature += "\n"; } - - content = nodeText; - startLine = node.startPosition.row; - endLine = node.endPosition.row; - - break; - case "parameters": - parameters = nodeText; - break; - case "return_type": - returnType = nodeText; - break; + } } } - if (signature === "") { - signature = `${title}${parameters}${returnType}`; - } - return { title, content, signature, startLine, endLine }; } @@ -347,7 +358,7 @@ export class CodeSnippetsCodebaseIndex implements CodebaseIndex { static async *getAllPathsAndSignatures( workspaceDir: string, batchSize: number = 1000, - ): AsyncGenerator<{ path: string; signatures: string[] }> { + ): AsyncGenerator<{ [path: string]: string[] }> { const db = await SqliteDb.get(); await CodeSnippetsCodebaseIndex._createTables(db); @@ -378,9 +389,7 @@ export class CodeSnippetsCodebaseIndex implements CodebaseIndex { groupedByPath[row.path].push(row.signature); } - for (const [path, signatures] of Object.entries(groupedByPath)) { - yield { path, signatures }; - } + yield groupedByPath; offset += batchSize; } diff --git a/extensions/vscode/tree-sitter/code-snippet-queries/c.scm b/extensions/vscode/tree-sitter/code-snippet-queries/c.scm index bb343911e..ff3cb6dc2 100644 --- a/extensions/vscode/tree-sitter/code-snippet-queries/c.scm +++ b/extensions/vscode/tree-sitter/code-snippet-queries/c.scm @@ -4,6 +4,6 @@ (declaration type: (union_specifier name: (type_identifier) @name.definition.class)) @definition.class -(type_definition declarator: (type_identifier) @name ) @body +(type_definition declarator: (type_identifier) @name ) @definition (enum_specifier name: (type_identifier) @name.definition.type) @definition.type diff --git a/extensions/vscode/tree-sitter/code-snippet-queries/go.scm b/extensions/vscode/tree-sitter/code-snippet-queries/go.scm index 9d2af874d..bac626605 100644 --- a/extensions/vscode/tree-sitter/code-snippet-queries/go.scm +++ b/extensions/vscode/tree-sitter/code-snippet-queries/go.scm @@ -1,19 +1,26 @@ ( - (comment)* @doc + (comment)* @comment . (function_declaration - name: (identifier) @name.definition.function) @definition.function - (#strip! @doc "^//\\s*") - (#set-adjacent! @doc @definition.function) + name: (identifier) @name.definition.function + parameters: (_) @parameters + result: (_)? @return_type + ) @definition.function + (#strip! @comment "^//\\s*") + (#set-adjacent! @comment @definition.function) ) ( - (comment)* @doc + (comment)* @comment . (method_declaration - name: (field_identifier) @name.definition.method) @definition.method - (#strip! @doc "^//\\s*") - (#set-adjacent! @doc @definition.method) + receiver: (_) @receiver + name: (field_identifier) @name.definition.method + parameters: (_) @parameters + result: (_)? @return_type + ) @definition.method + (#strip! @comment "^//\\s*") + (#set-adjacent! @comment @definition.method) ) (type_spec diff --git a/extensions/vscode/tree-sitter/code-snippet-queries/java.scm b/extensions/vscode/tree-sitter/code-snippet-queries/java.scm index 593a75fc6..0aeccc319 100644 --- a/extensions/vscode/tree-sitter/code-snippet-queries/java.scm +++ b/extensions/vscode/tree-sitter/code-snippet-queries/java.scm @@ -1,8 +1,15 @@ (class_declaration - name: (identifier) @name.definition.class) @definition.class + name: (_) @name.definition.class + interfaces: (_) @interfaces +) @definition.class + (method_declaration - name: (identifier) @name.definition.method) @definition.method + type: (_) @return_type + name: (_) @name.definition.method + parameters: (_) @parameters +) @definition.method (interface_declaration - name: (identifier) @name.definition.interface) @definition.interface \ No newline at end of file + name: (_) @name.definition.interface +) @definition.interface \ No newline at end of file diff --git a/extensions/vscode/tree-sitter/code-snippet-queries/javascript.scm b/extensions/vscode/tree-sitter/code-snippet-queries/javascript.scm index 36be2f491..3b6153c90 100644 --- a/extensions/vscode/tree-sitter/code-snippet-queries/javascript.scm +++ b/extensions/vscode/tree-sitter/code-snippet-queries/javascript.scm @@ -1,12 +1,22 @@ -(class_declaration - name: (_) @name) @body +( + (comment)? @comment + (class_declaration + name: (_) @name + ) @definition +) -(function_declaration - name: (_) @name - parameters: (_) @parameters -) @body +( + (comment)? @comment + (function_declaration + name: (_) @name + parameters: (_) @parameters + ) @definition +) -(method_definition - name: (_) @name - parameters: (_) @parameters -) @body \ No newline at end of file +( + (comment)? @comment + (method_definition + name: (_) @name + parameters: (_) @parameters + ) @definition +) \ No newline at end of file diff --git a/extensions/vscode/tree-sitter/code-snippet-queries/ocaml.scm b/extensions/vscode/tree-sitter/code-snippet-queries/ocaml.scm index 9bc034d8e..9040cad33 100644 --- a/extensions/vscode/tree-sitter/code-snippet-queries/ocaml.scm +++ b/extensions/vscode/tree-sitter/code-snippet-queries/ocaml.scm @@ -1,12 +1,12 @@ (value_definition (let_binding pattern: (_) @name) -) @body +) @definition (module_type_definition - name: (_) @name) @body + name: (_) @name) @definition (class_definition (class_binding name: (_) @name) -) @body \ No newline at end of file +) @definition \ No newline at end of file diff --git a/extensions/vscode/tree-sitter/code-snippet-queries/php.scm b/extensions/vscode/tree-sitter/code-snippet-queries/php.scm index c95663f3c..124de18ea 100644 --- a/extensions/vscode/tree-sitter/code-snippet-queries/php.scm +++ b/extensions/vscode/tree-sitter/code-snippet-queries/php.scm @@ -1,8 +1,8 @@ (class_declaration - name: (name) @name) @body + name: (name) @name) @definition (function_definition - name: (name) @name) @body + name: (name) @name) @definition (method_declaration - name: (name) @name) @body \ No newline at end of file + name: (name) @name) @definition \ No newline at end of file diff --git a/extensions/vscode/tree-sitter/code-snippet-queries/python.scm b/extensions/vscode/tree-sitter/code-snippet-queries/python.scm index 04289704f..586d738c7 100644 --- a/extensions/vscode/tree-sitter/code-snippet-queries/python.scm +++ b/extensions/vscode/tree-sitter/code-snippet-queries/python.scm @@ -1,5 +1,7 @@ (class_definition - name: (_) @name) @body + name: (_) @name) @definition (function_definition - name: (_) @name) @body \ No newline at end of file + name: (_) @name + parameters: (_) @parameters +) @definition \ No newline at end of file diff --git a/extensions/vscode/tree-sitter/code-snippet-queries/rust.scm b/extensions/vscode/tree-sitter/code-snippet-queries/rust.scm index e4125cabf..4b10e7c34 100644 --- a/extensions/vscode/tree-sitter/code-snippet-queries/rust.scm +++ b/extensions/vscode/tree-sitter/code-snippet-queries/rust.scm @@ -1,8 +1,11 @@ (function_item - name: (_) @name) @body + name: (_) @name + parameters: (_) @parameters + return_type: (_)? @return_type +) @definition (struct_item - name: (_) @name) @body + name: (_) @name) @definition (impl_item - type: (_) @name) @body \ No newline at end of file + type: (_) @name) @definition \ No newline at end of file diff --git a/extensions/vscode/tree-sitter/code-snippet-queries/typescript.scm b/extensions/vscode/tree-sitter/code-snippet-queries/typescript.scm index 02269b9ee..d564f406a 100644 --- a/extensions/vscode/tree-sitter/code-snippet-queries/typescript.scm +++ b/extensions/vscode/tree-sitter/code-snippet-queries/typescript.scm @@ -1,17 +1,28 @@ -(class_declaration - name: (_) @name) @body +( + (comment)? @comment + (class_declaration + name: (_) @name + ) @definition +) -(function_declaration - name: (_) @name - parameters: (_) @parameters - return_type: (_)? @return_type -) @body +( + (comment)? @comment + (function_declaration + name: (_) @name + parameters: (_) @parameters + ) @definition +) -(method_definition - name: (_) @name - parameters: (_) @parameters - return_type: (_)? @return_type -) @body +( + (comment)? @comment + (method_definition + name: (_) @name + parameters: (_) @parameters + ) @definition +) -(interface_declaration - name: (_) @name) @body \ No newline at end of file +( + (comment)? @comment + (interface_declaration + name: (_) @name) @definition +) \ No newline at end of file