continue-custom/core/util/treeSitter.ts
Nate Sesti 53b61288ec
🎨 Refactor core (#1281)
* 🎨 refactor out react context

* 🎨 refactor local storage

* 🎨 refactor IdeMessenger

* 🔥 removing unused files

* 🚚 move scripts

* ✅ setup VSCode integration testing

* 🎨 fix test-related stuff

* 🚧 testing

* 🎨 tweak terminal cmd+L

* 🧑‍💻 install biome in core

* 🎨 run biome check on core

* 🎨 run biome check in vscode

* 🎨 run biome check in binary

* 🧑‍💻 global biome config

* 🎨 run biome check on core

* 🎨 run biome check in vscode

* 🎨 fix a few biome warnings

* 🚧 WIP on protocols

* 🚧 progress, organizing, about to remove webviewCore protocol

* 🚧 now running

* 🚧 move indexing code to core

* 🚧 WIP on JetBrains indexing implementation

* 🎨 finish listDir

* 🚧 gui fixes

* 🏷️ update IMessenger.on return type

* 🐛 fix jetbrains types + IDE detection

* 🧑‍💻 set up debugging for binary with TcpMessenger

* 👷 fix prepackage.js

* 🧑‍💻 turn off debug mode for intellij

* 🐛 merge fixes

* 🐛 fixes after refactor

* 🐛 merge fixes

* 💄 increase font size in JB

* 🔀 merge changes

* 🩹 small fixes

* 🐛 fix jetbrains build

* 🩹 more jetbrains fixes

* 🎨 jetbrains improvements

* 🐛 fix lancedb prob in jetbrains build

* 👷 update jetbrains build process

* ✨ intellij problems context provider

* 👷 add script utils file

* 💚 fix jetbrains build regression

* 🎨 dynamic import transformers.js

* 🩹 small jetbrains updates

* ✨ folder context provider in jetbrains

* 🎨 many more jetbrains improvements

* 🔀 merge fixes

* ⚡️ small improvements

* 🎨 tell users transformers.js not supported

* 🎨 trial updates

* 📝 update jetbrains readmej

* ⚡️ only use smaller context for local autocomplete models

* ⚡️ improve bracket filter

* ✨ global .continue/.prompts folder

* 💄 improved model setup process

* 🥅 display VSCode req errs
2024-05-24 19:38:13 -07:00

117 lines
2.4 KiB
TypeScript

import * as path from "path";
import Parser, { Language } from "web-tree-sitter";
export const supportedLanguages: { [key: string]: string } = {
cpp: "cpp",
hpp: "cpp",
cc: "cpp",
cxx: "cpp",
hxx: "cpp",
cp: "cpp",
hh: "cpp",
inc: "cpp",
// Depended on this PR: https://github.com/tree-sitter/tree-sitter-cpp/pull/173
// ccm: "cpp",
// c++m: "cpp",
// cppm: "cpp",
// cxxm: "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",
// The .wasm file being used is faulty, and yaml is split line-by-line anyway for the most part
// 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",
sol: "solidity",
// jl: "julia",
// swift: "swift",
// kt: "kotlin",
// scala: "scala",
};
export async function getParserForFile(filepath: string) {
try {
await Parser.init();
const parser = new Parser();
const language = await getLanguageForFile(filepath);
parser.setLanguage(language);
return parser;
} catch (e) {
console.error("Unable to load language for file", filepath, e);
return undefined;
}
}
export async function getLanguageForFile(
filepath: string,
): Promise<Language | undefined> {
try {
await Parser.init();
const extension = path.extname(filepath).slice(1);
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);
return language;
} catch (e) {
console.error("Unable to load language for file", filepath, e);
return undefined;
}
}