This repository has been archived by the owner on Feb 17, 2021. It is now read-only.
forked from mdempsky/gocode
-
Notifications
You must be signed in to change notification settings - Fork 28
/
server.go
155 lines (137 loc) · 3.31 KB
/
server.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
package main
import (
"bytes"
"context"
"fmt"
"log"
"net"
"os"
"runtime/debug"
"strings"
"time"
"github.com/keegancsmith/rpc"
"github.com/stamblerre/gocode/internal/suggest"
)
func doServer(ctx context.Context, _ bool) {
for _, v := range strings.Fields(suggest.GoosList) {
suggest.KnownOS[v] = true
}
for _, v := range strings.Fields(suggest.GoarchList) {
suggest.KnownArch[v] = true
}
addr := *g_addr
if *g_sock == "unix" {
addr = getSocketPath()
}
lis, err := net.Listen(*g_sock, addr)
if err != nil {
log.Fatal(err)
}
go func() {
<-ctx.Done()
exitServer()
}()
if err = rpc.Register(&Server{ctx, false}); err != nil {
log.Fatal(err)
}
rpc.Accept(lis)
}
func exitServer() {
if *g_sock == "unix" {
_ = os.Remove(getSocketPath())
}
os.Exit(0)
}
type Server struct {
context context.Context
cache bool
}
type AutoCompleteRequest struct {
Filename string
Data []byte
Cursor int
Context *suggest.PackedContext
Source bool
Builtin bool
IgnoreCase bool
UnimportedPackages bool
FallbackToSource bool
}
type AutoCompleteReply struct {
Candidates []suggest.Candidate
Len int
}
func (s *Server) AutoComplete(ctx context.Context, req *AutoCompleteRequest, res *AutoCompleteReply) error {
defer func() {
if err := recover(); err != nil {
fmt.Printf("panic: %s\n\n", err)
debug.PrintStack()
res.Candidates = []suggest.Candidate{
{Class: "PANIC", Name: "PANIC", Type: "PANIC"},
}
}
}()
// cancel any pending request when server is shuting down
ctx, cancel := context.WithCancel(ctx)
defer cancel()
go func() {
select {
case <-ctx.Done():
cancel()
case <-s.context.Done():
cancel()
}
}()
if *g_debug {
var buf bytes.Buffer
log.Printf("Got autocompletion request for '%s'\n", req.Filename)
log.Printf("Cursor at: %d\n", req.Cursor)
if req.Cursor <= len(req.Data) {
buf.WriteString("-------------------------------------------------------\n")
buf.Write(req.Data[:req.Cursor])
buf.WriteString("#")
buf.Write(req.Data[req.Cursor:])
log.Print(buf.String())
log.Println("-------------------------------------------------------")
}
}
now := time.Now()
cfg := suggest.Config{
RequestContext: ctx,
Context: req.Context,
Builtin: req.Builtin,
IgnoreCase: req.IgnoreCase,
UnimportedPackages: req.UnimportedPackages,
Logf: func(string, ...interface{}) {},
}
cfg.Logf = func(string, ...interface{}) {}
if *g_debug {
cfg.Logf = log.Printf
}
candidates, d := cfg.Suggest(req.Filename, req.Data, req.Cursor)
if candidates == nil {
candidates = []suggest.Candidate{}
}
elapsed := time.Since(now)
if *g_debug {
log.Printf("Elapsed duration: %v\n", elapsed)
log.Printf("Offset: %d\n", res.Len)
log.Printf("Number of candidates found: %d\n", len(candidates))
log.Printf("Candidates are:\n")
for _, c := range candidates {
log.Printf(" %s\n", c.String())
}
log.Println("=======================================================")
}
res.Candidates, res.Len = candidates, d
return nil
}
type ExitRequest struct{}
type ExitReply struct{}
func (s *Server) Exit(ctx context.Context, req *ExitRequest, res *ExitReply) error {
go func() {
time.Sleep(time.Second)
exitServer()
}()
return nil
}