forked from vsapronov/gopoetry
/
statements.go
94 lines (80 loc) · 2.12 KB
/
statements.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
package scala
type StatementsDeclaration struct {
statements []Writable
block bool
scope bool
}
func (self *StatementsDeclaration) Add(code ...Writable) *StatementsDeclaration {
self.statements = append(self.statements, code...)
return self
}
func (self *StatementsDeclaration) Code(codeFormat string, args ...interface{}) *WritableCode {
code := Code(codeFormat, args...)
self.Add(code)
return code
}
func (self *StatementsDeclaration) Line(codeFormat string, args ...interface{}) *WritableCode {
line := Line(codeFormat, args...)
self.Add(line)
return line
}
func (self *StatementsDeclaration) Def(name string) *MethodDeclaration {
method := Def(name)
self.Add(method)
return method
}
func (self *StatementsDeclaration) Val(name string, type_ string) *FieldDeclaration {
field := Val(name, type_)
self.Add(field)
return field
}
func (self *StatementsDeclaration) Var(name string, type_ string) *FieldDeclaration {
field := Var(name, type_)
self.Add(field)
return field
}
func (self *StatementsDeclaration) Eol() *EolDefinition {
eol := Eol()
self.Add(eol)
return eol
}
func Statements(statements ...Writable) *StatementsDeclaration {
return &StatementsDeclaration{statements: statements, block: false, scope: false}
}
func Block(statements ...Writable) *StatementsDeclaration {
return &StatementsDeclaration{statements: statements, block: true, scope: false}
}
func Scope(statements ...Writable) *StatementsDeclaration {
return &StatementsDeclaration{statements: statements, block: true, scope: true}
}
func ScopeInline(statements ...Writable) *StatementsDeclaration {
return &StatementsDeclaration{statements: statements, block: false, scope: true}
}
func (self *StatementsDeclaration) WriteCode(writer CodeWriter) {
if self.scope {
writer.Write("{")
if self.block {
writer.Eol()
} else {
writer.Write(" ")
}
}
if self.block {
writer.Indent()
}
for _, statement := range self.statements {
if statement != nil {
statement.WriteCode(writer)
}
}
if self.block {
writer.UnIndent()
}
if self.scope {
if !self.block {
writer.Write(" ")
}
writer.Write("}")
writer.Eol()
}
}