blob: 162bc8c0004dcc7afe144082b624e971eda7852e [file] [log] [blame]
// Copyright 2022 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package work
import (
"context"
"golang.org/x/mod/modfile"
"golang.org/x/tools/gopls/internal/cache"
"golang.org/x/tools/gopls/internal/file"
"golang.org/x/tools/gopls/internal/protocol"
"golang.org/x/tools/internal/diff"
"golang.org/x/tools/internal/event"
)
func Format(ctx context.Context, snapshot *cache.Snapshot, fh file.Handle) ([]protocol.TextEdit, error) {
ctx, done := event.Start(ctx, "work.Format")
defer done()
pw, err := snapshot.ParseWork(ctx, fh)
if err != nil {
return nil, err
}
formatted := modfile.Format(pw.File.Syntax)
// Calculate the edits to be made due to the change.
diffs := diff.Bytes(pw.Mapper.Content, formatted)
return protocol.EditsFromDiffEdits(pw.Mapper, diffs)
}