Skip to content

Commit

Permalink
Add formatting to upper
Browse files Browse the repository at this point in the history
  • Loading branch information
lighttiger2505 committed Jan 30, 2021
1 parent 944c885 commit 62678a0
Show file tree
Hide file tree
Showing 8 changed files with 79 additions and 28 deletions.
33 changes: 20 additions & 13 deletions ast/ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -236,14 +236,20 @@ type Identifer struct {
Tok *SQLToken
}

func (i *Identifer) Type() NodeType { return TypeIdentifer }
func (i *Identifer) String() string { return i.Tok.String() }
func (i *Identifer) Render(opts *RenderOptions) string { return i.Tok.Render(opts) }
func (i *Identifer) NoQuateString() string { return i.Tok.NoQuateString() }
func (i *Identifer) GetToken() *SQLToken { return i.Tok }
func (i *Identifer) Pos() token.Pos { return i.Tok.From }
func (i *Identifer) End() token.Pos { return i.Tok.To }
func (i *Identifer) IsWildcard() bool { return i.Tok.MatchKind(token.Mult) }
func (i *Identifer) Type() NodeType { return TypeIdentifer }
func (i *Identifer) String() string { return i.Tok.String() }
func (i *Identifer) Render(opts *RenderOptions) string {
tmpOpts := &RenderOptions{
LowerCase: false,
IdentiferQuated: opts.IdentiferQuated,
}
return i.Tok.Render(tmpOpts)
}
func (i *Identifer) NoQuateString() string { return i.Tok.NoQuateString() }
func (i *Identifer) GetToken() *SQLToken { return i.Tok }
func (i *Identifer) Pos() token.Pos { return i.Tok.From }
func (i *Identifer) End() token.Pos { return i.Tok.To }
func (i *Identifer) IsWildcard() bool { return i.Tok.MatchKind(token.Mult) }

type Operator struct {
Toks []Node
Expand Down Expand Up @@ -540,22 +546,23 @@ func (t *SQLToken) Render(opts *RenderOptions) string {
}

func renderSQLWord(v *token.SQLWord, opts *RenderOptions) string {
isKeyword := false
if v.Kind != dialect.Unmatched {
isKeyword = true
isIdentifer := false
if v.Kind == dialect.Unmatched {
isIdentifer = true
}

if isKeyword {
if isIdentifer {
if opts.IdentiferQuated {
v.QuoteStyle = '`'
return v.String()
}
return v.NoQuateString()
} else {
// is keyword
if opts.LowerCase {
return strings.ToLower(v.String())
}
return strings.ToLower(v.String())
return strings.ToUpper(v.String())
}
}

Expand Down
6 changes: 3 additions & 3 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,22 +24,22 @@ type Config struct {
Connections []*database.DBConfig `json:"connections" yaml:"connections"`
}

func newConfig() *Config {
func NewConfig() *Config {
cfg := &Config{}
cfg.LowercaseKeywords = false
return cfg
}

func GetDefaultConfig() (*Config, error) {
cfg := newConfig()
cfg := NewConfig()
if err := cfg.Load(ymlConfigPath); err != nil {
return nil, err
}
return cfg, nil
}

func GetConfig(fp string) (*Config, error) {
cfg := newConfig()
cfg := NewConfig()
expandPath, err := expand(fp)
if err != nil {
return nil, err
Expand Down
8 changes: 5 additions & 3 deletions internal/formatter/formatter.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@ import (

"github.com/lighttiger2505/sqls/ast"
"github.com/lighttiger2505/sqls/ast/astutil"
"github.com/lighttiger2505/sqls/internal/config"
"github.com/lighttiger2505/sqls/internal/debug"
"github.com/lighttiger2505/sqls/internal/lsp"
"github.com/lighttiger2505/sqls/parser"
"github.com/lighttiger2505/sqls/token"
)

func Format(text string, params lsp.DocumentFormattingParams) ([]lsp.TextEdit, error) {
func Format(text string, params lsp.DocumentFormattingParams, cfg *config.Config) ([]lsp.TextEdit, error) {
if text == "" {
return nil, errors.New("empty")
}
Expand All @@ -32,9 +34,9 @@ func Format(text string, params lsp.DocumentFormattingParams) ([]lsp.TextEdit, e
}
formatted := Eval(parsed, env)

debug.DPrintln("LowercaseKeywords", cfg.LowercaseKeywords)
opts := &ast.RenderOptions{
LowerCase: false,
IdentiferQuated: false,
LowerCase: cfg.LowercaseKeywords,
}
res := []lsp.TextEdit{
{
Expand Down
2 changes: 1 addition & 1 deletion internal/handler/format.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func (s *Server) handleTextDocumentFormatting(ctx context.Context, conn *jsonrpc
return nil, fmt.Errorf("document not found: %s", params.TextDocument.URI)
}

textEdits, err := formatter.Format(f.Text, params)
textEdits, err := formatter.Format(f.Text, params, s.getConfig())
if err != nil {
return nil, err
}
Expand Down
30 changes: 23 additions & 7 deletions internal/handler/format_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"testing"

"github.com/google/go-cmp/cmp"
"github.com/lighttiger2505/sqls/internal/config"
"github.com/lighttiger2505/sqls/internal/lsp"
)

Expand Down Expand Up @@ -36,15 +37,24 @@ var formattingOptionIndentSpace4 = lsp.FormattingOptions{
TrimFinalNewlines: false,
}

var upperCaseConfig = &config.Config{
LowercaseKeywords: false,
}

var lowerCaseConfig = &config.Config{
LowercaseKeywords: true,
}

type formattingTestCase struct {
name string
input string
want string
}

func testFormatting(t *testing.T, testCases []formattingTestCase, options lsp.FormattingOptions) {
func testFormatting(t *testing.T, testCases []formattingTestCase, options lsp.FormattingOptions, cfg *config.Config) {
tx := newTestContext()
tx.initServer(t)
tx.server.SpecificFileCfg = cfg
defer tx.tearDown()

uri := "file:///Users/octref/Code/css-test/test.sql"
Expand Down Expand Up @@ -91,7 +101,7 @@ func TestFormattingBase(t *testing.T) {
if err != nil {
t.Fatal(err)
}
testFormatting(t, testCase, formattingOptionTab)
testFormatting(t, testCase, formattingOptionTab, lowerCaseConfig)
}

func TestFormattingMinimal(t *testing.T) {
Expand Down Expand Up @@ -133,23 +143,31 @@ func TestFormattingMinimal(t *testing.T) {
want: "1,\n2,\n3,\n4",
},
}
testFormatting(t, minimalTestCase, formattingOptionTab)
testFormatting(t, minimalTestCase, formattingOptionTab, lowerCaseConfig)
}

func TestFormattingWithOptionSpace2(t *testing.T) {
testCase, err := loadFormatTestCaseByTestdata("format_option_space2")
if err != nil {
t.Fatal(err)
}
testFormatting(t, testCase, formattingOptionIndentSpace2)
testFormatting(t, testCase, formattingOptionIndentSpace2, lowerCaseConfig)
}

func TestFormattingWithOptionSpace4(t *testing.T) {
testCase, err := loadFormatTestCaseByTestdata("format_option_space4")
if err != nil {
t.Fatal(err)
}
testFormatting(t, testCase, formattingOptionIndentSpace4)
testFormatting(t, testCase, formattingOptionIndentSpace4, lowerCaseConfig)
}

func TestFormattingWithOptionUpper(t *testing.T) {
testCase, err := loadFormatTestCaseByTestdata("upper_case")
if err != nil {
t.Fatal(err)
}
testFormatting(t, testCase, formattingOptionTab, upperCaseConfig)
}

func loadFormatTestCaseByTestdata(targetDir string) ([]formattingTestCase, error) {
Expand Down Expand Up @@ -182,12 +200,10 @@ func loadFormatTestCaseByTestdata(targetDir string) ([]formattingTestCase, error
input, err := ioutil.ReadFile(inputPath)
if err != nil {
return nil, fmt.Errorf("Cannot read input file, Path=%s, Err=%+v", inputPath, err)
continue
}
golden, err := ioutil.ReadFile(goldenPath)
if err != nil {
return nil, fmt.Errorf("Cannot read input file, Path=%s, Err=%+v", goldenPath, err)
continue
}
testCase = append(testCase, formattingTestCase{
name: testName,
Expand Down
2 changes: 1 addition & 1 deletion internal/handler/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -376,7 +376,7 @@ func (s *Server) getConfig() *config.Config {
case validConfig(s.DefaultFileCfg):
cfg = s.DefaultFileCfg
default:
cfg = nil
cfg = config.NewConfig()
}
return cfg
}
Expand Down
19 changes: 19 additions & 0 deletions internal/handler/testdata/upper_case/select_basic.golden.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
SELECT
a,
b AS bb,
c
FROM
tbl
JOIN (
SELECT
a * 2 AS a
FROM
new_table
) other
ON tbl.a = other.a
WHERE
c IS TRUE
AND b BETWEEN 3
AND 4
OR d IS 'blue'
LIMIT 10
7 changes: 7 additions & 0 deletions internal/handler/testdata/upper_case/select_basic.input.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
select a, b as bb,c from tbl
join (select a * 2 as a from new_table) other
on tbl.a = other.a
where c is true
and b between 3 and 4
or d is 'blue'
limit 10

0 comments on commit 62678a0

Please sign in to comment.