-
-
Notifications
You must be signed in to change notification settings - Fork 30
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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, { | ||
scenario(scenario, symbols) { | ||
inScenarioOutline = (scenario.examples || []).length > 0 | ||
return symbols | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The You should return a new accumulator here. I wouldn't use |
||
}, | ||
step(step, symbols) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
} |
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) | ||
}) | ||
}) |
There was a problem hiding this comment.
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
andbackground
.