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

Generate a single code action with relative path #50

Merged
merged 1 commit into from
May 24, 2022
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -20,58 +20,59 @@ import { stepDefinitionSnippet } from './snippet/stepDefinitionSnippet.js'
*
* @param diagnostics all the diagnostics
* @param link where the snippet should be added
* @param relativePath the relative path from the workspace root
* @param createFile true if link.targetUri does not exist
* @param mustacheTemplate template to generae the snippet
* @param languageName the name of the language we're generating for
* @param registry parameter types
*/
export function getGenerateSnippetCodeActions(
export function getGenerateSnippetCodeAction(
diagnostics: Diagnostic[],
link: LocationLink,
relativePath: string,
createFile: boolean,
mustacheTemplate: string | undefined,
languageName: LanguageName,
registry: ParameterTypeRegistry
): CodeAction[] {
): CodeAction | null {
const undefinedStepDiagnostic = diagnostics.find((d) => d.code === diagnosticCodeUndefinedStep)
const language = getLanguage(languageName)
const stepKeyword = undefinedStepDiagnostic?.data?.stepKeyword
const stepText = undefinedStepDiagnostic?.data?.stepText
if (undefinedStepDiagnostic && stepText) {
const generator = new CucumberExpressionGenerator(() => registry.parameterTypes)
const generatedExpressions = generator.generateExpressions(stepText)
if (!undefinedStepDiagnostic || !stepText) {
return null
}
const generator = new CucumberExpressionGenerator(() => registry.parameterTypes)
const generatedExpressions = generator.generateExpressions(stepText)

const snippet = stepDefinitionSnippet(
stepKeyword,
generatedExpressions,
mustacheTemplate || language.defaultSnippetTemplate,
language.snippetParameters
)
const snippet = stepDefinitionSnippet(
stepKeyword,
generatedExpressions,
mustacheTemplate || language.defaultSnippetTemplate,
language.snippetParameters
)

const documentChanges: (CreateFile | TextDocumentEdit)[] = []
if (createFile) {
documentChanges.push(
CreateFile.create(link.targetUri, {
ignoreIfExists: true,
overwrite: true,
})
)
}
const documentChanges: (CreateFile | TextDocumentEdit)[] = []
if (createFile) {
documentChanges.push(
TextDocumentEdit.create(VersionedTextDocumentIdentifier.create(link.targetUri, 0), [
TextEdit.replace(link.targetRange, snippet),
])
CreateFile.create(link.targetUri, {
ignoreIfExists: true,
overwrite: true,
})
)
const ca: CodeAction = {
title: 'Generate step definition',
diagnostics: [undefinedStepDiagnostic],
kind: CodeActionKind.QuickFix,
edit: {
documentChanges,
},
isPreferred: true,
}
return [ca]
}
return []
documentChanges.push(
TextDocumentEdit.create(VersionedTextDocumentIdentifier.create(link.targetUri, 0), [
TextEdit.replace(link.targetRange, snippet),
])
)
return {
title: `Define in ${relativePath}`,
diagnostics: [undefinedStepDiagnostic],
kind: CodeActionKind.QuickFix,
edit: {
documentChanges,
},
isPreferred: true,
}
}
2 changes: 1 addition & 1 deletion src/service/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export * from './getGenerateSnippetCodeActions.js'
export * from './getGenerateSnippetCodeAction.js'
export * from './getGherkinCompletionItems.js'
export * from './getGherkinDiagnostics.js'
export * from './getGherkinFormattingEdits.js'
Expand Down
178 changes: 178 additions & 0 deletions test/service/getGenerateSnippetCodeAction.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,178 @@
import { ParameterTypeRegistry } from '@cucumber/cucumber-expressions'
import assert from 'assert'
import { CodeAction, LocationLink, Range } from 'vscode-languageserver-types'

import { getGenerateSnippetCodeAction } from '../../src/service/getGenerateSnippetCodeAction.js'
import { makeUndefinedStepDiagnostic } from '../../src/service/getGherkinDiagnostics.js'

describe('getGenerateSnippetCodeAction', () => {
it('generates code in a new file', () => {
const diagnostic = makeUndefinedStepDiagnostic(10, 4, 'Given ', 'I have 43 cukes')

const targetRange = Range.create(10, 0, 10, 0)
const link: LocationLink = {
targetUri: 'file://home/features/step_definitions/steps.ts',
targetRange,
targetSelectionRange: targetRange,
}
const action = getGenerateSnippetCodeAction(
[diagnostic],
link,
'step_definitions/steps.ts',
true,
undefined,
'typescript',
new ParameterTypeRegistry()
)
const expectedAction: CodeAction = {
title: 'Define in step_definitions/steps.ts',
diagnostics: [
{
severity: 2,
range: {
start: {
line: 10,
character: 4,
},
end: {
line: 10,
character: 19,
},
},
message: 'Undefined step: I have 43 cukes',
source: 'Cucumber',
code: 'cucumber.undefined-step',
codeDescription: {
href: 'https://cucumber.io/docs/cucumber/step-definitions/',
},
data: {
stepKeyword: 'Given ',
stepText: 'I have 43 cukes',
},
},
],
kind: 'quickfix',
edit: {
documentChanges: [
{
kind: 'create',
uri: 'file://home/features/step_definitions/steps.ts',
options: {
ignoreIfExists: true,
overwrite: true,
},
},
{
textDocument: {
uri: 'file://home/features/step_definitions/steps.ts',
version: 0,
},
edits: [
{
range: {
start: {
line: 10,
character: 0,
},
end: {
line: 10,
character: 0,
},
},
newText: `
Given('I have {int} cukes', (int: number) => {
// Write code here that turns the phrase above into concrete actions
})
`,
},
],
},
],
},
isPreferred: true,
}

assert.deepStrictEqual(action, expectedAction)
})

it('generates code in an existing file', () => {
const diagnostic = makeUndefinedStepDiagnostic(10, 4, 'Given ', 'I have 43 cukes')

const targetRange = Range.create(10, 0, 10, 0)
const link: LocationLink = {
targetUri: 'file://home/features/step_definitions/steps.ts',
targetRange,
targetSelectionRange: targetRange,
}

const action = getGenerateSnippetCodeAction(
[diagnostic],
link,
'step_definitions/steps.ts',
false,
undefined,
'typescript',
new ParameterTypeRegistry()
)
const expectedAction: CodeAction = {
title: 'Define in step_definitions/steps.ts',
diagnostics: [
{
severity: 2,
range: {
start: {
line: 10,
character: 4,
},
end: {
line: 10,
character: 19,
},
},
message: 'Undefined step: I have 43 cukes',
source: 'Cucumber',
code: 'cucumber.undefined-step',
codeDescription: {
href: 'https://cucumber.io/docs/cucumber/step-definitions/',
},
data: {
stepKeyword: 'Given ',
stepText: 'I have 43 cukes',
},
},
],
kind: 'quickfix',
edit: {
documentChanges: [
{
textDocument: {
uri: 'file://home/features/step_definitions/steps.ts',
version: 0,
},
edits: [
{
range: {
start: {
line: 10,
character: 0,
},
end: {
line: 10,
character: 0,
},
},
newText: `
Given('I have {int} cukes', (int: number) => {
// Write code here that turns the phrase above into concrete actions
})
`,
},
],
},
],
},
isPreferred: true,
}
assert.deepStrictEqual(action, expectedAction)
})
})
Loading