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

Commit c1a08c0

Browse files
authored
fix: formatting use outdated file (#14)
Instead of using the filename as the input of `gno fmt`, which is by definition outdated since the save has not happened yet, we take the content of the file from the snapshots (updated by didChange/didSave) and write a temporary file with so we can pass it to `gno fmt`.
1 parent 336b382 commit c1a08c0

File tree

3 files changed

+24
-6
lines changed

3 files changed

+24
-6
lines changed

internal/lsp/format.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package lsp
33
import (
44
"context"
55
"encoding/json"
6+
"fmt"
67
"log/slog"
78
"math"
89

@@ -19,8 +20,12 @@ func (s *server) Formatting(ctx context.Context, reply jsonrpc2.Replier, req jso
1920
}
2021

2122
uri := params.TextDocument.URI
23+
file, ok := s.snapshot.Get(uri.Filename())
24+
if !ok {
25+
return replyErr(ctx, reply, fmt.Errorf("snapshot %s not found", uri.Filename()))
26+
}
2227

23-
formatted, err := tools.Format(uri.Filename())
28+
formatted, err := tools.Format(file.Src)
2429
if err != nil {
2530
return replyErr(ctx, reply, err)
2631
}

internal/lsp/server.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ func BuildServerHandler(conn jsonrpc2.Conn, e *env.Env) jsonrpc2.Handler {
4747
}
4848

4949
func (s *server) ServerHandler(ctx context.Context, reply jsonrpc2.Replier, req jsonrpc2.Request) error {
50+
slog.Info("handle", "method", req.Method())
5051
if req.Method() == protocol.MethodInitialize {
5152
err := s.Initialize(ctx, reply, req)
5253
if err != nil {

internal/tools/format.go

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,30 @@ package tools
33
import (
44
"bytes"
55
"fmt"
6+
"os"
67
"os/exec"
8+
"path/filepath"
79
"strings"
810
)
911

10-
func Format(file string) ([]byte, error) {
11-
cmd := exec.Command("gno", "fmt", file)
12+
func Format(source []byte) ([]byte, error) {
13+
// gno fmt accepts only files, so we need to write source in a temp file.
14+
tmpDir, err := os.MkdirTemp("", "gnopls-fmt")
15+
if err != nil {
16+
return nil, fmt.Errorf("format: %w", err)
17+
}
18+
tmpFile := filepath.Join(tmpDir, "file.gno")
19+
err = os.WriteFile(tmpFile, source, 0o600)
20+
if err != nil {
21+
return nil, fmt.Errorf("format: %w", err)
22+
}
23+
defer os.Remove(tmpDir)
24+
cmd := exec.Command("gno", "fmt", tmpFile)
1225
var stdin, stderr bytes.Buffer
1326
cmd.Stdout = &stdin
1427
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())
28+
if err := cmd.Run(); err != nil {
29+
return nil, fmt.Errorf("format: running '%s': %w: %s", strings.Join(cmd.Args, " "), err, stderr.String())
1830
}
1931
return stdin.Bytes(), nil
2032
}

0 commit comments

Comments
 (0)