@@ -10,148 +10,12 @@ package main // import "golang.org/x/tools/cmd/golsp"
10
10
11
11
import (
12
12
"context"
13
- "encoding/json"
14
- "flag"
15
- "fmt"
16
- "io"
17
- "log"
18
13
"os"
19
- "path/filepath"
20
- "runtime"
21
- "runtime/pprof"
22
- "runtime/trace"
23
- "strings"
24
- "time"
25
14
26
- "golang.org/x/tools/internal/jsonrpc2"
27
- "golang.org/x/tools/internal/lsp"
28
- )
29
-
30
- var (
31
- cpuprofile = flag .String ("cpuprofile" , "" , "write CPU profile to this file" )
32
- memprofile = flag .String ("memprofile" , "" , "write memory profile to this file" )
33
- traceFlag = flag .String ("trace" , "" , "write trace log to this file" )
34
- logfile = flag .String ("logfile" , "" , "filename to log to. if value is \" auto\" , then logging to a default output file is enabled" )
35
-
36
- // Flags for compatitibility with VSCode.
37
- mode = flag .String ("mode" , "" , "no effect" )
15
+ "golang.org/x/tools/internal/lsp/cmd"
16
+ "golang.org/x/tools/internal/tool"
38
17
)
39
18
40
19
func main () {
41
- flag .Usage = func () {
42
- fmt .Fprintf (os .Stderr , "usage: golsp [flags]\n " )
43
- flag .PrintDefaults ()
44
- }
45
- flag .Parse ()
46
- if flag .NArg () > 0 {
47
- flag .Usage ()
48
- os .Exit (2 )
49
- }
50
-
51
- if * cpuprofile != "" {
52
- f , err := os .Create (* cpuprofile )
53
- if err != nil {
54
- log .Fatal (err )
55
- }
56
- if err := pprof .StartCPUProfile (f ); err != nil {
57
- log .Fatal (err )
58
- }
59
- // NB: profile won't be written in case of error.
60
- defer pprof .StopCPUProfile ()
61
- }
62
-
63
- if * traceFlag != "" {
64
- f , err := os .Create (* traceFlag )
65
- if err != nil {
66
- log .Fatal (err )
67
- }
68
- if err := trace .Start (f ); err != nil {
69
- log .Fatal (err )
70
- }
71
- // NB: trace log won't be written in case of error.
72
- defer func () {
73
- trace .Stop ()
74
- log .Printf ("To view the trace, run:\n $ go tool trace view %s" , * traceFlag )
75
- }()
76
- }
77
-
78
- if * memprofile != "" {
79
- f , err := os .Create (* memprofile )
80
- if err != nil {
81
- log .Fatal (err )
82
- }
83
- // NB: memprofile won't be written in case of error.
84
- defer func () {
85
- runtime .GC () // get up-to-date statistics
86
- if err := pprof .WriteHeapProfile (f ); err != nil {
87
- log .Fatalf ("Writing memory profile: %v" , err )
88
- }
89
- f .Close ()
90
- }()
91
- }
92
-
93
- out := os .Stderr
94
- if * logfile != "" {
95
- filename := * logfile
96
- if filename == "auto" {
97
- filename = filepath .Join (os .TempDir (), fmt .Sprintf ("golsp-%d.log" , os .Getpid ()))
98
- }
99
- f , err := os .Create (filename )
100
- if err != nil {
101
- log .Fatalf ("Unable to create log file: %v" , err )
102
- }
103
- defer f .Close ()
104
- log .SetOutput (io .MultiWriter (os .Stderr , f ))
105
- out = f
106
- }
107
- if err := lsp .RunServer (
108
- context .Background (),
109
- jsonrpc2 .NewHeaderStream (os .Stdin , os .Stdout ),
110
- func (direction jsonrpc2.Direction , id * jsonrpc2.ID , elapsed time.Duration , method string , payload * json.RawMessage , err * jsonrpc2.Error ) {
111
-
112
- const eol = "\r \n \r \n \r \n "
113
- if err != nil {
114
- fmt .Fprintf (out , "[Error - %v] %s %s%s %v%s" , time .Now ().Format ("3:04:05 PM" ),
115
- direction , method , id , err , eol )
116
- return
117
- }
118
- outx := new (strings.Builder )
119
- fmt .Fprintf (outx , "[Trace - %v] " , time .Now ().Format ("3:04:05 PM" ))
120
- switch direction {
121
- case jsonrpc2 .Send :
122
- fmt .Fprint (outx , "Received " )
123
- case jsonrpc2 .Receive :
124
- fmt .Fprint (outx , "Sending " )
125
- }
126
- switch {
127
- case id == nil :
128
- fmt .Fprint (outx , "notification " )
129
- case elapsed >= 0 :
130
- fmt .Fprint (outx , "response " )
131
- default :
132
- fmt .Fprint (outx , "request " )
133
- }
134
- fmt .Fprintf (outx , "'%s" , method )
135
- switch {
136
- case id == nil :
137
- // do nothing
138
- case id .Name != "" :
139
- fmt .Fprintf (outx , " - (%s)" , id .Name )
140
- default :
141
- fmt .Fprintf (outx , " - (%d)" , id .Number )
142
- }
143
- fmt .Fprint (outx , "'" )
144
- if elapsed >= 0 {
145
- fmt .Fprintf (outx , " in %vms" , elapsed .Nanoseconds ()/ 1000 )
146
- }
147
- params := string (* payload )
148
- if params == "null" {
149
- params = "{}"
150
- }
151
- fmt .Fprintf (outx , ".\r \n Params: %s%s" , params , eol )
152
- fmt .Fprintf (out , "%s" , outx .String ())
153
- },
154
- ); err != nil {
155
- log .Fatal (err )
156
- }
20
+ tool .Main (context .Background (), & cmd.Application {}, os .Args [1 :])
157
21
}
0 commit comments