Merge pull request #721 from kohii/kotlin

feat: Add Kotlin support in list_code_definition_names
This commit is contained in:
Matt Rubens 2025-03-09 21:49:54 -04:00 committed by GitHub
commit f4734dedb9
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
7 changed files with 56 additions and 0 deletions

View file

@ -52,6 +52,7 @@ const copyWasmFiles = {
"java",
"php",
"swift",
"kotlin",
]
languages.forEach((lang) => {

View file

@ -169,6 +169,8 @@ describe("Tree-sitter Service", () => {
"/test/path/main.rs",
"/test/path/program.cpp",
"/test/path/code.go",
"/test/path/app.kt",
"/test/path/script.kts",
]
;(listFiles as jest.Mock).mockResolvedValue([mockFiles, new Set()])
@ -197,6 +199,8 @@ describe("Tree-sitter Service", () => {
rs: { parser: mockParser, query: mockQuery },
cpp: { parser: mockParser, query: mockQuery },
go: { parser: mockParser, query: mockQuery },
kt: { parser: mockParser, query: mockQuery },
kts: { parser: mockParser, query: mockQuery },
})
;(fs.readFile as jest.Mock).mockResolvedValue("function test() {}")
@ -207,6 +211,8 @@ describe("Tree-sitter Service", () => {
expect(result).toContain("main.rs")
expect(result).toContain("program.cpp")
expect(result).toContain("code.go")
expect(result).toContain("app.kt")
expect(result).toContain("script.kts")
})
it("should normalize paths in output", async () => {

View file

@ -92,6 +92,17 @@ describe("Language Parser", () => {
expect(parsers.hpp).toBeDefined()
})
it("should handle Kotlin files correctly", async () => {
const files = ["test.kt", "test.kts"]
const parsers = await loadRequiredLanguageParsers(files)
expect(ParserMock.Language.load).toHaveBeenCalledWith(expect.stringContaining("tree-sitter-kotlin.wasm"))
expect(parsers.kt).toBeDefined()
expect(parsers.kts).toBeDefined()
expect(parsers.kt.query).toBeDefined()
expect(parsers.kts.query).toBeDefined()
})
it("should throw error for unsupported file extensions", async () => {
const files = ["test.unsupported"]

View file

@ -80,6 +80,9 @@ function separateFiles(allFiles: string[]): { filesToParse: string[]; remainingF
"java",
"php",
"swift",
// Kotlin
"kt",
"kts",
].map((e) => `.${e}`)
const filesToParse = allFiles.filter((file) => extensions.includes(path.extname(file))).slice(0, 50) // 50 files max
const remainingFiles = allFiles.filter((file) => !filesToParse.includes(file))

View file

@ -13,6 +13,7 @@ import {
javaQuery,
phpQuery,
swiftQuery,
kotlinQuery,
} from "./queries"
export interface LanguageParser {
@ -120,6 +121,11 @@ export async function loadRequiredLanguageParsers(filesToParse: string[]): Promi
language = await loadLanguage("swift")
query = language.query(swiftQuery)
break
case "kt":
case "kts":
language = await loadLanguage("kotlin")
query = language.query(kotlinQuery)
break
default:
throw new Error(`Unsupported language: ${ext}`)
}

View file

@ -10,3 +10,4 @@ export { default as cQuery } from "./c"
export { default as csharpQuery } from "./c-sharp"
export { default as goQuery } from "./go"
export { default as swiftQuery } from "./swift"
export { default as kotlinQuery } from "./kotlin"

View file

@ -0,0 +1,28 @@
/*
- class declarations (including interfaces)
- function declarations
- object declarations
- property declarations
- type alias declarations
*/
export default `
(class_declaration
(type_identifier) @name.definition.class
) @definition.class
(function_declaration
(simple_identifier) @name.definition.function
) @definition.function
(object_declaration
(type_identifier) @name.definition.object
) @definition.object
(property_declaration
(simple_identifier) @name.definition.property
) @definition.property
(type_alias
(type_identifier) @name.definition.type
) @definition.type
`