comment.go 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. package ast
  2. import (
  3. "strings"
  4. "github.com/zeromicro/go-zero/tools/goctl/pkg/parser/api/token"
  5. "github.com/zeromicro/go-zero/tools/goctl/util"
  6. )
  7. // CommentGroup represents a list of comments.
  8. type CommentGroup []*CommentStmt
  9. // List returns the list of comments.
  10. func (cg CommentGroup) List() []string {
  11. var list = make([]string, 0, len(cg))
  12. for _, v := range cg {
  13. comment := v.Comment.Text
  14. if util.IsEmptyStringOrWhiteSpace(comment) {
  15. continue
  16. }
  17. list = append(list, comment)
  18. }
  19. return list
  20. }
  21. // String joins and returns the comment text.
  22. func (cg CommentGroup) String() string {
  23. return cg.Join(" ")
  24. }
  25. // Join joins the comments with the given separator.
  26. func (cg CommentGroup) Join(sep string) string {
  27. if !cg.Valid() {
  28. return ""
  29. }
  30. list := cg.List()
  31. return strings.Join(list, sep)
  32. }
  33. // Valid returns true if the comment is valid.
  34. func (cg CommentGroup) Valid() bool {
  35. return len(cg) > 0
  36. }
  37. // CommentStmt represents a comment statement.
  38. type CommentStmt struct {
  39. // Comment is the comment token.
  40. Comment token.Token
  41. }
  42. func (c *CommentStmt) HasHeadCommentGroup() bool {
  43. return false
  44. }
  45. func (c *CommentStmt) HasLeadingCommentGroup() bool {
  46. return false
  47. }
  48. func (c *CommentStmt) CommentGroup() (head, leading CommentGroup) {
  49. return
  50. }
  51. func (c *CommentStmt) stmtNode() {}
  52. func (c *CommentStmt) Pos() token.Position {
  53. return c.Comment.Position
  54. }
  55. func (c *CommentStmt) End() token.Position {
  56. return c.Comment.Position
  57. }
  58. func (c *CommentStmt) Format(prefix ...string) string {
  59. return peekOne(prefix) + c.Comment.Text
  60. }