forked from nsf/gocode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
170 lines (146 loc) · 3.3 KB
/
client.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
package main
import (
"flag"
"fmt"
"io/ioutil"
"net/rpc"
"os"
"path/filepath"
"strconv"
"time"
)
func do_client() int {
addr := *g_addr
if *g_sock == "unix" {
addr = get_socket_filename()
}
// client
client, err := rpc.Dial(*g_sock, addr)
if err != nil {
if *g_sock == "unix" && file_exists(addr) {
os.Remove(addr)
}
err = try_run_server()
if err != nil {
fmt.Printf("%s\n", err.Error())
return 1
}
client, err = try_to_connect(*g_sock, addr)
if err != nil {
fmt.Printf("%s\n", err.Error())
return 1
}
}
defer client.Close()
if flag.NArg() > 0 {
switch flag.Arg(0) {
case "autocomplete":
cmd_auto_complete(client)
case "cursortype":
cmd_cursor_type_pkg(client)
case "close":
cmd_close(client)
case "status":
cmd_status(client)
case "drop-cache":
cmd_drop_cache(client)
case "set":
cmd_set(client)
}
}
return 0
}
func try_run_server() error {
path := get_executable_filename()
args := []string{os.Args[0], "-s", "-sock", *g_sock, "-addr", *g_addr}
cwd, _ := os.Getwd()
procattr := os.ProcAttr{Dir: cwd, Env: os.Environ(), Files: []*os.File{nil, nil, nil}}
p, err := os.StartProcess(path, args, &procattr)
if err != nil {
return err
}
return p.Release()
}
func try_to_connect(network, address string) (client *rpc.Client, err error) {
t := 0
for {
client, err = rpc.Dial(network, address)
if err != nil && t < 1000 {
time.Sleep(10 * time.Millisecond)
t += 10
continue
}
break
}
return
}
func prepare_file_filename_cursor() ([]byte, string, int) {
var file []byte
var err error
if *g_input != "" {
file, err = ioutil.ReadFile(*g_input)
} else {
file, err = ioutil.ReadAll(os.Stdin)
}
if err != nil {
panic(err.Error())
}
var skipped int
file, skipped = filter_out_shebang(file)
filename := *g_input
cursor := -1
offset := ""
switch flag.NArg() {
case 2:
offset = flag.Arg(1)
case 3:
filename = flag.Arg(1) // Override default filename
offset = flag.Arg(2)
}
if offset != "" {
if offset[0] == 'c' || offset[0] == 'C' {
cursor, _ = strconv.Atoi(offset[1:])
cursor = char_to_byte_offset(file, cursor)
} else {
cursor, _ = strconv.Atoi(offset)
}
}
cursor -= skipped
if filename != "" && !filepath.IsAbs(filename) {
cwd, _ := os.Getwd()
filename = filepath.Join(cwd, filename)
}
return file, filename, cursor
}
//-------------------------------------------------------------------------
// commands
//-------------------------------------------------------------------------
func cmd_status(c *rpc.Client) {
fmt.Printf("%s\n", client_status(c, 0))
}
func cmd_auto_complete(c *rpc.Client) {
file, filename, cursor := prepare_file_filename_cursor()
f := get_formatter(*g_format)
f.write_candidates(client_auto_complete(c, file, filename, cursor))
}
func cmd_cursor_type_pkg(c *rpc.Client) {
file, filename, cursor := prepare_file_filename_cursor()
typ, pkg := client_cursor_type_pkg(c, file, filename, cursor)
fmt.Printf("%s,,%s\n", typ, pkg)
}
func cmd_close(c *rpc.Client) {
client_close(c, 0)
}
func cmd_drop_cache(c *rpc.Client) {
client_drop_cache(c, 0)
}
func cmd_set(c *rpc.Client) {
switch flag.NArg() {
case 1:
fmt.Print(client_set(c, "\x00", "\x00"))
case 2:
fmt.Print(client_set(c, flag.Arg(1), "\x00"))
case 3:
fmt.Print(client_set(c, flag.Arg(1), flag.Arg(2)))
}
}