Skip to content

Commit

Permalink
r# strings in rust (#176)
Browse files Browse the repository at this point in the history
* Support r# strings in rust

* CHANGELOG entry for r# rust string support
  • Loading branch information
kieran-ryan authored Jan 12, 2024
1 parent 9ae8b77 commit 42bb67a
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 3 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/)
and this project adheres to [Semantic Versioning](http://semver.org/).

## [Unreleased]
### Fixed
- (Rust) Support for r# raw strings with step definition patterns ([#176](https://github.com/cucumber/language-service/pull/176))

### Fixed
- (Python) Unexpected spaces and commas in generated step definitions [#160](https://github.com/cucumber/language-service/issues/160)
Expand Down
6 changes: 3 additions & 3 deletions src/language/rustLanguage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,13 +109,13 @@ fn {{ #lowercase }}{{ #underscore }}{{ expression }}{{ /underscore }}{{ /lowerca
`,
}

function stringLiteral(node: TreeSitterSyntaxNode | null): string {
export function stringLiteral(node: TreeSitterSyntaxNode | null): string {
if (node === null) throw new Error('node cannot be null')
if (node.text[0] === 'r') return unescapeString(node.text.slice(2, -1))
if (node.text.startsWith('r#')) return unescapeString(node.text.slice(3, -2))
if (node.text.startsWith('r')) return unescapeString(node.text.slice(2, -1))
return unescapeString(node.text.slice(1, -1))
}

// rust
function unescapeString(s: string): string {
return s.replace(/\\\\/g, '\\')
}
44 changes: 44 additions & 0 deletions test/language/rustLanguage.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import assert from 'assert'

import { stringLiteral } from '../../src/language/rustLanguage.js'
import { TreeSitterSyntaxNode } from '../../src/language/types.js'

describe('rustLanguage', () => {
it('should extract string literal pattern', () => {
const cases = [
{
input: '"the bee\'s knees"',
expected: "the bee's knees",
},
{
input: '"a {uuid}"',
expected: 'a {uuid}',
},
{
input: 'r"^a regexp$"',
expected: '^a regexp$',
},
{
// eslint-disable-next-line
input: 'r"^stream (\w+) field (\w+) is a key$"',
// eslint-disable-next-line
expected: '^stream (\w+) field (\w+) is a key$',
},
{
input: 'r#"I have cucumber in my belly"#',
expected: 'I have cucumber in my belly',
},
]

cases.forEach(({ input, expected }) => {
const node: TreeSitterSyntaxNode = {
type: '',
text: input,
children: [],
startPosition: { row: 0, column: 0 },
endPosition: { row: 0, column: 0 },
}
assert.strictEqual(stringLiteral(node), expected)
})
})
})

0 comments on commit 42bb67a

Please sign in to comment.