| 1 |
package ast |
| 2 |
|
| 3 |
import ( |
| 4 |
"fmt" |
| 5 |
|
| 6 |
"github.com/robertkrimen/otto/file" |
| 7 |
) |
| 8 |
|
| 9 |
|
| 10 |
type CommentPosition int |
| 11 |
|
| 12 |
|
| 13 |
const ( |
| 14 |
_ CommentPosition = iota |
| 15 |
|
| 16 |
LEADING |
| 17 |
|
| 18 |
TRAILING |
| 19 |
|
| 20 |
KEY |
| 21 |
|
| 22 |
COLON |
| 23 |
|
| 24 |
FINAL |
| 25 |
|
| 26 |
IF |
| 27 |
|
| 28 |
WHILE |
| 29 |
|
| 30 |
DO |
| 31 |
|
| 32 |
FOR |
| 33 |
|
| 34 |
WITH |
| 35 |
|
| 36 |
TBD |
| 37 |
) |
| 38 |
|
| 39 |
|
| 40 |
type Comment struct { |
| 41 |
Text string |
| 42 |
Begin file.Idx |
| 43 |
Position CommentPosition |
| 44 |
} |
| 45 |
|
| 46 |
|
| 47 |
func NewComment(text string, idx file.Idx) *Comment { |
| 48 |
comment := &Comment{ |
| 49 |
Begin: idx, |
| 50 |
Text: text, |
| 51 |
Position: TBD, |
| 52 |
} |
| 53 |
|
| 54 |
return comment |
| 55 |
} |
| 56 |
|
| 57 |
|
| 58 |
func (cp CommentPosition) String() string { |
| 59 |
switch cp { |
| 60 |
case LEADING: |
| 61 |
return "Leading" |
| 62 |
case TRAILING: |
| 63 |
return "Trailing" |
| 64 |
case KEY: |
| 65 |
return "Key" |
| 66 |
case COLON: |
| 67 |
return "Colon" |
| 68 |
case FINAL: |
| 69 |
return "Final" |
| 70 |
case IF: |
| 71 |
return "If" |
| 72 |
case WHILE: |
| 73 |
return "While" |
| 74 |
case DO: |
| 75 |
return "Do" |
| 76 |
case FOR: |
| 77 |
return "For" |
| 78 |
case WITH: |
| 79 |
return "With" |
| 80 |
default: |
| 81 |
return "???" |
| 82 |
} |
| 83 |
} |
| 84 |
|
| 85 |
|
| 86 |
func (c Comment) String() string { |
| 87 |
return fmt.Sprintf("Comment: %v", c.Text) |
| 88 |
} |
| 89 |
|
| 90 |
|
| 91 |
type Comments struct { |
| 92 |
|
| 93 |
CommentMap CommentMap |
| 94 |
|
| 95 |
Comments []*Comment |
| 96 |
|
| 97 |
Current Expression |
| 98 |
|
| 99 |
|
| 100 |
future []*Comment |
| 101 |
|
| 102 |
wasLineBreak bool |
| 103 |
|
| 104 |
primary bool |
| 105 |
|
| 106 |
afterBlock bool |
| 107 |
} |
| 108 |
|
| 109 |
|
| 110 |
func NewComments() *Comments { |
| 111 |
comments := &Comments{ |
| 112 |
CommentMap: CommentMap{}, |
| 113 |
} |
| 114 |
|
| 115 |
return comments |
| 116 |
} |
| 117 |
|
| 118 |
func (c *Comments) String() string { |
| 119 |
return fmt.Sprintf("NODE: %v, Comments: %v, Future: %v(LINEBREAK:%v)", c.Current, len(c.Comments), len(c.future), c.wasLineBreak) |
| 120 |
} |
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
func (c *Comments) FetchAll() []*Comment { |
| 125 |
defer func() { |
| 126 |
c.Comments = nil |
| 127 |
c.future = nil |
| 128 |
}() |
| 129 |
|
| 130 |
return append(c.Comments, c.future...) |
| 131 |
} |
| 132 |
|
| 133 |
|
| 134 |
func (c *Comments) Fetch() []*Comment { |
| 135 |
defer func() { |
| 136 |
c.Comments = nil |
| 137 |
}() |
| 138 |
|
| 139 |
return c.Comments |
| 140 |
} |
| 141 |
|
| 142 |
|
| 143 |
func (c *Comments) ResetLineBreak() { |
| 144 |
c.wasLineBreak = false |
| 145 |
} |
| 146 |
|
| 147 |
|
| 148 |
func (c *Comments) MarkPrimary() { |
| 149 |
c.primary = true |
| 150 |
c.wasLineBreak = false |
| 151 |
} |
| 152 |
|
| 153 |
|
| 154 |
func (c *Comments) AfterBlock() { |
| 155 |
c.afterBlock = true |
| 156 |
} |
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
func (c *Comments) AddComment(comment *Comment) { |
| 161 |
if c.primary { |
| 162 |
if !c.wasLineBreak { |
| 163 |
c.Comments = append(c.Comments, comment) |
| 164 |
} else { |
| 165 |
c.future = append(c.future, comment) |
| 166 |
} |
| 167 |
} else { |
| 168 |
if !c.wasLineBreak || (c.Current == nil && !c.afterBlock) { |
| 169 |
c.Comments = append(c.Comments, comment) |
| 170 |
} else { |
| 171 |
c.future = append(c.future, comment) |
| 172 |
} |
| 173 |
} |
| 174 |
} |
| 175 |
|
| 176 |
|
| 177 |
func (c *Comments) MarkComments(position CommentPosition) { |
| 178 |
for _, comment := range c.Comments { |
| 179 |
if comment.Position == TBD { |
| 180 |
comment.Position = position |
| 181 |
} |
| 182 |
} |
| 183 |
for _, c := range c.future { |
| 184 |
if c.Position == TBD { |
| 185 |
c.Position = position |
| 186 |
} |
| 187 |
} |
| 188 |
} |
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
func (c *Comments) Unset() { |
| 193 |
if c.Current != nil { |
| 194 |
c.applyComments(c.Current, c.Current, TRAILING) |
| 195 |
c.Current = nil |
| 196 |
} |
| 197 |
c.wasLineBreak = false |
| 198 |
c.primary = false |
| 199 |
c.afterBlock = false |
| 200 |
} |
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
func (c *Comments) SetExpression(node Expression) { |
| 206 |
|
| 207 |
if c.Current == node { |
| 208 |
return |
| 209 |
} |
| 210 |
if c.Current != nil && c.Current.Idx1() == node.Idx1() { |
| 211 |
c.Current = node |
| 212 |
return |
| 213 |
} |
| 214 |
previous := c.Current |
| 215 |
c.Current = node |
| 216 |
|
| 217 |
|
| 218 |
c.applyComments(node, previous, TRAILING) |
| 219 |
} |
| 220 |
|
| 221 |
|
| 222 |
func (c *Comments) PostProcessNode(node Node) { |
| 223 |
c.applyComments(node, nil, TRAILING) |
| 224 |
} |
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
func (c *Comments) applyComments(node, previous Node, position CommentPosition) { |
| 229 |
if previous != nil { |
| 230 |
c.CommentMap.AddComments(previous, c.Comments, position) |
| 231 |
c.Comments = nil |
| 232 |
} else { |
| 233 |
c.CommentMap.AddComments(node, c.Comments, position) |
| 234 |
c.Comments = nil |
| 235 |
} |
| 236 |
|
| 237 |
|
| 238 |
if previous != nil { |
| 239 |
c.CommentMap.AddComments(node, c.future, position) |
| 240 |
c.future = nil |
| 241 |
} |
| 242 |
} |
| 243 |
|
| 244 |
|
| 245 |
func (c *Comments) AtLineBreak() { |
| 246 |
c.wasLineBreak = true |
| 247 |
} |
| 248 |
|
| 249 |
|
| 250 |
type CommentMap map[Node][]*Comment |
| 251 |
|
| 252 |
|
| 253 |
func (cm CommentMap) AddComment(node Node, comment *Comment) { |
| 254 |
list := cm[node] |
| 255 |
list = append(list, comment) |
| 256 |
|
| 257 |
cm[node] = list |
| 258 |
} |
| 259 |
|
| 260 |
|
| 261 |
func (cm CommentMap) AddComments(node Node, comments []*Comment, position CommentPosition) { |
| 262 |
for _, comment := range comments { |
| 263 |
if comment.Position == TBD { |
| 264 |
comment.Position = position |
| 265 |
} |
| 266 |
cm.AddComment(node, comment) |
| 267 |
} |
| 268 |
} |
| 269 |
|
| 270 |
|
| 271 |
func (cm CommentMap) Size() int { |
| 272 |
size := 0 |
| 273 |
for _, comments := range cm { |
| 274 |
size += len(comments) |
| 275 |
} |
| 276 |
|
| 277 |
return size |
| 278 |
} |
| 279 |
|
| 280 |
|
| 281 |
func (cm CommentMap) MoveComments(from, to Node, position CommentPosition) { |
| 282 |
for i, c := range cm[from] { |
| 283 |
if c.Position == position { |
| 284 |
cm.AddComment(to, c) |
| 285 |
|
| 286 |
|
| 287 |
cm[from][i] = cm[from][len(cm[from])-1] |
| 288 |
cm[from][len(cm[from])-1] = nil |
| 289 |
cm[from] = cm[from][:len(cm[from])-1] |
| 290 |
} |
| 291 |
} |
| 292 |
} |