Skip to content

Add java/kotlin parser. #24

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions packages/quick-question-indexer/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
"simple-git": "^3.17.0",
"superstring": "^2.4.4",
"tree-sitter": "^0.20.1",
"tree-sitter-java": "^0.19.1",
"tree-sitter-kotlin": "^0.2.11",
"tree-sitter-python": "^0.20.1",
"tree-sitter-typescript": "^0.20.1",
"typescript": "4.9.5"
Expand Down
19 changes: 17 additions & 2 deletions packages/quick-question-indexer/src/parser.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
import fs from "fs";
import path from "path";
import { Location } from "superstring";
import { PythonParser, TsxParser } from "./treesitter-parser";
import {
PythonParser,
TsxParser,
JavaParser,
KotlinParser,
} from "./treesitter-parser";

export interface Chunk {
language: string;
Expand All @@ -25,7 +30,7 @@ interface LanguageInfo {
const LanguageInfos: Array<LanguageInfo> = [
{
languageName: "python",
parser: new PythonParser,
parser: new PythonParser(),
extensions: [".py"],
},
{
Expand All @@ -48,6 +53,16 @@ const LanguageInfos: Array<LanguageInfo> = [
parser: new TsxParser({ languageName: "tsx" }),
extensions: [".tsx"],
},
{
languageName: "java",
parser: new JavaParser(),
extensions: [".java"],
},
{
languageName: "kotlin",
parser: new KotlinParser(),
extensions: [".kt"],
},
];

export async function parseFile(file: string): Promise<Chunk[]> {
Expand Down
10 changes: 10 additions & 0 deletions packages/quick-question-indexer/src/treesitter-parser.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import TreeSitter, { SyntaxNode } from "tree-sitter";
import TreeSitterPython from "tree-sitter-python";
import TreeSitterTypescript from "tree-sitter-typescript";
import TreeSitterJava from "tree-sitter-java";
import TreeSitterKotlin from "tree-sitter-kotlin";
import { TextBuffer } from "superstring";
import { Chunk, CodeParser } from "./parser";

Expand Down Expand Up @@ -95,3 +97,11 @@ export class PythonParser extends TreeSitterParser {
export class TsxParser extends TreeSitterParser {
language = TreeSitterTypescript.tsx;
}

export class JavaParser extends TreeSitterParser {
language = TreeSitterJava;
}

export class KotlinParser extends TreeSitterParser {
language = TreeSitterKotlin;
}
20 changes: 20 additions & 0 deletions packages/quick-question-indexer/tests/data/java.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package tests.data;

/**
* Simple class printing messages.
*/
@AnyAnnotation("this")
public class Greeter {
String hello = "Hello ";

/**
* Print greeting message.
* @param message
*/
public void greet(String message) {
System.out.println(hello + message);
}
}

public class Ignore {
}
31 changes: 31 additions & 0 deletions packages/quick-question-indexer/tests/data/kotlin.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package tests.data;

// Print words with space
fun printWithSpace(words: List<String>) {
words.forEach {
print(it)
print(" ")
}
}

/**
* Simple class printing messages.
*/
class Greeter(message: String) {
val words = listOf(hello, message)

fun greet() {
printWithSpace(words);
}

companion object {
val hello = "Hello"
}
}

/**
* Class with annotation.
*/
@AnyAnnotation("this")
class Foo() {
}
36 changes: 36 additions & 0 deletions packages/quick-question-indexer/tests/parser.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,39 @@ describe("parseFile: tsx", function () {
assert.match(chunks[1].code, /class ContainerComponent.*/);
});
});

describe("parseFile: java", function () {
it("should success", async function () {
const chunks = await parseFile("./tests/data/java.java");
assert.equal(chunks.length, 1);
assert.ok(
chunks[0].code.startsWith(
'/**\n * Simple class printing messages.\n */\n@AnyAnnotation("this")\npublic class Greeter'
)
);
assert.ok(chunks[0].code.endsWith("}"));
});
});

describe("parseFile: kotlin", function () {
it("should success", async function () {
const chunks = await parseFile("./tests/data/kotlin.kt");
assert.equal(chunks.length, 3);
assert.ok(
chunks[0].code.startsWith("// Print words with space\nfun printWithSpace")
);
assert.ok(chunks[0].code.endsWith("}"));
assert.ok(
chunks[1].code.startsWith(
"/**\n * Simple class printing messages.\n */\nclass Greeter"
)
);
assert.ok(chunks[1].code.endsWith("}"));
assert.ok(
chunks[2].code.startsWith(
'/**\n * Class with annotation.\n */\n@AnyAnnotation("this")\nclass Foo'
)
);
assert.ok(chunks[2].code.endsWith("}"));
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
declare module "tree-sitter-java";
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
declare module "tree-sitter-kotlin";
16 changes: 15 additions & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4954,7 +4954,7 @@ mz@^2.4.0:
object-assign "^4.0.1"
thenify-all "^1.0.0"

nan@^2.14.0, nan@^2.14.2, nan@^2.15.0:
nan@^2.12.1, nan@^2.14.0, nan@^2.14.1, nan@^2.14.2, nan@^2.15.0:
version "2.17.0"
resolved "https://registry.npmjs.org/nan/-/nan-2.17.0.tgz"
integrity sha512-2ZTgtl0nJsO0KQCjEpxcIr5D+Yv90plTitZt9JBfQvVJDS5seMl3FOvsh3+9CoYWXf/1l5OaZzzF6nDm4cagaQ==
Expand Down Expand Up @@ -6962,6 +6962,20 @@ tr46@~0.0.3:
resolved "https://registry.npmmirror.com/tr46/-/tr46-0.0.3.tgz"
integrity sha512-N3WMsuqV66lT30CrXNbEjx4GEwlow3v6rr4mCcv6prnfwhS01rkgyFdjPNBYd9br7LpXV1+Emh01fHnq2Gdgrw==

tree-sitter-java@^0.19.1:
version "0.19.1"
resolved "https://registry.yarnpkg.com/tree-sitter-java/-/tree-sitter-java-0.19.1.tgz#c1bf59caaa81bf8a4a6dda99e7c97e51b3bd26d4"
integrity sha512-yVm+4q1D4niaHcEf2iqhOcIaiSp3wxHjeC4eoLAqSQNVxSrhThmT1FEfM4yDgHV4XaxH+62xpKHCwYG9NzRt6Q==
dependencies:
nan "^2.14.1"

tree-sitter-kotlin@^0.2.11:
version "0.2.11"
resolved "https://registry.yarnpkg.com/tree-sitter-kotlin/-/tree-sitter-kotlin-0.2.11.tgz#1ffaa4b81ea0dd57940981e0070493d9f6622345"
integrity sha512-xrLaWDZws7kYakkVGclw9lC8X78lOi+pbYt1MoTmtzkhUFcmJSIaOpM7sPaiWlXaVcAk1jRY/0ZbcRMMn0P6Mw==
dependencies:
nan "^2.12.1"

tree-sitter-python@^0.20.1:
version "0.20.1"
resolved "https://registry.npmjs.org/tree-sitter-python/-/tree-sitter-python-0.20.1.tgz"
Expand Down