Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Syntax highlighting for comments #212

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
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: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/)
and this project adheres to [Semantic Versioning](http://semver.org/).

## [Unreleased]
### Added
- Syntax highlighting for comments (`#`) ([#212](https://github.com/cucumber/language-service/pull/212))

### Fixed
- (Ruby) Support `And` and `But` step definition annotations ([#211](https://github.com/cucumber/language-service/pull/211))

Expand Down
41 changes: 39 additions & 2 deletions src/service/getGherkinSemanticTokens.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,12 @@ export const semanticTokenTypes: SemanticTokenTypes[] = [
SemanticTokenTypes.type, // @tags and DocString ```type
SemanticTokenTypes.variable, // step <placeholder>
SemanticTokenTypes.property, // examples table header row
SemanticTokenTypes.comment, // # comments
]

const indexByType = Object.fromEntries(semanticTokenTypes.map((type, index) => [type, index]))

// https://microsoft.github.io/language-server-protocol/specifications/specification-3-17/#textDocument_semanticTokens
// https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#textDocument_semanticTokens
export function getGherkinSemanticTokens(
gherkinSource: string,
expressions: readonly Expression[]
Expand Down Expand Up @@ -170,9 +171,45 @@ export function getGherkinSemanticTokens(
inExamples = false
return arr
},
comment(comment, arr) {
return makeLocationToken(comment.location, comment.text, SemanticTokenTypes.comment, arr)
},
})

// Order tokens by positive relative line numbers. Required as table rows
// are processed before comments. Without this ordering, table rows can
// have negative relative line numbers, which is not supported by the
// Language Server Protocol.
const orderedData = orderRelativeTokenPositions(data)

return {
data,
data: orderedData,
}
}

function orderRelativeTokenPositions(data: number[]): number[] {
// Create tokens and convert relative line numbers to absolute
const tokens = []
let line = 0
for (let tokenStartIndex = 0; tokenStartIndex < data.length; tokenStartIndex += 5) {
line += data[tokenStartIndex]
tokens.push([
line,
data[tokenStartIndex + 1],
data[tokenStartIndex + 2],
data[tokenStartIndex + 3],
data[tokenStartIndex + 4],
])
}

// Sort tokens by line number
tokens.sort((a, b) => a[0] - b[0])

// Convert absolute line numbers back to relative
for (let i = tokens.length - 1; i > 0; i--) {
tokens[i][0] -= tokens[i - 1][0]
}

// Flatten the array
return tokens.flat()
}
1 change: 1 addition & 0 deletions test/service/getGherkinSemanticTokens.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ Feature: a
])
const actual = tokenize(gherkinSource, semanticTokens.data)
const expected: TokenWithType[] = [
['# some comment', SemanticTokenTypes.comment],
['@foo', SemanticTokenTypes.type],
['@bar', SemanticTokenTypes.type],
['Feature', SemanticTokenTypes.keyword],
Expand Down