This repository has been archived by the owner on May 29, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support formatting using
gofmt
and gofumpt
- Loading branch information
Showing
3 changed files
with
73 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package lsp | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"errors" | ||
"log/slog" | ||
"math" | ||
|
||
"github.com/harry-hov/gnopls/internal/tools" | ||
|
||
"go.lsp.dev/jsonrpc2" | ||
"go.lsp.dev/protocol" | ||
) | ||
|
||
func (s *server) Formatting(ctx context.Context, reply jsonrpc2.Replier, req jsonrpc2.Request) error { | ||
var params protocol.DocumentFormattingParams | ||
if err := json.Unmarshal(req.Params(), ¶ms); err != nil { | ||
return sendParseError(ctx, reply, err) | ||
} | ||
|
||
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) | ||
if err != nil { | ||
return reply(ctx, nil, err) | ||
} | ||
|
||
slog.Info("format " + string(params.TextDocument.URI.Filename())) | ||
return reply(ctx, []protocol.TextEdit{ | ||
{ | ||
Range: protocol.Range{ | ||
Start: protocol.Position{Line: 0, Character: 0}, | ||
End: protocol.Position{ | ||
Line: math.MaxInt32, | ||
Character: math.MaxInt32, | ||
}, | ||
}, | ||
NewText: string(formatted), | ||
}, | ||
}, nil) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,34 @@ | ||
package tools | ||
|
||
import ( | ||
"errors" | ||
"go/format" | ||
|
||
gofumpt "mvdan.cc/gofumpt/format" | ||
) | ||
|
||
type FormattingOption int | ||
|
||
const ( | ||
Gofmt FormattingOption = iota | ||
Gofumpt | ||
) | ||
|
||
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 RunGofmt(data string) ([]byte, error) { | ||
return format.Source([]byte(data)) | ||
} | ||
|
||
func RunGofumpt(data string) ([]byte, error) { | ||
return gofumpt.Source([]byte(data), gofumpt.Options{}) | ||
} |