-
Notifications
You must be signed in to change notification settings - Fork 42
Expand file tree
/
Copy pathnimlangserver.nim
More file actions
215 lines (197 loc) · 7.62 KB
/
nimlangserver.nim
File metadata and controls
215 lines (197 loc) · 7.62 KB
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
import json_rpc/[servers/socketserver, private/jrpc_sys, jsonmarshal, rpcclient, router]
import chronicles, chronos
import std/[syncio, os, json, strutils, strformat]
import ls, routes, suggestapi, utils, lstransports, asyncprocmonitor
import protocol/types
when defined(posix):
import posix
proc registerRoutes(srv: RpcSocketServer, ls: LanguageServer) =
srv.register("initialize", wrapRpc(partial(initialize, (ls: ls, onExit: ls.onExit))))
#use from ls
srv.register(
"textDocument/completion", ls.addRpcToCancellable(wrapRpc(partial(completion, ls)))
)
srv.register(
"textDocument/definition", ls.addRpcToCancellable(wrapRpc(partial(definition, ls)))
)
srv.register(
"textDocument/declaration",
ls.addRpcToCancellable(wrapRpc(partial(declaration, ls))),
)
srv.register(
"textDocument/typeDefinition",
ls.addRpcToCancellable(wrapRpc(partial(typeDefinition, ls))),
)
srv.register(
"textDocument/documentSymbol",
ls.addRpcToCancellable(wrapRpc(partial(documentSymbols, ls))),
)
srv.register(
"textDocument/hover", ls.addRpcToCancellable(wrapRpc(partial(hover, ls)))
)
srv.register("textDocument/references", wrapRpc(partial(references, ls)))
srv.register("textDocument/codeAction", wrapRpc(partial(codeAction, ls)))
srv.register(
"textDocument/prepareRename",
ls.addRpcToCancellable(wrapRpc(partial(prepareRename, ls))),
)
srv.register(
"textDocument/rename", ls.addRpcToCancellable(wrapRpc(partial(rename, ls)))
)
srv.register(
"textDocument/inlayHint", ls.addRpcToCancellable(wrapRpc(partial(inlayHint, ls)))
)
srv.register(
"textDocument/signatureHelp",
ls.addRpcToCancellable(wrapRpc(partial(signatureHelp, ls))),
)
srv.register(
"textDocument/formatting", ls.addRpcToCancellable(wrapRpc(partial(formatting, ls)))
)
srv.register("workspace/executeCommand", wrapRpc(partial(executeCommand, ls)))
srv.register(
"workspace/symbol", ls.addRpcToCancellable(wrapRpc(partial(workspaceSymbol, ls)))
)
srv.register(
"textDocument/documentHighlight",
ls.addRpcToCancellable(wrapRpc(partial(documentHighlight, ls))),
)
srv.register("shutdown", wrapRpc(partial(shutdown, ls)))
srv.register("exit", wrapRpc(partial(exit, (ls: ls, onExit: ls.onExit))))
#Extension
srv.register("extension/macroExpand", wrapRpc(partial(expand, ls)))
srv.register("extension/status", wrapRpc(partial(status, ls)))
srv.register("extension/capabilities", wrapRpc(partial(extensionCapabilities, ls)))
srv.register("extension/suggest", wrapRpc(partial(extensionSuggest, ls)))
srv.register("extension/tasks", wrapRpc(partial(tasks, ls)))
srv.register("extension/runTask", wrapRpc(partial(runTask, ls)))
srv.register("extension/listTests", wrapRpc(partial(listTests, ls)))
srv.register("extension/runTests", wrapRpc(partial(runTests, ls)))
srv.register("extension/cancelTest", wrapRpc(partial(cancelTest, ls)))
#Notifications
srv.register("$/cancelRequest", wrapRpc(partial(cancelRequest, ls)))
srv.register("initialized", wrapRpc(partial(initialized, ls)))
srv.register("textDocument/didOpen", wrapRpc(partial(didOpen, ls)))
srv.register("textDocument/didSave", wrapRpc(partial(didSave, ls)))
srv.register("textDocument/didClose", wrapRpc(partial(didClose, ls)))
srv.register(
"workspace/didChangeConfiguration", wrapRpc(partial(didChangeConfiguration, ls))
)
srv.register("textDocument/didChange", wrapRpc(partial(didChange, ls)))
srv.register(
"textDocument/willSaveWaitUntil", wrapRpc(partial(willSaveWaitUntil, ls))
)
srv.register("$/setTrace", wrapRpc(partial(setTrace, ls)))
proc showHelp() =
echo "nimlangserver: The Nim Language Server"
echo "Version: ", LSPVersion
echo ""
echo "Options:"
echo " --help, -h Show this help message"
echo " --version, -v Show version information"
echo " --stdio Use stdio transport (default)"
echo " --socket Use socket transport"
echo " --port=<port> Port to use for socket transport"
echo " --clientProcessId=<pid> Exit when the given process ID terminates"
echo ""
const readme = staticRead("README.md")
echo "CONFIGURATION OPTIONS"
proc formatForConsole(md: string): string =
var inCodeBlock = false
result = ""
for line in md.splitLines():
if line.startsWith("```"):
inCodeBlock = not inCodeBlock
continue
var cleaned = line
if not inCodeBlock:
cleaned = cleaned.multiReplace([("`", ""), ("\\", "")])
if cleaned.len > 0:
result.add(cleaned & "\n")
result = result.strip()
let section = readme.split("## Configuration Options")[1].split("##")[0]
echo formatForConsole(section)
quit(0)
proc handleParams(): CommandLineParams =
if paramCount() > 0 and paramStr(1) in ["-v", "--version"]:
echo LSPVersion
quit()
var i = 1
while i <= paramCount():
var param = paramStr(i)
if param.startsWith("--clientProcessId="):
var pidStr = param.substr(18)
try:
var pid = pidStr.parseInt
result.clientProcessId = some(pid)
except ValueError:
stderr.writeLine("Invalid client process ID: ", pidStr)
quit 1
if param == "--stdio":
result.transport = some TransportMode.stdio
if param == "--socket":
result.transport = some TransportMode.socket
if param.startsWith "--port":
let port = param.substr(7)
try:
result.port = Port(parseInt(port))
except ValueError:
error "Invalid port ", port = port
quit(1)
if param in ["help", "--help", "-h"]:
showHelp()
inc i
if result.transport.isSome and result.transport.get == socket:
if result.port == default(Port):
result.port = getNextFreePort()
echo &"port={result.port}"
if result.transport.isNone:
result.transport = some stdio
proc registerProcMonitor(ls: LanguageServer) =
if ls.cmdLineClientProcessId.isSome:
debug "Registering monitor for process id, specified on command line",
clientProcessId = ls.cmdLineClientProcessId.get
proc onCmdLineClientProcessExitAsync(): Future[void] {.async.} =
debug "onCmdLineClientProcessExitAsync"
await ls.stopNimsuggestProcesses
waitFor ls.onExit()
proc onCmdLineClientProcessExit() {.closure.} =
debug "onCmdLineClientProcessExit"
try:
waitFor onCmdLineClientProcessExitAsync()
except CatchableError as ex:
error "Error in onCmdLineClientProcessExit"
writeStackTrace(ex)
hookAsyncProcMonitor(ls.cmdLineClientProcessId.get, onCmdLineClientProcessExit)
proc tickLs*(ls: LanguageServer, time = 1.seconds) {.async.} =
await ls.tick()
await sleepAsync(time)
await ls.tickLs()
proc main*(cmdLineParams: CommandLineParams): LanguageServer =
debug "Starting nimlangserver", version = LSPVersion, params = cmdLineParams
#[
`nimlangserver` supports both transports: stdio and socket. By default it uses stdio transport.
But we do construct a RPC socket server even in stdio mode, so that we can reuse the same code for both transports.
]#
result = initLs(cmdLineParams, ensureStorageDir())
case result.transportMode
of stdio:
result.startStdioServer()
of socket:
result.startSocketServer(cmdLineParams.port)
result.srv.registerRoutes(result)
result.registerProcMonitor()
when isMainModule:
try:
let ls = main(handleParams())
asyncSpawn ls.tickLs()
when defined(posix):
onSignal(SIGINT, SIGTERM, SIGHUP, SIGQUIT, SIGPIPE):
debug "Terminated via signal", sig
ls.stopNimsuggestProcessesP()
exitnow(1)
runForever()
except Exception as e:
error "Error in main"
writeStackTrace e
quit(1)