Skip to content

Commit

Permalink
Release 0.18.0
Browse files Browse the repository at this point in the history
  • Loading branch information
aslakhellesoy committed May 10, 2022
1 parent 31c8199 commit b34ea18
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 13 deletions.
8 changes: 7 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ and this project adheres to [Semantic Versioning](http://semver.org/).

## [Unreleased]

## [0.18.0] - 2022-05-10
### Changed
- Autocomlete suggestions now set `filterText` to prevent VSCode from filtering out suggestions
- Autocomlete suggestions now set `sortText` to display suggestions from undefined steps last

## [0.17.0] - 2022-05-09
### Added
- Ruby support ([#44](https://github.com/cucumber/language-service/pull/44))
Expand Down Expand Up @@ -141,7 +146,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
([#1732](https://github.com/cucumber/common/pull/1732)
[aslakhellesoy](https://github.com/aslakhellesoy))

[Unreleased]: https://github.com/cucumber/language-service/compare/v0.17.0...HEAD
[Unreleased]: https://github.com/cucumber/language-service/compare/v0.18.0...HEAD
[0.18.0]: https://github.com/cucumber/language-service/compare/v0.17.0...v0.18.0
[0.17.0]: https://github.com/cucumber/language-service/compare/v0.16.0...v0.17.0
[0.16.0]: https://github.com/cucumber/language-service/compare/v0.15.0...v0.16.0
[0.15.0]: https://github.com/cucumber/language-service/compare/v0.14.4...v0.15.0
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@cucumber/language-service",
"version": "0.17.0",
"version": "0.18.0",
"description": "Cucumber Language Service",
"type": "module",
"main": "dist/cjs/src/index.js",
Expand Down
1 change: 1 addition & 0 deletions src/index/fuseIndex.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export function fuseIndex(suggestions: readonly Suggestion[]): Index {
threshold: 0.1,
ignoreLocation: true,
fieldNormWeight: 1,
shouldSort: true,
})

return (text) => {
Expand Down
29 changes: 23 additions & 6 deletions src/service/getGherkinCompletionItems.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import { walkGherkinDocument } from '@cucumber/gherkin-utils'
import { CompletionItem, CompletionItemKind, InsertTextFormat } from 'vscode-languageserver-types'
import {
CompletionItem,
CompletionItemKind,
InsertTextFormat,
Position,
} from 'vscode-languageserver-types'

import { parseGherkinDocument } from '../gherkin/parseGherkinDocument.js'
import { Index } from '../index/index.js'
Expand All @@ -8,7 +13,7 @@ import { lspCompletionSnippet } from './snippet/lspCompletionSnippet.js'
// https://microsoft.github.io/language-server-protocol/specifications/specification-3-17/#textDocument_completion
export function getGherkinCompletionItems(
gherkinSource: string,
line: number,
position: Position,
index: Index
): readonly CompletionItem[] {
const { gherkinDocument } = parseGherkinDocument(gherkinSource)
Expand All @@ -20,7 +25,7 @@ export function getGherkinCompletionItems(
let endCharacter: number
walkGherkinDocument(gherkinDocument, undefined, {
step(step) {
if (step.location.line === line + 1 && step.location.column !== undefined) {
if (step.location.line === position.line + 1 && step.location.column !== undefined) {
text = step.text
startCharacter = step.location.column + step.keyword.length - 1
endCharacter = startCharacter + text.length
Expand All @@ -29,28 +34,40 @@ export function getGherkinCompletionItems(
})
if (text === undefined) return []
const suggestions = index(text)
return suggestions.map((suggestion) => {
// https://github.com/microsoft/language-server-protocol/issues/898#issuecomment-593968008
return suggestions.map((suggestion, i) => {
// The index has already sorted the syggestions by match score.
// We're moving suggestions that are from undefined steps to the bottom
const sortText = (suggestion.matched ? i + 1000 : i + 2000).toString()
const item: CompletionItem = {
label: suggestion.label,
insertTextFormat: InsertTextFormat.Snippet,
kind: CompletionItemKind.Text,
labelDetails: {
...(suggestion.matched ? {} : { detail: ' (undefined step)' }),
},
// VSCode will only display suggestions that literally match the label.
// We're overriding this behaviour by setting filterText to what the user has typed,
// so that the suggestions are always displayed
filterText: text,
sortText,
textEdit: {
newText: lspCompletionSnippet(suggestion.segments),
range: {
start: {
line,
line: position.line,
character: startCharacter,
},
end: {
line,
line: position.line,
character: endCharacter,
},
},
},
}
const { label } = item
console.log({ label, sortText })
console.log()
return item
})
}
13 changes: 12 additions & 1 deletion test/index/Index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,15 @@ function verifyIndexContract(name: string, buildIndex: BuildIndex) {
segments: ['I am a teapot'],
matched: true,
}

const s3: Suggestion = {
label: '{word} can do it',
segments: [['You', 'They'], 'can do it'],
matched: true,
}
let index: Index
beforeEach(() => {
index = buildIndex([s1, s2])
index = buildIndex([s1, s2, s3])
})

it('matches two words in the beginning of an expression', () => {
Expand All @@ -39,6 +45,11 @@ function verifyIndexContract(name: string, buildIndex: BuildIndex) {
assert.deepStrictEqual(suggestions, [s1])
})

it('matches another word in a choice', () => {
const suggestions = index('They')
assert.deepStrictEqual(suggestions, [s3])
})

it('matches nothing', () => {
const suggestions = index('nope')
assert.deepStrictEqual(suggestions, [])
Expand Down
8 changes: 6 additions & 2 deletions test/service/getGherkinCompletionItems.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,15 @@ describe('getGherkinCompletionItems', () => {
Scenario: World
Given cukes
`
const completions = getGherkinCompletionItems(gherkinSource, 2, index)
const completions = getGherkinCompletionItems(gherkinSource, { line: 2, character: 15 }, index)
const expectedCompletions: CompletionItem[] = [
{
label: 'I have {int} cukes in my belly',
insertTextFormat: InsertTextFormat.Snippet,
kind: CompletionItemKind.Text,
labelDetails: {},
filterText: 'cukes',
sortText: '1000',
textEdit: {
newText: 'I have ${1|42,98|} cukes in my belly',
range: {
Expand Down Expand Up @@ -65,7 +67,7 @@ describe('getGherkinCompletionItems', () => {
Scenario: World
Given teapot
`
const completions = getGherkinCompletionItems(gherkinSource, 2, index)
const completions = getGherkinCompletionItems(gherkinSource, { line: 2, character: 16 }, index)
const expectedCompletions: CompletionItem[] = [
{
label: 'I am a teapot',
Expand All @@ -74,6 +76,8 @@ describe('getGherkinCompletionItems', () => {
labelDetails: {
detail: ' (undefined step)',
},
filterText: 'teapot',
sortText: '2000',
textEdit: {
newText: 'I am a teapot',
range: {
Expand Down

0 comments on commit b34ea18

Please sign in to comment.