Skip to content

Commit

Permalink
Merge pull request #28 from antham/fix-last-comment-indent
Browse files Browse the repository at this point in the history
Fix last comment indent
  • Loading branch information
antham committed Jan 18, 2020
2 parents 75fc309 + bd9595a commit ca54d79
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 12 deletions.
10 changes: 10 additions & 0 deletions ghokin/fixtures/comment-after-background.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
Feature: A Feature
Description

####
Background:
Given a thing
"""
"""
Given another thing
####
13 changes: 13 additions & 0 deletions ghokin/fixtures/comment-after-scenario.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
Feature: A Feature
Description

####
Background:

####
Scenario: A scenario to test
Given a thing
"""
"""
Given another thing
####
13 changes: 9 additions & 4 deletions ghokin/transformer.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,10 @@ func transform(section *section, indentConf indent, aliases aliases) (bytes.Buff
switch sec.kind {
case gherkin.TokenType_Comment, gherkin.TokenType_Language:
cmd = extractCommand(sec.values, aliases)
padding = getTagOrCommentPadding(paddings, sec)
padding = getTagOrCommentPadding(paddings, indentConf, sec)
lines = trimLinesSpace(lines)
case gherkin.TokenType_TagLine:
padding = getTagOrCommentPadding(paddings, sec)
padding = getTagOrCommentPadding(paddings, indentConf, sec)
case gherkin.TokenType_DocStringSeparator, gherkin.TokenType_RuleLine:
lines = extractKeyword(sec.values)
case gherkin.TokenType_Other:
Expand Down Expand Up @@ -118,9 +118,9 @@ func buildBuffer(document []string) (bytes.Buffer, error) {
return buf, nil
}

func getTagOrCommentPadding(paddings map[gherkin.TokenType]int, sec *section) int {
func getTagOrCommentPadding(paddings map[gherkin.TokenType]int, indentConf indent, sec *section) int {
var kind gherkin.TokenType
excluded := []gherkin.TokenType{gherkin.TokenType_Empty, gherkin.TokenType_TagLine, gherkin.TokenType_Comment}
excluded := []gherkin.TokenType{gherkin.TokenType_TagLine, gherkin.TokenType_Comment}

if sec.next(excluded) != nil {
kind = sec.next(excluded).kind
Expand All @@ -130,6 +130,11 @@ func getTagOrCommentPadding(paddings map[gherkin.TokenType]int, sec *section) in
kind = sec.previous(excluded).kind
}

// indent the last comment line at the same level than scenario and background
if sec.next([]gherkin.TokenType{gherkin.TokenType_Empty}) == nil {
return indentConf.backgroundAndScenario
}

return paddings[kind]
}

Expand Down
8 changes: 8 additions & 0 deletions ghokin/transformer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -424,6 +424,14 @@ func TestTransform(t *testing.T) {
"fixtures/docstring-empty.input.feature",
"fixtures/docstring-empty.expected.feature",
},
{
"fixtures/comment-after-scenario.feature",
"fixtures/comment-after-scenario.feature",
},
{
"fixtures/comment-after-background.feature",
"fixtures/comment-after-background.feature",
},
}

for _, scenario := range scenarios {
Expand Down
11 changes: 3 additions & 8 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,18 +1,13 @@
module github.com/antham/ghokin

require (
github.com/BurntSushi/toml v0.3.1 // indirect
github.com/cucumber/cucumber-messages-go v0.0.0-20180726152503-9cc16e7821a4 // indirect
github.com/cucumber/gherkin-go v0.0.0-20180702115136-c016180aa7b9
github.com/fatih/color v1.9.0
github.com/gogo/protobuf v1.1.1 // indirect
github.com/golang/protobuf v1.1.0 // indirect
github.com/inconshreveable/mousetrap v1.0.0 // indirect
github.com/mattn/go-colorable v0.0.9 // indirect
github.com/mattn/go-isatty v0.0.4 // indirect
github.com/mitchellh/go-homedir v0.0.0-20180523094522-3864e76763d9
github.com/mitchellh/go-homedir v1.1.0
github.com/spf13/cobra v0.0.5
github.com/spf13/viper v1.6.2
github.com/stretchr/testify v1.4.0
golang.org/x/sync v0.0.0-20181108010431-42b317875d0f // indirect
)

go 1.13
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ github.com/golang/mock v1.1.1/go.mod h1:oTYuIxOrZwtPieC+H1uAHpcLFnEyAGVDL/k47Jfb
github.com/golang/protobuf v1.1.0 h1:0iH4Ffd/meGoXqF2lSAhZHt8X+cPgkfn/cb6Cce5Vpc=
github.com/golang/protobuf v1.1.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/golang/protobuf v1.3.1 h1:YF8+flBXS5eO826T4nzqPrxfhQThhXl0YzfuUPu4SBg=
github.com/golang/protobuf v1.3.1/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/google/btree v1.0.0/go.mod h1:lNA+9X1NB3Zf8V7Ke586lFgjr2dZNuvo3lPJSGZ5JPQ=
github.com/google/go-cmp v0.2.0/go.mod h1:oXzfMopK8JAjlY9xF4vHSVASa0yLyX7SntLO5aqRK0M=
Expand Down Expand Up @@ -86,6 +87,7 @@ github.com/mattn/go-isatty v0.0.11/go.mod h1:PhnuNfih5lzO57/f3n+odYbM4JtupLOxQOA
github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5Ld7szi9bcBfOoFv/3dc6xSMkL2PC0=
github.com/mitchellh/go-homedir v0.0.0-20180523094522-3864e76763d9 h1:Y94YB7jrsihrbGSqRNMwRWJ2/dCxr0hdC2oPRohkx0A=
github.com/mitchellh/go-homedir v0.0.0-20180523094522-3864e76763d9/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0=
github.com/mitchellh/go-homedir v1.1.0 h1:lukF9ziXFxDFPkA1vsr5zpc1XuPDn/wFntq5mG+4E0Y=
github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0=
github.com/mitchellh/mapstructure v0.0.0-20180715050151-f15292f7a699 h1:KXZJFdun9knAVAR8tg/aHJEr5DgtcbqyvzacK+CDCaI=
github.com/mitchellh/mapstructure v0.0.0-20180715050151-f15292f7a699/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y=
Expand Down

0 comments on commit ca54d79

Please sign in to comment.