Skip to content

Commit

Permalink
Merge pull request #123 from sqls-server/line-break
Browse files Browse the repository at this point in the history
format with semicolon and comments
  • Loading branch information
mattn authored Dec 30, 2023
2 parents 973c119 + bff56e1 commit b74f719
Show file tree
Hide file tree
Showing 8 changed files with 90 additions and 44 deletions.
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

0 comments on commit b74f719

Please sign in to comment.