mirror of
https://github.com/golang/go
synced 2024-11-12 05:30:21 -07:00
go/printer: align ImportPaths in ImportDecls if PackageName is given.
Fixes #1044. R=gri CC=golang-dev https://golang.org/cl/1958047
This commit is contained in:
parent
20198d69f9
commit
1a0b62a16a
@ -1192,25 +1192,25 @@ func (p *printer) stmt(stmt ast.Stmt, nextIsRBrace bool, multiLine *bool) {
|
|||||||
// ----------------------------------------------------------------------------
|
// ----------------------------------------------------------------------------
|
||||||
// Declarations
|
// Declarations
|
||||||
|
|
||||||
// The parameter n is the number of specs in the group. If indent is set,
|
// The parameter n is the number of specs in the group. If doIndent is set,
|
||||||
// multi-line identifier lists in the spec are indented when the first
|
// multi-line identifier lists in the spec are indented when the first
|
||||||
// linebreak is encountered.
|
// linebreak is encountered.
|
||||||
// Sets multiLine to true if the spec spans multiple lines.
|
// Sets multiLine to true if the spec spans multiple lines.
|
||||||
//
|
//
|
||||||
func (p *printer) spec(spec ast.Spec, n int, indent bool, multiLine *bool) {
|
func (p *printer) spec(spec ast.Spec, n int, doIndent bool, multiLine *bool) {
|
||||||
switch s := spec.(type) {
|
switch s := spec.(type) {
|
||||||
case *ast.ImportSpec:
|
case *ast.ImportSpec:
|
||||||
p.setComment(s.Doc)
|
p.setComment(s.Doc)
|
||||||
if s.Name != nil {
|
if s.Name != nil {
|
||||||
p.expr(s.Name, multiLine)
|
p.expr(s.Name, multiLine)
|
||||||
p.print(blank)
|
p.print(vtab)
|
||||||
}
|
}
|
||||||
p.expr(s.Path, multiLine)
|
p.expr(s.Path, multiLine)
|
||||||
p.setComment(s.Comment)
|
p.setComment(s.Comment)
|
||||||
|
|
||||||
case *ast.ValueSpec:
|
case *ast.ValueSpec:
|
||||||
p.setComment(s.Doc)
|
p.setComment(s.Doc)
|
||||||
p.identList(s.Names, indent, multiLine) // always present
|
p.identList(s.Names, doIndent, multiLine) // always present
|
||||||
if n == 1 {
|
if n == 1 {
|
||||||
if s.Type != nil {
|
if s.Type != nil {
|
||||||
p.print(blank)
|
p.print(blank)
|
||||||
|
52
src/pkg/go/printer/testdata/declarations.golden
vendored
52
src/pkg/go/printer/testdata/declarations.golden
vendored
@ -7,10 +7,10 @@ package imports
|
|||||||
import "io"
|
import "io"
|
||||||
|
|
||||||
import (
|
import (
|
||||||
_ "io"
|
_ "io"
|
||||||
)
|
)
|
||||||
|
|
||||||
import _ "io"
|
import _ "io"
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"io"
|
"io"
|
||||||
@ -20,40 +20,60 @@ import (
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"io"
|
"io"
|
||||||
aLongRename "io"
|
aLongRename "io"
|
||||||
|
|
||||||
b "io"
|
b "io"
|
||||||
|
)
|
||||||
|
|
||||||
|
import (
|
||||||
|
"unrenamed"
|
||||||
|
renamed "renameMe"
|
||||||
|
. "io"
|
||||||
|
_ "io"
|
||||||
|
"io"
|
||||||
|
. "os"
|
||||||
)
|
)
|
||||||
|
|
||||||
// no newlines between consecutive single imports, but
|
// no newlines between consecutive single imports, but
|
||||||
// respect extra line breaks in the source (at most one empty line)
|
// respect extra line breaks in the source (at most one empty line)
|
||||||
import _ "io"
|
import _ "io"
|
||||||
import _ "io"
|
import _ "io"
|
||||||
import _ "io"
|
import _ "io"
|
||||||
|
|
||||||
import _ "os"
|
import _ "os"
|
||||||
import _ "os"
|
import _ "os"
|
||||||
import _ "os"
|
import _ "os"
|
||||||
|
|
||||||
|
|
||||||
import _ "fmt"
|
import _ "fmt"
|
||||||
import _ "fmt"
|
import _ "fmt"
|
||||||
import _ "fmt"
|
import _ "fmt"
|
||||||
|
|
||||||
import "foo" // a comment
|
import "foo" // a comment
|
||||||
import "bar" // a comment
|
import "bar" // a comment
|
||||||
|
|
||||||
import (
|
import (
|
||||||
_ "foo"
|
_ "foo"
|
||||||
// a comment
|
// a comment
|
||||||
"bar"
|
"bar"
|
||||||
"foo" // a comment
|
"foo" // a comment
|
||||||
"bar" // a comment
|
"bar" // a comment
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// comments + renames
|
||||||
|
import (
|
||||||
|
"unrenamed" // a comment
|
||||||
|
renamed "renameMe"
|
||||||
|
. "io" /* a comment */
|
||||||
|
_ "io/ioutil" // a comment
|
||||||
|
"io" // testing alignment
|
||||||
|
. "os"
|
||||||
|
// a comment
|
||||||
|
)
|
||||||
|
|
||||||
// a case that caused problems in the past (comment placement)
|
// a case that caused problems in the past (comment placement)
|
||||||
import (
|
import (
|
||||||
. "fmt"
|
. "fmt"
|
||||||
"io"
|
"io"
|
||||||
"malloc" // for the malloc count test only
|
"malloc" // for the malloc count test only
|
||||||
"math"
|
"math"
|
||||||
@ -63,7 +83,7 @@ import (
|
|||||||
|
|
||||||
|
|
||||||
// at least one empty line between declarations of different kind
|
// at least one empty line between declarations of different kind
|
||||||
import _ "io"
|
import _ "io"
|
||||||
|
|
||||||
var _ int
|
var _ int
|
||||||
|
|
||||||
|
20
src/pkg/go/printer/testdata/declarations.input
vendored
20
src/pkg/go/printer/testdata/declarations.input
vendored
@ -25,6 +25,15 @@ import (
|
|||||||
b "io"
|
b "io"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
import (
|
||||||
|
"unrenamed"
|
||||||
|
renamed "renameMe"
|
||||||
|
. "io"
|
||||||
|
_ "io"
|
||||||
|
"io"
|
||||||
|
. "os"
|
||||||
|
)
|
||||||
|
|
||||||
// no newlines between consecutive single imports, but
|
// no newlines between consecutive single imports, but
|
||||||
// respect extra line breaks in the source (at most one empty line)
|
// respect extra line breaks in the source (at most one empty line)
|
||||||
import _ "io"
|
import _ "io"
|
||||||
@ -51,6 +60,17 @@ import (
|
|||||||
"bar" // a comment
|
"bar" // a comment
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// comments + renames
|
||||||
|
import (
|
||||||
|
"unrenamed" // a comment
|
||||||
|
renamed "renameMe"
|
||||||
|
. "io" /* a comment */
|
||||||
|
_ "io/ioutil" // a comment
|
||||||
|
"io" // testing alignment
|
||||||
|
. "os"
|
||||||
|
// a comment
|
||||||
|
)
|
||||||
|
|
||||||
// a case that caused problems in the past (comment placement)
|
// a case that caused problems in the past (comment placement)
|
||||||
import (
|
import (
|
||||||
. "fmt"
|
. "fmt"
|
||||||
|
Loading…
Reference in New Issue
Block a user