mirror of
https://github.com/golang/go
synced 2024-11-18 16:14:46 -07:00
internal/lsp: format files in packages with errors
Packages with errors may still contain files that can be formatted. Try to format the source of the files in packages that have errors. This change will still not format files with parse errors. Updates golang/go#31291 Change-Id: Ia5168d7908948d201eac7f2ee28534022a2d4eb0 Reviewed-on: https://go-review.googlesource.com/c/tools/+/187757 Run-TryBot: Suzy Mueller <suzmue@golang.org> Reviewed-by: Rebecca Stambler <rstambler@golang.org>
This commit is contained in:
parent
2e34cfcb95
commit
1bd56024c6
@ -31,7 +31,15 @@ func Format(ctx context.Context, f GoFile, rng span.Range) ([]TextEdit, error) {
|
||||
}
|
||||
pkg := f.GetPackage(ctx)
|
||||
if hasListErrors(pkg.GetErrors()) || hasParseErrors(pkg.GetErrors()) {
|
||||
return nil, fmt.Errorf("%s has parse errors, not formatting", f.URI())
|
||||
// Even if this package has list or parse errors, this file may not
|
||||
// have any parse errors and can still be formatted. Using format.Node
|
||||
// on an ast with errors may result in code being added or removed.
|
||||
// Attempt to format the source of this file instead.
|
||||
formatted, err := formatSource(ctx, f)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return computeTextEdits(ctx, f, string(formatted)), nil
|
||||
}
|
||||
path, exact := astutil.PathEnclosingInterval(file, rng.Start, rng.End)
|
||||
if !exact || len(path) == 0 {
|
||||
@ -52,6 +60,16 @@ func Format(ctx context.Context, f GoFile, rng span.Range) ([]TextEdit, error) {
|
||||
return computeTextEdits(ctx, f, buf.String()), nil
|
||||
}
|
||||
|
||||
func formatSource(ctx context.Context, file File) ([]byte, error) {
|
||||
ctx, done := trace.StartSpan(ctx, "source.formatSource")
|
||||
defer done()
|
||||
data, _, err := file.Handle(ctx).Read(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return format.Source(data)
|
||||
}
|
||||
|
||||
// Imports formats a file using the goimports tool.
|
||||
func Imports(ctx context.Context, view View, f GoFile, rng span.Range) ([]TextEdit, error) {
|
||||
ctx, done := trace.StartSpan(ctx, "source.Imports")
|
||||
|
9
internal/lsp/testdata/noparse_format/parse_format.go.golden
vendored
Normal file
9
internal/lsp/testdata/noparse_format/parse_format.go.golden
vendored
Normal file
@ -0,0 +1,9 @@
|
||||
-- gofmt --
|
||||
package noparse_format //@format("package")
|
||||
|
||||
func _() {
|
||||
f()
|
||||
}
|
||||
|
||||
-- gofmt-d --
|
||||
|
5
internal/lsp/testdata/noparse_format/parse_format.go.in
vendored
Normal file
5
internal/lsp/testdata/noparse_format/parse_format.go.in
vendored
Normal file
@ -0,0 +1,5 @@
|
||||
package noparse_format //@format("package")
|
||||
|
||||
func _() {
|
||||
f()
|
||||
}
|
@ -29,7 +29,7 @@ const (
|
||||
ExpectedCompletionsCount = 144
|
||||
ExpectedCompletionSnippetCount = 15
|
||||
ExpectedDiagnosticsCount = 17
|
||||
ExpectedFormatCount = 5
|
||||
ExpectedFormatCount = 6
|
||||
ExpectedImportCount = 2
|
||||
ExpectedDefinitionsCount = 38
|
||||
ExpectedTypeDefinitionsCount = 2
|
||||
|
Loading…
Reference in New Issue
Block a user