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

issue #98 - adding initial changes #105

Closed
Closed
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
51 changes: 51 additions & 0 deletions src/service/getGherkinDocumentSymbols.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import { Expression } from '@cucumber/cucumber-expressions'
import { dialects, Errors } from '@cucumber/gherkin'
import { walkGherkinDocument } from '@cucumber/gherkin-utils'
import { DocumentSymbol } from 'vscode-languageserver-types'

import { parseGherkinDocument } from '../gherkin/parseGherkinDocument.js'

export function getGherkinDocumentSymbols(gherkinSource: string): readonly DocumentSymbol[] {
const symbols: DocumentSymbol[] = []
const lines = gherkinSource.split(/\r?\n/)
const { gherkinDocument, error } = parseGherkinDocument(gherkinSource)

if (!gherkinDocument?.feature) {
return symbols
}
let inScenarioOutline = false
const dialect = dialects[gherkinDocument.feature.language]
const noStars = (keyword: string) => keyword !== '* '
const codeKeywords = [...dialect.given, ...dialect.when, ...dialect.then].filter(noStars)
let snippetKeyword = dialect.given.filter(noStars)[0]

return walkGherkinDocument<DocumentSymbol[]>(gherkinDocument, symbols, {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should also have callbacks for feature, rule and background.

scenario(scenario, symbols) {
inScenarioOutline = (scenario.examples || []).length > 0
return symbols
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The walkGherkinDocument function works similarly to Array.reduce - the accumulator (symbols in yor case) is passed into each callback, and the callback should return a new accumulator.

You should return a new accumulator here.

I wouldn't use DocumentSymbol[] as an accumulator. We should build a tree, not a list.

},
step(step, symbols) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we include steps in the outline, the outline is going to look more or less like the original Gherkin document. I don't think this would be particularly helpful. I see the outline as a navigation aid, so it's got to be smaller than the original document.

I think we should exclude steps from the outline.

if (inScenarioOutline) {
return symbols
}
if (codeKeywords.includes(step.keyword)) {
snippetKeyword = step.keyword
}

if (step.location.column !== undefined) {
const line = step.location.line - 1
const character = step.location.column - 1 + step.keyword.length

return symbols.concat(symbols)
}
return symbols
},
})
}

function isUndefined(stepText: string, expressions: readonly Expression[]): boolean {
for (const expression of expressions) {
if (expression.match(stepText)) return false
}
return true
}
42 changes: 42 additions & 0 deletions test/service/getGherkinDocumentSymbols.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import assert from 'assert'
import { DocumentSymbol, SymbolKind } from 'vscode-languageserver-types'

import { getGherkinDocumentSymbols } from '../../src/service/getGherkinDocumentSymbols.js'

describe('getGherkinFormattingEdits', () => {
it('returns text Document Symbols from a Gherkin document', () => {
const gherkinSource = `Feature: Hello
Scenario: World
Given something`
const sourceDocumentSymbols = getGherkinDocumentSymbols(gherkinSource)
const featureSymbol: DocumentSymbol[] = [
{
name: 'Scenario',
detail: 'Feature',
kind: SymbolKind.Field,
range: {
start: {
line: 1,
character: 0,
},
end: {
line: 3,
character: 15,
},
},
selectionRange: {
start: {
line: 1,
character: 0,
},
end: {
line: 3,
character: 15,
},
},
},
]

assert.deepStrictEqual(featureSymbol, sourceDocumentSymbols)
})
})