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

feat(formatting): use gno fmt #13

Merged
merged 2 commits into from
Dec 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 8 additions & 34 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,63 +4,37 @@ go 1.23

require (
github.com/dave/jennifer v1.7.0
github.com/gnolang/gno v0.0.0-20240118150545-7aa81d138701
github.com/gnolang/gno v0.0.0-20241217133227-fdedae90bfc1
github.com/gnolang/tlin v1.0.1-0.20240930090350-be21dd15c7aa
github.com/google/go-github v17.0.0+incompatible
github.com/orcaman/concurrent-map/v2 v2.0.1
github.com/rogpeppe/go-internal v1.11.0
github.com/rogpeppe/go-internal v1.12.0
github.com/spf13/cobra v1.5.0
go.lsp.dev/jsonrpc2 v0.10.0
go.lsp.dev/pkg v0.0.0-20210717090340-384b27a52fb2
go.lsp.dev/protocol v0.12.0
go.lsp.dev/uri v0.3.0
go.uber.org/multierr v1.10.0
go.uber.org/multierr v1.11.0
golang.org/x/mod v0.21.0
golang.org/x/tools v0.25.0
mvdan.cc/gofumpt v0.4.0
)

require (
github.com/btcsuite/btcd/btcec/v2 v2.3.2 // indirect
github.com/btcsuite/btcd/btcutil v1.1.3 // indirect
github.com/cespare/xxhash v1.1.0 // indirect
github.com/cespare/xxhash/v2 v2.1.1 // indirect
github.com/btcsuite/btcd/btcec/v2 v2.3.4 // indirect
github.com/btcsuite/btcd/btcutil v1.1.6 // indirect
github.com/cockroachdb/apd/v3 v3.2.1 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.2.0 // indirect
github.com/dgraph-io/badger/v3 v3.2103.4 // indirect
github.com/dgraph-io/ristretto v0.1.1 // indirect
github.com/dustin/go-humanize v1.0.0 // indirect
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.3.0 // indirect
github.com/fzipp/gocyclo v0.6.0 // indirect
github.com/gnolang/goleveldb v0.0.9 // indirect
github.com/gnolang/overflow v0.0.0-20170615021017-4d914c927216 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b // indirect
github.com/golang/groupcache v0.0.0-20190702054246-869f871628b6 // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/golang/snappy v0.0.4 // indirect
github.com/google/flatbuffers v1.12.1 // indirect
github.com/google/go-cmp v0.6.0 // indirect
github.com/google/go-querystring v1.1.0 // indirect
github.com/gorilla/websocket v1.5.1 // indirect
github.com/inconshreveable/mousetrap v1.0.0 // indirect
github.com/jmhodges/levigo v1.0.0 // indirect
github.com/klauspost/compress v1.12.3 // indirect
github.com/libp2p/go-buffer-pool v0.1.0 // indirect
github.com/linxGnu/grocksdb v1.8.5 // indirect
github.com/pelletier/go-toml v1.9.5 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/rs/cors v1.10.1 // indirect
github.com/segmentio/asm v1.1.3 // indirect
github.com/segmentio/encoding v0.3.4 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/tecbot/gorocksdb v0.0.0-20191217155057-f0fad39f321c // indirect
go.etcd.io/bbolt v1.3.8 // indirect
go.opencensus.io v0.22.5 // indirect
go.uber.org/zap v1.27.0 // indirect
golang.org/x/crypto v0.27.0 // indirect
golang.org/x/net v0.29.0 // indirect
golang.org/x/exp v0.0.0-20240613232115-7f521ea00fb8 // indirect
golang.org/x/sync v0.8.0 // indirect
golang.org/x/sys v0.25.0 // indirect
google.golang.org/protobuf v1.31.0 // indirect
google.golang.org/protobuf v1.35.1 // indirect
)
214 changes: 31 additions & 183 deletions go.sum

Large diffs are not rendered by default.

9 changes: 2 additions & 7 deletions internal/lsp/format.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package lsp
import (
"context"
"encoding/json"
"errors"
"log/slog"
"math"

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

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

formatted, err := tools.Format(string(file.Src), s.formatOpt)
formatted, err := tools.Format(uri.Filename())
if err != nil {
return reply(ctx, nil, err)
return replyErr(ctx, reply, err)
}

slog.Info("format " + string(params.TextDocument.URI.Filename()))
Expand Down
4 changes: 0 additions & 4 deletions internal/lsp/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (
"go.lsp.dev/protocol"

"github.com/gnolang/gnopls/internal/env"
"github.com/gnolang/gnopls/internal/tools"
"github.com/gnolang/gnopls/internal/version"
)

Expand All @@ -24,7 +23,6 @@ type server struct {
completionStore *CompletionStore
cache *Cache

formatOpt tools.FormattingOption
initialized atomic.Bool
}

Expand All @@ -42,8 +40,6 @@ func BuildServerHandler(conn jsonrpc2.Conn, e *env.Env) jsonrpc2.Handler {
snapshot: NewSnapshot(),
completionStore: InitCompletionStore(dirs),
cache: NewCache(),

formatOpt: tools.Gofumpt,
}
env.GlobalEnv = e
return jsonrpc2.ReplyHandler(server.ServerHandler)
Expand Down
40 changes: 13 additions & 27 deletions internal/tools/format.go
Original file line number Diff line number Diff line change
@@ -1,34 +1,20 @@
package tools

import (
"errors"
"go/format"

gofumpt "mvdan.cc/gofumpt/format"
)

type FormattingOption int

const (
Gofmt FormattingOption = iota
Gofumpt
"bytes"
"fmt"
"os/exec"
"strings"
)

func Format(data string, opt FormattingOption) ([]byte, error) {
switch opt {
case Gofmt:
return RunGofmt(data)
case Gofumpt:
return RunGofumpt(data)
default:
return nil, errors.New("gnopls: invalid formatting option")
func Format(file string) ([]byte, error) {
cmd := exec.Command("gno", "fmt", file)
var stdin, stderr bytes.Buffer
cmd.Stdout = &stdin
cmd.Stderr = &stderr
err := cmd.Run()
if err != nil {
return nil, fmt.Errorf("running '%s': %w: %s", strings.Join(cmd.Args, " "), err, stderr.String())
}
}

func RunGofmt(data string) ([]byte, error) {
return format.Source([]byte(data))
}

func RunGofumpt(data string) ([]byte, error) {
return gofumpt.Source([]byte(data), gofumpt.Options{})
return stdin.Bytes(), nil
}
58 changes: 58 additions & 0 deletions testdata/document_formatting.txtar
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
# Init phase
lsp initialize input/initialize.json
lsp initialized input/initialized.json
lsp workspace/didChangeConfiguration input/didChangeConfiguration.json
lsp textDocument/didOpen input/didOpen_x.json

lsp textDocument/formatting input/formatting.json
cmp output/formatting.json expected/formatting.json
-- x.gno --
package foo
func Hello(){
_=42
}
-- input/initialize.json --
{
"rootUri": "file://$WORK"
}
-- input/initialized.json --
{}
-- input/didChangeConfiguration.json --
{
"settings": {
"gno": "$GOBIN/gno",
"gopls": "$GOBIN/gopls",
"root": "$GNOPATH",
"precompileOnSave": true,
"buildOnSave": true
}
}
-- input/didOpen_x.json --
{
"textDocument": {
"uri":"file://$WORK/x.gno",
"text":"${FILE_x.gno}"
}
}
-- input/formatting.json --
{
"textDocument": {
"uri":"file://$WORK/x.gno"
}
}
-- expected/formatting.json --
[
{
"newText": "package foo\n\nfunc Hello() {\n\t_ = 42\n}\n",
"range": {
"end": {
"character": 2147483647,
"line": 2147483647
},
"start": {
"character": 0,
"line": 0
}
}
}
]
58 changes: 58 additions & 0 deletions testdata/document_formatting_imports.txtar
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
# Init phase
lsp initialize input/initialize.json
lsp initialized input/initialized.json
lsp workspace/didChangeConfiguration input/didChangeConfiguration.json
lsp textDocument/didOpen input/didOpen_x.json

lsp textDocument/formatting input/formatting.json
cmp output/formatting.json expected/formatting.json
-- x.gno --
package foo
func Hello() {
ufmt.Sprintf()
}
-- input/initialize.json --
{
"rootUri": "file://$WORK"
}
-- input/initialized.json --
{}
-- input/didChangeConfiguration.json --
{
"settings": {
"gno": "$GOBIN/gno",
"gopls": "$GOBIN/gopls",
"root": "$GNOPATH",
"precompileOnSave": true,
"buildOnSave": true
}
}
-- input/didOpen_x.json --
{
"textDocument": {
"uri":"file://$WORK/x.gno",
"text":"${FILE_x.gno}"
}
}
-- input/formatting.json --
{
"textDocument": {
"uri":"file://$WORK/x.gno"
}
}
-- expected/formatting.json --
[
{
"newText": "package foo\n\nimport \"gno.land/p/demo/ufmt\"\n\nfunc Hello() {\n\tufmt.Sprintf()\n}\n",
"range": {
"end": {
"character": 2147483647,
"line": 2147483647
},
"start": {
"character": 0,
"line": 0
}
}
}
]
Loading