Merge pull request #2944 from continuedev/tomasz/context-cleanup

refactor: group tree sitter queries by language
This commit is contained in:
Nate Sesti 2024-11-15 16:11:06 -08:00 • committed by GitHub
commit 283bb36a05
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
16 changed files with 38 additions and 24 deletions

View file

@ -2,7 +2,7 @@ import { IDE } from "../..";
import { RangeInFileWithContents } from "../../commands/util";
import { PrecalculatedLruCache } from "../../util/LruCache";
import {
TSQueryType,
getFullLanguageName,
getParserForFile,
getQueryForFile,
} from "../../util/treeSitter";
@ -47,7 +47,11 @@ export class ImportDefinitionsService {
},
],
});
const query = await getQueryForFile(filepath, TSQueryType.Imports);
const language = getFullLanguageName(filepath);
const query = await getQueryForFile(
filepath,
`import-queries/${language}.scm`,
);
if (!query) {
return {
imports: {},

View file

@ -4,7 +4,7 @@ import { LRUCache } from "lru-cache";
import Parser from "web-tree-sitter";
import { IDE } from "../../..";
import { getQueryForFile } from "../../../util/treeSitter";
import { getFullLanguageName, getQueryForFile } from "../../../util/treeSitter";
import { AstPath } from "../../util/ast";
import { ImportDefinitionsService } from "../ImportDefinitionsService";
import { AutocompleteSnippet } from "../ranking";
@ -78,10 +78,11 @@ export class RootPathContextService {
// const type = node.type;
// debugger;
// console.log(getSyntaxTreeString(node));
const language = getFullLanguageName(filepath);
query = await getQueryForFile(
filepath,
`root-path-context-queries/${node.type}`,
`root-path-context-queries/${language}/${node.type}.scm`,
);
break;
}
@ -93,9 +94,14 @@ export class RootPathContextService {
const queries = query.matches(node).map(async (match) => {
for (const item of match.captures) {
const endPosition = item.node.endPosition;
const newSnippets = await this.getSnippets(filepath, endPosition);
snippets.push(...newSnippets);
try {
const endPosition = item.node.endPosition;
const newSnippets = await this.getSnippets(filepath, endPosition);
snippets.push(...newSnippets);
} catch (e) {
debugger;
throw e;
}
}
});
@ -151,7 +157,7 @@ export class RootPathContextService {
parentKey = key;
}
debugger;
return snippets;
}
}

View file

@ -3,12 +3,17 @@ import Parser from "web-tree-sitter";
import { getBasename, getLastNPathParts } from "../util/";
import { migrate } from "../util/paths";
import {
TSQueryType,
getFullLanguageName,
getParserForFile,
getQueryForFile,
} from "../util/treeSitter";
import { DatabaseConnection, SqliteDb, tagToString, truncateSqliteLikePattern } from "./refreshIndex";
import {
DatabaseConnection,
SqliteDb,
tagToString,
truncateSqliteLikePattern,
} from "./refreshIndex";
import {
IndexResultType,
MarkCompleteCallback,
@ -31,7 +36,7 @@ export class CodeSnippetsCodebaseIndex implements CodebaseIndex {
relativeExpectedTime: number = 1;
artifactId = "codeSnippets";
constructor(private readonly ide: IDE) { }
constructor(private readonly ide: IDE) {}
private static async _createTables(db: DatabaseConnection) {
await db.exec(`CREATE TABLE IF NOT EXISTS code_snippets (
@ -183,7 +188,12 @@ export class CodeSnippetsCodebaseIndex implements CodebaseIndex {
}
const ast = parser.parse(contents);
const query = await getQueryForFile(filepath, TSQueryType.CodeSnippets);
const language = getFullLanguageName(filepath);
const query = await getQueryForFile(
filepath,
`code-snippet-queries/${language}.scm`,
);
const matches = query?.matches(ast.rootNode);
if (!matches) {
@ -386,7 +396,9 @@ export class CodeSnippetsCodebaseIndex implements CodebaseIndex {
const db = await SqliteDb.get();
await CodeSnippetsCodebaseIndex._createTables(db);
const likePatterns = workspaceDirs.map((dir) => truncateSqliteLikePattern(`${dir}%`));
const likePatterns = workspaceDirs.map((dir) =>
truncateSqliteLikePattern(`${dir}%`),
);
const placeholders = likePatterns.map(() => "?").join(" OR path LIKE ");
const query = `

View file

@ -128,15 +128,9 @@ export async function getLanguageForFile(
}
}
export enum TSQueryType {
CodeSnippets = "code-snippet-queries",
Imports = "import-queries",
// Used in RootPathContextService.ts
FunctionDeclaration = "root-path-context-queries/function_declaration",
MethodDefinition = "root-path-context-queries/method_definition",
FunctionDefinition = "root-path-context-queries/function_definition",
MethodDeclaration = "root-path-context-queries/method_declaration",
}
export const getFullLanguageName = (filepath: string) => {
return supportedLanguages[filepath.split(".").pop() ?? ""];
};
export async function getQueryForFile(
filepath: string,
@ -147,7 +141,6 @@ export async function getQueryForFile(
return undefined;
}
const fullLangName = supportedLanguages[filepath.split(".").pop() ?? ""];
const sourcePath = path.join(
__dirname,
"..",
@ -155,7 +148,6 @@ export async function getQueryForFile(
? ["extensions", "vscode", "tree-sitter"]
: ["tree-sitter"]),
queryPath,
`${fullLangName}.scm`,
);
if (!fs.existsSync(sourcePath)) {
return undefined;