@@ -8,17 +8,18 @@ public class GherkinDocumentFormatter
88 public void FormatGherkinDocument ( DeveroomGherkinDocument gherkinDocument , DocumentLinesEditBuffer lines ,
99 GherkinFormatSettings formatSettings )
1010 {
11- if ( gherkinDocument . Feature != null )
12- {
13- SetTagsAndLine ( lines , gherkinDocument . Feature , string . Empty ) ;
14- SetLinesForChildren ( lines , gherkinDocument . Feature . Children , formatSettings ,
15- formatSettings . FeatureChildrenIndentLevel ) ;
16- }
11+ if ( gherkinDocument . Feature == null )
12+ return ;
13+
14+ SetTagsAndLine ( lines , gherkinDocument . Feature , string . Empty ) ;
15+ SetLinesForChildren ( lines , gherkinDocument . Feature . Children , formatSettings , formatSettings . FeatureChildrenIndentLevel ) ;
1716 }
1817
1918 private void SetLinesForChildren ( DocumentLinesEditBuffer lines , IEnumerable < IHasLocation > hasLocation ,
2019 GherkinFormatSettings formatSettings , int indentLevel )
2120 {
21+ var dialectProvider = ReqnrollGherkinDialectProvider . Get ( formatSettings . Language ) ;
22+
2223 foreach ( var featureChild in hasLocation )
2324 {
2425 SetTagsAndLine ( lines , featureChild , GetIndent ( formatSettings , indentLevel ) ) ;
@@ -37,24 +38,55 @@ private void SetLinesForChildren(DocumentLinesEditBuffer lines, IEnumerable<IHas
3738 examplesBlockIndentLevel + formatSettings . ExamplesTableIndentLevelWithinExamplesBlock ) ;
3839 }
3940
40- if ( featureChild is IHasSteps hasSteps )
41- foreach ( var step in hasSteps . Steps )
42- {
43- var stepIndentLevel = indentLevel + formatSettings . StepIndentLevelWithinStepContainer ;
44- if ( step is DeveroomGherkinStep deveroomGherkinStep &&
45- ( deveroomGherkinStep . StepKeyword == StepKeyword . And ||
46- deveroomGherkinStep . StepKeyword == StepKeyword . But ) )
47- stepIndentLevel += formatSettings . AndStepIndentLevelWithinSteps ;
48- SetLine ( lines , step , $ "{ GetIndent ( formatSettings , stepIndentLevel ) } { step . Keyword } { step . Text } ") ;
49- if ( step . Argument is DataTable dataTable )
50- FormatTable ( lines , dataTable , formatSettings ,
51- stepIndentLevel + formatSettings . DataTableIndentLevelWithinStep ) ;
52-
53- if ( step . Argument is DocString docString )
54- FormatDocString ( lines , docString , formatSettings ,
55- stepIndentLevel + formatSettings . DocStringIndentLevelWithinStep ) ;
56- }
41+ if ( featureChild is IHasSteps hasSteps )
42+ FormatSteps ( lines , formatSettings , indentLevel , hasSteps , dialectProvider ) ;
43+ }
44+ }
45+
46+ private void FormatSteps ( DocumentLinesEditBuffer lines , GherkinFormatSettings formatSettings , int indentLevel ,
47+ IHasSteps hasSteps , GherkinDialectProvider dialectProvider )
48+ {
49+ var previousKeyword = "" ;
50+
51+ foreach ( var step in hasSteps . Steps )
52+ {
53+ var stepIndentLevel = indentLevel + formatSettings . StepIndentLevelWithinStepContainer ;
54+
55+ var newKeyword = step . Keyword ;
56+
57+ if ( step is DeveroomGherkinStep { StepKeyword : StepKeyword . And or StepKeyword . But } )
58+ {
59+ stepIndentLevel += formatSettings . AndStepIndentLevelWithinSteps ;
60+ }
61+ else
62+ {
63+ if ( step . Keyword == previousKeyword )
64+ {
65+ var andKeyword = GetAndKeyword ( dialectProvider ) ;
66+ newKeyword = $ "{ andKeyword } ";
67+ }
68+ else
69+ previousKeyword = step . Keyword ;
70+ }
71+
72+
73+ SetLine ( lines , step , $ "{ GetIndent ( formatSettings , stepIndentLevel ) } { newKeyword } { step . Text } ") ;
74+
75+ switch ( step . Argument )
76+ {
77+ case DataTable dataTable :
78+ FormatTable ( lines , dataTable , formatSettings , stepIndentLevel + formatSettings . DataTableIndentLevelWithinStep ) ;
79+ break ;
80+ case DocString docString :
81+ FormatDocString ( lines , docString , formatSettings , stepIndentLevel + formatSettings . DocStringIndentLevelWithinStep ) ;
82+ break ;
5783 }
84+ }
85+ }
86+
87+ private static string GetAndKeyword ( GherkinDialectProvider dialectProvider )
88+ {
89+ return dialectProvider . DefaultDialect . AndStepKeywords . First ( keyword => keyword != GherkinDialect . AsteriskKeyword ) ;
5890 }
5991
6092 private void SetTagsAndLine ( DocumentLinesEditBuffer lines , IHasLocation hasLocation , string indent )
0 commit comments