Skip to content

Commit

Permalink
Fix Sequential Multiline Comments
Browse files Browse the repository at this point in the history
  • Loading branch information
cwarden committed Jan 8, 2025
1 parent 0f3dd8a commit 443971c
Show file tree
Hide file tree
Showing 6 changed files with 93 additions and 21 deletions.
27 changes: 25 additions & 2 deletions formatter/comments_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

"github.com/antlr4-go/antlr/v4"
"github.com/octoberswimmer/apexfmt/parser"
"github.com/sergi/go-diff/diffmatchpatch"

log "github.com/sirupsen/logrus"
)
Expand Down Expand Up @@ -108,7 +109,26 @@ go();`,
}
}`,
},
{
`
/**
* comment 1
*/
/*
* comment 2
*/
go();`,
`
/**
* comment 1
*/
/*
* comment 2
*/
go();`,
},
}
dmp := diffmatchpatch.New()
for i, tt := range tests {
t.Run(fmt.Sprintf("%d", i), func(t *testing.T) {
input := antlr.NewInputStream(tt.input)
Expand All @@ -126,7 +146,8 @@ go();`,
}
out = removeExtraCommentIndentation(out)
if out != tt.output {
t.Errorf("unexpected format. expected:\n%q\ngot:\n%q\n", tt.output, out)
diffs := dmp.DiffMain(tt.output, out, false)
t.Errorf("unexpected format. expected:\n%q\ngot:\n%q\ndiff:\n%s\n", tt.output, out, dmp.DiffPrettyText(diffs))
}
})
}
Expand Down Expand Up @@ -233,6 +254,7 @@ go();}}`,
}`,
},
}
dmp := diffmatchpatch.New()
for i, tt := range tests {
t.Run(fmt.Sprintf("%d", i), func(t *testing.T) {
input := antlr.NewInputStream(tt.input)
Expand All @@ -250,7 +272,8 @@ go();}}`,
}
out = removeExtraCommentIndentation(out)
if out != tt.output {
t.Errorf("unexpected format. expected:\n%q\ngot:\n%q\n", tt.output, out)
diffs := dmp.DiffMain(tt.output, out, false)
t.Errorf("unexpected format. expected:\n%q\ngot:\n%q\ndiff:\n%s\n", tt.output, out, dmp.DiffPrettyText(diffs))
}
})
}
Expand Down
29 changes: 28 additions & 1 deletion formatter/format_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

"github.com/antlr4-go/antlr/v4"
"github.com/octoberswimmer/apexfmt/parser"
"github.com/sergi/go-diff/diffmatchpatch"

log "github.com/sirupsen/logrus"
)
Expand Down Expand Up @@ -594,7 +595,32 @@ public class A {
@IsTest
public class A {}`,
},
{
`class X {
/*
* Property getters
**/
/**
* @return the sid
**/
public String getSid() {
return this.getProperty(SID_PROPERTY);
}
}`,
`class X {
/*
* Property getters
**/
/**
* @return the sid
**/
public String getSid() {
return this.getProperty(SID_PROPERTY);
}
}`,
},
}
dmp := diffmatchpatch.New()
for i, tt := range tests {
t.Run(fmt.Sprintf("%d", i), func(t *testing.T) {

Expand All @@ -613,7 +639,8 @@ public class A {}`,
}
out = removeExtraCommentIndentation(out)
if out != tt.output {
t.Errorf("unexpected format. expected:\n%q\ngot:\n%q\n", tt.output, out)
diffs := dmp.DiffMain(tt.output, out, false)
t.Errorf("unexpected format. expected:\n%q\ngot:\n%q\ndiff:\n%s\n", tt.output, out, dmp.DiffPrettyText(diffs))
}
})
}
Expand Down
39 changes: 25 additions & 14 deletions formatter/indent_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@ import (
"fmt"
"testing"

"github.com/sergi/go-diff/diffmatchpatch"
log "github.com/sirupsen/logrus"
)

func TestIndent(t *testing.T) {
if testing.Verbose() {
log.SetLevel(log.DebugLevel)
log.SetLevel(log.TraceLevel)
log.SetFormatter(&log.TextFormatter{
DisableQuote: true,
})
Expand Down Expand Up @@ -41,18 +42,24 @@ func TestIndent(t *testing.T) {
},
{
"public class B {\n\t\ufffa\n\t/**\n\t\t\t */\n\ufffb\tpublic X(Y client) {}\n}",
"\tpublic class B {\n\t\t\ufffa\n\t\t/**\n\t\t\t\t */\ufffb\n\t\tpublic X(Y client) {}\n\t}",
"\tpublic class B {\n\t\t\ufffa\n\t\t/**\n\t\t\t\t */\n\ufffb\n\t\tpublic X(Y client) {}\n\t}",
},
{
"\ufffa\n// First Comment\n\n\ufffb\ufffa// Second Comment\n\ufffbgo();",
"\t\ufffa\n\t// First Comment\n\ufffb\n\t\ufffa// Second Comment\n\ufffb\n\tgo();",
},
{
"\ufffa\n/*\n\t * Property getters\n\t **/\n\ufffb",
"\t\ufffa\n\t/*\n\t\t * Property getters\n\t\t **/\n\ufffb",
},
}
dmp := diffmatchpatch.New()
for i, tt := range tests {
t.Run(fmt.Sprintf("%d", i), func(t *testing.T) {
out := indent(tt.input)
if out != tt.output {
t.Errorf("unexpected indent format. expected:\n%q\ngot:\n%q\n", tt.output, out)
diffs := dmp.DiffMain(tt.output, out, false)
t.Errorf("unexpected format. expected:\n%q\ngot:\n%q\ndiff:\n%s\n", tt.output, out, dmp.DiffPrettyText(diffs))
}
})
}
Expand Down Expand Up @@ -97,7 +104,7 @@ func TestRemoveIndentation(t *testing.T) {

func TestSplitLeadingFFFAOrFFFBOrNewline(t *testing.T) {
if testing.Verbose() {
log.SetLevel(log.DebugLevel)
log.SetLevel(log.TraceLevel)
log.SetFormatter(&log.TextFormatter{
DisableQuote: true,
})
Expand Down Expand Up @@ -132,6 +139,7 @@ func TestSplitLeadingFFFAOrFFFBOrNewline(t *testing.T) {
"\t\ufffa",
"\t/**",
"\t\t */",
"",
"\ufffb",
"}",
},
Expand Down Expand Up @@ -281,21 +289,12 @@ func TestSplitLeadingFFFAOrFFFBOrNewline(t *testing.T) {
input: "public class B {\n\ufffb\tpublic X(Y client) {}\n}",
expected: []string{
"public class B {",
"",
"\ufffb",
"\tpublic X(Y client) {}",
"}",
},
},
{
name: "Multiple \ufffb delimiters on separate lines",
input: "public class B {\n\ufffb\n\ufffb\n}",
expected: []string{
"public class B {",
"\ufffb",
"\ufffb",
"}",
},
},
{
name: "Include content after \\ufffa",
input: "\ufffa// Second Comment\n\ufffbgo();",
Expand All @@ -304,6 +303,18 @@ func TestSplitLeadingFFFAOrFFFBOrNewline(t *testing.T) {
"go();",
},
},
{
name: "Preserve newlines in comments",
input: "\ufffa\n/*\n\t * Property getters\n\t **/\n\ufffb",
expected: []string{
"\ufffa",
"/*",
"\t * Property getters",
"\t **/",
"",
"\ufffb",
},
},
}

for _, tc := range testCases {
Expand Down
5 changes: 5 additions & 0 deletions formatter/visitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,11 @@ func SplitLeadingFFFAOrFFFBOrNewline(data []byte, atEOF bool) (advance int, toke
// 2c. No Delimiters => Return Entire Line
// ----------------------------------------------------------------
log.Trace(fmt.Sprintf("NO DELIMITER: %q", string(line)))
if len(line) > 0 && bytes.Index(data, fffb) == newlineIdx+1 {
// \uFFFB follows newline. We want to keep the newline by returning an
// extra empty line so we don't advance over the newline.
return newlineIdx, line, nil
}
return newlineIdx + 1, line, nil
}

Expand Down
5 changes: 3 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,19 @@ go 1.21.4

require (
github.com/antlr4-go/antlr/v4 v4.13.0
github.com/sergi/go-diff v1.3.1
github.com/sirupsen/logrus v1.9.3
github.com/spf13/cobra v1.8.0
)

replace github.com/sergi/go-diff => github.com/cwarden/go-diff v0.0.0-20250108131129-65165c5bf578

require (
github.com/cpuguy83/go-md2man/v2 v2.0.3 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/kr/pretty v0.1.0 // indirect
github.com/russross/blackfriday/v2 v2.1.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
golang.org/x/exp v0.0.0-20230515195305-f3d0a9c9a5cc // indirect
golang.org/x/sys v0.1.0 // indirect
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
9 changes: 7 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ github.com/antlr4-go/antlr/v4 v4.13.0 h1:lxCg3LAv+EUK6t1i0y1V6/SLeUi0eKEKdhQAlS8
github.com/antlr4-go/antlr/v4 v4.13.0/go.mod h1:pfChB/xh/Unjila75QW7+VU4TSnWnnk9UTnmpPaOR2g=
github.com/cpuguy83/go-md2man/v2 v2.0.3 h1:qMCsGGgs+MAzDFyp9LpAe1Lqy/fY/qCovCm0qnXZOBM=
github.com/cpuguy83/go-md2man/v2 v2.0.3/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/cwarden/go-diff v0.0.0-20250108131129-65165c5bf578 h1:ZkWyM46IipYbAPQ1M32DnmUCq4V6avn0d/zt/z+REcI=
github.com/cwarden/go-diff v0.0.0-20250108131129-65165c5bf578/go.mod h1:A0bzQcvG0E7Rwjx0REVgAGH58e96+X0MeOfepqsbeW4=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
Expand All @@ -23,6 +25,7 @@ github.com/spf13/cobra v1.8.0/go.mod h1:WXLWApfZ71AjXPya3WOlMsY9yMs7YeiHhFVlvLyh
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
golang.org/x/exp v0.0.0-20230515195305-f3d0a9c9a5cc h1:mCRnTeVUjcrhlRmO0VK8a6k6Rrf6TF9htwo2pJVSjIU=
Expand All @@ -31,8 +34,10 @@ golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBc
golang.org/x/sys v0.1.0 h1:kunALQeHf1/185U1i0GOB/fy1IPRDDpuoOOqRReG57U=
golang.org/x/sys v0.1.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 h1:YR8cESwS4TdDjEe65xsg0ogRM/Nc3DYOhEAlW+xobZo=
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

0 comments on commit 443971c

Please sign in to comment.