Skip to content

Commit

Permalink
feat(treesitter): try for API #2
Browse files Browse the repository at this point in the history
  • Loading branch information
phodal committed Apr 9, 2024
1 parent d150001 commit 891244f
Show file tree
Hide file tree
Showing 4 changed files with 140 additions and 16 deletions.
4 changes: 2 additions & 2 deletions src/codelens.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as vscode from 'vscode';
import { SUPPORTED_LANGUAGES, SupportedLanguage } from "./language/supported";
import { parse } from './language/parser';
import { getParser } from './language/parser';
import { AutoDevContext } from "./autodev-context";

class AutoDevCodeLensProvider implements vscode.CodeLensProvider {
Expand All @@ -13,7 +13,7 @@ class AutoDevCodeLensProvider implements vscode.CodeLensProvider {
}

try {
const parsed = await parse(langid, document.getText());
const parsed = await getParser(langid, document.getText());
console.log(parsed);
} catch (e) {
console.log(e);
Expand Down
9 changes: 7 additions & 2 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { DocumentManager } from "./document/DocumentManager";
import { DiffManager } from "./diff/DiffManager";
import { AutoDevContext } from "./autodev-context";
import { JavaSemanticLsp } from "./semantic-lsp/JavaSemanticLsp";
import { getParserForFile } from "./language/parser";

const channel = vscode.window.createOutputChannel("AUTO-DEV-VSCODE");
export function activate(context: vscode.ExtensionContext) {
Expand All @@ -23,7 +24,7 @@ export function activate(context: vscode.ExtensionContext) {
registerCommands(autoDevContext);

vscode.window.onDidChangeActiveTextEditor(
(editor: vscode.TextEditor | undefined) => {
async (editor: vscode.TextEditor | undefined) => {
if (!editor) {
return;
}
Expand All @@ -38,8 +39,12 @@ export function activate(context: vscode.ExtensionContext) {
var language = editor.document.languageId;
if (language === "java") {
const lsp = new JavaSemanticLsp(autoDevContext);
const client = lsp?.getLanguageClient(language);
const client = lsp?.getLanguageClient();
console.log(client);

let parser = await getParserForFile(uri.fsPath);
console.log(parser);
// getParserForFile(uri, language, editor.document.getText());
}
}
);
Expand Down
89 changes: 87 additions & 2 deletions src/language/parser.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type {SupportedLanguage}from "./supported";
import Parser from "web-tree-sitter";
import {extensionLanguageMap, type SupportedLanguage}from "./supported";
import Parser, { Language } from "web-tree-sitter";

// @ts-ignore
import Tc from "@unit-mesh/treesitter-artifacts/wasm/tree-sitter-c.wasm?raw";
Expand All @@ -19,6 +19,7 @@ import Tts from "@unit-mesh/treesitter-artifacts/wasm/tree-sitter-typescript.was
import Tpython from "@unit-mesh/treesitter-artifacts/wasm/tree-sitter-python.wasm?raw";
// @ts-ignore
import Trust from "@unit-mesh/treesitter-artifacts/wasm/tree-sitter-rust.wasm?raw";
import path from "path";

const PREFIX = "data:application/wasm;base64,";
const LanguageMap: Map<SupportedLanguage, Parser.Language> = new Map();
Expand Down Expand Up @@ -145,3 +146,87 @@ export async function parse(langid: SupportedLanguage, source: string): Promise<
await loadLanguageOndemand(langid);
return ParserMap[langid](source);
}

export async function getLanguageForFile(
filepath: string
): Promise<Language | undefined> {
try {
await Parser.init();
const extension = path.extname(filepath).slice(1);

if (!extensionLanguageMap[extension]) {
return undefined;
}

const wasmPath = path.join(
__dirname,
"tree-sitter-wasms",
`tree-sitter-${extensionLanguageMap[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;
}
}

export async function getParserForFile(filepath: string) {
if (process.env.IS_BINARY) {
return undefined;
}

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 getAst(
filepath: string,
fileContents: string
): Promise<Parser.Tree | undefined> {
const parser = await getParserForFile(filepath);

if (!parser) {
return undefined;
}

try {
const ast = parser.parse(fileContents);
return ast;
} catch (e) {
return undefined;
}
}

export async function getTreePathAtCursor(
ast: Parser.Tree,
cursorIndex: number
): Promise<Parser.SyntaxNode[] | undefined> {
const path = [ast.rootNode];
while (path[path.length - 1].childCount > 0) {
let foundChild = false;
for (let child of path[path.length - 1].children) {
if (child.startIndex <= cursorIndex && child.endIndex >= cursorIndex) {
path.push(child);
foundChild = true;
break;
}
}

if (!foundChild) {
break;
}
}

return path;
}
54 changes: 44 additions & 10 deletions src/language/supported.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,47 @@
export const extensionLanguageMap: { [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",
ts: "typescript",
mts: "typescript",
cts: "typescript",
js: "javascript",
jsx: "javascript",
mjs: "javascript",
cjs: "javascript",
py: "python",
pyw: "python",
pyi: "python",
go: "go",
java: "java",
rs: "rust",
// kt: "kotlin",
};

export const SUPPORTED_LANGUAGES = [
"c",
"cpp",
"csharp",
"go",
"java",
"javascript",
"typescript",
"python",
"rust"
"c",
"cpp",
"csharp",
"go",
"java",
"javascript",
"typescript",
"python",
"rust",
] as const;

export type SupportedLanguage = typeof SUPPORTED_LANGUAGES[number];
export type SupportedLanguage = (typeof SUPPORTED_LANGUAGES)[number];

0 comments on commit 891244f

Please sign in to comment.