fix: addTag

This commit is contained in:
Patrick Erichsen 2024-08-22 13:13:55 -07:00
parent b9114bbdab
commit 53f886fd40
2 changed files with 56 additions and 34 deletions

2
.gitignore vendored
View file

@ -166,6 +166,6 @@ Icon?
.continue
# Sandbox env default file
extensions/vscode/manual-testing-sandbox/example.ts
manual-testing-sandbox
.prompts/

View file

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