nimsuggest.nim 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976
  1. #
  2. #
  3. # The Nim Compiler
  4. # (c) Copyright 2017 Andreas Rumpf
  5. #
  6. # See the file "copying.txt", included in this
  7. # distribution, for details about the copyright.
  8. #
  9. import compiler/renderer
  10. import strformat
  11. import tables
  12. import std/sha1
  13. import times
  14. ## Nimsuggest is a tool that helps to give editors IDE like capabilities.
  15. when not defined(nimcore):
  16. {.error: "nimcore MUST be defined for Nim's core tooling".}
  17. import strutils, os, parseopt, parseutils, sequtils, net, rdstdin, sexp
  18. # Do NOT import suggest. It will lead to weird bugs with
  19. # suggestionResultHook, because suggest.nim is included by sigmatch.
  20. # So we import that one instead.
  21. import compiler / [options, commands, modules, sem,
  22. passes, passaux, msgs,
  23. sigmatch, ast,
  24. idents, modulegraphs, prefixmatches, lineinfos, cmdlinehelper,
  25. pathutils]
  26. when defined(windows):
  27. import winlean
  28. else:
  29. import posix
  30. const DummyEof = "!EOF!"
  31. const Usage = """
  32. Nimsuggest - Tool to give every editor IDE like capabilities for Nim
  33. Usage:
  34. nimsuggest [options] projectfile.nim
  35. Options:
  36. --autobind automatically binds into a free port
  37. --port:PORT port, by default 6000
  38. --address:HOST binds to that address, by default ""
  39. --stdin read commands from stdin and write results to
  40. stdout instead of using sockets
  41. --epc use emacs epc mode
  42. --debug enable debug output
  43. --log enable verbose logging to nimsuggest.log file
  44. --v1 use version 1 of the protocol; for backwards compatibility
  45. --v2 use version 2(default) of the protocol
  46. --v3 use version 3 of the protocol
  47. --refresh perform automatic refreshes to keep the analysis precise
  48. --maxresults:N limit the number of suggestions to N
  49. --tester implies --stdin and outputs a line
  50. '""" & DummyEof & """' for the tester
  51. --find attempts to find the project file of the current project
  52. The server then listens to the connection and takes line-based commands.
  53. If --autobind is used, the binded port number will be printed to stdout.
  54. In addition, all command line options of Nim that do not affect code generation
  55. are supported.
  56. """
  57. type
  58. Mode = enum mstdin, mtcp, mepc, mcmdsug, mcmdcon
  59. CachedMsg = object
  60. info: TLineInfo
  61. msg: string
  62. sev: Severity
  63. CachedMsgs = seq[CachedMsg]
  64. var
  65. gPort = 6000.Port
  66. gAddress = ""
  67. gMode: Mode
  68. gEmitEof: bool # whether we write '!EOF!' dummy lines
  69. gLogging = defined(logging)
  70. gRefresh: bool
  71. gAutoBind = false
  72. requests: Channel[string]
  73. results: Channel[Suggest]
  74. proc executeNoHooksV3(cmd: IdeCmd, file: AbsoluteFile, dirtyfile: AbsoluteFile, line, col: int;
  75. graph: ModuleGraph);
  76. proc writelnToChannel(line: string) =
  77. results.send(Suggest(section: ideMsg, doc: line))
  78. proc sugResultHook(s: Suggest) =
  79. results.send(s)
  80. proc errorHook(conf: ConfigRef; info: TLineInfo; msg: string; sev: Severity) =
  81. results.send(Suggest(section: ideChk, filePath: toFullPath(conf, info),
  82. line: toLinenumber(info), column: toColumn(info), doc: msg,
  83. forth: $sev))
  84. proc myLog(s: string) =
  85. if gLogging: log(s)
  86. const
  87. seps = {':', ';', ' ', '\t'}
  88. Help = "usage: sug|con|def|use|dus|chk|mod|highlight|outline|known|project file.nim[;dirtyfile.nim]:line:col\n" &
  89. "type 'quit' to quit\n" &
  90. "type 'debug' to toggle debug mode on/off\n" &
  91. "type 'terse' to toggle terse mode on/off"
  92. proc parseQuoted(cmd: string; outp: var string; start: int): int =
  93. var i = start
  94. i += skipWhitespace(cmd, i)
  95. if i < cmd.len and cmd[i] == '"':
  96. i += parseUntil(cmd, outp, '"', i+1)+2
  97. else:
  98. i += parseUntil(cmd, outp, seps, i)
  99. result = i
  100. proc sexp(s: IdeCmd|TSymKind|PrefixMatch): SexpNode = sexp($s)
  101. proc sexp(s: Suggest): SexpNode =
  102. # If you change the order here, make sure to change it over in
  103. # nim-mode.el too.
  104. let qp = if s.qualifiedPath.len == 0: @[] else: s.qualifiedPath
  105. result = convertSexp([
  106. s.section,
  107. TSymKind s.symkind,
  108. qp.map(newSString),
  109. s.filePath,
  110. s.forth,
  111. s.line,
  112. s.column,
  113. s.doc,
  114. s.quality
  115. ])
  116. if s.section == ideSug:
  117. result.add convertSexp(s.prefix)
  118. proc sexp(s: seq[Suggest]): SexpNode =
  119. result = newSList()
  120. for sug in s:
  121. result.add(sexp(sug))
  122. proc listEpc(): SexpNode =
  123. # This function is called from Emacs to show available options.
  124. let
  125. argspecs = sexp("file line column dirtyfile".split(" ").map(newSSymbol))
  126. docstring = sexp("line starts at 1, column at 0, dirtyfile is optional")
  127. result = newSList()
  128. for command in ["sug", "con", "def", "use", "dus", "chk", "mod", "globalSymbols", "recompile", "saved", "chkFile"]:
  129. let
  130. cmd = sexp(command)
  131. methodDesc = newSList()
  132. methodDesc.add(cmd)
  133. methodDesc.add(argspecs)
  134. methodDesc.add(docstring)
  135. result.add(methodDesc)
  136. proc findNode(n: PNode; trackPos: TLineInfo): PSym =
  137. #echo "checking node ", n.info
  138. if n.kind == nkSym:
  139. if isTracked(n.info, trackPos, n.sym.name.s.len): return n.sym
  140. else:
  141. for i in 0 ..< safeLen(n):
  142. let res = findNode(n[i], trackPos)
  143. if res != nil: return res
  144. proc symFromInfo(graph: ModuleGraph; trackPos: TLineInfo): PSym =
  145. let m = graph.getModule(trackPos.fileIndex)
  146. if m != nil and m.ast != nil:
  147. result = findNode(m.ast, trackPos)
  148. proc executeNoHooks(cmd: IdeCmd, file, dirtyfile: AbsoluteFile, line, col: int;
  149. graph: ModuleGraph) =
  150. let conf = graph.config
  151. if conf.suggestVersion == 3:
  152. executeNoHooksV3(cmd, file, dirtyfile, line, col, graph)
  153. return
  154. myLog("cmd: " & $cmd & ", file: " & file.string &
  155. ", dirtyFile: " & dirtyfile.string &
  156. "[" & $line & ":" & $col & "]")
  157. conf.ideCmd = cmd
  158. if cmd == ideUse and conf.suggestVersion != 0:
  159. graph.resetAllModules()
  160. var isKnownFile = true
  161. let dirtyIdx = fileInfoIdx(conf, file, isKnownFile)
  162. if not dirtyfile.isEmpty: msgs.setDirtyFile(conf, dirtyIdx, dirtyfile)
  163. else: msgs.setDirtyFile(conf, dirtyIdx, AbsoluteFile"")
  164. conf.m.trackPos = newLineInfo(dirtyIdx, line, col)
  165. conf.m.trackPosAttached = false
  166. conf.errorCounter = 0
  167. if conf.suggestVersion == 1:
  168. graph.usageSym = nil
  169. if not isKnownFile:
  170. graph.compileProject(dirtyIdx)
  171. if conf.suggestVersion == 0 and conf.ideCmd in {ideUse, ideDus} and
  172. dirtyfile.isEmpty:
  173. discard "no need to recompile anything"
  174. else:
  175. let modIdx = graph.parentModule(dirtyIdx)
  176. graph.markDirty dirtyIdx
  177. graph.markClientsDirty dirtyIdx
  178. if conf.ideCmd != ideMod:
  179. if isKnownFile:
  180. graph.compileProject(modIdx)
  181. if conf.ideCmd in {ideUse, ideDus}:
  182. let u = if conf.suggestVersion != 1: graph.symFromInfo(conf.m.trackPos) else: graph.usageSym
  183. if u != nil:
  184. listUsages(graph, u)
  185. else:
  186. localError(conf, conf.m.trackPos, "found no symbol at this position " & (conf $ conf.m.trackPos))
  187. proc execute(cmd: IdeCmd, file, dirtyfile: AbsoluteFile, line, col: int;
  188. graph: ModuleGraph) =
  189. if cmd == ideChk:
  190. graph.config.structuredErrorHook = errorHook
  191. graph.config.writelnHook = myLog
  192. else:
  193. graph.config.structuredErrorHook = nil
  194. graph.config.writelnHook = myLog
  195. executeNoHooks(cmd, file, dirtyfile, line, col, graph)
  196. proc executeEpc(cmd: IdeCmd, args: SexpNode;
  197. graph: ModuleGraph) =
  198. let
  199. file = AbsoluteFile args[0].getStr
  200. line = args[1].getNum
  201. column = args[2].getNum
  202. var dirtyfile = AbsoluteFile""
  203. if len(args) > 3:
  204. dirtyfile = AbsoluteFile args[3].getStr("")
  205. execute(cmd, file, dirtyfile, int(line), int(column), graph)
  206. proc returnEpc(socket: Socket, uid: BiggestInt, s: SexpNode|string,
  207. returnSymbol = "return") =
  208. let response = $convertSexp([newSSymbol(returnSymbol), uid, s])
  209. socket.send(toHex(len(response), 6))
  210. socket.send(response)
  211. template checkSanity(client, sizeHex, size, messageBuffer: typed) =
  212. if client.recv(sizeHex, 6) != 6:
  213. raise newException(ValueError, "didn't get all the hexbytes")
  214. if parseHex(sizeHex, size) == 0:
  215. raise newException(ValueError, "invalid size hex: " & $sizeHex)
  216. if client.recv(messageBuffer, size) != size:
  217. raise newException(ValueError, "didn't get all the bytes")
  218. proc toStdout() {.gcsafe.} =
  219. while true:
  220. let res = results.recv()
  221. case res.section
  222. of ideNone: break
  223. of ideMsg: echo res.doc
  224. of ideKnown: echo res.quality == 1
  225. of ideProject: echo res.filePath
  226. else: echo res
  227. proc toSocket(stdoutSocket: Socket) {.gcsafe.} =
  228. while true:
  229. let res = results.recv()
  230. case res.section
  231. of ideNone: break
  232. of ideMsg: stdoutSocket.send(res.doc & "\c\L")
  233. of ideKnown: stdoutSocket.send($(res.quality == 1) & "\c\L")
  234. of ideProject: stdoutSocket.send(res.filePath & "\c\L")
  235. else: stdoutSocket.send($res & "\c\L")
  236. proc toEpc(client: Socket; uid: BiggestInt) {.gcsafe.} =
  237. var list = newSList()
  238. while true:
  239. let res = results.recv()
  240. case res.section
  241. of ideNone: break
  242. of ideMsg:
  243. list.add sexp(res.doc)
  244. of ideKnown:
  245. list.add sexp(res.quality == 1)
  246. of ideProject:
  247. list.add sexp(res.filePath)
  248. else:
  249. list.add sexp(res)
  250. returnEpc(client, uid, list)
  251. template setVerbosity(level: typed) =
  252. gVerbosity = level
  253. conf.notes = NotesVerbosity[gVerbosity]
  254. proc connectToNextFreePort(server: Socket, host: string): Port =
  255. server.bindAddr(Port(0), host)
  256. let (_, port) = server.getLocalAddr
  257. result = port
  258. type
  259. ThreadParams = tuple[port: Port; address: string]
  260. proc replStdinSingleCmd(line: string) =
  261. requests.send line
  262. toStdout()
  263. echo ""
  264. flushFile(stdout)
  265. proc replStdin(x: ThreadParams) {.thread.} =
  266. if gEmitEof:
  267. echo DummyEof
  268. while true:
  269. let line = readLine(stdin)
  270. requests.send line
  271. if line == "quit": break
  272. toStdout()
  273. echo DummyEof
  274. flushFile(stdout)
  275. else:
  276. echo Help
  277. var line = ""
  278. while readLineFromStdin("> ", line):
  279. replStdinSingleCmd(line)
  280. requests.send "quit"
  281. proc replCmdline(x: ThreadParams) {.thread.} =
  282. replStdinSingleCmd(x.address)
  283. requests.send "quit"
  284. proc replTcp(x: ThreadParams) {.thread.} =
  285. var server = newSocket()
  286. if gAutoBind:
  287. let port = server.connectToNextFreePort(x.address)
  288. server.listen()
  289. echo port
  290. stdout.flushFile()
  291. else:
  292. server.bindAddr(x.port, x.address)
  293. server.listen()
  294. var inp = ""
  295. var stdoutSocket: Socket
  296. while true:
  297. accept(server, stdoutSocket)
  298. stdoutSocket.readLine(inp)
  299. requests.send inp
  300. toSocket(stdoutSocket)
  301. stdoutSocket.send("\c\L")
  302. stdoutSocket.close()
  303. proc argsToStr(x: SexpNode): string =
  304. if x.kind != SList: return x.getStr
  305. doAssert x.kind == SList
  306. doAssert x.len >= 4
  307. let file = x[0].getStr
  308. let line = x[1].getNum
  309. let col = x[2].getNum
  310. let dirty = x[3].getStr
  311. result = x[0].getStr.escape
  312. if dirty.len > 0:
  313. result.add ';'
  314. result.add dirty.escape
  315. result.add ':'
  316. result.addInt line
  317. result.add ':'
  318. result.addInt col
  319. proc replEpc(x: ThreadParams) {.thread.} =
  320. var server = newSocket()
  321. let port = connectToNextFreePort(server, "localhost")
  322. server.listen()
  323. echo port
  324. stdout.flushFile()
  325. var client: Socket
  326. # Wait for connection
  327. accept(server, client)
  328. while true:
  329. var
  330. sizeHex = ""
  331. size = 0
  332. messageBuffer = ""
  333. checkSanity(client, sizeHex, size, messageBuffer)
  334. let
  335. message = parseSexp($messageBuffer)
  336. epcApi = message[0].getSymbol
  337. case epcApi
  338. of "call":
  339. let
  340. uid = message[1].getNum
  341. cmd = message[2].getSymbol
  342. args = message[3]
  343. when false:
  344. x.ideCmd[] = parseIdeCmd(message[2].getSymbol)
  345. case x.ideCmd[]
  346. of ideSug, ideCon, ideDef, ideUse, ideDus, ideOutline, ideHighlight:
  347. setVerbosity(0)
  348. else: discard
  349. let fullCmd = cmd & " " & args.argsToStr
  350. myLog "MSG CMD: " & fullCmd
  351. requests.send(fullCmd)
  352. toEpc(client, uid)
  353. of "methods":
  354. returnEpc(client, message[1].getNum, listEpc())
  355. of "epc-error":
  356. # an unhandled exception forces down the whole process anyway, so we
  357. # use 'quit' here instead of 'raise'
  358. quit("received epc error: " & $messageBuffer)
  359. else:
  360. let errMessage = case epcApi
  361. of "return", "return-error":
  362. "no return expected"
  363. else:
  364. "unexpected call: " & epcApi
  365. quit errMessage
  366. proc execCmd(cmd: string; graph: ModuleGraph; cachedMsgs: CachedMsgs) =
  367. let conf = graph.config
  368. template sentinel() =
  369. # send sentinel for the input reading thread:
  370. results.send(Suggest(section: ideNone))
  371. template toggle(sw) =
  372. if sw in conf.globalOptions:
  373. excl(conf.globalOptions, sw)
  374. else:
  375. incl(conf.globalOptions, sw)
  376. sentinel()
  377. return
  378. template err() =
  379. echo Help
  380. sentinel()
  381. return
  382. var opc = ""
  383. var i = parseIdent(cmd, opc, 0)
  384. case opc.normalize
  385. of "sug": conf.ideCmd = ideSug
  386. of "con": conf.ideCmd = ideCon
  387. of "def": conf.ideCmd = ideDef
  388. of "use": conf.ideCmd = ideUse
  389. of "dus": conf.ideCmd = ideDus
  390. of "mod": conf.ideCmd = ideMod
  391. of "chk": conf.ideCmd = ideChk
  392. of "highlight": conf.ideCmd = ideHighlight
  393. of "outline": conf.ideCmd = ideOutline
  394. of "quit":
  395. sentinel()
  396. quit()
  397. of "debug": toggle optIdeDebug
  398. of "terse": toggle optIdeTerse
  399. of "known": conf.ideCmd = ideKnown
  400. of "project": conf.ideCmd = ideProject
  401. of "changed": conf.ideCmd = ideChanged
  402. of "globalsymbols": conf.ideCmd = ideGlobalSymbols
  403. of "chkfile": conf.ideCmd = ideChkFile
  404. of "recompile": conf.ideCmd = ideRecompile
  405. else: err()
  406. var dirtyfile = ""
  407. var orig = ""
  408. i += skipWhitespace(cmd, i)
  409. if i < cmd.len and cmd[i] in {'0'..'9'}:
  410. orig = string conf.projectFull
  411. else:
  412. i = parseQuoted(cmd, orig, i)
  413. if i < cmd.len and cmd[i] == ';':
  414. i = parseQuoted(cmd, dirtyfile, i+1)
  415. i += skipWhile(cmd, seps, i)
  416. var line = 0
  417. var col = -1
  418. i += parseInt(cmd, line, i)
  419. i += skipWhile(cmd, seps, i)
  420. i += parseInt(cmd, col, i)
  421. if conf.ideCmd == ideKnown:
  422. results.send(Suggest(section: ideKnown, quality: ord(fileInfoKnown(conf, AbsoluteFile orig))))
  423. elif conf.ideCmd == ideProject:
  424. results.send(Suggest(section: ideProject, filePath: string conf.projectFull))
  425. else:
  426. if conf.ideCmd == ideChk:
  427. for cm in cachedMsgs: errorHook(conf, cm.info, cm.msg, cm.sev)
  428. execute(conf.ideCmd, AbsoluteFile orig, AbsoluteFile dirtyfile, line, col, graph)
  429. sentinel()
  430. template benchmark(benchmarkName: string, code: untyped) =
  431. block:
  432. myLog "Started [" & benchmarkName & "]..."
  433. let t0 = epochTime()
  434. code
  435. let elapsed = epochTime() - t0
  436. let elapsedStr = elapsed.formatFloat(format = ffDecimal, precision = 3)
  437. myLog "CPU Time [" & benchmarkName & "] " & elapsedStr & "s"
  438. proc recompileFullProject(graph: ModuleGraph) =
  439. benchmark "Recompilation(clean)":
  440. graph.resetForBackend()
  441. graph.resetSystemArtifacts()
  442. graph.vm = nil
  443. graph.resetAllModules()
  444. GC_fullCollect()
  445. graph.compileProject()
  446. proc mainThread(graph: ModuleGraph) =
  447. let conf = graph.config
  448. if gLogging:
  449. for it in conf.searchPaths:
  450. log(it.string)
  451. proc wrHook(line: string) {.closure.} =
  452. if gMode == mepc:
  453. if gLogging: log(line)
  454. else:
  455. writelnToChannel(line)
  456. conf.writelnHook = wrHook
  457. conf.suggestionResultHook = sugResultHook
  458. graph.doStopCompile = proc (): bool = requests.peek() > 0
  459. var idle = 0
  460. var cachedMsgs: CachedMsgs = @[]
  461. while true:
  462. let (hasData, req) = requests.tryRecv()
  463. if hasData:
  464. conf.writelnHook = wrHook
  465. conf.suggestionResultHook = sugResultHook
  466. execCmd(req, graph, cachedMsgs)
  467. idle = 0
  468. else:
  469. os.sleep 250
  470. idle += 1
  471. if idle == 20 and gRefresh and conf.suggestVersion != 3:
  472. # we use some nimsuggest activity to enable a lazy recompile:
  473. conf.ideCmd = ideChk
  474. conf.writelnHook = proc (s: string) = discard
  475. cachedMsgs.setLen 0
  476. conf.structuredErrorHook = proc (conf: ConfigRef; info: TLineInfo; msg: string; sev: Severity) =
  477. cachedMsgs.add(CachedMsg(info: info, msg: msg, sev: sev))
  478. conf.suggestionResultHook = proc (s: Suggest) = discard
  479. recompileFullProject(graph)
  480. var
  481. inputThread: Thread[ThreadParams]
  482. proc mainCommand(graph: ModuleGraph) =
  483. let conf = graph.config
  484. clearPasses(graph)
  485. registerPass graph, verbosePass
  486. registerPass graph, semPass
  487. conf.setCmd cmdIdeTools
  488. wantMainModule(conf)
  489. if not fileExists(conf.projectFull):
  490. quit "cannot find file: " & conf.projectFull.string
  491. add(conf.searchPaths, conf.libpath)
  492. conf.setErrorMaxHighMaybe # honor --errorMax even if it may not make sense here
  493. # do not print errors, but log them
  494. conf.writelnHook = proc (msg: string) = discard
  495. if graph.config.suggestVersion == 3:
  496. graph.config.structuredErrorHook = proc (conf: ConfigRef; info: TLineInfo; msg: string; sev: Severity) =
  497. let suggest = Suggest(section: ideChk, filePath: toFullPath(conf, info),
  498. line: toLinenumber(info), column: toColumn(info), doc: msg, forth: $sev)
  499. graph.suggestErrors.mgetOrPut(info.fileIndex, @[]).add suggest
  500. # compile the project before showing any input so that we already
  501. # can answer questions right away:
  502. benchmark "Initial compilation":
  503. compileProject(graph)
  504. open(requests)
  505. open(results)
  506. case gMode
  507. of mstdin: createThread(inputThread, replStdin, (gPort, gAddress))
  508. of mtcp: createThread(inputThread, replTcp, (gPort, gAddress))
  509. of mepc: createThread(inputThread, replEpc, (gPort, gAddress))
  510. of mcmdsug: createThread(inputThread, replCmdline,
  511. (gPort, "sug \"" & conf.projectFull.string & "\":" & gAddress))
  512. of mcmdcon: createThread(inputThread, replCmdline,
  513. (gPort, "con \"" & conf.projectFull.string & "\":" & gAddress))
  514. mainThread(graph)
  515. joinThread(inputThread)
  516. close(requests)
  517. close(results)
  518. proc processCmdLine*(pass: TCmdLinePass, cmd: string; conf: ConfigRef) =
  519. var p = parseopt.initOptParser(cmd)
  520. var findProject = false
  521. while true:
  522. parseopt.next(p)
  523. case p.kind
  524. of cmdEnd: break
  525. of cmdLongOption, cmdShortOption:
  526. case p.key.normalize
  527. of "help", "h":
  528. stdout.writeLine(Usage)
  529. quit()
  530. of "autobind":
  531. gMode = mtcp
  532. gAutoBind = true
  533. of "port":
  534. gPort = parseInt(p.val).Port
  535. gMode = mtcp
  536. of "address":
  537. gAddress = p.val
  538. gMode = mtcp
  539. of "stdin": gMode = mstdin
  540. of "cmdsug":
  541. gMode = mcmdsug
  542. gAddress = p.val
  543. incl(conf.globalOptions, optIdeDebug)
  544. of "cmdcon":
  545. gMode = mcmdcon
  546. gAddress = p.val
  547. incl(conf.globalOptions, optIdeDebug)
  548. of "epc":
  549. gMode = mepc
  550. conf.verbosity = 0 # Port number gotta be first.
  551. of "debug": incl(conf.globalOptions, optIdeDebug)
  552. of "v1": conf.suggestVersion = 1
  553. of "v2": conf.suggestVersion = 0
  554. of "v3": conf.suggestVersion = 3
  555. of "tester":
  556. gMode = mstdin
  557. gEmitEof = true
  558. gRefresh = false
  559. of "log": gLogging = true
  560. of "refresh":
  561. if p.val.len > 0:
  562. gRefresh = parseBool(p.val)
  563. else:
  564. gRefresh = true
  565. of "maxresults":
  566. conf.suggestMaxResults = parseInt(p.val)
  567. of "find":
  568. findProject = true
  569. else: processSwitch(pass, p, conf)
  570. of cmdArgument:
  571. let a = unixToNativePath(p.key)
  572. if dirExists(a) and not fileExists(a.addFileExt("nim")):
  573. conf.projectName = findProjectNimFile(conf, a)
  574. # don't make it worse, report the error the old way:
  575. if conf.projectName.len == 0: conf.projectName = a
  576. else:
  577. if findProject:
  578. conf.projectName = findProjectNimFile(conf, a.parentDir())
  579. if conf.projectName.len == 0:
  580. conf.projectName = a
  581. else:
  582. conf.projectName = a
  583. # if processArgument(pass, p, argsCount): break
  584. proc handleCmdLine(cache: IdentCache; conf: ConfigRef) =
  585. let self = NimProg(
  586. suggestMode: true,
  587. processCmdLine: processCmdLine
  588. )
  589. self.initDefinesProg(conf, "nimsuggest")
  590. if paramCount() == 0:
  591. stdout.writeLine(Usage)
  592. return
  593. self.processCmdLineAndProjectPath(conf)
  594. if gMode != mstdin:
  595. conf.writelnHook = proc (msg: string) = discard
  596. # Find Nim's prefix dir.
  597. let binaryPath = findExe("nim")
  598. if binaryPath == "":
  599. raise newException(IOError,
  600. "Cannot find Nim standard library: Nim compiler not in PATH")
  601. conf.prefixDir = AbsoluteDir binaryPath.splitPath().head.parentDir()
  602. if not dirExists(conf.prefixDir / RelativeDir"lib"):
  603. conf.prefixDir = AbsoluteDir""
  604. #msgs.writelnHook = proc (line: string) = log(line)
  605. myLog("START " & conf.projectFull.string)
  606. var graph = newModuleGraph(cache, conf)
  607. if self.loadConfigsAndProcessCmdLine(cache, conf, graph):
  608. mainCommand(graph)
  609. # v3 start
  610. proc recompilePartially(graph: ModuleGraph, projectFileIdx = InvalidFileIdx) =
  611. if projectFileIdx == InvalidFileIdx:
  612. myLog "Recompiling partially from root"
  613. else:
  614. myLog fmt "Recompiling partially starting from {graph.getModule(projectFileIdx)}"
  615. # inst caches are breaking incremental compilation when the cache caches stuff
  616. # from dirty buffer
  617. # TODO: investigate more efficient way to achieve the same
  618. # graph.typeInstCache.clear()
  619. # graph.procInstCache.clear()
  620. GC_fullCollect()
  621. try:
  622. benchmark "Recompilation":
  623. graph.compileProject(projectFileIdx)
  624. except Exception as e:
  625. myLog fmt "Failed to recompile partially with the following error:\n {e.msg} \n\n {e.getStackTrace()}"
  626. try:
  627. graph.recompileFullProject()
  628. except Exception as e:
  629. myLog fmt "Failed clean recompilation:\n {e.msg} \n\n {e.getStackTrace()}"
  630. proc fileSymbols(graph: ModuleGraph, fileIdx: FileIndex): seq[tuple[sym: PSym, info: TLineInfo]] =
  631. result = graph.suggestSymbols.getOrDefault(fileIdx, @[]).deduplicate
  632. proc findSymData(graph: ModuleGraph, file: AbsoluteFile; line, col: int):
  633. tuple[sym: PSym, info: TLineInfo] =
  634. let
  635. fileIdx = fileInfoIdx(graph.config, file)
  636. trackPos = newLineInfo(fileIdx, line, col)
  637. for (sym, info) in graph.fileSymbols(fileIdx):
  638. if isTracked(info, trackPos, sym.name.s.len):
  639. return (sym, info)
  640. proc markDirtyIfNeeded(graph: ModuleGraph, file: string, originalFileIdx: FileIndex) =
  641. let sha = $sha1.secureHashFile(file)
  642. if graph.config.m.fileInfos[originalFileIdx.int32].hash != sha or graph.config.ideCmd == ideSug:
  643. myLog fmt "{file} changed compared to last compilation"
  644. graph.markDirty originalFileIdx
  645. graph.markClientsDirty originalFileIdx
  646. else:
  647. myLog fmt "No changes in file {file} compared to last compilation"
  648. proc suggestResult(graph: ModuleGraph, sym: PSym, info: TLineInfo, defaultSection = ideNone) =
  649. let section = if defaultSection != ideNone:
  650. defaultSection
  651. elif sym.info == info:
  652. ideDef
  653. else:
  654. ideUse
  655. let suggest = symToSuggest(graph, sym, isLocal=false, section,
  656. info, 100, PrefixMatch.None, false, 0)
  657. suggestResult(graph.config, suggest)
  658. const
  659. # kinds for ideOutline and ideGlobalSymbols
  660. searchableSymKinds = {skField, skEnumField, skIterator, skMethod, skFunc, skProc, skConverter, skTemplate}
  661. proc executeNoHooksV3(cmd: IdeCmd, file: AbsoluteFile, dirtyfile: AbsoluteFile, line, col: int;
  662. graph: ModuleGraph) =
  663. let conf = graph.config
  664. conf.writelnHook = proc (s: string) = discard
  665. conf.structuredErrorHook = proc (conf: ConfigRef; info: TLineInfo;
  666. msg: string; sev: Severity) =
  667. let suggest = Suggest(section: ideChk, filePath: toFullPath(conf, info),
  668. line: toLinenumber(info), column: toColumn(info), doc: msg, forth: $sev)
  669. graph.suggestErrors.mgetOrPut(info.fileIndex, @[]).add suggest
  670. conf.ideCmd = cmd
  671. myLog fmt "cmd: {cmd}, file: {file}[{line}:{col}], dirtyFile: {dirtyfile}"
  672. var fileIndex: FileIndex
  673. if not (cmd in {ideRecompile, ideGlobalSymbols}):
  674. if not fileInfoKnown(conf, file):
  675. myLog fmt "{file} is unknown, returning no results"
  676. return
  677. fileIndex = fileInfoIdx(conf, file)
  678. msgs.setDirtyFile(
  679. conf,
  680. fileIndex,
  681. if dirtyfile.isEmpty: AbsoluteFile"" else: dirtyfile)
  682. if not dirtyfile.isEmpty:
  683. graph.markDirtyIfNeeded(dirtyFile.string, fileInfoIdx(conf, file))
  684. # these commands require fully compiled project
  685. if cmd in {ideUse, ideDus, ideGlobalSymbols, ideChk} and graph.needsCompilation():
  686. graph.recompilePartially()
  687. # when doing incremental build for the project root we should make sure that
  688. # everything is unmarked as no longer beeing dirty in case there is no
  689. # longer reference to a particular module. E. g. A depends on B, B is marked
  690. # as dirty and A loses B import.
  691. graph.unmarkAllDirty()
  692. # these commands require partially compiled project
  693. elif cmd in {ideSug, ideOutline, ideHighlight, ideDef, ideChkFile} and
  694. (graph.needsCompilation(fileIndex) or cmd == ideSug):
  695. # for ideSug use v2 implementation
  696. if cmd == ideSug:
  697. conf.m.trackPos = newLineInfo(fileIndex, line, col)
  698. conf.m.trackPosAttached = false
  699. else:
  700. conf.m.trackPos = default(TLineInfo)
  701. graph.recompilePartially(fileIndex)
  702. case cmd
  703. of ideDef:
  704. let (sym, info) = graph.findSymData(file, line, col)
  705. if sym != nil:
  706. graph.suggestResult(sym, sym.info)
  707. of ideUse, ideDus:
  708. let symbol = graph.findSymData(file, line, col).sym
  709. if symbol != nil:
  710. for (sym, info) in graph.suggestSymbolsIter:
  711. if sym == symbol:
  712. graph.suggestResult(sym, info)
  713. of ideHighlight:
  714. let sym = graph.findSymData(file, line, col).sym
  715. if sym != nil:
  716. let usages = graph.fileSymbols(fileIndex).filterIt(it.sym == sym)
  717. myLog fmt "Found {usages.len} usages in {file.string}"
  718. for (sym, info) in usages:
  719. graph.suggestResult(sym, info)
  720. of ideRecompile:
  721. graph.recompileFullProject()
  722. of ideChanged:
  723. graph.markDirtyIfNeeded(file.string, fileIndex)
  724. of ideSug:
  725. # ideSug performs partial build of the file, thus mark it dirty for the
  726. # future calls.
  727. graph.markDirtyIfNeeded(file.string, fileIndex)
  728. of ideOutline:
  729. let
  730. module = graph.getModule fileIndex
  731. symbols = graph.fileSymbols(fileIndex)
  732. .filterIt(it.sym.info == it.info and
  733. (it.sym.owner == module or
  734. it.sym.kind in searchableSymKinds))
  735. for (sym, _) in symbols:
  736. suggestResult(
  737. conf,
  738. symToSuggest(graph, sym, false,
  739. ideOutline, sym.info, 100, PrefixMatch.None, false, 0))
  740. of ideChk:
  741. myLog fmt "Reporting errors for {graph.suggestErrors.len} file(s)"
  742. for sug in graph.suggestErrorsIter:
  743. suggestResult(graph.config, sug)
  744. of ideChkFile:
  745. let errors = graph.suggestErrors.getOrDefault(fileIndex, @[])
  746. myLog fmt "Reporting {errors.len} error(s) for {file.string}"
  747. for error in errors:
  748. suggestResult(graph.config, error)
  749. of ideGlobalSymbols:
  750. var counter = 0
  751. for (sym, info) in graph.suggestSymbolsIter:
  752. if sfGlobal in sym.flags or sym.kind in searchableSymKinds:
  753. if contains(sym.name.s, file.string):
  754. inc counter
  755. suggestResult(conf,
  756. symToSuggest(graph, sym, isLocal=false,
  757. ideDef, info, 100, PrefixMatch.None, false, 0))
  758. # stop after first 100 results
  759. if counter > 100:
  760. break
  761. else:
  762. myLog fmt "Discarding {cmd}"
  763. # v3 end
  764. when isMainModule:
  765. handleCmdLine(newIdentCache(), newConfigRef())
  766. else:
  767. export Suggest
  768. export IdeCmd
  769. export AbsoluteFile
  770. type NimSuggest* = ref object
  771. graph: ModuleGraph
  772. idle: int
  773. cachedMsgs: CachedMsgs
  774. proc initNimSuggest*(project: string, nimPath: string = ""): NimSuggest =
  775. var retval: ModuleGraph
  776. proc mockCommand(graph: ModuleGraph) =
  777. retval = graph
  778. let conf = graph.config
  779. clearPasses(graph)
  780. registerPass graph, verbosePass
  781. registerPass graph, semPass
  782. conf.setCmd cmdIdeTools
  783. wantMainModule(conf)
  784. if not fileExists(conf.projectFull):
  785. quit "cannot find file: " & conf.projectFull.string
  786. add(conf.searchPaths, conf.libpath)
  787. conf.setErrorMaxHighMaybe
  788. # do not print errors, but log them
  789. conf.writelnHook = myLog
  790. conf.structuredErrorHook = nil
  791. # compile the project before showing any input so that we already
  792. # can answer questions right away:
  793. compileProject(graph)
  794. proc mockCmdLine(pass: TCmdLinePass, cmd: string; conf: ConfigRef) =
  795. conf.suggestVersion = 0
  796. let a = unixToNativePath(project)
  797. if dirExists(a) and not fileExists(a.addFileExt("nim")):
  798. conf.projectName = findProjectNimFile(conf, a)
  799. # don't make it worse, report the error the old way:
  800. if conf.projectName.len == 0: conf.projectName = a
  801. else:
  802. conf.projectName = a
  803. # if processArgument(pass, p, argsCount): break
  804. let
  805. cache = newIdentCache()
  806. conf = newConfigRef()
  807. self = NimProg(
  808. suggestMode: true,
  809. processCmdLine: mockCmdLine
  810. )
  811. self.initDefinesProg(conf, "nimsuggest")
  812. self.processCmdLineAndProjectPath(conf)
  813. if gMode != mstdin:
  814. conf.writelnHook = proc (msg: string) = discard
  815. # Find Nim's prefix dir.
  816. if nimPath == "":
  817. let binaryPath = findExe("nim")
  818. if binaryPath == "":
  819. raise newException(IOError,
  820. "Cannot find Nim standard library: Nim compiler not in PATH")
  821. conf.prefixDir = AbsoluteDir binaryPath.splitPath().head.parentDir()
  822. if not dirExists(conf.prefixDir / RelativeDir"lib"):
  823. conf.prefixDir = AbsoluteDir""
  824. else:
  825. conf.prefixDir = AbsoluteDir nimPath
  826. #msgs.writelnHook = proc (line: string) = log(line)
  827. myLog("START " & conf.projectFull.string)
  828. var graph = newModuleGraph(cache, conf)
  829. if self.loadConfigsAndProcessCmdLine(cache, conf, graph):
  830. mockCommand(graph)
  831. if gLogging:
  832. log("Search paths:")
  833. for it in conf.searchPaths:
  834. log(" " & it.string)
  835. retval.doStopCompile = proc (): bool = false
  836. return NimSuggest(graph: retval, idle: 0, cachedMsgs: @[])
  837. proc runCmd*(nimsuggest: NimSuggest, cmd: IdeCmd, file, dirtyfile: AbsoluteFile, line, col: int): seq[Suggest] =
  838. var retval: seq[Suggest] = @[]
  839. let conf = nimsuggest.graph.config
  840. conf.ideCmd = cmd
  841. conf.writelnHook = proc (line: string) =
  842. retval.add(Suggest(section: ideMsg, doc: line))
  843. conf.suggestionResultHook = proc (s: Suggest) =
  844. retval.add(s)
  845. conf.writelnHook = proc (s: string) =
  846. stderr.write s & "\n"
  847. if conf.ideCmd == ideKnown:
  848. retval.add(Suggest(section: ideKnown, quality: ord(fileInfoKnown(conf, file))))
  849. elif conf.ideCmd == ideProject:
  850. retval.add(Suggest(section: ideProject, filePath: string conf.projectFull))
  851. else:
  852. if conf.ideCmd == ideChk:
  853. for cm in nimsuggest.cachedMsgs: errorHook(conf, cm.info, cm.msg, cm.sev)
  854. if conf.ideCmd == ideChk:
  855. conf.structuredErrorHook = proc (conf: ConfigRef; info: TLineInfo; msg: string; sev: Severity) =
  856. retval.add(Suggest(section: ideChk, filePath: toFullPath(conf, info),
  857. line: toLinenumber(info), column: toColumn(info), doc: msg,
  858. forth: $sev))
  859. else:
  860. conf.structuredErrorHook = nil
  861. executeNoHooks(conf.ideCmd, file, dirtyfile, line, col, nimsuggest.graph)
  862. return retval