1package mod
2
3import (
4	"context"
5
6	"golang.org/x/tools/internal/event"
7	"golang.org/x/tools/internal/lsp/protocol"
8	"golang.org/x/tools/internal/lsp/source"
9)
10
11func Format(ctx context.Context, snapshot source.Snapshot, fh source.FileHandle) ([]protocol.TextEdit, error) {
12	ctx, done := event.Start(ctx, "mod.Format")
13	defer done()
14
15	pm, err := snapshot.ParseMod(ctx, fh)
16	if err != nil {
17		return nil, err
18	}
19	formatted, err := pm.File.Format()
20	if err != nil {
21		return nil, err
22	}
23	// Calculate the edits to be made due to the change.
24	diff := snapshot.View().Options().ComputeEdits(fh.URI(), string(pm.Mapper.Content), string(formatted))
25	return source.ToProtocolEdits(pm.Mapper, diff)
26}
27