-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathsuggestapi.nim
578 lines (525 loc) · 17 KB
/
suggestapi.nim
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
import
osproc,
strutils,
strformat,
times,
deques,
sets,
os,
sequtils,
streams,
protocol/enums,
./utils,
chronicles,
protocol/types,
std/options,
chronos,
chronos/asyncproc,
stew/[byteutils]
const REQUEST_TIMEOUT* = 120000
const HighestSupportedNimSuggestProtocolVersion = 4
# coppied from Nim repo
type
PrefixMatch* {.pure.} = enum
None ## no prefix detected
Abbrev ## prefix is an abbreviation of the symbol
Substr ## prefix is a substring of the symbol
Prefix ## prefix does match the symbol
IdeCmd* = enum
ideNone
ideSug
ideCon
ideDef
ideUse
ideDus
ideChk
ideMod
ideHighlight
ideOutline
ideKnown
ideMsg
ideProject
ideType
ideExpand
NimsuggestCallback* = proc(self: Nimsuggest): void {.gcsafe, raises: [].}
ProjectCallback* = proc(self: Project): void {.gcsafe, raises: [].}
Suggest* = ref object
section*: IdeCmd
qualifiedPath*: seq[string] # part of 'qualifiedPath'
filePath*: string
line*: int # Starts at 1
column*: int # Starts at 0
doc*: string # Not escaped (yet)
forth*: string # type
quality*: range[0 .. 100] # matching quality
isGlobal*: bool # is a global variable
contextFits*: bool # type/non-type context matches
prefix*: PrefixMatch
symkind*: string
scope*, localUsages*, globalUsages*: int # more usages is better
tokenLen*: int
version*: int
endLine*: int
endCol*: int
inlayHintInfo*: SuggestInlayHint
SuggestCall* = ref object
commandString: string
future: Future[seq[Suggest]]
command: string
SuggestInlayHintKind* = enum
sihkType = "Type"
sihkParameter = "Parameter"
sihkException = "Exception"
SuggestInlayHint* = ref object
kind*: SuggestInlayHintKind
line*: int # Starts at 1
column*: int # Starts at 0
label*: string
paddingLeft*: bool
paddingRight*: bool
allowInsert*: bool
tooltip*: string
NimsuggestImpl* = object
checkProjectInProgress*: bool
needsCheckProject*: bool
openFiles*: OrderedSet[string]
successfullCall*: bool
port*: int
root: string
requestQueue: Deque[SuggestCall]
processing: bool
timeout: int
timeoutCallback: NimsuggestCallback
protocolVersion*: int
capabilities*: set[NimSuggestCapability]
nimSuggestPath*: string
version*: string
project*: Project
NimSuggest* = ref NimsuggestImpl
Project* = ref object
ns*: Future[NimSuggest]
file*: string
process*: AsyncProcessRef
errorCallback*: Option[ProjectCallback]
errorMessage*: string
failed*: bool
lastCmd*: string
lastCmdDate*: Option[DateTime]
func canHandleUnknown*(ns: Nimsuggest): bool =
nsUnknownFile in ns.capabilities
template benchmark(benchmarkName: string, code: untyped) =
block:
debug "Started...", benchmark = benchmarkName
let t0 = epochTime()
code
let elapsed = epochTime() - t0
let elapsedStr = elapsed.formatFloat(format = ffDecimal, precision = 3)
debug "CPU Time", benchmark = benchmarkName, time = elapsedStr
func nimSymToLSPKind*(suggest: Suggest): CompletionItemKind =
case suggest.symKind
of "skConst": CompletionItemKind.Value
of "skEnumField": CompletionItemKind.Enum
of "skForVar": CompletionItemKind.Variable
of "skIterator": CompletionItemKind.Keyword
of "skLabel": CompletionItemKind.Keyword
of "skLet": CompletionItemKind.Value
of "skMacro": CompletionItemKind.Snippet
of "skMethod": CompletionItemKind.Method
of "skParam": CompletionItemKind.Variable
of "skProc": CompletionItemKind.Function
of "skResult": CompletionItemKind.Value
of "skTemplate": CompletionItemKind.Snippet
of "skType": CompletionItemKind.Class
of "skVar": CompletionItemKind.Field
of "skFunc": CompletionItemKind.Function
else: CompletionItemKind.Property
func nimSymToLSPSymbolKind*(suggest: string): SymbolKind =
case suggest
of "skConst": SymbolKind.Constant
of "skEnumField": SymbolKind.EnumMember
of "skField": SymbolKind.Field
of "skIterator": SymbolKind.Function
of "skConverter": SymbolKind.Function
of "skLet": SymbolKind.Variable
of "skMacro": SymbolKind.Function
of "skMethod": SymbolKind.Method
of "skProc": SymbolKind.Function
of "skTemplate": SymbolKind.Function
of "skType": SymbolKind.Class
of "skVar": SymbolKind.Variable
of "skFunc": SymbolKind.Function
else: SymbolKind.Function
func nimSymDetails*(suggest: Suggest): string =
case suggest.symKind
of "skConst":
"const " & suggest.qualifiedPath.join(".") & ": " & suggest.forth
of "skEnumField":
"enum " & suggest.forth
of "skForVar":
"for var of " & suggest.forth
of "skIterator":
suggest.forth
of "skLabel":
"label"
of "skLet":
"let of " & suggest.forth
of "skMacro":
"macro"
of "skMethod":
suggest.forth
of "skParam":
"param"
of "skProc":
suggest.forth
of "skResult":
"result"
of "skTemplate":
suggest.forth
of "skType":
"type " & suggest.qualifiedPath.join(".")
of "skVar":
"var of " & suggest.forth
else:
suggest.forth
const failedToken = "::Failed::"
proc parseQualifiedPath*(input: string): seq[string] =
result = @[]
var
item = ""
escaping = false
for c in input:
if c == '`':
item = item & c
escaping = not escaping
elif escaping:
item = item & c
elif c == '.':
result.add item
item = ""
else:
item = item & c
if item != "":
result.add item
proc parseSuggestDef*(line: string): Option[Suggest] =
let tokens = line.split('\t')
if tokens.len < 8:
error "Failed to parse: ", line = line
return none(Suggest)
var sug = Suggest(
qualifiedPath: tokens[2].parseQualifiedPath,
filePath: tokens[4],
line: parseInt(tokens[5]),
column: parseInt(tokens[6]),
doc: tokens[7].unescape(),
forth: tokens[3],
symKind: tokens[1],
section: parseEnum[IdeCmd]("ide" & capitalizeAscii(tokens[0])),
)
if tokens.len == 11:
sug.endLine = parseInt(tokens[9])
sug.endCol = parseInt(tokens[10])
some sug
proc parseSuggestInlayHint*(line: string): SuggestInlayHint =
let tokens = line.split('\t')
if tokens.len < 8:
error "Failed to parse: ", line = line
raise newException(ValueError, fmt "Failed to parse line {line}")
result = SuggestInlayHint(
kind: parseEnum[SuggestInlayHintKind](capitalizeAscii(tokens[0])),
line: parseInt(tokens[1]),
column: parseInt(tokens[2]),
label: tokens[3],
paddingLeft: parseBool(tokens[4]),
paddingRight: parseBool(tokens[5]),
allowInsert: parseBool(tokens[6]),
tooltip: tokens[7],
)
proc name*(sug: Suggest): string =
return sug.qualifiedPath[^1]
proc markFailed(self: Project, errMessage: string) {.raises: [].} =
self.failed = true
self.errorMessage = errMessage
if self.errorCallback.isSome:
self.errorCallback.get()(self)
proc stop*(self: Project) =
debug "Stopping nimsuggest for ", root = self.file
try:
let res = self.process.kill()
debug "Stopped nimsuggest ", res = res
except Exception as ex:
writeStackTrace(ex)
proc doWithTimeout*[T](fut: Future[T], timeout: int, s: string): owned(Future[bool]) =
var retFuture = newFuture[bool]("asyncdispatch.`doWithTimeout`")
var timeoutFuture = sleepAsync(timeout)
fut.addCallback do():
if not retFuture.finished:
retFuture.complete(true)
timeoutFuture.addCallback do():
if not retFuture.finished:
retFuture.complete(false)
return retFuture
proc detectNimsuggestVersion(
root: string, nimsuggestPath: string, workingDir: string
): int {.gcsafe.} =
var process = startProcess(
command = nimsuggestPath,
workingDir = workingDir,
args = @[root, "--info:protocolVer"],
options = {poUsePath},
)
var l: string
if not process.outputStream.readLine(l):
l = ""
var exitCode = process.waitForExit()
if exitCode != 0 or l == "":
# older versions of NimSuggest don't support the --info:protocolVer option
# use protocol version 3 with them
return 3
else:
return parseInt(l)
proc getNimsuggestCapabilities*(
nimsuggestPath: string
): set[NimSuggestCapability] {.gcsafe.} =
proc parseCapability(c: string): Option[NimSuggestCapability] =
debug "Parsing nimsuggest capability", capability = c
try:
result = some(parseEnum[NimSuggestCapability](c))
except:
debug "Capability not supported. Ignoring.", capability = c
result = none(NimSuggestCapability)
var process = startProcess(
command = nimsuggestPath, args = @["--info:capabilities"], options = {poUsePath}
)
var l: string
if not process.outputStream.readLine(l):
l = ""
var exitCode = process.waitForExit()
if exitCode == 0:
# older versions of NimSuggest don't support the --info:capabilities option
for cap in l.split(" ").mapIt(parseCapability(it)):
if cap.isSome:
result.incl(cap.get)
proc logNsError(project: Project) {.async.} =
let err = string.fromBytes(project.process.stderrStream.read().await)
error "NimSuggest Error (stderr)", err = err
project.markFailed(err)
proc createNimsuggest*(
root: string,
nimsuggestPath: string,
version: string,
timeout: int,
timeoutCallback: NimsuggestCallback,
errorCallback: ProjectCallback,
workingDir = getCurrentDir(),
enableLog: bool = false,
enableExceptionInlayHints: bool = false,
): Future[Project] {.async, gcsafe.} =
result = Project(file: root)
result.ns = newFuture[NimSuggest]()
result.errorCallback = some errorCallback
let isNimble = root.endsWith(".nimble")
let isNimScript = root.endsWith(".nims") or isNimble
var extraArgs = newSeq[string]()
if isNimScript:
extraArgs.add("--import: system/nimscript")
#Nimsuggest crashes when including the file.
if isNimble:
let nimScriptApiPath = getNimScriptAPITemplatePath()
extraArgs.add("--include: " & nimScriptApiPath)
let ns = Nimsuggest()
ns.requestQueue = Deque[SuggestCall]()
ns.root = root
ns.timeout = timeout
ns.timeoutCallback = timeoutCallback
ns.nimSuggestPath = nimsuggestPath
ns.version = version
ns.project = result
info "Starting nimsuggest",
root = root, timeout = timeout, path = nimsuggestPath, workingDir = workingDir
if nimsuggestPath != "":
ns.protocolVersion = detectNimsuggestVersion(root, nimsuggestPath, workingDir)
if ns.protocolVersion > HighestSupportedNimSuggestProtocolVersion:
ns.protocolVersion = HighestSupportedNimSuggestProtocolVersion
var args = @[root, "--v" & $ns.protocolVersion, "--autobind"] & extraArgs
if ns.protocolVersion >= 4:
args.add("--clientProcessId:" & $getCurrentProcessId())
if enableLog:
args.add("--log")
ns.capabilities = getNimsuggestCapabilities(nimsuggestPath)
debug "Nimsuggest Capabilities", capabilities = ns.capabilities
if nsExceptionInlayHints in ns.capabilities:
if enableExceptionInlayHints:
args.add("--exceptionInlayHints:on")
else:
args.add("--exceptionInlayHints:off")
result.process = await startProcess(
nimsuggestPath,
arguments = args,
options = {UsePath},
stdoutHandle = AsyncProcess.Pipe,
stderrHandle = AsyncProcess.Pipe,
)
asyncSpawn logNsError(result)
ns.port = (await result.process.stdoutStream.readLine(sep = "\n")).parseInt
result.ns.complete(ns)
else:
error "Unable to start nimsuggest. Unable to find binary on the $PATH",
nimsuggestPath = nimsuggestPath
result.markFailed fmt "Unable to start nimsuggest. `{nimsuggestPath}` is not present on the PATH"
proc createNimsuggest*(root: string): Future[Project] {.gcsafe.} =
result = createNimsuggest(
root,
"nimsuggest",
"",
REQUEST_TIMEOUT,
proc(ns: Nimsuggest) =
discard,
proc(pr: Project) =
discard,
)
proc toString*(bytes: openarray[byte]): string =
result = newString(bytes.len)
if bytes.len > 0:
copyMem(result[0].addr, bytes[0].unsafeAddr, bytes.len)
proc processQueue(self: Nimsuggest): Future[void] {.async.} =
debug "processQueue", size = self.requestQueue.len
while self.requestQueue.len != 0:
let req = self.requestQueue.popFirst
self.project.lastCmd = req.commandString
self.project.lastCmdDate = some(now())
logScope:
command = req.commandString
if req.future.finished:
debug "Call cancelled before executed", command = req.command
elif self.project.failed:
debug "Nimsuggest is not working, returning empty result...", port = self.port
req.future.complete @[]
else:
benchmark req.commandString:
var res: seq[Suggest] = @[]
if not self.timeoutCallback.isNil:
debug "timeoutCallback is set", timeout = self.timeout
doWithTimeout(req.future, self.timeout, fmt "running {req.commandString}").addCallback do(
f: Future[bool]
):
if not f.failed and not f.read():
debug "Calling restart"
self.timeoutCallback(self)
let ta = initTAddress(&"127.0.0.1:{self.port}")
let transport = await ta.connect()
discard await transport.write(req.commandString & "\c\L")
const bufferSize = 1024 * 1024 * 4
var buffer: seq[byte] = newSeq[byte](bufferSize)
var data = await transport.read()
let content = data.toString()
for lineStr in content.splitLines:
if lineStr != "":
case req.command
of "known":
let sug = Suggest()
sug.section = ideKnown
sug.forth = lineStr
res.add sug
of "inlayHints":
res.add Suggest(inlayHintInfo: parseSuggestInlayHint(lineStr))
else:
let sug = parseSuggestDef(lineStr)
if sug.isSome:
res.add sug.get
if (content == ""):
self.project.markFailed "Server crashed/socket closed."
debug "Server socket closed"
if not req.future.finished:
debug "Call cancelled before sending error", command = req.command
req.future.fail newException(
CatchableError, "Server crashed/socket closed."
)
if not req.future.finished:
debug "Sending result(s)", length = res.len
req.future.complete res
self.successfullCall = true
transport.close()
else:
debug "Call was cancelled before sending the result", command = req.command
transport.close()
self.processing = false
proc call*(
self: Nimsuggest,
command: string,
file: string,
dirtyFile: string,
line: int,
column: int,
tag = "",
): Future[seq[Suggest]] =
result = Future[seq[Suggest]]()
let commandString =
if dirtyFile != "":
fmt "{command} \"{file}\";\"{dirtyFile}\":{line}:{column}{tag}"
else:
fmt "{command} \"{file}\":{line}:{column}{tag}"
self.requestQueue.addLast(
SuggestCall(commandString: commandString, future: result, command: command)
)
if not self.processing:
self.processing = true
traceAsyncErrors processQueue(self)
template createFullCommand(command: untyped) {.dirty.} =
proc command*(
self: Nimsuggest, file: string, dirtyfile = "", line: int, col: int, tag = ""
): Future[seq[Suggest]] =
return self.call(astToStr(command), file, dirtyfile, line, col, tag)
template createFileOnlyCommand(command: untyped) {.dirty.} =
proc command*(self: Nimsuggest, file: string, dirtyfile = ""): Future[seq[Suggest]] =
return self.call(astToStr(command), file, dirtyfile, 0, 0)
template createGlobalCommand(command: untyped) {.dirty.} =
proc command*(self: Nimsuggest): Future[seq[Suggest]] =
return self.call(astToStr(command), "-", "", 0, 0)
template createRangeCommand(command: untyped) {.dirty.} =
proc command*(
self: Nimsuggest,
file: string,
dirtyfile = "",
startLine, startCol, endLine, endCol: int,
extra: string,
): Future[seq[Suggest]] =
return self.call(
astToStr(command),
file,
dirtyfile,
startLine,
startCol,
fmt ":{endLine}:{endCol}{extra}",
)
# create commands
createFullCommand(sug)
createFullCommand(con)
createFullCommand(def)
createFullCommand(declaration)
createFullCommand(use)
createFullCommand(expand)
createFullCommand(highlight)
createFullCommand(type)
createFileOnlyCommand(chk)
createFileOnlyCommand(chkFile)
createFileOnlyCommand(changed)
createFileOnlyCommand(outline)
createFileOnlyCommand(known)
createFileOnlyCommand(globalSymbols)
createGlobalCommand(recompile)
createRangeCommand(inlayHints)
proc `mod`*(
nimsuggest: Nimsuggest, file: string, dirtyfile = ""
): Future[seq[Suggest]] =
return nimsuggest.call("ideMod", file, dirtyfile, 0, 0)
proc isKnown*(nimsuggest: Nimsuggest, filePath: string): Future[bool] {.async.} =
let res = await withTimeout(nimsuggest.known(filePath))
if res.isNone:
debug "Timeout reached running [isKnown], assuming the file is not known",
file = filePath
return
let sug = res.get()
if sug.len == 0:
return false
debug "isKnown", filePath = filePath, sug = sug[0].forth
return sug.len > 0 and sug[0].forth == "true"