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

Adding support for duplicates steps #419

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
8 changes: 7 additions & 1 deletion gclient/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -153,10 +153,16 @@
"default": false
},
"cucumberautocomplete.stepRegExSymbol": {
"descrioption": "Provide step regex symbol. Ex. it would be \"'\" for When('I do something') definition",
"description": "Provide step regex symbol. Ex. it would be \"'\" for When('I do something') definition",
"type": "string",
"required": false,
"default": false
},
"cucumberautocomplete.allowDuplicates": {
"description": "Allow duplicate step definitions for multiple context in same feature.",
"type": "boolean",
"required": false,
"default": false
}
}
}
Expand Down
45 changes: 33 additions & 12 deletions gserver/src/steps.handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,9 @@ export default class StepsHandler {
text.split(/\r?\n/g).forEach(line => {
const match = this.getGherkinMatch(line, text);
if (match) {
const step = this.getStepByText(match[4]);
if (step) {
this.incrementElementCount(step.id);
const steps = this.getStepByText(match[4]);
if (steps) {
this.incrementElementCount(Array.isArray(steps) ? steps[0].id : steps.id);
}
}
});
Expand Down Expand Up @@ -480,16 +480,22 @@ export default class StepsHandler {
this.getFileSteps(f).reduce((steps, step) => {
if (!this.elementsHash[step.id]) {
steps.push(step);
this.elementsHash[step.id] = true;
this.elementsHash[step.id] = !this.settings.cucumberautocomplete.allowDuplicates;
}
return steps;
}, [])
), []);
}

getStepByText(text: string, gherkin?: GherkinType): Step {
return this.elements
.find(s => (gherkin !== undefined ? s.gherkin === gherkin : true) && s.reg.test(text));
getStepByText(text: string, gherkin?: GherkinType): Step[] | Step | undefined {
const filteredElements = this.elements
.filter(s => (gherkin !== undefined ? s.gherkin === gherkin : true) && s.reg.test(text));

if (this.settings.cucumberautocomplete.allowDuplicates) {
return filteredElements.length > 0 ? filteredElements : undefined;
}

return filteredElements.length > 0 ? filteredElements[0] : undefined;
}

validate(line: string, lineNum: number, text: string): Diagnostic | null {
Expand All @@ -501,11 +507,11 @@ export default class StepsHandler {
}
const beforeGherkin = match[1];
const gherkinPart = match[2];
const step = this.getStepByText(match[4], this.settings.cucumberautocomplete.strictGherkinValidation
const steps = this.getStepByText(match[4], this.settings.cucumberautocomplete.strictGherkinValidation
? this.getStrictGherkinType(gherkinPart, lineNum, text)
: undefined
);
if (step) {
if (steps) {
return null;
} else {
return {
Expand All @@ -520,13 +526,28 @@ export default class StepsHandler {
}
}

getDefinition(line: string, text: string): Definition | null {
getDefinition(line: string, text: string): Definition | Location[] | null {
const match = this.getGherkinMatch(line, text);
if (!match) {
return null;
}
const step = this.getStepByText(match[4]);
return step ? step.def : null;

let steps : any = this.getStepByText(match[4]);

if (!steps)
return null;

if (!Array.isArray(steps)) {
return steps.def;
}

let locations: Location[] = [];

for (let step of steps) {
locations.push(Location.create(step.def.uri, step.def.range));
}

return locations.length > 0 ? locations : null;
}

getStrictGherkinType(gherkinPart: string, lineNumber: number, text: string) {
Expand Down
3 changes: 2 additions & 1 deletion gserver/src/typings.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ interface Settings {
formatConfOverride?: FormatConf[],
onTypeFormat?: boolean,
gherkinDefinitionPart?: string,
stepRegExSymbol?: string
stepRegExSymbol?: string,
allowDuplicates?: boolean
}
}