Skip to content

Commit e4abc20

Browse files
committed
Add linebreak before comment line, same as goimport
Signed-off-by: Xiang Dai <[email protected]>
1 parent e0e26a3 commit e4abc20

File tree

1 file changed

+7
-3
lines changed

1 file changed

+7
-3
lines changed

pkg.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ func newPkg(data [][]byte, localFlag string) *pkg {
5858
// 2. commentFlag after import path
5959
commentIndex := strings.Index(line, commentFlag)
6060
if commentIndex == 0 {
61-
pkg, _, _ := getPkgInfo(formatData[i+1], true)
61+
pkg, _, _ := getPkgInfo(formatData[i+1], strings.Index(formatData[i+1], commentFlag) >= 0)
6262
p.comment[pkg] = line
6363
continue
6464
} else if commentIndex > 0 {
@@ -94,7 +94,7 @@ func (p *pkg) fmt() []byte {
9494
sort.Strings(p.list[pkgType])
9595
for _, s := range p.list[pkgType] {
9696
if p.comment[s] != "" {
97-
l := fmt.Sprintf("%s%s%s", indent, p.comment[s], linebreak)
97+
l := fmt.Sprintf("%s%s%s%s", linebreak, indent, p.comment[s], linebreak)
9898
ret = append(ret, l)
9999
}
100100

@@ -114,7 +114,11 @@ func (p *pkg) fmt() []byte {
114114
if ret[len(ret)-1] == linebreak {
115115
ret = ret[:len(ret)-1]
116116
}
117-
return []byte(strings.Join(ret, ""))
117+
118+
// remove duplicate empty lines
119+
s1 := fmt.Sprintf("%s%s%s%s", linebreak, linebreak, linebreak, indent)
120+
s2 := fmt.Sprintf("%s%s%s", linebreak, linebreak, indent)
121+
return []byte(strings.ReplaceAll(strings.Join(ret, ""), s1, s2))
118122
}
119123

120124
// getPkgInfo assume line is a import path, and return (path, alias, comment)

0 commit comments

Comments
 (0)