Skip to content

Commit

Permalink
Merge pull request #14 from dave/space-type
Browse files Browse the repository at this point in the history
Space type
  • Loading branch information
dave committed Oct 10, 2018
2 parents 1348546 + eca8154 commit a4cda7a
Show file tree
Hide file tree
Showing 20 changed files with 1,260 additions and 616 deletions.
40 changes: 26 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,51 +13,63 @@ comments don't remain attached to the correct nodes:
```go
code := `package a
var a int // foo
var b string // bar
`
func main() {
var a int // foo
var b string // bar
}`
fset := token.NewFileSet()
f, err := parser.ParseFile(fset, "a.go", code, parser.ParseComments)
if err != nil {
panic(err)
}
f.Decls = []ast.Decl{f.Decls[1], f.Decls[0]}

body := f.Decls[0].(*ast.FuncDecl).Body
body.List = []ast.Stmt{body.List[1], body.List[0]}

if err := format.Node(os.Stdout, fset, f); err != nil {
panic(err)
}

//Output:
//package a
//
//// foo
//var b string
//var a int
//
//// bar
//func main() {
// // foo
// var b string
// var a int
// // bar
//}
```

Here's the same example using `dst`:

```go
code := `package a
var a int // foo
var b string // bar
func main() {
var a int // foo
var b string // bar
}
`
f, err := decorator.Parse(code)
if err != nil {
panic(err)
}
f.Decls = []dst.Decl{f.Decls[1], f.Decls[0]}

body := f.Decls[0].(*dst.FuncDecl).Body
body.List = []dst.Stmt{body.List[1], body.List[0]}

if err := decorator.Print(f); err != nil {
panic(err)
}

//Output:
//package a
//
//var b string // bar
//var a int // foo
//func main() {
// var b string // bar
// var a int // foo
//}
```

### Example
Expand Down
8 changes: 8 additions & 0 deletions decorations.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,11 @@ type DecorationStmtDecorations struct {
type DecorationDeclDecorations struct {
Start Decorations
}

type SpaceType int

const (
None SpaceType = 0
NewLine SpaceType = 1
EmptyLine SpaceType = 2
)
40 changes: 26 additions & 14 deletions decorations_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,47 +57,59 @@ func ExampleDecorations() {
func ExampleAstBroken() {
code := `package a
var a int // foo
var b string // bar
`
func main() {
var a int // foo
var b string // bar
}`
fset := token.NewFileSet()
f, err := parser.ParseFile(fset, "a.go", code, parser.ParseComments)
if err != nil {
panic(err)
}
f.Decls = []ast.Decl{f.Decls[1], f.Decls[0]}

body := f.Decls[0].(*ast.FuncDecl).Body
body.List = []ast.Stmt{body.List[1], body.List[0]}

if err := format.Node(os.Stdout, fset, f); err != nil {
panic(err)
}

//Output:
//package a
//
//// foo
//var b string
//var a int
//
//// bar
//func main() {
// // foo
// var b string
// var a int
// // bar
//}
}

func ExampleDstFixed() {
code := `package a
var a int // foo
var b string // bar
func main() {
var a int // foo
var b string // bar
}
`
f, err := decorator.Parse(code)
if err != nil {
panic(err)
}
f.Decls = []dst.Decl{f.Decls[1], f.Decls[0]}

body := f.Decls[0].(*dst.FuncDecl).Body
body.List = []dst.Stmt{body.List[1], body.List[0]}

if err := decorator.Print(f); err != nil {
panic(err)
}

//Output:
//package a
//
//var b string // bar
//var a int // foo
//func main() {
// var b string // bar
// var a int // foo
//}
}
Loading

0 comments on commit a4cda7a

Please sign in to comment.