mirror of
https://github.com/golang/go
synced 2024-11-18 18:34:40 -07:00
4025ed8474
This change moves the suggested fixes logic for fillstruct out of the analysis and into internal/lsp/source. This logic is then used as part of a new fillstruct command. This command is returned along with the code action results, to be executed only when the user accepts the code action. This led to a number of changes to testing. The suggested fix tests in internal/lsp doesn't support executing commands, so we skip them. The suggested fix tests in internal/lsp/source are changed to call fillstruct directly. A new regtest is added to check the command execution, which led to a few regtest changes. Also, remove the `go mod tidy` code action, as it's made redundant by the existence of the suggested fixes coming from internal/lsp/mod. Change-Id: I35ca0aff1ace8f0097fe7cb57232997facb516a4 Reviewed-on: https://go-review.googlesource.com/c/tools/+/241983 Reviewed-by: Heschi Kreinick <heschi@google.com>
70 lines
1.7 KiB
Go
70 lines
1.7 KiB
Go
// Copyright 2020 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 source
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
|
|
"golang.org/x/tools/internal/lsp/analysis/fillstruct"
|
|
"golang.org/x/tools/internal/lsp/protocol"
|
|
"golang.org/x/tools/internal/span"
|
|
)
|
|
|
|
func FillStruct(ctx context.Context, snapshot Snapshot, fh FileHandle, pRng protocol.Range) ([]protocol.TextDocumentEdit, error) {
|
|
pkg, pgh, err := getParsedFile(ctx, snapshot, fh, NarrowestPackageHandle)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("getting file for Identifier: %w", err)
|
|
}
|
|
file, _, m, _, err := pgh.Cached()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
spn, err := m.RangeSpan(pRng)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
rng, err := spn.Range(m.Converter)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
content, err := fh.Read()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
fset := snapshot.View().Session().Cache().FileSet()
|
|
fix, err := fillstruct.SuggestedFix(fset, rng.Start, content, file, pkg.GetTypes(), pkg.GetTypesInfo())
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
var edits []protocol.TextDocumentEdit
|
|
for _, edit := range fix.TextEdits {
|
|
rng := span.NewRange(fset, edit.Pos, edit.End)
|
|
spn, err = rng.Span()
|
|
if err != nil {
|
|
return nil, nil
|
|
}
|
|
clRng, err := m.Range(spn)
|
|
if err != nil {
|
|
return nil, nil
|
|
}
|
|
edits = append(edits, protocol.TextDocumentEdit{
|
|
TextDocument: protocol.VersionedTextDocumentIdentifier{
|
|
Version: fh.Version(),
|
|
TextDocumentIdentifier: protocol.TextDocumentIdentifier{
|
|
URI: protocol.URIFromSpanURI(fh.URI()),
|
|
},
|
|
},
|
|
Edits: []protocol.TextEdit{
|
|
{
|
|
Range: clRng,
|
|
NewText: string(edit.NewText),
|
|
},
|
|
},
|
|
})
|
|
}
|
|
return edits, nil
|
|
}
|