Skip to content
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
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@

## Improvements:

* Update autoformatting to replace sequential equivalent keywords with the `And` keyword

## Bug fixes:

*Contributors of this release (in alphabetical order):*
*Contributors of this release (in alphabetical order):* @jdb0123

# v2024.8.234 - 2025-01-22

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,23 @@ public class GherkinDocumentFormatter
public void FormatGherkinDocument(DeveroomGherkinDocument gherkinDocument, DocumentLinesEditBuffer lines,
GherkinFormatSettings formatSettings)
{
if (gherkinDocument.Feature != null)
{
SetTagsAndLine(lines, gherkinDocument.Feature, string.Empty);
SetLinesForChildren(lines, gherkinDocument.Feature.Children, formatSettings,
formatSettings.FeatureChildrenIndentLevel);
}
if (gherkinDocument.Feature == null)
return;

SetTagsAndLine(lines, gherkinDocument.Feature, string.Empty);
SetLinesForChildren(lines, gherkinDocument.Feature.Children, formatSettings, formatSettings.FeatureChildrenIndentLevel, gherkinDocument.GherkinDialect);
}

private void SetLinesForChildren(DocumentLinesEditBuffer lines, IEnumerable<IHasLocation> hasLocation,
GherkinFormatSettings formatSettings, int indentLevel)
GherkinFormatSettings formatSettings, int indentLevel, GherkinDialect gherkinDialect)
{
foreach (var featureChild in hasLocation)
{
SetTagsAndLine(lines, featureChild, GetIndent(formatSettings, indentLevel));

if (featureChild is Rule rule)
SetLinesForChildren(lines, rule.Children, formatSettings,
indentLevel + formatSettings.RuleChildrenIndentLevelWithinRule);
indentLevel + formatSettings.RuleChildrenIndentLevelWithinRule, gherkinDialect);

if (featureChild is ScenarioOutline scenarioOutline)
foreach (var example in scenarioOutline.Examples)
Expand All @@ -37,24 +36,55 @@ private void SetLinesForChildren(DocumentLinesEditBuffer lines, IEnumerable<IHas
examplesBlockIndentLevel + formatSettings.ExamplesTableIndentLevelWithinExamplesBlock);
}

if (featureChild is IHasSteps hasSteps)
foreach (var step in hasSteps.Steps)
{
var stepIndentLevel = indentLevel + formatSettings.StepIndentLevelWithinStepContainer;
if (step is DeveroomGherkinStep deveroomGherkinStep &&
(deveroomGherkinStep.StepKeyword == StepKeyword.And ||
deveroomGherkinStep.StepKeyword == StepKeyword.But))
stepIndentLevel += formatSettings.AndStepIndentLevelWithinSteps;
SetLine(lines, step, $"{GetIndent(formatSettings, stepIndentLevel)}{step.Keyword}{step.Text}");
if (step.Argument is DataTable dataTable)
FormatTable(lines, dataTable, formatSettings,
stepIndentLevel + formatSettings.DataTableIndentLevelWithinStep);

if (step.Argument is DocString docString)
FormatDocString(lines, docString, formatSettings,
stepIndentLevel + formatSettings.DocStringIndentLevelWithinStep);
}
if (featureChild is IHasSteps hasSteps)
FormatSteps(lines, formatSettings, indentLevel, hasSteps, gherkinDialect);
}
}

private void FormatSteps(DocumentLinesEditBuffer lines, GherkinFormatSettings formatSettings, int indentLevel,
IHasSteps hasSteps, GherkinDialect gherkinDialect)
{
var previousKeyword = "";

foreach (var step in hasSteps.Steps)
{
var stepIndentLevel = indentLevel + formatSettings.StepIndentLevelWithinStepContainer;

var newKeyword = step.Keyword;

if (step is DeveroomGherkinStep { StepKeyword: StepKeyword.And or StepKeyword.But })
{
stepIndentLevel += formatSettings.AndStepIndentLevelWithinSteps;
}
else
{
if (step.Keyword == previousKeyword)
{
var andKeyword = GetAndKeyword(gherkinDialect);
newKeyword = $"{andKeyword}";
}
else
previousKeyword = step.Keyword;
}


SetLine(lines, step, $"{GetIndent(formatSettings, stepIndentLevel)}{newKeyword}{step.Text}");

switch (step.Argument)
{
case DataTable dataTable:
FormatTable(lines, dataTable, formatSettings, stepIndentLevel + formatSettings.DataTableIndentLevelWithinStep);
break;
case DocString docString:
FormatDocString(lines, docString, formatSettings, stepIndentLevel + formatSettings.DocStringIndentLevelWithinStep);
break;
}
}
}

private static string GetAndKeyword(GherkinDialect gherkinDialect)
{
return gherkinDialect.AndStepKeywords.First(keyword => keyword != GherkinDialect.AsteriskKeyword);
}

private void SetTagsAndLine(DocumentLinesEditBuffer lines, IHasLocation hasLocation, string indent)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -301,4 +301,35 @@ Scenario: Formatting of Descriptions and Comments are not changed
Todo: handle negative numbers

Given I have entered 50 into the calculator
"""

Rule: Auto format should replace repeated keywords with "And"

Scenario: Repeating keywords are replaced with "And"

Given there is a Reqnroll project scope
And the following feature file in the editor
"""
Feature: Addition

Scenario: Add two numbers
Given I have entered 50 into the calculator
Given I have entered 70 into the calculator
When I add them
When I check the result
Then there should be no error
Then the result should be 120
"""
When I invoke the "Auto Format Document" command without waiting for the tag changes
Then the editor should be updated to
"""
Feature: Addition

Scenario: Add two numbers
Given I have entered 50 into the calculator
And I have entered 70 into the calculator
When I add them
And I check the result
Then there should be no error
And the result should be 120
"""
Loading