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

Fix table formatting #130

Merged
merged 1 commit into from
Mar 25, 2024
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
21 changes: 21 additions & 0 deletions ghokin/fixtures/comment-in-a-midst-of-row.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
Feature: Test Formatter
Scenario: Comment between rows
Given table my_table
| col a | col b |
| 1 | 2 |
# A comment about the row below
| 3 | 4 |
# Another comment about the row below
| 5 | 6 |
| 7 | 8 |
| 9 | 10 |
Given table my_table
| col a | col b |
| 1 | 2 |
# A comment about the row below
| 3 | 4 |
# Another comment about the row below
| 5 | 6 |
Given a second thing
Given another second thing
Then another thing happened
80 changes: 58 additions & 22 deletions ghokin/transformer.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,22 +56,40 @@ func transform(section *section, indent int, aliases aliases) ([]byte, error) {
gherkin.TokenTypeRuleLine: extractKeywordAndTextSeparatedWithAColon,
gherkin.TokenTypeOther: extractTokensText,
gherkin.TokenTypeStepLine: extractTokensKeywordAndText,
gherkin.TokenTypeTableRow: extractTableRows,
gherkin.TokenTypeTableRow: extractTableRowsAndComments,
gherkin.TokenTypeEmpty: extractTokensItemsText,
gherkin.TokenTypeLanguage: extractLanguage,
}

var cmd *exec.Cmd
document := []string{}
optionalRulePadding := 0
accumulator := []*gherkin.Token{}

for sec := section; sec != nil; sec = sec.nex {
if sec.kind == 0 {
values := sec.values
if len(accumulator) > 0 &&
sec.kind == gherkin.TokenTypeTableRow &&
(sec.nex != nil && sec.nex.kind != gherkin.TokenTypeComment) || sec.nex == nil {
values = append(accumulator, sec.values...)
accumulator = []*gherkin.Token{}
}
if sec.kind == gherkin.TokenTypeTableRow &&
sec.nex != nil &&
sec.nex.kind == gherkin.TokenTypeComment &&
sec.nex.nex != nil &&
sec.nex.nex.kind == gherkin.TokenTypeTableRow ||
len(accumulator) > 0 && sec.kind == gherkin.TokenTypeComment ||
len(accumulator) > 0 && sec.kind == gherkin.TokenTypeTableRow {
accumulator = append(accumulator, sec.values...)
continue
}

if sec.kind == 0 {
continue
}
padding := paddings[sec.kind] + optionalRulePadding
lines := formats[sec.kind](sec.values)
lines := formats[sec.kind](values)
switch sec.kind {
case gherkin.TokenTypeRuleLine:
optionalRulePadding = indent
Expand Down Expand Up @@ -216,33 +234,51 @@ func extractKeyword(tokens []*gherkin.Token) []string {
return content
}

func extractTableRows(tokens []*gherkin.Token) []string {
func extractTableRowsAndComments(tokens []*gherkin.Token) []string {
type tableElement struct {
content []string
kind gherkin.TokenType
}
rows := [][]string{}
for _, tab := range tokens {
row := []string{}

for _, data := range tab.Items {
// A remaining pipe means it was escaped before to not be messed with pipe column delimiter
// so here we introduce the escaping sequence back
text := data.Text
text = strings.ReplaceAll(text, "\\", "\\\\")
text = strings.ReplaceAll(text, "\n", "\\n")
text = strings.ReplaceAll(text, "|", "\\|")
row = append(row, text)
tableElements := []tableElement{}
for _, token := range tokens {
element := tableElement{}
if token.Type == gherkin.TokenTypeComment {
element.kind = token.Type
element.content = []string{token.Text}
} else {
row := []string{}
for _, data := range token.Items {
// A remaining pipe means it was escaped before to not be messed with pipe column delimiter
// so here we introduce the escaping sequence back
text := data.Text
text = strings.ReplaceAll(text, "\\", "\\\\")
text = strings.ReplaceAll(text, "\n", "\\n")
text = strings.ReplaceAll(text, "|", "\\|")
row = append(row, text)
}
element.kind = token.Type
element.content = row
rows = append(rows, row)
}

rows = append(rows, row)
tableElements = append(tableElements, element)
}

var tableRows []string
lengths := calculateLonguestLineLengthPerColumn(rows)
for _, row := range rows {
for _, tableElement := range tableElements {
inputs := []interface{}{}
fmtDirective := ""
for i, str := range row {
inputs = append(inputs, str)
fmtDirective += "| %-" + strconv.Itoa(lengths[i]) + "s "
if tableElement.kind == gherkin.TokenTypeComment {
inputs = append(inputs, trimLinesSpace(tableElement.content)[0])
fmtDirective = "%s"
} else {
for i, str := range tableElement.content {
inputs = append(inputs, str)
fmtDirective += "| %-" + strconv.Itoa(lengths[i]) + "s "
}
fmtDirective += "|"
}
fmtDirective += "|"
tableRows = append(tableRows, fmt.Sprintf(fmtDirective, inputs...))
}
return tableRows
Expand Down
6 changes: 5 additions & 1 deletion ghokin/transformer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ func TestExtractTableRows(t *testing.T) {
}

for _, scenario := range scenarios {
scenario.test(extractTableRows(scenario.tokens))
scenario.test(extractTableRowsAndComments(scenario.tokens))
}
}

Expand Down Expand Up @@ -455,6 +455,10 @@ func TestTransform(t *testing.T) {
"fixtures/double-escaping.feature",
"fixtures/double-escaping.feature",
},
{
"fixtures/comment-in-a-midst-of-row.feature",
"fixtures/comment-in-a-midst-of-row.feature",
},
}

for _, scenario := range scenarios {
Expand Down
Loading