Skip to content

Commit f61b225

Browse files
jbagopherbot
authored andcommitted
internal/analysisinternal: AddImport puts new import in a group
If AddImport needs to add a new import, and the file's first declaration is a grouped import, then add it to that import. This is one step towards a full implementation of the issue below, and perhaps is good enough. For golang/go#71647. Change-Id: I8327b07c21c3efbd189c519e51c339b7aa4751d8 Reviewed-on: https://go-review.googlesource.com/c/tools/+/648136 LUCI-TryBot-Result: Go LUCI <[email protected]> Reviewed-by: Alan Donovan <[email protected]> Auto-Submit: Jonathan Amsterdam <[email protected]>
1 parent 91bac86 commit f61b225

File tree

2 files changed

+37
-7
lines changed

2 files changed

+37
-7
lines changed

internal/analysisinternal/addimport_test.go

+22
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,28 @@ import . "fmt"
219219
220220
func _(Print fmt.Stringer) {
221221
fmt
222+
}`,
223+
},
224+
{
225+
descr: descr("add import to group"),
226+
src: `package a
227+
228+
import (
229+
"io"
230+
)
231+
232+
func _(io.Reader) {
233+
«fmt fmt»
234+
}`,
235+
want: `package a
236+
237+
import (
238+
"io"
239+
"fmt"
240+
)
241+
242+
func _(io.Reader) {
243+
fmt
222244
}`,
223245
},
224246
} {

internal/analysisinternal/analysis.go

+15-7
Original file line numberDiff line numberDiff line change
@@ -255,16 +255,16 @@ func AddImport(info *types.Info, file *ast.File, preferredName, pkgpath, member
255255
newName = fmt.Sprintf("%s%d", preferredName, i)
256256
}
257257

258-
// For now, keep it real simple: create a new import
259-
// declaration before the first existing declaration (which
260-
// must exist), including its comments, and let goimports tidy it up.
258+
// Create a new import declaration either before the first existing
259+
// declaration (which must exist), including its comments; or
260+
// inside the declaration, if it is an import group.
261261
//
262262
// Use a renaming import whenever the preferred name is not
263263
// available, or the chosen name does not match the last
264264
// segment of its path.
265-
newText := fmt.Sprintf("import %q\n\n", pkgpath)
265+
newText := fmt.Sprintf("%q", pkgpath)
266266
if newName != preferredName || newName != pathpkg.Base(pkgpath) {
267-
newText = fmt.Sprintf("import %s %q\n\n", newName, pkgpath)
267+
newText = fmt.Sprintf("%s %q", newName, pkgpath)
268268
}
269269
decl0 := file.Decls[0]
270270
var before ast.Node = decl0
@@ -278,9 +278,17 @@ func AddImport(info *types.Info, file *ast.File, preferredName, pkgpath, member
278278
before = decl0.Doc
279279
}
280280
}
281+
// If the first decl is an import group, add this new import at the end.
282+
if gd, ok := before.(*ast.GenDecl); ok && gd.Tok == token.IMPORT && gd.Rparen.IsValid() {
283+
pos = gd.Rparen
284+
newText = "\t" + newText + "\n"
285+
} else {
286+
pos = before.Pos()
287+
newText = "import " + newText + "\n\n"
288+
}
281289
return newName, newName + ".", []analysis.TextEdit{{
282-
Pos: before.Pos(),
283-
End: before.Pos(),
290+
Pos: pos,
291+
End: pos,
284292
NewText: []byte(newText),
285293
}}
286294
}

0 commit comments

Comments
 (0)