/
server.go
201 lines (191 loc) · 7.38 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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
package langserver
import (
"context"
"os"
"runtime"
"strings"
"github.com/dbaumgarten/yodk/pkg/jsonrpc2"
"github.com/dbaumgarten/yodk/pkg/lsp"
)
type LangServer struct {
client lsp.Client
cache *Cache
settings *Settings
}
func Run(ctx context.Context, stream jsonrpc2.Stream, enableHotkeys bool, opts ...interface{}) error {
s := &LangServer{}
conn, client := lsp.RunServer(ctx, stream, s, opts...)
s.client = client
s.cache = NewCache()
s.settings = DefaultSettings()
if enableHotkeys {
// Register the global hotkeys
s.ListenForHotkeys()
}
return conn.Wait(ctx)
}
func unsupported() error {
return jsonrpc2.NewErrorf(jsonrpc2.CodeMethodNotFound, "method %q not yet implemented", callingFunctionName())
}
// get the name of the funtion that called the function that called this function
func callingFunctionName() string {
fpcs := make([]uintptr, 1)
// Skip 3 levels to get the caller
n := runtime.Callers(3, fpcs)
if n == 0 {
return ""
}
caller := runtime.FuncForPC(fpcs[0] - 1)
if caller == nil {
return ""
}
return caller.Name()
}
func (ls *LangServer) storeLastOpenedScript(uri string) {
if strings.HasSuffix(uri, ".yolol") {
ls.cache.Lock.Lock()
ls.cache.LastOpenedYololFile = lsp.DocumentURI(uri)
ls.cache.Lock.Unlock()
}
}
func (ls *LangServer) Initialize(ctx context.Context, params *lsp.InitializeParams) (*lsp.InitializeResult, error) {
return &lsp.InitializeResult{
Capabilities: lsp.ServerCapabilities{
TextDocumentSync: lsp.TextDocumentSyncOptions{
Change: float64(lsp.Full), // full contents of file sent on each update
OpenClose: true,
},
DocumentFormattingProvider: true,
CompletionProvider: &lsp.CompletionOptions{
TriggerCharacters: []string{" ", ":", "+", "-", "*", "/", "%", "=", "^", ">", "<"},
},
},
}, nil
}
func (ls *LangServer) Initialized(ctx context.Context, params *lsp.InitializedParams) error {
return nil
}
func (ls *LangServer) Shutdown(ctx context.Context) error {
return nil
}
func (ls *LangServer) Exit(ctx context.Context) error {
os.Exit(0)
return unsupported()
}
func (ls *LangServer) DidChangeWorkspaceFolders(ctx context.Context, params *lsp.DidChangeWorkspaceFoldersParams) error {
return unsupported()
}
func (ls *LangServer) DidChangeConfiguration(ctx context.Context, params *lsp.DidChangeConfigurationParams) error {
return ls.settings.Read(params.Settings)
}
func (ls *LangServer) DidChangeWatchedFiles(ctx context.Context, params *lsp.DidChangeWatchedFilesParams) error {
return unsupported()
}
func (ls *LangServer) Symbols(ctx context.Context, params *lsp.WorkspaceSymbolParams) ([]lsp.SymbolInformation, error) {
return nil, unsupported()
}
func (ls *LangServer) ExecuteCommand(ctx context.Context, params *lsp.ExecuteCommandParams) (interface{}, error) {
if params.Command == "activeDocument" {
if len(params.Arguments) == 1 {
if argstr, is := params.Arguments[0].(string); is {
ls.storeLastOpenedScript(argstr)
return nil, nil
}
}
}
return nil, unsupported()
}
func (ls *LangServer) DidOpen(ctx context.Context, params *lsp.DidOpenTextDocumentParams) error {
ls.cache.Set(params.TextDocument.URI, params.TextDocument.Text)
ls.storeLastOpenedScript(string(params.TextDocument.URI))
ls.Diagnose(ctx, params.TextDocument.URI)
return nil
}
func (ls *LangServer) DidChange(ctx context.Context, params *lsp.DidChangeTextDocumentParams) error {
// We expect the full content of file, i.e. a single change with no range.
if change := params.ContentChanges[0]; change.RangeLength == 0 {
ls.cache.Set(params.TextDocument.URI, change.Text)
ls.Diagnose(ctx, params.TextDocument.URI)
}
return nil
}
func (ls *LangServer) WillSave(ctx context.Context, params *lsp.WillSaveTextDocumentParams) error {
return unsupported()
}
func (ls *LangServer) WillSaveWaitUntil(ctx context.Context, params *lsp.WillSaveTextDocumentParams) ([]lsp.TextEdit, error) {
return nil, unsupported()
}
func (ls *LangServer) DidSave(ctx context.Context, params *lsp.DidSaveTextDocumentParams) error {
// vscode does not care if the server supports this or not
// ignore it instead of logging an error
return nil
}
func (ls *LangServer) DidClose(ctx context.Context, params *lsp.DidCloseTextDocumentParams) error {
return unsupported()
}
func (ls *LangServer) Completion(ctx context.Context, params *lsp.CompletionParams) (*lsp.CompletionList, error) {
return ls.GetCompletions(params)
}
func (ls *LangServer) CompletionResolve(ctx context.Context, params *lsp.CompletionItem) (*lsp.CompletionItem, error) {
return nil, unsupported()
}
func (ls *LangServer) Hover(ctx context.Context, params *lsp.TextDocumentPositionParams) (*lsp.Hover, error) {
return nil, unsupported()
}
func (ls *LangServer) SignatureHelp(ctx context.Context, params *lsp.TextDocumentPositionParams) (*lsp.SignatureHelp, error) {
return nil, unsupported()
}
func (ls *LangServer) Definition(ctx context.Context, params *lsp.TextDocumentPositionParams) ([]lsp.Location, error) {
return nil, unsupported()
}
func (ls *LangServer) TypeDefinition(ctx context.Context, params *lsp.TextDocumentPositionParams) ([]lsp.Location, error) {
return nil, unsupported()
}
func (ls *LangServer) Implementation(ctx context.Context, params *lsp.TextDocumentPositionParams) ([]lsp.Location, error) {
return nil, unsupported()
}
func (ls *LangServer) References(ctx context.Context, params *lsp.ReferenceParams) ([]lsp.Location, error) {
return nil, unsupported()
}
func (ls *LangServer) DocumentHighlight(ctx context.Context, params *lsp.TextDocumentPositionParams) ([]lsp.DocumentHighlight, error) {
return nil, unsupported()
}
func (ls *LangServer) DocumentSymbol(ctx context.Context, params *lsp.DocumentSymbolParams) ([]lsp.DocumentSymbol, error) {
return nil, unsupported()
}
func (ls *LangServer) CodeAction(ctx context.Context, params *lsp.CodeActionParams) ([]lsp.CodeAction, error) {
return nil, unsupported()
}
func (ls *LangServer) CodeLens(ctx context.Context, params *lsp.CodeLensParams) ([]lsp.CodeLens, error) {
return nil, nil
}
func (ls *LangServer) CodeLensResolve(ctx context.Context, params *lsp.CodeLens) (*lsp.CodeLens, error) {
return nil, unsupported()
}
func (ls *LangServer) DocumentLink(ctx context.Context, params *lsp.DocumentLinkParams) ([]lsp.DocumentLink, error) {
return nil, nil
}
func (ls *LangServer) DocumentLinkResolve(ctx context.Context, params *lsp.DocumentLink) (*lsp.DocumentLink, error) {
return nil, unsupported()
}
func (ls *LangServer) DocumentColor(ctx context.Context, params *lsp.DocumentColorParams) ([]lsp.ColorInformation, error) {
return nil, unsupported()
}
func (ls *LangServer) ColorPresentation(ctx context.Context, params *lsp.ColorPresentationParams) ([]lsp.ColorPresentation, error) {
return nil, unsupported()
}
func (ls *LangServer) Formatting(ctx context.Context, params *lsp.DocumentFormattingParams) ([]lsp.TextEdit, error) {
return ls.Format(params)
}
func (ls *LangServer) RangeFormatting(ctx context.Context, params *lsp.DocumentRangeFormattingParams) ([]lsp.TextEdit, error) {
return nil, unsupported()
}
func (ls *LangServer) OnTypeFormatting(ctx context.Context, params *lsp.DocumentOnTypeFormattingParams) ([]lsp.TextEdit, error) {
return nil, unsupported()
}
func (ls *LangServer) Rename(ctx context.Context, params *lsp.RenameParams) ([]lsp.WorkspaceEdit, error) {
return nil, unsupported()
}
func (ls *LangServer) FoldingRanges(ctx context.Context, params *lsp.FoldingRangeRequestParam) ([]lsp.FoldingRange, error) {
return nil, unsupported()
}