Skip to content

Commit

Permalink
format: don't group interface members of different kinds
Browse files Browse the repository at this point in the history
For instance, don't group exported and unexported methods,
nor methods with non-methods.

Fixes #168.
  • Loading branch information
mvdan committed Dec 5, 2021
1 parent 7ca7e6c commit 93927ca
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 1 deletion.
6 changes: 5 additions & 1 deletion format/format.go
Original file line number Diff line number Diff line change
Expand Up @@ -483,7 +483,11 @@ func (f *fumpter) applyPre(c *astutil.Cursor) {
f.removeLines(f.Line(node.Interface)+1, f.Line(removeToPos))

case len(f.commentsBetween(prev.End(), method.Pos())) > 0:
// continue
// comments in between; leave newlines alone
case len(prev.Names) != len(method.Names):
// don't group type unions with methods
case len(prev.Names) == 1 && token.IsExported(prev.Names[0].Name) != token.IsExported(method.Names[0].Name):
// don't group exported and unexported methods together
default:
f.removeLinesBetween(prev.End(), method.Pos())
}
Expand Down
10 changes: 10 additions & 0 deletions testdata/scripts/interface.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ type i1 interface {

c(x int) int

D()

E()

f()
}

type i2 interface {
Expand Down Expand Up @@ -103,6 +108,11 @@ type i1 interface {
a(x int) int
b(x int) int
c(x int) int

D()
E()

f()
}

type i2 interface {
Expand Down
17 changes: 17 additions & 0 deletions testdata/scripts/typeparams.txt
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,20 @@ type PredeclaredSignedInteger interface {
}

type StringableSignedInteger interface {

~int | ~int8 | ~int16 | ~int32 | ~int64

String() string

}

type CombineEmbeds interface {
fmt.Stringer
comparable | io.Reader

Foo()
}

func Caller() {
Foo[int,int](1,2)
}
Expand All @@ -45,9 +54,17 @@ type PredeclaredSignedInteger interface {

type StringableSignedInteger interface {
~int | ~int8 | ~int16 | ~int32 | ~int64

String() string
}

type CombineEmbeds interface {
fmt.Stringer
comparable | io.Reader

Foo()
}

func Caller() {
Foo[int, int](1, 2)
}

0 comments on commit 93927ca

Please sign in to comment.