infostatement.go 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. package ast
  2. import "github.com/zeromicro/go-zero/tools/goctl/pkg/parser/api/token"
  3. // InfoStmt is the info statement.
  4. type InfoStmt struct {
  5. // Info is the info keyword.
  6. Info *TokenNode
  7. // LParen is the left parenthesis.
  8. LParen *TokenNode
  9. // Values is the info values.
  10. Values []*KVExpr
  11. // RParen is the right parenthesis.
  12. RParen *TokenNode
  13. }
  14. func (i *InfoStmt) HasHeadCommentGroup() bool {
  15. return i.Info.HasHeadCommentGroup()
  16. }
  17. func (i *InfoStmt) HasLeadingCommentGroup() bool {
  18. return i.RParen.HasLeadingCommentGroup()
  19. }
  20. func (i *InfoStmt) CommentGroup() (head, leading CommentGroup) {
  21. return i.Info.HeadCommentGroup, i.RParen.LeadingCommentGroup
  22. }
  23. func (i *InfoStmt) Format(prefix ...string) string {
  24. if len(i.Values) == 0 {
  25. return ""
  26. }
  27. var textList []string
  28. for _, v := range i.Values {
  29. if v.Value.IsZeroString() {
  30. continue
  31. }
  32. textList = append(textList, v.Format(Indent))
  33. }
  34. if len(textList) == 0 {
  35. return ""
  36. }
  37. w := NewBufferWriter()
  38. infoNode := transferTokenNode(i.Info, withTokenNodePrefix(prefix...), ignoreLeadingComment())
  39. w.Write(withNode(infoNode, i.LParen))
  40. w.NewLine()
  41. for _, v := range i.Values {
  42. node := transferTokenNode(v.Key, withTokenNodePrefix(peekOne(prefix)+Indent), ignoreLeadingComment())
  43. w.Write(withNode(node, v.Value), expectIndentInfix(), expectSameLine())
  44. w.NewLine()
  45. }
  46. w.Write(withNode(transferTokenNode(i.RParen, withTokenNodePrefix(prefix...))))
  47. return w.String()
  48. }
  49. func (i *InfoStmt) End() token.Position {
  50. return i.RParen.End()
  51. }
  52. func (i *InfoStmt) Pos() token.Position {
  53. return i.Info.Pos()
  54. }
  55. func (i *InfoStmt) stmtNode() {}