importstatement.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. package ast
  2. import "github.com/zeromicro/go-zero/tools/goctl/pkg/parser/api/token"
  3. // ImportStmt represents an import statement.
  4. type ImportStmt interface {
  5. Stmt
  6. importNode()
  7. }
  8. // ImportLiteralStmt represents an import literal statement.
  9. type ImportLiteralStmt struct {
  10. // Import is the import token.
  11. Import *TokenNode
  12. // Value is the import value.
  13. Value *TokenNode
  14. }
  15. func (i *ImportLiteralStmt) HasHeadCommentGroup() bool {
  16. return i.Import.HasHeadCommentGroup()
  17. }
  18. func (i *ImportLiteralStmt) HasLeadingCommentGroup() bool {
  19. return i.Value.HasLeadingCommentGroup()
  20. }
  21. func (i *ImportLiteralStmt) CommentGroup() (head, leading CommentGroup) {
  22. return i.Import.HeadCommentGroup, i.Value.LeadingCommentGroup
  23. }
  24. func (i *ImportLiteralStmt) Format(prefix ...string) (result string) {
  25. if i.Value.IsZeroString() {
  26. return ""
  27. }
  28. w := NewBufferWriter()
  29. importNode := transferTokenNode(i.Import, ignoreLeadingComment(), withTokenNodePrefix(prefix...))
  30. w.Write(withNode(importNode, i.Value), withMode(ModeExpectInSameLine))
  31. return w.String()
  32. }
  33. func (i *ImportLiteralStmt) End() token.Position {
  34. return i.Value.End()
  35. }
  36. func (i *ImportLiteralStmt) importNode() {}
  37. func (i *ImportLiteralStmt) Pos() token.Position {
  38. return i.Import.Pos()
  39. }
  40. func (i *ImportLiteralStmt) stmtNode() {}
  41. type ImportGroupStmt struct {
  42. // Import is the import token.
  43. Import *TokenNode
  44. // LParen is the left parenthesis token.
  45. LParen *TokenNode
  46. // Values is the import values.
  47. Values []*TokenNode
  48. // RParen is the right parenthesis token.
  49. RParen *TokenNode
  50. }
  51. func (i *ImportGroupStmt) HasHeadCommentGroup() bool {
  52. return i.Import.HasHeadCommentGroup()
  53. }
  54. func (i *ImportGroupStmt) HasLeadingCommentGroup() bool {
  55. return i.RParen.HasLeadingCommentGroup()
  56. }
  57. func (i *ImportGroupStmt) CommentGroup() (head, leading CommentGroup) {
  58. return i.Import.HeadCommentGroup, i.RParen.LeadingCommentGroup
  59. }
  60. func (i *ImportGroupStmt) Format(prefix ...string) string {
  61. var textList []string
  62. for _, v := range i.Values {
  63. if v.IsZeroString() {
  64. continue
  65. }
  66. textList = append(textList, v.Format(Indent))
  67. }
  68. if len(textList) == 0 {
  69. return ""
  70. }
  71. importNode := transferTokenNode(i.Import, ignoreLeadingComment(), withTokenNodePrefix(prefix...))
  72. w := NewBufferWriter()
  73. w.Write(withNode(importNode, i.LParen), expectSameLine())
  74. w.NewLine()
  75. for _, v := range i.Values {
  76. node := transferTokenNode(v, withTokenNodePrefix(peekOne(prefix)+Indent))
  77. w.Write(withNode(node), expectSameLine())
  78. w.NewLine()
  79. }
  80. w.Write(withNode(transferTokenNode(i.RParen, withTokenNodePrefix(prefix...))))
  81. return w.String()
  82. }
  83. func (i *ImportGroupStmt) End() token.Position {
  84. return i.RParen.End()
  85. }
  86. func (i *ImportGroupStmt) importNode() {}
  87. func (i *ImportGroupStmt) Pos() token.Position {
  88. return i.Import.Pos()
  89. }
  90. func (i *ImportGroupStmt) stmtNode() {}