golang.org/x/tools/gopls@v0.15.3/internal/mod/format.go (about) 1 // Copyright 2020 The Go Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package mod 6 7 import ( 8 "context" 9 10 "golang.org/x/tools/gopls/internal/cache" 11 "golang.org/x/tools/gopls/internal/file" 12 "golang.org/x/tools/gopls/internal/protocol" 13 "golang.org/x/tools/internal/diff" 14 "golang.org/x/tools/internal/event" 15 ) 16 17 func Format(ctx context.Context, snapshot *cache.Snapshot, fh file.Handle) ([]protocol.TextEdit, error) { 18 ctx, done := event.Start(ctx, "mod.Format") 19 defer done() 20 21 pm, err := snapshot.ParseMod(ctx, fh) 22 if err != nil { 23 return nil, err 24 } 25 formatted, err := pm.File.Format() 26 if err != nil { 27 return nil, err 28 } 29 // Calculate the edits to be made due to the change. 30 diffs := diff.Bytes(pm.Mapper.Content, formatted) 31 return protocol.EditsFromDiffEdits(pm.Mapper, diffs) 32 }