From 53f886fd40c7bca29de06156f0e713a9700cc189 Mon Sep 17 00:00:00 2001 From: Patrick Erichsen Date: Thu, 22 Aug 2024 13:13:55 -0700 Subject: [PATCH] fix: addTag --- .gitignore | 2 +- core/indexing/CodeSnippetsIndex.ts | 88 +++++++++++++++++++----------- 2 files changed, 56 insertions(+), 34 deletions(-) diff --git a/.gitignore b/.gitignore index bf28524cd..16956223a 100644 --- a/.gitignore +++ b/.gitignore @@ -166,6 +166,6 @@ Icon? .continue # Sandbox env default file -extensions/vscode/manual-testing-sandbox/example.ts +manual-testing-sandbox .prompts/ \ No newline at end of file diff --git a/core/indexing/CodeSnippetsIndex.ts b/core/indexing/CodeSnippetsIndex.ts index 9d7e08274..3ef21f3d7 100644 --- a/core/indexing/CodeSnippetsIndex.ts +++ b/core/indexing/CodeSnippetsIndex.ts @@ -180,26 +180,23 @@ export class CodeSnippetsCodebaseIndex implements CodebaseIndex { } // Delete - // - // Should this be deleting all entries that match a given path + cacheKey? - // - // When renaming a file, we get a `delete` and an `addTag`. Is this correct? - // `addTag` is throwing an error since we just deleted the `code_snippets` row for (let i = 0; i < results.del.length; i++) { const del = results.del[i]; - const snippetsToDelete = await db.all( + const snippets = await db.all( "SELECT id FROM code_snippets WHERE path = ? AND cacheKey = ?", [del.path, del.cacheKey], ); - const snippetIds = snippetsToDelete.map((row) => row.id).join(","); + if (snippets) { + const snippetIds = snippets.map((row) => row.id).join(","); - await db.run(`DELETE FROM code_snippets WHERE id IN (${snippetIds})`); + await db.run(`DELETE FROM code_snippets WHERE id IN (${snippetIds})`); - await db.run( - `DELETE FROM code_snippets_tags WHERE snippetId IN (${snippetIds})`, - ); + await db.run( + `DELETE FROM code_snippets_tags WHERE snippetId IN (${snippetIds})`, + ); + } markComplete([del], IndexResultType.Delete); } @@ -207,36 +204,61 @@ export class CodeSnippetsCodebaseIndex implements CodebaseIndex { // Add tag for (let i = 0; i < results.addTag.length; i++) { const addTag = results.addTag[i]; + let snippets: (ChunkWithoutID & { title: string })[] = []; + try { + snippets = await this.getSnippetsInFile( + addTag.path, + await this.ide.readFile(addTag.path), + ); + } catch (e) { + // If can't parse, assume malformatted code + console.error(`Error parsing ${addTag.path}:`, e); + } - await db.run( - ` - REPLACE INTO code_snippets_tags (tag, snippetId) - SELECT ?, ( - SELECT id - FROM code_snippets - WHERE cacheKey = ? AND path = ? - ) - `, - [tagString, addTag.cacheKey, addTag.path], - ); + for (const snippet of snippets) { + const { lastID } = await db.run( + "INSERT INTO code_snippets (path, cacheKey, content, title, startLine, endLine) VALUES (?, ?, ?, ?, ?, ?)", + [ + addTag.path, + addTag.cacheKey, + snippet.content, + snippet.title, + snippet.startLine, + snippet.endLine, + ], + ); + await db.run( + "INSERT INTO code_snippets_tags (snippetId, tag) VALUES (?, ?)", + [lastID, tagString], + ); + } markComplete([results.addTag[i]], IndexResultType.AddTag); } // Remove tag for (let i = 0; i < results.removeTag.length; i++) { - const item = results.removeTag[i]; - await db.run( - ` - DELETE FROM code_snippets_tags - WHERE tag = ? - AND snippetId IN ( - SELECT id FROM code_snippets - WHERE cacheKey = ? AND path = ? - ) - `, - [tagString, item.cacheKey, item.path], + const removeTag = results.removeTag[i]; + + const snippets = await db.get( + `SELECT id FROM code_snippets + WHERE cacheKey = ? AND path = ?`, + [removeTag.cacheKey, removeTag.path], ); + + if (snippets) { + const snippetIds = snippets.map((row: any) => row.id).join(","); + + await db.run( + ` + DELETE FROM code_snippets_tags + WHERE tag = ? + AND snippetId IN (${snippetIds}) + `, + [tagString], + ); + } + markComplete([results.removeTag[i]], IndexResultType.RemoveTag); } }