update tree-sitter queries

This commit is contained in:
Test 2024-08-27 14:04:35 -07:00
parent 4e5276dde9
commit bb4dd228f3
11 changed files with 182 additions and 100 deletions

View file

@ -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<ContextItem[]> {
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<void> {
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;
}
}

View file

@ -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;
}

View file

@ -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

View file

@ -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

View file

@ -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
name: (_) @name.definition.interface
) @definition.interface

View file

@ -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
(
(comment)? @comment
(method_definition
name: (_) @name
parameters: (_) @parameters
) @definition
)

View file

@ -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
) @definition

View file

@ -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
name: (name) @name) @definition

View file

@ -1,5 +1,7 @@
(class_definition
name: (_) @name) @body
name: (_) @name) @definition
(function_definition
name: (_) @name) @body
name: (_) @name
parameters: (_) @parameters
) @definition

View file

@ -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
type: (_) @name) @definition

View file

@ -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
(
(comment)? @comment
(interface_declaration
name: (_) @name) @definition
)