-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
internal/refactor/inline: fix comment movement due to added imports
Logically separate the rewriting of imports from the rest of the file, so that floating comments don't wander into the import block. Fixes golang/go#67336 Updates golang/go#67335 Change-Id: I14bcbe1d15bf9abaed7535bdcf871b25c47c9a11 Reviewed-on: https://go-review.googlesource.com/c/tools/+/629979 LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Reviewed-by: Alan Donovan <adonovan@google.com>
- Loading branch information
Showing
4 changed files
with
276 additions
and
17 deletions.
There are no files selected for viewing
72 changes: 72 additions & 0 deletions
72
gopls/internal/test/marker/testdata/codeaction/inline_issue67336.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
This is the test case from golang/go#67335, where the inlining resulted in bad | ||
formatting. | ||
|
||
-- go.mod -- | ||
module example.com | ||
|
||
go 1.20 | ||
|
||
-- define/my/typ/foo.go -- | ||
package typ | ||
type T int | ||
|
||
-- some/other/pkg/foo.go -- | ||
package pkg | ||
import "context" | ||
import "example.com/define/my/typ" | ||
func Foo(typ.T) context.Context{ return nil } | ||
|
||
-- one/more/pkg/foo.go -- | ||
package pkg | ||
func Bar() {} | ||
|
||
-- to/be/inlined/foo.go -- | ||
package inlined | ||
|
||
import "context" | ||
import "example.com/some/other/pkg" | ||
import "example.com/define/my/typ" | ||
|
||
func Baz(ctx context.Context) context.Context { | ||
return pkg.Foo(typ.T(5)) | ||
} | ||
|
||
-- b/c/foo.go -- | ||
package c | ||
import ( | ||
"context" | ||
"example.com/to/be/inlined" | ||
"example.com/one/more/pkg" | ||
) | ||
|
||
const ( | ||
// This is a variable | ||
someConst = 5 | ||
) | ||
|
||
func foo() { | ||
inlined.Baz(context.TODO()) //@ codeaction("Baz", "refactor.inline.call", result=inline) | ||
pkg.Bar() | ||
} | ||
|
||
-- @inline/b/c/foo.go -- | ||
package c | ||
|
||
import ( | ||
"context" | ||
|
||
"example.com/define/my/typ" | ||
"example.com/one/more/pkg" | ||
pkg0 "example.com/some/other/pkg" | ||
) | ||
|
||
const ( | ||
// This is a variable | ||
someConst = 5 | ||
) | ||
|
||
func foo() { | ||
var _ context.Context = context.TODO() | ||
pkg0.Foo(typ.T(5)) //@ codeaction("Baz", "refactor.inline.call", result=inline) | ||
pkg.Bar() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
113 changes: 113 additions & 0 deletions
113
internal/refactor/inline/testdata/import-comments.txtar
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
This file checks various handling of comments when adding imports. | ||
|
||
-- go.mod -- | ||
module testdata | ||
go 1.12 | ||
|
||
-- a/empty.go -- | ||
package a // This is package a. | ||
|
||
func _() { | ||
a() //@ inline(re"a", empty) | ||
} | ||
|
||
-- empty -- | ||
package a // This is package a. | ||
|
||
import "testdata/b" | ||
|
||
func _() { | ||
b.B() //@ inline(re"a", empty) | ||
} | ||
-- a/existing.go -- | ||
package a // This is package a. | ||
|
||
// This is an import block. | ||
import ( | ||
// This is an import of io. | ||
"io" | ||
|
||
// This is an import of c. | ||
"testdata/c" | ||
) | ||
|
||
var ( | ||
// This is an io.Writer. | ||
_ io.Writer | ||
// This is c.C | ||
_ c.C | ||
) | ||
|
||
func _() { | ||
a() //@ inline(re"a", existing) | ||
} | ||
|
||
-- existing -- | ||
package a // This is package a. | ||
|
||
// This is an import block. | ||
import ( | ||
// This is an import of io. | ||
"io" | ||
|
||
// This is an import of c. | ||
"testdata/b" | ||
"testdata/c" | ||
) | ||
|
||
var ( | ||
// This is an io.Writer. | ||
_ io.Writer | ||
// This is c.C | ||
_ c.C | ||
) | ||
|
||
func _() { | ||
b.B() //@ inline(re"a", existing) | ||
} | ||
|
||
-- a/noparens.go -- | ||
package a // This is package a. | ||
|
||
// This is an import of c. | ||
import "testdata/c" | ||
|
||
func _() { | ||
var _ c.C | ||
a() //@ inline(re"a", noparens) | ||
} | ||
|
||
-- noparens -- | ||
package a // This is package a. | ||
|
||
// This is an import of c. | ||
import ( | ||
"testdata/b" | ||
"testdata/c" | ||
) | ||
|
||
func _() { | ||
var _ c.C | ||
b.B() //@ inline(re"a", noparens) | ||
} | ||
|
||
-- a/a.go -- | ||
package a | ||
|
||
// This is an import of b. | ||
import "testdata/b" | ||
|
||
func a() { | ||
// This is a call to B. | ||
b.B() | ||
} | ||
|
||
-- b/b.go -- | ||
package b | ||
|
||
func B() {} | ||
|
||
-- c/c.go -- | ||
package c | ||
|
||
type C int |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters