Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

format with semicolon and comments #123

Merged
merged 9 commits into from
Dec 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions ast/ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -517,6 +517,12 @@ func (t *SQLToken) String() string {
case *token.SQLWord:
return v.String()
case string:
if t.Kind == token.Comment {
return "--" + v
}
if t.Kind == token.MultilineComment {
return "/*" + v + "*/"
}
return v
default:
return " "
Expand All @@ -528,6 +534,12 @@ func (t *SQLToken) NoQuoteString() string {
case *token.SQLWord:
return v.NoQuoteString()
case string:
if t.Kind == token.Comment {
return "--" + v
}
if t.Kind == token.MultilineComment {
return "/*" + v + "*/"
}
return v
default:
return " "
Expand All @@ -539,6 +551,12 @@ func (t *SQLToken) Render(opts *RenderOptions) string {
case *token.SQLWord:
return renderSQLWord(v, opts)
case string:
if t.Kind == token.Comment {
return "--" + v
}
if t.Kind == token.MultilineComment {
return "/*" + v + "*/"
}
return v
default:
return " "
Expand Down
18 changes: 18 additions & 0 deletions internal/formatter/formatter.go
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,24 @@ func formatItem(node ast.Node, env *formatEnvironment) ast.Node {
results = append(results, linebreakNode)
results = append(results, env.genIndent()...)
}
commentAfterMatcher := astutil.NodeMatcher{
ExpectTokens: []token.Kind{
token.Comment,
},
}
if commentAfterMatcher.IsMatch(node) {
results = append(results, linebreakNode)
}

breakStatementAfterMatcher := astutil.NodeMatcher{
ExpectTokens: []token.Kind{
token.Semicolon,
},
}
if breakStatementAfterMatcher.IsMatch(node) {
results = append(results, linebreakNode)
env.indentLevelDown()
}

return &ast.ItemWith{Toks: results}
}
Expand Down
14 changes: 10 additions & 4 deletions parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,6 @@ func NewParser(src io.Reader, d dialect.Dialect) (*Parser, error) {

parsed := []ast.Node{}
for _, tok := range tokens {
if tok.Kind == token.Comment {
continue
}
parsed = append(parsed, ast.NewItem(tok))
}

Expand Down Expand Up @@ -529,6 +526,12 @@ func parseAliased(reader *astutil.NodeReader) ast.Node {
}
}

var commentInfixMatcher = astutil.NodeMatcher{
ExpectTokens: []token.Kind{
token.Comment,
token.MultilineComment,
},
}
var identifierListInfixMatcher = astutil.NodeMatcher{
ExpectTokens: []token.Kind{
token.Comma,
Expand Down Expand Up @@ -570,7 +573,7 @@ func parseIdentifierList(reader *astutil.NodeReader) ast.Node {
peekNode ast.Node
)
for {
if !tmpReader.PeekNodeIs(true, identifierListTargetMatcher) {
if !tmpReader.PeekNodeIs(true, identifierListTargetMatcher) && !tmpReader.PeekNodeIs(true, commentInfixMatcher) {
// Include white space after the comma
peekIndex, peekNode := tmpReader.PeekNode(true)
if peekNode != nil {
Expand All @@ -584,6 +587,9 @@ func parseIdentifierList(reader *astutil.NodeReader) ast.Node {
}
break
}
for tmpReader.PeekNodeIs(true, commentInfixMatcher) {
tmpReader.NextNode(true)
}

peekIndex, peekNode = tmpReader.PeekNode(true)
idents = append(idents, peekNode)
Expand Down
21 changes: 11 additions & 10 deletions parser/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,42 +84,43 @@ func TestParseComments(t *testing.T) {
name: "line comment with identiger",
input: "-- foo\nbar",
checkFn: func(t *testing.T, stmts []*ast.Statement, input string) {
testStatement(t, stmts[0], 2, "\nbar")
testStatement(t, stmts[0], 3, "-- foo\nbar")

list := stmts[0].GetTokens()
testItem(t, list[0], "\n")
testIdentifier(t, list[1], "bar")
testItem(t, list[0], "-- foo")
testIdentifier(t, list[2], "bar")
},
},
{
name: "range comment with identiger",
input: "/* foo */bar",
checkFn: func(t *testing.T, stmts []*ast.Statement, input string) {
testStatement(t, stmts[0], 1, "bar")
testStatement(t, stmts[0], 2, "/* foo */bar")

list := stmts[0].GetTokens()
testIdentifier(t, list[0], "bar")
testIdentifier(t, list[1], "bar")
},
},
{
name: "range comment with identiger list",
input: "foo, /* foo */bar",
checkFn: func(t *testing.T, stmts []*ast.Statement, input string) {
testStatement(t, stmts[0], 1, "foo, bar")
testStatement(t, stmts[0], 1, "foo, /* foo */bar")

list := stmts[0].GetTokens()
testIdentifierList(t, list[0], "foo, bar")
testIdentifierList(t, list[0], "foo, /* foo */bar")
},
},
{
name: "multi line range comment with identiger",
input: "/*\n * foo\n */\nbar",
checkFn: func(t *testing.T, stmts []*ast.Statement, input string) {
testStatement(t, stmts[0], 2, "\nbar")
testStatement(t, stmts[0], 3, "/*\n foo\n */\nbar")

list := stmts[0].GetTokens()
testItem(t, list[0], "\n")
testIdentifier(t, list[1], "bar")
testItem(t, list[0], "/*\n foo\n */")
testItem(t, list[1], "\n")
testIdentifier(t, list[2], "bar")
},
},
}
Expand Down
2 changes: 2 additions & 0 deletions token/kind.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ const (
Whitespace
// comment node
Comment
// multiline comment node
MultilineComment
// = operator
Eq
// != or <> operator
Expand Down
57 changes: 29 additions & 28 deletions token/kind_string.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion token/lexer.go
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ func (t *Tokenizer) next() (Kind, interface{}, error) {
if err != nil {
return ILLEGAL, str, err
}
return Comment, str, nil
return MultilineComment, str, nil
}
t.Col++
return Div, "/", nil
Expand Down
2 changes: 1 addition & 1 deletion token/lexer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -462,7 +462,7 @@ multiline
comment */`,
out: []*Token{
{
Kind: Comment,
Kind: MultilineComment,
Value: " test\nmultiline\ncomment ",
From: Pos{Line: 0, Col: 0},
To: Pos{Line: 2, Col: 10},
Expand Down