-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
150 lines (127 loc) · 2.96 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
package main
import (
"context"
"errors"
"flag"
"fmt"
"io"
"math/rand"
"os"
"path/filepath"
"time"
"github.com/joyme123/thrift-ls/format"
tlog "github.com/joyme123/thrift-ls/log"
"github.com/joyme123/thrift-ls/lsp"
"github.com/joyme123/thrift-ls/parser"
"github.com/joyme123/thrift-ls/utils/diff"
"go.lsp.dev/jsonrpc2"
"go.lsp.dev/pkg/fakenet"
"gopkg.in/yaml.v2"
)
type Options struct {
LogLevel int `yaml:"logLevel"` // 1: fatal, 2: error, 3: warn, 4: info, 5: debug, 6: trace
}
func main_format(opt format.Options, file string) error {
if file == "" {
err := errors.New("must specified a thrift file to format")
fmt.Println(err)
return err
}
content, err := os.ReadFile(file)
if err != nil {
fmt.Println(err)
return err
}
thrift_file := filepath.Base(file)
ast, err := parser.Parse(thrift_file, content)
if err != nil {
fmt.Println(err)
return err
}
formated, err := format.FormatDocumentWithValidation(ast.(*parser.Document), true)
if err != nil {
fmt.Println(err)
return err
}
if opt.Write {
var perms os.FileMode
fileInfo, err := os.Stat(file)
if err != nil {
fmt.Println(err)
return err
}
perms = fileInfo.Mode() // 使用原文件的权限
// overwrite
err = os.WriteFile(file, []byte(formated), perms)
if err != nil {
fmt.Println(err)
return err
}
} else {
if opt.Diff {
diffLines := diff.Diff("old", content, "new", []byte(formated))
fmt.Print(string(diffLines))
} else {
fmt.Print(formated)
}
return err
}
return nil
}
func main() {
rand.Seed(time.Now().UnixMilli())
formatter := false
formatFile := ""
flag.BoolVar(&formatter, "format", false, "use thrift-ls as a format tool")
flag.StringVar(&formatFile, "f", "", "file path to format")
formatOpts := format.Options{}
formatOpts.SetFlags()
flag.Parse()
formatOpts.InitDefault()
opts := configInit()
tlog.Init(opts.LogLevel)
if formatter {
main_format(formatOpts, formatFile)
os.Exit(1)
return
}
ctx := context.Background()
// server := &lsp.Server{}
// handler := protocol.ServerHandler(server, nil)
//
// streamServer := jsonrpc2.HandlerServer(handler)
// if err := jsonrpc2.ListenAndServe(ctx, "tcp", "127.0.0.1:8000", streamServer, 60*time.Second); err != nil {
// panic(err)
// }
ss := lsp.NewStreamServer()
stream := jsonrpc2.NewStream(fakenet.NewConn("stdio", os.Stdin, os.Stdout))
conn := jsonrpc2.NewConn(stream)
err := ss.ServeStream(ctx, conn)
if errors.Is(err, io.EOF) {
return
}
panic(err)
}
func configInit() *Options {
opts := &Options{}
logLevel := -1
flag.IntVar(&logLevel, "logLevel", -1, "set log level")
flag.Parse()
dir, err := os.UserHomeDir()
if err != nil {
dir = os.TempDir()
}
dir = dir + "/.thriftls"
configFile := dir + "/config.yaml"
data, err := os.ReadFile(configFile)
if err == nil {
yaml.Unmarshal(data, opts)
}
if logLevel >= 0 {
opts.LogLevel = logLevel // flag can override config file
}
if opts.LogLevel == 0 {
opts.LogLevel = 3
}
return opts
}