🚸 add titles to code context provider

This commit is contained in:
Nate Sesti 2024-03-09 16:24:52 -08:00
parent 4bb20cd557
commit 283c7ebcad
4 changed files with 39 additions and 36 deletions

View file

@ -33,6 +33,7 @@ export class CodeSnippetsCodebaseIndex implements CodebaseIndex {
path TEXT NOT NULL,
cacheKey TEXT NOT NULL,
content TEXT NOT NULL,
title TEXT NOT NULL,
startLine INTEGER NOT NULL,
endLine INTEGER NOT NULL
)`);
@ -42,7 +43,7 @@ export class CodeSnippetsCodebaseIndex implements CodebaseIndex {
tag TEXT NOT NULL,
snippetId INTEGER NOT NULL,
FOREIGN KEY (snippetId) REFERENCES code_snippets (id)
)`);
)`);
}
private getQuerySource(filepath: string) {
@ -62,8 +63,11 @@ export class CodeSnippetsCodebaseIndex implements CodebaseIndex {
async getSnippetsInFile(
filepath: string,
contents: string,
): Promise<ChunkWithoutID[]> {
): Promise<(ChunkWithoutID & { title: string })[]> {
const lang = await getLanguageForFile(filepath);
if (!lang) {
return [];
}
const parser = await getParserForFile(filepath);
const ast = parser.parse(contents);
const query = lang?.query(this.getQuerySource(filepath));
@ -71,12 +75,14 @@ export class CodeSnippetsCodebaseIndex implements CodebaseIndex {
return (
matches?.flatMap((match) => {
const nodes = match.captures.map((capture) => capture.node);
const results = nodes.map((node) => ({
const node = match.captures[0].node;
const title = match.captures[1].node.text;
const results = {
title,
content: node.text,
startLine: node.startPosition.row,
endLine: node.endPosition.row,
}));
};
return results;
}) ?? []
);
@ -101,11 +107,12 @@ export class CodeSnippetsCodebaseIndex implements CodebaseIndex {
// Add snippets to sqlite
for (const snippet of snippets) {
const { lastID } = await db.run(
`INSERT INTO code_snippets (path, cacheKey, content, startLine, endLine) VALUES (?, ?, ?, ?, ?)`,
`INSERT INTO code_snippets (path, cacheKey, content, title, startLine, endLine) VALUES (?, ?, ?, ?, ?, ?)`,
[
compute.path,
compute.cacheKey,
snippet.content,
snippet.title,
snippet.startLine,
snippet.endLine,
],
@ -174,7 +181,7 @@ export class CodeSnippetsCodebaseIndex implements CodebaseIndex {
const row = await db.get(`SELECT * FROM code_snippets WHERE id = ?`, [id]);
return {
name: getBasename(row.path),
name: row.title,
description: getBasename(row.path, 2),
content: `\`\`\`${getBasename(row.path)}\n${row.content}\n\`\`\``,
};
@ -182,19 +189,24 @@ export class CodeSnippetsCodebaseIndex implements CodebaseIndex {
static async getAll(tag: IndexTag): Promise<ContextSubmenuItem[]> {
const db = await SqliteDb.get();
const rows = await db.all(
`SELECT *
FROM code_snippets cs
JOIN code_snippets_tags cst ON cs.id = cst.snippetId
WHERE cst.tag = ?;
`,
[tagToString(tag)],
);
try {
const rows = await db.all(
`SELECT *
FROM code_snippets cs
JOIN code_snippets_tags cst ON cs.id = cst.snippetId
WHERE cst.tag = ?;
`,
[tagToString(tag)],
);
return rows.map((row) => ({
title: getBasename(row.path),
description: getBasename(row.path, 2),
id: row.id.toString(),
}));
return rows.map((row) => ({
title: row.title,
description: getBasename(row.path, 2),
id: row.id.toString(),
}));
} catch (e) {
console.warn("Error getting all code snippets: ", e);
return [];
}
}
}

View file

@ -90,12 +90,6 @@ export async function getLanguageForFile(
const extension = path.extname(filepath).slice(1);
if (!supportedLanguages[extension]) {
console.warn(
"Unable to load language for file",
extension,
"from path: ",
filepath,
);
return undefined;
}

View file

@ -22,14 +22,9 @@ class VsCodeIde implements IDE {
async getTags(artifactId: string): Promise<IndexTag[]> {
const workspaceDirs = await this.getWorkspaceDirs();
const branches = (await Promise.race([
Promise.all(workspaceDirs.map((dir) => this.getBranch(dir))),
new Promise((resolve) => {
setTimeout(() => {
resolve(["NONE"]);
}, 500);
}),
])) as string[];
const branches = await Promise.all(
workspaceDirs.map((dir) => this.getBranch(dir)),
);
const tags: IndexTag[] = workspaceDirs.map((directory, i) => ({
directory,

View file

@ -5,6 +5,7 @@ import {
BeakerIcon,
BookOpenIcon,
ChevronDoubleRightIcon,
CodeBracketIcon,
Cog6ToothIcon,
CommandLineIcon,
ExclamationCircleIcon,
@ -47,6 +48,7 @@ import { ComboBoxItem } from "./types";
const ICONS_FOR_DROPDOWN: { [key: string]: any } = {
file: FolderIcon,
code: CodeBracketIcon,
terminal: CommandLineIcon,
diff: PlusIcon,
search: MagnifyingGlassIcon,
@ -163,7 +165,7 @@ const MentionList = forwardRef((props: MentionListProps, ref) => {
const [selectedIndex, setSelectedIndex] = useState(0);
const [subMenuTitle, setSubMenuTitle] = useState<string | undefined>(
undefined
undefined,
);
const [querySubmenuItem, setQuerySubmenuItem] = useState<
ComboBoxItem | undefined
@ -186,7 +188,7 @@ const MentionList = forwardRef((props: MentionListProps, ref) => {
const text = tr.doc.textBetween(0, tr.selection.from);
const start = text.lastIndexOf("@");
props.editor.view.dispatch(
tr.delete(start, tr.selection.from).scrollIntoView()
tr.delete(start, tr.selection.from).scrollIntoView(),
);
},
description: "Add a new documentation source",