Skip to content
This repository was archived by the owner on Jan 31, 2025. It is now read-only.

Commit 944349f

Browse files
authored
feat(formatting): use gno fmt (#13)
* feat(formatting): use gno fmt Replace the usage of gofumpt/gofmt by `gno fmt` which has the benefit of updating gno imports in addition to the formatting. Added 2 txtar files to assert the behavior. * fix: `Format` returns only the stdout of gno fmt
1 parent 0847953 commit 944349f

File tree

7 files changed

+170
-255
lines changed

7 files changed

+170
-255
lines changed

go.mod

Lines changed: 8 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -4,63 +4,37 @@ go 1.23
44

55
require (
66
github.com/dave/jennifer v1.7.0
7-
github.com/gnolang/gno v0.0.0-20240118150545-7aa81d138701
7+
github.com/gnolang/gno v0.0.0-20241217133227-fdedae90bfc1
88
github.com/gnolang/tlin v1.0.1-0.20240930090350-be21dd15c7aa
99
github.com/google/go-github v17.0.0+incompatible
1010
github.com/orcaman/concurrent-map/v2 v2.0.1
11-
github.com/rogpeppe/go-internal v1.11.0
11+
github.com/rogpeppe/go-internal v1.12.0
1212
github.com/spf13/cobra v1.5.0
1313
go.lsp.dev/jsonrpc2 v0.10.0
1414
go.lsp.dev/pkg v0.0.0-20210717090340-384b27a52fb2
1515
go.lsp.dev/protocol v0.12.0
1616
go.lsp.dev/uri v0.3.0
17-
go.uber.org/multierr v1.10.0
17+
go.uber.org/multierr v1.11.0
1818
golang.org/x/mod v0.21.0
1919
golang.org/x/tools v0.25.0
20-
mvdan.cc/gofumpt v0.4.0
2120
)
2221

2322
require (
24-
github.com/btcsuite/btcd/btcec/v2 v2.3.2 // indirect
25-
github.com/btcsuite/btcd/btcutil v1.1.3 // indirect
26-
github.com/cespare/xxhash v1.1.0 // indirect
27-
github.com/cespare/xxhash/v2 v2.1.1 // indirect
23+
github.com/btcsuite/btcd/btcec/v2 v2.3.4 // indirect
24+
github.com/btcsuite/btcd/btcutil v1.1.6 // indirect
2825
github.com/cockroachdb/apd/v3 v3.2.1 // indirect
2926
github.com/davecgh/go-spew v1.1.1 // indirect
30-
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.2.0 // indirect
31-
github.com/dgraph-io/badger/v3 v3.2103.4 // indirect
32-
github.com/dgraph-io/ristretto v0.1.1 // indirect
33-
github.com/dustin/go-humanize v1.0.0 // indirect
27+
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.3.0 // indirect
3428
github.com/fzipp/gocyclo v0.6.0 // indirect
35-
github.com/gnolang/goleveldb v0.0.9 // indirect
36-
github.com/gnolang/overflow v0.0.0-20170615021017-4d914c927216 // indirect
37-
github.com/gogo/protobuf v1.3.2 // indirect
38-
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b // indirect
39-
github.com/golang/groupcache v0.0.0-20190702054246-869f871628b6 // indirect
40-
github.com/golang/protobuf v1.5.3 // indirect
41-
github.com/golang/snappy v0.0.4 // indirect
42-
github.com/google/flatbuffers v1.12.1 // indirect
43-
github.com/google/go-cmp v0.6.0 // indirect
4429
github.com/google/go-querystring v1.1.0 // indirect
45-
github.com/gorilla/websocket v1.5.1 // indirect
4630
github.com/inconshreveable/mousetrap v1.0.0 // indirect
47-
github.com/jmhodges/levigo v1.0.0 // indirect
48-
github.com/klauspost/compress v1.12.3 // indirect
49-
github.com/libp2p/go-buffer-pool v0.1.0 // indirect
50-
github.com/linxGnu/grocksdb v1.8.5 // indirect
51-
github.com/pelletier/go-toml v1.9.5 // indirect
52-
github.com/pkg/errors v0.9.1 // indirect
53-
github.com/rs/cors v1.10.1 // indirect
5431
github.com/segmentio/asm v1.1.3 // indirect
5532
github.com/segmentio/encoding v0.3.4 // indirect
5633
github.com/spf13/pflag v1.0.5 // indirect
57-
github.com/tecbot/gorocksdb v0.0.0-20191217155057-f0fad39f321c // indirect
58-
go.etcd.io/bbolt v1.3.8 // indirect
59-
go.opencensus.io v0.22.5 // indirect
6034
go.uber.org/zap v1.27.0 // indirect
6135
golang.org/x/crypto v0.27.0 // indirect
62-
golang.org/x/net v0.29.0 // indirect
36+
golang.org/x/exp v0.0.0-20240613232115-7f521ea00fb8 // indirect
6337
golang.org/x/sync v0.8.0 // indirect
6438
golang.org/x/sys v0.25.0 // indirect
65-
google.golang.org/protobuf v1.31.0 // indirect
39+
google.golang.org/protobuf v1.35.1 // indirect
6640
)

go.sum

Lines changed: 31 additions & 183 deletions
Large diffs are not rendered by default.

internal/lsp/format.go

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package lsp
33
import (
44
"context"
55
"encoding/json"
6-
"errors"
76
"log/slog"
87
"math"
98

@@ -20,14 +19,10 @@ func (s *server) Formatting(ctx context.Context, reply jsonrpc2.Replier, req jso
2019
}
2120

2221
uri := params.TextDocument.URI
23-
file, ok := s.snapshot.Get(uri.Filename())
24-
if !ok {
25-
return reply(ctx, nil, errors.New("snapshot not found"))
26-
}
2722

28-
formatted, err := tools.Format(string(file.Src), s.formatOpt)
23+
formatted, err := tools.Format(uri.Filename())
2924
if err != nil {
30-
return reply(ctx, nil, err)
25+
return replyErr(ctx, reply, err)
3126
}
3227

3328
slog.Info("format " + string(params.TextDocument.URI.Filename()))

internal/lsp/server.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import (
1212
"go.lsp.dev/protocol"
1313

1414
"github.com/gnolang/gnopls/internal/env"
15-
"github.com/gnolang/gnopls/internal/tools"
1615
"github.com/gnolang/gnopls/internal/version"
1716
)
1817

@@ -24,7 +23,6 @@ type server struct {
2423
completionStore *CompletionStore
2524
cache *Cache
2625

27-
formatOpt tools.FormattingOption
2826
initialized atomic.Bool
2927
}
3028

@@ -42,8 +40,6 @@ func BuildServerHandler(conn jsonrpc2.Conn, e *env.Env) jsonrpc2.Handler {
4240
snapshot: NewSnapshot(),
4341
completionStore: InitCompletionStore(dirs),
4442
cache: NewCache(),
45-
46-
formatOpt: tools.Gofumpt,
4743
}
4844
env.GlobalEnv = e
4945
return jsonrpc2.ReplyHandler(server.ServerHandler)

internal/tools/format.go

Lines changed: 13 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,20 @@
11
package tools
22

33
import (
4-
"errors"
5-
"go/format"
6-
7-
gofumpt "mvdan.cc/gofumpt/format"
8-
)
9-
10-
type FormattingOption int
11-
12-
const (
13-
Gofmt FormattingOption = iota
14-
Gofumpt
4+
"bytes"
5+
"fmt"
6+
"os/exec"
7+
"strings"
158
)
169

17-
func Format(data string, opt FormattingOption) ([]byte, error) {
18-
switch opt {
19-
case Gofmt:
20-
return RunGofmt(data)
21-
case Gofumpt:
22-
return RunGofumpt(data)
23-
default:
24-
return nil, errors.New("gnopls: invalid formatting option")
10+
func Format(file string) ([]byte, error) {
11+
cmd := exec.Command("gno", "fmt", file)
12+
var stdin, stderr bytes.Buffer
13+
cmd.Stdout = &stdin
14+
cmd.Stderr = &stderr
15+
err := cmd.Run()
16+
if err != nil {
17+
return nil, fmt.Errorf("running '%s': %w: %s", strings.Join(cmd.Args, " "), err, stderr.String())
2518
}
26-
}
27-
28-
func RunGofmt(data string) ([]byte, error) {
29-
return format.Source([]byte(data))
30-
}
31-
32-
func RunGofumpt(data string) ([]byte, error) {
33-
return gofumpt.Source([]byte(data), gofumpt.Options{})
19+
return stdin.Bytes(), nil
3420
}

testdata/document_formatting.txtar

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
# Init phase
2+
lsp initialize input/initialize.json
3+
lsp initialized input/initialized.json
4+
lsp workspace/didChangeConfiguration input/didChangeConfiguration.json
5+
lsp textDocument/didOpen input/didOpen_x.json
6+
7+
lsp textDocument/formatting input/formatting.json
8+
cmp output/formatting.json expected/formatting.json
9+
-- x.gno --
10+
package foo
11+
func Hello(){
12+
_=42
13+
}
14+
-- input/initialize.json --
15+
{
16+
"rootUri": "file://$WORK"
17+
}
18+
-- input/initialized.json --
19+
{}
20+
-- input/didChangeConfiguration.json --
21+
{
22+
"settings": {
23+
"gno": "$GOBIN/gno",
24+
"gopls": "$GOBIN/gopls",
25+
"root": "$GNOPATH",
26+
"precompileOnSave": true,
27+
"buildOnSave": true
28+
}
29+
}
30+
-- input/didOpen_x.json --
31+
{
32+
"textDocument": {
33+
"uri":"file://$WORK/x.gno",
34+
"text":"${FILE_x.gno}"
35+
}
36+
}
37+
-- input/formatting.json --
38+
{
39+
"textDocument": {
40+
"uri":"file://$WORK/x.gno"
41+
}
42+
}
43+
-- expected/formatting.json --
44+
[
45+
{
46+
"newText": "package foo\n\nfunc Hello() {\n\t_ = 42\n}\n",
47+
"range": {
48+
"end": {
49+
"character": 2147483647,
50+
"line": 2147483647
51+
},
52+
"start": {
53+
"character": 0,
54+
"line": 0
55+
}
56+
}
57+
}
58+
]
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
# Init phase
2+
lsp initialize input/initialize.json
3+
lsp initialized input/initialized.json
4+
lsp workspace/didChangeConfiguration input/didChangeConfiguration.json
5+
lsp textDocument/didOpen input/didOpen_x.json
6+
7+
lsp textDocument/formatting input/formatting.json
8+
cmp output/formatting.json expected/formatting.json
9+
-- x.gno --
10+
package foo
11+
func Hello() {
12+
ufmt.Sprintf()
13+
}
14+
-- input/initialize.json --
15+
{
16+
"rootUri": "file://$WORK"
17+
}
18+
-- input/initialized.json --
19+
{}
20+
-- input/didChangeConfiguration.json --
21+
{
22+
"settings": {
23+
"gno": "$GOBIN/gno",
24+
"gopls": "$GOBIN/gopls",
25+
"root": "$GNOPATH",
26+
"precompileOnSave": true,
27+
"buildOnSave": true
28+
}
29+
}
30+
-- input/didOpen_x.json --
31+
{
32+
"textDocument": {
33+
"uri":"file://$WORK/x.gno",
34+
"text":"${FILE_x.gno}"
35+
}
36+
}
37+
-- input/formatting.json --
38+
{
39+
"textDocument": {
40+
"uri":"file://$WORK/x.gno"
41+
}
42+
}
43+
-- expected/formatting.json --
44+
[
45+
{
46+
"newText": "package foo\n\nimport \"gno.land/p/demo/ufmt\"\n\nfunc Hello() {\n\tufmt.Sprintf()\n}\n",
47+
"range": {
48+
"end": {
49+
"character": 2147483647,
50+
"line": 2147483647
51+
},
52+
"start": {
53+
"character": 0,
54+
"line": 0
55+
}
56+
}
57+
}
58+
]

0 commit comments

Comments
 (0)