Skip to content

Commit

Permalink
cl: fix ast.GenDecl pos for commentStmtEx
Browse files Browse the repository at this point in the history
  • Loading branch information
visualfc committed Feb 22, 2024
1 parent 1f534da commit d54d6d3
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 0 deletions.
69 changes: 69 additions & 0 deletions cl/compile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4269,3 +4269,72 @@ func main() {
}
`)
}

func TestCommentVar(t *testing.T) {
gopClTestEx(t, gblConfLine, "main", `
// doc a line2
var a int
println a
// doc b line6
var b int
println b
var c int
println c
`, `package main
import "fmt"
// doc a line2
var a int
//line /foo/bar.gop:4
func main() {
//line /foo/bar.gop:4:1
fmt.Println(a)
//line /foo/bar.gop:6:1
// doc b line6
var b int
//line /foo/bar.gop:8:1
fmt.Println(b)
//line /foo/bar.gop:10:1
var c int
//line /foo/bar.gop:11:1
fmt.Println(c)
}
`)

gopClTestEx(t, gblConfLine, "main", `
func demo() {
// doc a line3
var a int
println a
// doc b line7
var b int
println b
var c int
println c
}
`, `package main
import "fmt"
//line /foo/bar.gop:2:1
func demo() {
//line /foo/bar.gop:3:1
// doc a line3
var a int
//line /foo/bar.gop:5:1
fmt.Println(a)
//line /foo/bar.gop:7:1
// doc b line7
var b int
//line /foo/bar.gop:9:1
fmt.Println(b)
//line /foo/bar.gop:11:1
var c int
//line /foo/bar.gop:12:1
fmt.Println(c)
}
`)
}
12 changes: 12 additions & 0 deletions cl/stmt.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ func commentStmt(ctx *blockCtx, stmt ast.Stmt) {

func commentStmtEx(cb *gox.CodeBuilder, ctx *pkgCtx, stmt ast.Stmt) {
start := stmt.Pos()
if doc := checkStmtDoc(stmt); doc != nil {
start = doc.Pos()
}
pos := ctx.fset.Position(start)
if ctx.relBaseDir != "" {
pos.Filename = fileLineFile(ctx.relBaseDir, pos.Filename)
Expand All @@ -64,6 +67,15 @@ func commentStmtEx(cb *gox.CodeBuilder, ctx *pkgCtx, stmt ast.Stmt) {
cb.SetComments(comments, false)
}

func checkStmtDoc(stmt ast.Stmt) *ast.CommentGroup {
if decl, ok := stmt.(*ast.DeclStmt); ok {
if d, ok := decl.Decl.(*ast.GenDecl); ok {
return d.Doc
}
}
return nil
}

func commentFunc(ctx *blockCtx, fn *gox.Func, decl *ast.FuncDecl) {
start := decl.Name.Pos()
if ctx.fileLine && start != token.NoPos {
Expand Down

0 comments on commit d54d6d3

Please sign in to comment.