continue-custom/core/util/treeSitter.ts

87 lines
1.6 KiB
TypeScript

import * as path from "path";
import Parser from "web-tree-sitter";
export const supportedLanguages: { [key: string]: string } = {
cpp: "cpp",
hpp: "cpp",
cc: "cpp",
cxx: "cpp",
hxx: "cpp",
cs: "c_sharp",
c: "c",
h: "c",
css: "css",
php: "php",
phtml: "php",
php3: "php",
php4: "php",
php5: "php",
php7: "php",
phps: "php",
"php-s": "php",
bash: "bash",
sh: "bash",
json: "json",
ts: "typescript",
mts: "typescript",
cts: "typescript",
tsx: "tsx",
vue: "vue",
yaml: "yaml",
yml: "yaml",
elm: "elm",
js: "javascript",
jsx: "javascript",
mjs: "javascript",
cjs: "javascript",
py: "python",
pyw: "python",
pyi: "python",
el: "elisp",
emacs: "elisp",
ex: "elixir",
exs: "elixir",
go: "go",
eex: "embedded_template",
heex: "embedded_template",
leex: "embedded_template",
html: "html",
htm: "html",
java: "java",
lua: "lua",
ocaml: "ocaml",
ml: "ocaml",
mli: "ocaml",
ql: "ql",
res: "rescript",
resi: "rescript",
rb: "ruby",
erb: "ruby",
rs: "rust",
rdl: "systemrdl",
toml: "toml",
// jl: "julia",
// swift: "swift",
// kt: "kotlin",
// scala: "scala",
};
export async function getParserForFile(filepath: string) {
await Parser.init();
const parser = new Parser();
const extension = path.extname(filepath);
if (!supportedLanguages[extension]) {
return undefined;
}
const wasmPath = path.join(
__dirname,
"tree-sitter-wasms",
`tree-sitter-${supportedLanguages[extension]}.wasm`
);
const Language = await Parser.Language.load(wasmPath);
parser.setLanguage(Language);
return parser;
}