/
format.go
30 lines (25 loc) · 928 Bytes
/
format.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
// 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"
"cuelang.org/go/internal/golangorgx/gopls/cache"
"cuelang.org/go/internal/golangorgx/gopls/file"
"cuelang.org/go/internal/golangorgx/gopls/protocol"
"cuelang.org/go/internal/golangorgx/tools/diff"
"cuelang.org/go/internal/golangorgx/tools/event"
"golang.org/x/mod/modfile"
)
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)
}