|
| 1 | +import { Language, TreeSitterSyntaxNode } from './types.js' |
| 2 | + |
| 3 | +export const scalaLanguage: Language = { |
| 4 | + toParameterTypeName(node) { |
| 5 | + switch (node.type) { |
| 6 | + case 'string': { |
| 7 | + return stringLiteral(node) |
| 8 | + } |
| 9 | + case 'identifier': { |
| 10 | + return node.text |
| 11 | + } |
| 12 | + default: { |
| 13 | + throw new Error(`Unsupported node type ${node.type}`) |
| 14 | + } |
| 15 | + } |
| 16 | + }, |
| 17 | + toParameterTypeRegExps(node) { |
| 18 | + if (node === null) { |
| 19 | + return /.*/ |
| 20 | + } |
| 21 | + return stringLiteral(node) |
| 22 | + }, |
| 23 | + toStepDefinitionExpression(node) { |
| 24 | + if (node.type === 'string') { |
| 25 | + const text = stringLiteral(node) |
| 26 | + const hasRegExpAnchors = text[0] == '^' || text[text.length - 1] == '$' |
| 27 | + return hasRegExpAnchors ? new RegExp(text) : text |
| 28 | + } |
| 29 | + throw new Error(`Unsupported node type ${node.type}`) |
| 30 | + }, |
| 31 | + |
| 32 | + defineParameterTypeQueries: [ |
| 33 | + ` |
| 34 | +(call_expression |
| 35 | + function: (identifier) @function-name |
| 36 | + arguments: (arguments |
| 37 | + ( |
| 38 | + (string) @name |
| 39 | + (string) |
| 40 | + ) |
| 41 | + ) |
| 42 | + (#match? @function-name "ParameterType") |
| 43 | +) @root |
| 44 | +`, |
| 45 | + ], |
| 46 | + defineStepDefinitionQueries: [ |
| 47 | + ` |
| 48 | +(call_expression |
| 49 | + function: (identifier) @function-name |
| 50 | + arguments: (arguments |
| 51 | + ( |
| 52 | + (string) @expression |
| 53 | + ) |
| 54 | + ) |
| 55 | + (#match? @function-name "Given|When|Then|And|But") |
| 56 | +) @root |
| 57 | +`, |
| 58 | + ], |
| 59 | + snippetParameters: { |
| 60 | + int: { type: 'Int', name: 'i' }, |
| 61 | + float: { type: 'Float', name: 'f' }, |
| 62 | + word: { type: 'String' }, |
| 63 | + string: { type: 'String', name: 's' }, |
| 64 | + double: { type: 'Double', name: 'd' }, |
| 65 | + bigdecimal: { type: 'scala.math.BigDecimal', name: 'bigDecimal' }, |
| 66 | + byte: { type: 'Byte', name: 'b' }, |
| 67 | + short: { type: 'Short', name: 's' }, |
| 68 | + long: { type: 'Long', name: 'l' }, |
| 69 | + biginteger: { type: 'scala.math.BigInteger', name: 'bigInteger' }, |
| 70 | + '': { type: 'Any', name: 'arg' }, |
| 71 | + }, |
| 72 | + defaultSnippetTemplate: ` |
| 73 | +{{ keyword }}("""{{ expression }}""") { ({{ #parameters }}{{ #seenParameter }}, {{ /seenParameter }}{{ name }}: {{ type }}{{ /parameters }}) => |
| 74 | + // {{ blurb }} |
| 75 | +} |
| 76 | +`, |
| 77 | +} |
| 78 | + |
| 79 | +export function stringLiteral(node: TreeSitterSyntaxNode | null): string { |
| 80 | + if (node === null) throw new Error('node cannot be null') |
| 81 | + if (node.text.startsWith('"""')) { |
| 82 | + return node.text.slice(3, -3) |
| 83 | + } |
| 84 | + return node.text.slice(1, -1) |
| 85 | +} |
0 commit comments