Skip to content

Commit

Permalink
Name Whitespace/Comments Channels; Skip Tokenless Nodes
Browse files Browse the repository at this point in the history
  • Loading branch information
cwarden committed Dec 12, 2023
1 parent 3a986a4 commit 096c738
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions formatter/visitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ import (
"github.com/octoberswimmer/apexfmt/parser"
)

const (
WHITESPACE_CHANNEL = 2
COMMENTS_CHANNEL = 3
)

type FormatVisitor struct {
tokens *antlr.CommonTokenStream
commentsOutput map[int]struct{}
Expand All @@ -27,8 +32,11 @@ func NewFormatVisitor(tokens *antlr.CommonTokenStream) *FormatVisitor {

func (v *FormatVisitor) visitRule(node antlr.RuleNode) interface{} {
start := node.(antlr.ParserRuleContext).GetStart()
beforeWhitespace := v.tokens.GetHiddenTokensToLeft(start.GetTokenIndex(), 2)
beforeComments := v.tokens.GetHiddenTokensToLeft(start.GetTokenIndex(), 3)
var beforeWhitespace, beforeComments []antlr.Token
if start != nil {
beforeWhitespace = v.tokens.GetHiddenTokensToLeft(start.GetTokenIndex(), WHITESPACE_CHANNEL)
beforeComments = v.tokens.GetHiddenTokensToLeft(start.GetTokenIndex(), COMMENTS_CHANNEL)
}
result := node.Accept(v)
if result == nil {
panic(fmt.Sprintf("MISSING VISIT FUNCTION FOR %T", node))
Expand Down

0 comments on commit 096c738

Please sign in to comment.