Skip to content

Commit f4734de

Browse files
authored
Merge pull request RooCodeInc#721 from kohii/kotlin
feat: Add Kotlin support in list_code_definition_names
2 parents e5ac33a + 1cd90a6 commit f4734de

File tree

7 files changed

+56
-0
lines changed

7 files changed

+56
-0
lines changed

esbuild.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ const copyWasmFiles = {
5252
"java",
5353
"php",
5454
"swift",
55+
"kotlin",
5556
]
5657

5758
languages.forEach((lang) => {

src/services/tree-sitter/__tests__/index.test.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,8 @@ describe("Tree-sitter Service", () => {
169169
"/test/path/main.rs",
170170
"/test/path/program.cpp",
171171
"/test/path/code.go",
172+
"/test/path/app.kt",
173+
"/test/path/script.kts",
172174
]
173175

174176
;(listFiles as jest.Mock).mockResolvedValue([mockFiles, new Set()])
@@ -197,6 +199,8 @@ describe("Tree-sitter Service", () => {
197199
rs: { parser: mockParser, query: mockQuery },
198200
cpp: { parser: mockParser, query: mockQuery },
199201
go: { parser: mockParser, query: mockQuery },
202+
kt: { parser: mockParser, query: mockQuery },
203+
kts: { parser: mockParser, query: mockQuery },
200204
})
201205
;(fs.readFile as jest.Mock).mockResolvedValue("function test() {}")
202206

@@ -207,6 +211,8 @@ describe("Tree-sitter Service", () => {
207211
expect(result).toContain("main.rs")
208212
expect(result).toContain("program.cpp")
209213
expect(result).toContain("code.go")
214+
expect(result).toContain("app.kt")
215+
expect(result).toContain("script.kts")
210216
})
211217

212218
it("should normalize paths in output", async () => {

src/services/tree-sitter/__tests__/languageParser.test.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,17 @@ describe("Language Parser", () => {
9292
expect(parsers.hpp).toBeDefined()
9393
})
9494

95+
it("should handle Kotlin files correctly", async () => {
96+
const files = ["test.kt", "test.kts"]
97+
const parsers = await loadRequiredLanguageParsers(files)
98+
99+
expect(ParserMock.Language.load).toHaveBeenCalledWith(expect.stringContaining("tree-sitter-kotlin.wasm"))
100+
expect(parsers.kt).toBeDefined()
101+
expect(parsers.kts).toBeDefined()
102+
expect(parsers.kt.query).toBeDefined()
103+
expect(parsers.kts.query).toBeDefined()
104+
})
105+
95106
it("should throw error for unsupported file extensions", async () => {
96107
const files = ["test.unsupported"]
97108

src/services/tree-sitter/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,9 @@ function separateFiles(allFiles: string[]): { filesToParse: string[]; remainingF
8080
"java",
8181
"php",
8282
"swift",
83+
// Kotlin
84+
"kt",
85+
"kts",
8386
].map((e) => `.${e}`)
8487
const filesToParse = allFiles.filter((file) => extensions.includes(path.extname(file))).slice(0, 50) // 50 files max
8588
const remainingFiles = allFiles.filter((file) => !filesToParse.includes(file))

src/services/tree-sitter/languageParser.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import {
1313
javaQuery,
1414
phpQuery,
1515
swiftQuery,
16+
kotlinQuery,
1617
} from "./queries"
1718

1819
export interface LanguageParser {
@@ -120,6 +121,11 @@ export async function loadRequiredLanguageParsers(filesToParse: string[]): Promi
120121
language = await loadLanguage("swift")
121122
query = language.query(swiftQuery)
122123
break
124+
case "kt":
125+
case "kts":
126+
language = await loadLanguage("kotlin")
127+
query = language.query(kotlinQuery)
128+
break
123129
default:
124130
throw new Error(`Unsupported language: ${ext}`)
125131
}

src/services/tree-sitter/queries/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,4 @@ export { default as cQuery } from "./c"
1010
export { default as csharpQuery } from "./c-sharp"
1111
export { default as goQuery } from "./go"
1212
export { default as swiftQuery } from "./swift"
13+
export { default as kotlinQuery } from "./kotlin"
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*
2+
- class declarations (including interfaces)
3+
- function declarations
4+
- object declarations
5+
- property declarations
6+
- type alias declarations
7+
*/
8+
export default `
9+
(class_declaration
10+
(type_identifier) @name.definition.class
11+
) @definition.class
12+
13+
(function_declaration
14+
(simple_identifier) @name.definition.function
15+
) @definition.function
16+
17+
(object_declaration
18+
(type_identifier) @name.definition.object
19+
) @definition.object
20+
21+
(property_declaration
22+
(simple_identifier) @name.definition.property
23+
) @definition.property
24+
25+
(type_alias
26+
(type_identifier) @name.definition.type
27+
) @definition.type
28+
`

0 commit comments

Comments
 (0)