Skip to content

Commit

Permalink
support format blank (#108)
Browse files Browse the repository at this point in the history
Signed-off-by: Loong <loong.dai@intel.com>
  • Loading branch information
daixiang0 committed Sep 1, 2022
1 parent 4606d17 commit edf2fd9
Show file tree
Hide file tree
Showing 34 changed files with 56 additions and 7 deletions.
11 changes: 6 additions & 5 deletions pkg/gci/gci.go
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"errors"
"fmt"
goFormat "go/format"
"os"
"sync"

Expand Down Expand Up @@ -172,11 +173,6 @@ func LoadFormatGoFile(file io.FileObj, cfg config.Config) (src, dist []byte, err
}
}

// remove breakline in the end
for body[len(body)-1] == utils.Linebreak {
body = body[:len(body)-1]
}

if tail[0] != utils.Linebreak {
body = append(body, utils.Linebreak)
}
Expand All @@ -192,6 +188,11 @@ func LoadFormatGoFile(file io.FileObj, cfg config.Config) (src, dist []byte, err
i += copy(dist[i:], s)
}

dist, err = goFormat.Source(dist)
if err != nil {
return nil, nil, err
}

return src, dist, nil
}

Expand Down
1 change: 1 addition & 0 deletions pkg/gci/internal/testdata/already-good.in.go
@@ -1,4 +1,5 @@
package main

import (
"fmt"

Expand Down
1 change: 1 addition & 0 deletions pkg/gci/internal/testdata/already-good.out.go
@@ -1,4 +1,5 @@
package main

import (
"fmt"

Expand Down
1 change: 1 addition & 0 deletions pkg/gci/internal/testdata/blank-format.cfg.yaml
9 changes: 9 additions & 0 deletions pkg/gci/internal/testdata/blank-format.in.go
@@ -0,0 +1,9 @@
package main
import (
"fmt"

// comment
g "github.com/golang" // comment

"github.com/daixiang0/gci"
)
10 changes: 10 additions & 0 deletions pkg/gci/internal/testdata/blank-format.out.go
@@ -0,0 +1,10 @@
package main

import (
"fmt"

// comment
g "github.com/golang" // comment

"github.com/daixiang0/gci"
)
2 changes: 1 addition & 1 deletion pkg/gci/internal/testdata/comment-in-the-tail.in.go
@@ -1,4 +1,5 @@
package main

import (
"fmt"

Expand All @@ -7,7 +8,6 @@ import (
"github.com/daixiang0/gci"
)


type test int

// test
2 changes: 1 addition & 1 deletion pkg/gci/internal/testdata/comment-in-the-tail.out.go
@@ -1,4 +1,5 @@
package main

import (
"fmt"

Expand All @@ -7,7 +8,6 @@ import (
"github.com/daixiang0/gci"
)


type test int

// test
1 change: 1 addition & 0 deletions pkg/gci/internal/testdata/custom-order.in.go
@@ -1,4 +1,5 @@
package main

import (
"fmt"

Expand Down
1 change: 1 addition & 0 deletions pkg/gci/internal/testdata/custom-order.out.go
@@ -1,4 +1,5 @@
package main

import (
"github.com/daixiang0/a"

Expand Down
1 change: 1 addition & 0 deletions pkg/gci/internal/testdata/default-order.in.go
@@ -1,4 +1,5 @@
package main

import (
"fmt"

Expand Down
1 change: 1 addition & 0 deletions pkg/gci/internal/testdata/default-order.out.go
@@ -1,4 +1,5 @@
package main

import (
"fmt"

Expand Down
1 change: 1 addition & 0 deletions pkg/gci/internal/testdata/dot-and-blank.in.go
@@ -1,4 +1,5 @@
package main

import (
"fmt"

Expand Down
1 change: 1 addition & 0 deletions pkg/gci/internal/testdata/dot-and-blank.out.go
@@ -1,4 +1,5 @@
package main

import (
"fmt"

Expand Down
1 change: 1 addition & 0 deletions pkg/gci/internal/testdata/duplicate-imports.in.go
@@ -1,4 +1,5 @@
package main

import (
"fmt"

Expand Down
1 change: 1 addition & 0 deletions pkg/gci/internal/testdata/duplicate-imports.out.go
@@ -1,4 +1,5 @@
package main

import (
"fmt"

Expand Down
1 change: 1 addition & 0 deletions pkg/gci/internal/testdata/linebreak-no-custom.in.go
@@ -1,4 +1,5 @@
package main

import (
g "github.com/golang"

Expand Down
1 change: 1 addition & 0 deletions pkg/gci/internal/testdata/linebreak-no-custom.out.go
@@ -1,4 +1,5 @@
package main

import (
"fmt"

Expand Down
1 change: 1 addition & 0 deletions pkg/gci/internal/testdata/linebreak.in.go
@@ -1,4 +1,5 @@
package main

import (
g "github.com/golang"

Expand Down
1 change: 1 addition & 0 deletions pkg/gci/internal/testdata/linebreak.out.go
@@ -1,4 +1,5 @@
package main

import (
"fmt"

Expand Down
1 change: 1 addition & 0 deletions pkg/gci/internal/testdata/mismatch-section.in.go
@@ -1,4 +1,5 @@
package main

import (
"fmt"

Expand Down
1 change: 1 addition & 0 deletions pkg/gci/internal/testdata/mismatch-section.out.go
@@ -1,4 +1,5 @@
package main

import (
"fmt"

Expand Down
1 change: 1 addition & 0 deletions pkg/gci/internal/testdata/multiple-custom.in.go
@@ -1,4 +1,5 @@
package main

import (
"fmt"

Expand Down
1 change: 1 addition & 0 deletions pkg/gci/internal/testdata/multiple-custom.out.go
@@ -1,4 +1,5 @@
package main

import (
"fmt"

Expand Down
1 change: 1 addition & 0 deletions pkg/gci/internal/testdata/number-in-alias.in.go
@@ -1,4 +1,5 @@
package main

import (
"fmt"

Expand Down
1 change: 1 addition & 0 deletions pkg/gci/internal/testdata/number-in-alias.out.go
@@ -1,4 +1,5 @@
package main

import (
"fmt"

Expand Down
1 change: 1 addition & 0 deletions pkg/gci/internal/testdata/same-prefix-custom.in.go
@@ -1,4 +1,5 @@
package main

import (
"fmt"

Expand Down
1 change: 1 addition & 0 deletions pkg/gci/internal/testdata/same-prefix-custom.out.go
@@ -1,4 +1,5 @@
package main

import (
"fmt"

Expand Down
1 change: 1 addition & 0 deletions pkg/gci/internal/testdata/simple-case.in.go
@@ -1,4 +1,5 @@
package main

import (
"golang.org/x/tools"

Expand Down
1 change: 1 addition & 0 deletions pkg/gci/internal/testdata/simple-case.out.go
@@ -1,4 +1,5 @@
package main

import (
"fmt"

Expand Down
@@ -1,4 +1,5 @@
package main

import (
"fmt"
// golang
Expand Down
@@ -1,4 +1,5 @@
package main

import (
"fmt"

Expand Down
1 change: 1 addition & 0 deletions pkg/gci/internal/testdata/with-comment-and-alias.in.go
@@ -1,4 +1,5 @@
package main

import (
"fmt"
_ "github.com/golang" // golang
Expand Down
1 change: 1 addition & 0 deletions pkg/gci/internal/testdata/with-comment-and-alias.out.go
@@ -1,4 +1,5 @@
package main

import (
"fmt"

Expand Down

0 comments on commit edf2fd9

Please sign in to comment.