Skip to content

Commit 2756c52

Browse files
committed
internal/lsp: format files that parse in packages with parse errors
Updates golang/go#31291 Change-Id: Ibbd0b6cef9b9ec588c8a2e0c5e7ee6e3512b8a22 Reviewed-on: https://go-review.googlesource.com/c/tools/+/188767 Reviewed-by: Ian Cottrell <[email protected]> Run-TryBot: Rebecca Stambler <[email protected]>
1 parent 773fe55 commit 2756c52

File tree

1 file changed

+6
-5
lines changed

1 file changed

+6
-5
lines changed

internal/lsp/source/format.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ func Format(ctx context.Context, f GoFile, rng span.Range) ([]TextEdit, error) {
3030
return nil, err
3131
}
3232
pkg := f.GetPackage(ctx)
33-
if hasListErrors(pkg.GetErrors()) || hasParseErrors(pkg.GetErrors()) {
33+
if hasListErrors(pkg.GetErrors()) || hasParseErrors(pkg, f.URI()) {
3434
// Even if this package has list or parse errors, this file may not
3535
// have any parse errors and can still be formatted. Using format.Node
3636
// on an ast with errors may result in code being added or removed.
@@ -104,7 +104,6 @@ func Imports(ctx context.Context, view View, f GoFile, rng span.Range) ([]TextEd
104104
if err != nil {
105105
return nil, err
106106
}
107-
108107
return computeTextEdits(ctx, f, string(formatted)), nil
109108
}
110109

@@ -174,9 +173,11 @@ func AllImportsFixes(ctx context.Context, view View, f GoFile, rng span.Range) (
174173
return edits, editsPerFix, nil
175174
}
176175

177-
func hasParseErrors(errors []packages.Error) bool {
178-
for _, err := range errors {
179-
if err.Kind == packages.ParseError {
176+
// hasParseErrors returns true if the given file has parse errors.
177+
func hasParseErrors(pkg Package, uri span.URI) bool {
178+
for _, err := range pkg.GetErrors() {
179+
spn := packagesErrorSpan(err)
180+
if spn.URI() == uri && err.Kind == packages.ParseError {
180181
return true
181182
}
182183
}

0 commit comments

Comments
 (0)