Skip to content

Commit 33d5988

Browse files
committed
gopls/internal/server: Organize Imports of generated files
This CL causes CodeAction to offer the Organize Imports operation even in generated files. Unlike most code actions with fixes, which are a nuisance in generated files since the fix should be applied to the generator logic not its output, Organize Imports is invaluable when making experimental temporary edits in generated files, such as adding logging statements. Also, it is silent on files that are well formed, which all unedited generated files must be. + test Updates golang/go#49555 Fixes golang/go#73959 Change-Id: Ia72f5b40402175ecd80dc62e7c34e8b3cf51d011 Reviewed-on: https://go-review.googlesource.com/c/tools/+/678835 Reviewed-by: Robert Findley <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]>
1 parent cb39a5f commit 33d5988

File tree

3 files changed

+39
-2
lines changed

3 files changed

+39
-2
lines changed

gopls/internal/server/code_action.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,10 +180,16 @@ func (s *server) CodeAction(ctx context.Context, params *protocol.CodeActionPara
180180
}
181181
actions = append(actions, moreActions...)
182182

183-
// Don't suggest fixes for generated files, since they are generally
183+
// Don't suggest most fixes for generated files, since they are generally
184184
// not useful and some editors may apply them automatically on save.
185185
// (Unfortunately there's no reliable way to distinguish fixes from
186186
// queries, so we must list all kinds of queries here.)
187+
//
188+
// We make an exception for OrganizeImports, because
189+
// (a) it is needed when making temporary experimental
190+
// changes (e.g. adding logging) in generated files, and
191+
// (b) it doesn't report diagnostics on well-formed code, and
192+
// unedited generated files must be well formed.
187193
if golang.IsGenerated(ctx, snapshot, uri) {
188194
actions = slices.DeleteFunc(actions, func(a protocol.CodeAction) bool {
189195
switch a.Kind {
@@ -194,6 +200,8 @@ func (s *server) CodeAction(ctx context.Context, params *protocol.CodeActionPara
194200
settings.GoplsDocFeatures,
195201
settings.GoToggleCompilerOptDetails:
196202
return false // read-only query
203+
case settings.OrganizeImports:
204+
return false // fix allowed in generated files (see #73959)
197205
}
198206
return true // potential write operation
199207
})
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
This test verifies that the 'source.organizeImports' code action
2+
is offered in generated files (see #73959).
3+
4+
-- go.mod --
5+
module example.com
6+
go 1.21
7+
8+
-- a.go --
9+
// Code generated by me. DO NOT EDIT.
10+
11+
package a //@codeaction("a", "source.organizeImports", result=out)
12+
13+
func _() {
14+
fmt.Println("hello") //@diag("fmt", re"undefined")
15+
}
16+
17+
-- @out/a.go --
18+
// Code generated by me. DO NOT EDIT.
19+
20+
package a //@codeaction("a", "source.organizeImports", result=out)
21+
22+
import "fmt"
23+
24+
func _() {
25+
fmt.Println("hello") //@diag("fmt", re"undefined")
26+
}
27+

gopls/internal/test/marker/testdata/format/generated.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ go 1.21
1313
-- a/a.go --
1414
// Code generated by me. DO NOT EDIT.
1515

16-
package a; func main() { fmt.Println("hello") }
16+
package a; import "fmt"; func main() { fmt.Println("hello") }
1717

1818
//@format(out)
1919

@@ -22,6 +22,8 @@ package a; func main() { fmt.Println("hello") }
2222

2323
package a
2424

25+
import "fmt"
26+
2527
func main() { fmt.Println("hello") }
2628

2729
//@format(out)

0 commit comments

Comments
 (0)