mirror of
https://github.com/golang/go
synced 2024-11-18 19:54:44 -07:00
206ec5b82a
Change-Id: Idc662385ed08bd62593ccd1d54afd3fa8c1a7d29 Reviewed-on: https://go-review.googlesource.com/c/tools/+/222558 Run-TryBot: Ian Cottrell <iancottrell@google.com> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Robert Findley <rfindley@google.com>
27 lines
712 B
Go
27 lines
712 B
Go
package mod
|
|
|
|
import (
|
|
"context"
|
|
|
|
"golang.org/x/tools/internal/lsp/protocol"
|
|
"golang.org/x/tools/internal/lsp/source"
|
|
"golang.org/x/tools/internal/telemetry/event"
|
|
)
|
|
|
|
func Format(ctx context.Context, snapshot source.Snapshot, fh source.FileHandle) ([]protocol.TextEdit, error) {
|
|
ctx, done := event.StartSpan(ctx, "mod.Format")
|
|
defer done()
|
|
|
|
file, m, err := snapshot.ModHandle(ctx, fh).Parse(ctx)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
formatted, err := file.Format()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
// Calculate the edits to be made due to the change.
|
|
diff := snapshot.View().Options().ComputeEdits(fh.Identity().URI, string(m.Content), string(formatted))
|
|
return source.ToProtocolEdits(m, diff)
|
|
}
|