github.com/jd-ly/tools@v0.5.7/internal/lsp/mod/format.go (about) 1 package mod 2 3 import ( 4 "context" 5 6 "github.com/jd-ly/tools/internal/event" 7 "github.com/jd-ly/tools/internal/lsp/protocol" 8 "github.com/jd-ly/tools/internal/lsp/source" 9 ) 10 11 func 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 }