Skip to content
Open
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
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,8 @@
"tree-sitter-ruby": "0.23.1",
"tree-sitter-rust": "0.23.1",
"tree-sitter-scala": "^0.23.4",
"tree-sitter-typescript": "0.23.2"
"tree-sitter-typescript": "0.23.2",
"tree-sitter-kotlin": "^0.3.8"
},
"devDependencies": {
"@cucumber/cucumber": "^13.0.0",
Expand Down
5 changes: 5 additions & 0 deletions scripts/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@ const languages = [
dir: '',
wasm: 'scala',
},
{
npm: 'tree-sitter-kotlin',
dir: '',
wasm: 'kotlin',
},
]

// Build wasm parsers for supported languages
Expand Down
88 changes: 88 additions & 0 deletions src/language/kotlinLanguage.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
import { Language, TreeSitterSyntaxNode } from './types.js'

export const kotlinLanguage: Language = {
toParameterTypeName(node) {
switch (node.type) {
case 'string_literal': {
return stringLiteral(node)
}
case 'simple_identifier': {
return node.text
}
default: {
throw new Error(`Unsupported node type ${node.type}`)
}
}
},
toParameterTypeRegExps(node) {
return stringLiteral(node)
},
toStepDefinitionExpression(node) {
if (node.type === 'string_literal') {
const text = stringLiteral(node)
const hasRegExpAnchors = text[0] == '^' || text[text.length - 1] == '$'
return hasRegExpAnchors ? new RegExp(text) : text
}
throw new Error(`Unsupported node type ${node.type}`)
},

defineParameterTypeQueries: [
`
(function_declaration
(modifiers
(annotation
(constructor_invocation
(user_type (type_identifier) @annotation-name)
(value_arguments (value_argument (string_literal) @expression))
)
)
)
(simple_identifier) @name
(#eq? @annotation-name "ParameterType")
) @root
`,
],
defineStepDefinitionQueries: [
`
(function_declaration
(modifiers
(annotation
(constructor_invocation
(user_type (type_identifier) @annotation-name)
(value_arguments (value_argument (string_literal) @expression))
)
)
)
(#match? @annotation-name "Given|When|Then|And|But")
) @root
`,
],
snippetParameters: {
int: { type: 'Int', name: 'i' },
float: { type: 'Float', name: 'f' },
word: { type: 'String' },
string: { type: 'String', name: 's' },
double: { type: 'Double', name: 'd' },
bigdecimal: { type: 'java.math.BigDecimal', name: 'bigDecimal' },
byte: { type: 'Byte', name: 'b' },
short: { type: 'Short', name: 's' },
long: { type: 'Long', name: 'l' },
biginteger: { type: 'java.math.BigInteger', name: 'bigInteger' },
'': { type: 'Any', name: 'arg' },
},
defaultSnippetTemplate: `
@{{ keyword }}("{{ expression }}")
fun {{ #underscore }}{{ expression }}{{ /underscore }}({{ #parameters }}{{ #seenParameter }}, {{ /seenParameter }}{{ name }}: {{ type }}{{ /parameters }}) {
// {{ blurb }}
}
`,
}

function stringLiteral(node: TreeSitterSyntaxNode | null): string {
if (node === null) throw new Error('node cannot be null')
const text = node.text
if (text.startsWith('"') && text.endsWith('"')) {
return text.slice(1, -1).replace(/\\\\/g, '\\')
}
return text
}
2 changes: 2 additions & 0 deletions src/language/languages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { csharpLanguage } from './csharpLanguage.js'
import { goLanguage } from './goLanguage.js'
import { javaLanguage } from './javaLanguage.js'
import { javascriptLanguage } from './javascriptLanguage.js'
import { kotlinLanguage } from './kotlinLanguage.js'
import { phpLanguage } from './phpLanguage.js'
import { pythonLanguage } from './pythonLanguage.js'
import { rubyLanguage } from './rubyLanguage.js'
Expand All @@ -21,6 +22,7 @@ const languageByName: Record<LanguageName, Language> = {
javascript: javascriptLanguage,
go: goLanguage,
scala: scalaLanguage,
kotlin: kotlinLanguage,
}

export function getLanguage(languageName: LanguageName): Language {
Expand Down
1 change: 1 addition & 0 deletions src/language/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ export const LanguageNames = [
'javascript',
'go',
'scala',
'kotlin',
] as const
export type LanguageName = (typeof LanguageNames)[number]

Expand Down
5 changes: 5 additions & 0 deletions src/tree-sitter-node/NodeParserAdapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import Go from 'tree-sitter-go'
// @ts-ignore
import Java from 'tree-sitter-java'
// @ts-ignore
import Kotlin from 'tree-sitter-kotlin'
// @ts-ignore
import Php from 'tree-sitter-php'
// @ts-ignore
import Python from 'tree-sitter-python'
Expand Down Expand Up @@ -58,6 +60,9 @@ export class NodeParserAdapter implements ParserAdapter {
case 'scala':
this.parser.setLanguage(Scala)
break
case 'kotlin':
this.parser.setLanguage(Kotlin)
break
default:
throw new Error(`Unsupported language: ${languageName}`)
}
Expand Down