suggest.nim 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768
  1. #
  2. #
  3. # The Nim Compiler
  4. # (c) Copyright 2015 Andreas Rumpf
  5. #
  6. # See the file "copying.txt", included in this
  7. # distribution, for details about the copyright.
  8. #
  9. ## This file implements features required for IDE support.
  10. ##
  11. ## Due to Nim's nature and the fact that ``system.nim`` is always imported,
  12. ## there are lots of potential symbols. Furthermore thanks to templates and
  13. ## macros even context based analysis does not help much: In a context like
  14. ## ``let x: |`` where a type has to follow, that type might be constructed from
  15. ## a template like ``extractField(MyObject, fieldName)``. We deal with this
  16. ## problem by smart sorting so that the likely symbols come first. This sorting
  17. ## is done this way:
  18. ##
  19. ## - If there is a prefix (foo|), symbols starting with this prefix come first.
  20. ## - If the prefix is part of the name (but the name doesn't start with it),
  21. ## these symbols come second.
  22. ## - If we have a prefix, only symbols matching this prefix are returned and
  23. ## nothing else.
  24. ## - If we have no prefix, consider the context. We currently distinguish
  25. ## between type and non-type contexts.
  26. ## - Finally, sort matches by relevance. The relevance is determined by the
  27. ## number of usages, so ``strutils.replace`` comes before
  28. ## ``strutils.wordWrap``.
  29. ## - In any case, sorting also considers scoping information. Local variables
  30. ## get high priority.
  31. # included from sigmatch.nim
  32. import algorithm, sets, prefixmatches, parseutils, tables
  33. from wordrecg import wDeprecated, wError, wAddr, wYield
  34. when defined(nimsuggest):
  35. import passes, tables, pathutils # importer
  36. const
  37. sep = '\t'
  38. #template sectionSuggest(): expr = "##begin\n" & getStackTrace() & "##end\n"
  39. template origModuleName(m: PSym): string = m.name.s
  40. proc findDocComment(n: PNode): PNode =
  41. if n == nil: return nil
  42. if n.comment.len > 0: return n
  43. if n.kind in {nkStmtList, nkStmtListExpr, nkObjectTy, nkRecList} and n.len > 0:
  44. result = findDocComment(n[0])
  45. if result != nil: return
  46. if n.len > 1:
  47. result = findDocComment(n[1])
  48. elif n.kind in {nkAsgn, nkFastAsgn} and n.len == 2:
  49. result = findDocComment(n[1])
  50. proc extractDocComment(g: ModuleGraph; s: PSym): string =
  51. var n = findDocComment(s.ast)
  52. if n.isNil and s.kind in routineKinds and s.ast != nil:
  53. n = findDocComment(getBody(g, s))
  54. if not n.isNil:
  55. result = n.comment.replace("\n##", "\n").strip
  56. else:
  57. result = ""
  58. proc cmpSuggestions(a, b: Suggest): int =
  59. template cf(field) {.dirty.} =
  60. result = b.field.int - a.field.int
  61. if result != 0: return result
  62. cf prefix
  63. cf contextFits
  64. cf scope
  65. # when the first type matches, it's better when it's a generic match:
  66. cf quality
  67. cf localUsages
  68. cf globalUsages
  69. # if all is equal, sort alphabetically for deterministic output,
  70. # independent of hashing order:
  71. result = cmp(a.name[], b.name[])
  72. proc getTokenLenFromSource(conf: ConfigRef; ident: string; info: TLineInfo): int =
  73. let
  74. line = sourceLine(conf, info)
  75. column = toColumn(info)
  76. proc isOpeningBacktick(col: int): bool =
  77. if col >= 0 and col < line.len:
  78. if line[col] == '`':
  79. not isOpeningBacktick(col - 1)
  80. else:
  81. isOpeningBacktick(col - 1)
  82. else:
  83. false
  84. if column > line.len:
  85. result = 0
  86. elif column > 0 and line[column - 1] == '`' and isOpeningBacktick(column - 1):
  87. result = skipUntil(line, '`', column)
  88. if cmpIgnoreStyle(line[column..column + result - 1], ident) != 0:
  89. result = 0
  90. elif ident[0] in linter.Letters and ident[^1] != '=':
  91. result = identLen(line, column)
  92. if cmpIgnoreStyle(line[column..column + result - 1], ident[0..min(result-1,len(ident)-1)]) != 0:
  93. result = 0
  94. else:
  95. var sourceIdent: string
  96. result = parseWhile(line, sourceIdent,
  97. OpChars + {'[', '(', '{', ']', ')', '}'}, column)
  98. if ident[^1] == '=' and ident[0] in linter.Letters:
  99. if sourceIdent != "=":
  100. result = 0
  101. elif sourceIdent.len > ident.len and sourceIdent[0..ident.high] == ident:
  102. result = ident.len
  103. elif sourceIdent != ident:
  104. result = 0
  105. proc symToSuggest*(g: ModuleGraph; s: PSym, isLocal: bool, section: IdeCmd, info: TLineInfo;
  106. quality: range[0..100]; prefix: PrefixMatch;
  107. inTypeContext: bool; scope: int;
  108. useSuppliedInfo = false,
  109. endLine: uint16 = 0,
  110. endCol = 0): Suggest =
  111. new(result)
  112. result.section = section
  113. result.quality = quality
  114. result.isGlobal = sfGlobal in s.flags
  115. result.prefix = prefix
  116. result.contextFits = inTypeContext == (s.kind in {skType, skGenericParam})
  117. result.scope = scope
  118. result.name = addr s.name.s
  119. when defined(nimsuggest):
  120. result.globalUsages = s.allUsages.len
  121. var c = 0
  122. for u in s.allUsages:
  123. if u.fileIndex == info.fileIndex: inc c
  124. result.localUsages = c
  125. result.symkind = byte s.kind
  126. if optIdeTerse notin g.config.globalOptions:
  127. result.qualifiedPath = @[]
  128. if not isLocal and s.kind != skModule:
  129. let ow = s.owner
  130. if ow != nil and ow.kind != skModule and ow.owner != nil:
  131. let ow2 = ow.owner
  132. result.qualifiedPath.add(ow2.origModuleName)
  133. if ow != nil:
  134. result.qualifiedPath.add(ow.origModuleName)
  135. if s.name.s[0] in OpChars + {'[', '{', '('} or
  136. s.name.id in ord(wAddr)..ord(wYield):
  137. result.qualifiedPath.add('`' & s.name.s & '`')
  138. else:
  139. result.qualifiedPath.add(s.name.s)
  140. if s.typ != nil:
  141. if section == ideInlayHints:
  142. result.forth = typeToString(s.typ, preferInlayHint)
  143. else:
  144. result.forth = typeToString(s.typ)
  145. else:
  146. result.forth = ""
  147. when defined(nimsuggest) and not defined(noDocgen) and not defined(leanCompiler):
  148. result.doc = extractDocComment(g, s)
  149. if s.kind == skModule and s.ast.len != 0 and section != ideHighlight:
  150. result.filePath = toFullPath(g.config, s.ast[0].info)
  151. result.line = 1
  152. result.column = 0
  153. result.tokenLen = 0
  154. else:
  155. let infox =
  156. if useSuppliedInfo or section in {ideUse, ideHighlight, ideOutline, ideDeclaration}:
  157. info
  158. else:
  159. s.info
  160. result.filePath = toFullPath(g.config, infox)
  161. result.line = toLinenumber(infox)
  162. result.column = toColumn(infox)
  163. result.tokenLen = if section notin {ideHighlight, ideInlayHints}:
  164. s.name.s.len
  165. else:
  166. getTokenLenFromSource(g.config, s.name.s, infox)
  167. result.version = g.config.suggestVersion
  168. result.endLine = endLine
  169. result.endCol = endCol
  170. proc `$`*(suggest: SuggestInlayHint): string =
  171. result = $suggest.kind
  172. result.add(sep)
  173. result.add($suggest.line)
  174. result.add(sep)
  175. result.add($suggest.column)
  176. result.add(sep)
  177. result.add(suggest.label)
  178. result.add(sep)
  179. result.add($suggest.paddingLeft)
  180. result.add(sep)
  181. result.add($suggest.paddingRight)
  182. result.add(sep)
  183. result.add($suggest.allowInsert)
  184. result.add(sep)
  185. result.add(suggest.tooltip)
  186. proc `$`*(suggest: Suggest): string =
  187. if suggest.section == ideInlayHints:
  188. result = $suggest.inlayHintInfo
  189. else:
  190. result = $suggest.section
  191. result.add(sep)
  192. if suggest.section == ideHighlight:
  193. if suggest.symkind.TSymKind == skVar and suggest.isGlobal:
  194. result.add("skGlobalVar")
  195. elif suggest.symkind.TSymKind == skLet and suggest.isGlobal:
  196. result.add("skGlobalLet")
  197. else:
  198. result.add($suggest.symkind.TSymKind)
  199. result.add(sep)
  200. result.add($suggest.line)
  201. result.add(sep)
  202. result.add($suggest.column)
  203. result.add(sep)
  204. result.add($suggest.tokenLen)
  205. else:
  206. result.add($suggest.symkind.TSymKind)
  207. result.add(sep)
  208. if suggest.qualifiedPath.len != 0:
  209. result.add(suggest.qualifiedPath.join("."))
  210. result.add(sep)
  211. result.add(suggest.forth)
  212. result.add(sep)
  213. result.add(suggest.filePath)
  214. result.add(sep)
  215. result.add($suggest.line)
  216. result.add(sep)
  217. result.add($suggest.column)
  218. result.add(sep)
  219. when defined(nimsuggest) and not defined(noDocgen) and not defined(leanCompiler):
  220. result.add(suggest.doc.escape)
  221. if suggest.version in {0, 3}:
  222. result.add(sep)
  223. result.add($suggest.quality)
  224. if suggest.section == ideSug:
  225. result.add(sep)
  226. result.add($suggest.prefix)
  227. if (suggest.version == 3 and suggest.section in {ideOutline, ideExpand}):
  228. result.add(sep)
  229. result.add($suggest.endLine)
  230. result.add(sep)
  231. result.add($suggest.endCol)
  232. proc suggestToSuggestInlayHint*(sug: Suggest): SuggestInlayHint =
  233. SuggestInlayHint(
  234. kind: sihkType,
  235. line: sug.line,
  236. column: sug.column + sug.tokenLen,
  237. label: ": " & sug.forth,
  238. paddingLeft: false,
  239. paddingRight: false,
  240. allowInsert: true,
  241. tooltip: ""
  242. )
  243. proc suggestResult*(conf: ConfigRef; s: Suggest) =
  244. if not isNil(conf.suggestionResultHook):
  245. conf.suggestionResultHook(s)
  246. else:
  247. conf.suggestWriteln($s)
  248. proc produceOutput(a: var Suggestions; conf: ConfigRef) =
  249. if conf.ideCmd in {ideSug, ideCon}:
  250. a.sort cmpSuggestions
  251. when defined(debug):
  252. # debug code
  253. writeStackTrace()
  254. if a.len > conf.suggestMaxResults: a.setLen(conf.suggestMaxResults)
  255. if not isNil(conf.suggestionResultHook):
  256. for s in a:
  257. conf.suggestionResultHook(s)
  258. else:
  259. for s in a:
  260. conf.suggestWriteln($s)
  261. proc filterSym(s: PSym; prefix: PNode; res: var PrefixMatch): bool {.inline.} =
  262. proc prefixMatch(s: PSym; n: PNode): PrefixMatch =
  263. case n.kind
  264. of nkIdent: result = n.ident.s.prefixMatch(s.name.s)
  265. of nkSym: result = n.sym.name.s.prefixMatch(s.name.s)
  266. of nkOpenSymChoice, nkClosedSymChoice, nkAccQuoted:
  267. if n.len > 0:
  268. result = prefixMatch(s, n[0])
  269. else: discard
  270. if s.kind != skModule:
  271. if prefix != nil:
  272. res = prefixMatch(s, prefix)
  273. result = res != PrefixMatch.None
  274. else:
  275. result = true
  276. proc filterSymNoOpr(s: PSym; prefix: PNode; res: var PrefixMatch): bool {.inline.} =
  277. result = filterSym(s, prefix, res) and s.name.s[0] in lexer.SymChars and
  278. not isKeyword(s.name)
  279. proc fieldVisible*(c: PContext, f: PSym): bool {.inline.} =
  280. let fmoduleId = getModule(f).id
  281. result = sfExported in f.flags or fmoduleId == c.module.id
  282. if not result:
  283. for module in c.friendModules:
  284. if fmoduleId == module.id: return true
  285. if f.kind == skField:
  286. var symObj = f.owner
  287. if symObj.typ.kind in {tyRef, tyPtr}:
  288. symObj = symObj.typ[0].sym
  289. for scope in allScopes(c.currentScope):
  290. for sym in scope.allowPrivateAccess:
  291. if symObj.id == sym.id: return true
  292. proc getQuality(s: PSym): range[0..100] =
  293. result = 100
  294. if s.typ != nil and s.typ.len > 1:
  295. var exp = s.typ[1].skipTypes({tyGenericInst, tyVar, tyLent, tyAlias, tySink})
  296. if exp.kind == tyVarargs: exp = elemType(exp)
  297. if exp.kind in {tyUntyped, tyTyped, tyGenericParam, tyAnything}: result = 50
  298. # penalize deprecated symbols
  299. if sfDeprecated in s.flags:
  300. result = result - 5
  301. proc suggestField(c: PContext, s: PSym; f: PNode; info: TLineInfo; outputs: var Suggestions) =
  302. var pm: PrefixMatch
  303. if filterSym(s, f, pm) and fieldVisible(c, s):
  304. outputs.add(symToSuggest(c.graph, s, isLocal=true, ideSug, info,
  305. s.getQuality, pm, c.inTypeContext > 0, 0))
  306. template wholeSymTab(cond, section: untyped) {.dirty.} =
  307. for (item, scopeN, isLocal) in allSyms(c):
  308. let it = item
  309. var pm: PrefixMatch
  310. if cond:
  311. outputs.add(symToSuggest(c.graph, it, isLocal = isLocal, section, info, getQuality(it),
  312. pm, c.inTypeContext > 0, scopeN))
  313. proc suggestSymList(c: PContext, list, f: PNode; info: TLineInfo, outputs: var Suggestions) =
  314. for i in 0..<list.len:
  315. if list[i].kind == nkSym:
  316. suggestField(c, list[i].sym, f, info, outputs)
  317. #else: InternalError(list.info, "getSymFromList")
  318. proc suggestObject(c: PContext, n, f: PNode; info: TLineInfo, outputs: var Suggestions) =
  319. case n.kind
  320. of nkRecList:
  321. for i in 0..<n.len: suggestObject(c, n[i], f, info, outputs)
  322. of nkRecCase:
  323. if n.len > 0:
  324. suggestObject(c, n[0], f, info, outputs)
  325. for i in 1..<n.len: suggestObject(c, lastSon(n[i]), f, info, outputs)
  326. of nkSym: suggestField(c, n.sym, f, info, outputs)
  327. else: discard
  328. proc nameFits(c: PContext, s: PSym, n: PNode): bool =
  329. var op = if n.kind in nkCallKinds: n[0] else: n
  330. if op.kind in {nkOpenSymChoice, nkClosedSymChoice}: op = op[0]
  331. if op.kind == nkDotExpr: op = op[1]
  332. var opr: PIdent
  333. case op.kind
  334. of nkSym: opr = op.sym.name
  335. of nkIdent: opr = op.ident
  336. else: return false
  337. result = opr.id == s.name.id
  338. proc argsFit(c: PContext, candidate: PSym, n, nOrig: PNode): bool =
  339. case candidate.kind
  340. of OverloadableSyms:
  341. var m = newCandidate(c, candidate, nil)
  342. sigmatch.partialMatch(c, n, nOrig, m)
  343. result = m.state != csNoMatch
  344. else:
  345. result = false
  346. proc suggestCall(c: PContext, n, nOrig: PNode, outputs: var Suggestions) =
  347. let info = n.info
  348. wholeSymTab(filterSym(it, nil, pm) and nameFits(c, it, n) and argsFit(c, it, n, nOrig),
  349. ideCon)
  350. proc suggestVar(c: PContext, n: PNode, outputs: var Suggestions) =
  351. let info = n.info
  352. wholeSymTab(nameFits(c, it, n), ideCon)
  353. proc typeFits(c: PContext, s: PSym, firstArg: PType): bool {.inline.} =
  354. if s.typ != nil and s.typ.len > 1 and s.typ[1] != nil:
  355. # special rule: if system and some weird generic match via 'tyUntyped'
  356. # or 'tyGenericParam' we won't list it either to reduce the noise (nobody
  357. # wants 'system.`-|` as suggestion
  358. let m = s.getModule()
  359. if m != nil and sfSystemModule in m.flags:
  360. if s.kind == skType: return
  361. var exp = s.typ[1].skipTypes({tyGenericInst, tyVar, tyLent, tyAlias, tySink})
  362. if exp.kind == tyVarargs: exp = elemType(exp)
  363. if exp.kind in {tyUntyped, tyTyped, tyGenericParam, tyAnything}: return
  364. result = sigmatch.argtypeMatches(c, s.typ[1], firstArg)
  365. proc suggestOperations(c: PContext, n, f: PNode, typ: PType, outputs: var Suggestions) =
  366. assert typ != nil
  367. let info = n.info
  368. wholeSymTab(filterSymNoOpr(it, f, pm) and typeFits(c, it, typ), ideSug)
  369. proc suggestEverything(c: PContext, n, f: PNode, outputs: var Suggestions) =
  370. # do not produce too many symbols:
  371. for (it, scopeN, isLocal) in allSyms(c):
  372. var pm: PrefixMatch
  373. if filterSym(it, f, pm):
  374. outputs.add(symToSuggest(c.graph, it, isLocal = isLocal, ideSug, n.info,
  375. it.getQuality, pm, c.inTypeContext > 0, scopeN))
  376. proc suggestFieldAccess(c: PContext, n, field: PNode, outputs: var Suggestions) =
  377. # special code that deals with ``myObj.``. `n` is NOT the nkDotExpr-node, but
  378. # ``myObj``.
  379. var typ = n.typ
  380. var pm: PrefixMatch
  381. when defined(nimsuggest):
  382. if n.kind == nkSym and n.sym.kind == skError and c.config.suggestVersion == 0:
  383. # consider 'foo.|' where 'foo' is some not imported module.
  384. let fullPath = findModule(c.config, n.sym.name.s, toFullPath(c.config, n.info))
  385. if fullPath.isEmpty:
  386. # error: no known module name:
  387. typ = nil
  388. else:
  389. let m = c.graph.importModuleCallback(c.graph, c.module, fileInfoIdx(c.config, fullPath))
  390. if m == nil: typ = nil
  391. else:
  392. for it in allSyms(c.graph, n.sym):
  393. if filterSym(it, field, pm):
  394. outputs.add(symToSuggest(c.graph, it, isLocal=false, ideSug,
  395. n.info, it.getQuality, pm,
  396. c.inTypeContext > 0, -100))
  397. outputs.add(symToSuggest(c.graph, m, isLocal=false, ideMod, n.info,
  398. 100, PrefixMatch.None, c.inTypeContext > 0,
  399. -99))
  400. if typ == nil:
  401. # a module symbol has no type for example:
  402. if n.kind == nkSym and n.sym.kind == skModule:
  403. if n.sym == c.module:
  404. # all symbols accessible, because we are in the current module:
  405. for it in items(c.topLevelScope.symbols):
  406. if filterSym(it, field, pm):
  407. outputs.add(symToSuggest(c.graph, it, isLocal=false, ideSug,
  408. n.info, it.getQuality, pm,
  409. c.inTypeContext > 0, -99))
  410. else:
  411. for it in allSyms(c.graph, n.sym):
  412. if filterSym(it, field, pm):
  413. outputs.add(symToSuggest(c.graph, it, isLocal=false, ideSug,
  414. n.info, it.getQuality, pm,
  415. c.inTypeContext > 0, -99))
  416. else:
  417. # fallback:
  418. suggestEverything(c, n, field, outputs)
  419. else:
  420. let orig = typ
  421. typ = skipTypes(orig, {tyTypeDesc, tyGenericInst, tyVar, tyLent, tyPtr, tyRef, tyAlias, tySink, tyOwned})
  422. if typ.kind == tyEnum and n.kind == nkSym and n.sym.kind == skType:
  423. # look up if the identifier belongs to the enum:
  424. var t = typ
  425. while t != nil:
  426. suggestSymList(c, t.n, field, n.info, outputs)
  427. t = t[0]
  428. elif typ.kind == tyObject:
  429. var t = typ
  430. while true:
  431. suggestObject(c, t.n, field, n.info, outputs)
  432. if t[0] == nil: break
  433. t = skipTypes(t[0], skipPtrs)
  434. elif typ.kind == tyTuple and typ.n != nil:
  435. # All tuple fields are in scope
  436. # So go through each field and add it to the suggestions (If it passes the filter)
  437. for node in typ.n:
  438. if node.kind == nkSym:
  439. let s = node.sym
  440. var pm: PrefixMatch
  441. if filterSym(s, field, pm):
  442. outputs.add(symToSuggest(c.graph, s, isLocal=true, ideSug, n.info,
  443. s.getQuality, pm, c.inTypeContext > 0, 0))
  444. suggestOperations(c, n, field, orig, outputs)
  445. if typ != orig:
  446. suggestOperations(c, n, field, typ, outputs)
  447. type
  448. TCheckPointResult* = enum
  449. cpNone, cpFuzzy, cpExact
  450. proc inCheckpoint*(current, trackPos: TLineInfo): TCheckPointResult =
  451. if current.fileIndex == trackPos.fileIndex:
  452. if current.line == trackPos.line and
  453. abs(current.col-trackPos.col) < 4:
  454. return cpExact
  455. if current.line >= trackPos.line:
  456. return cpFuzzy
  457. proc isTracked*(current, trackPos: TLineInfo, tokenLen: int): bool =
  458. if current.fileIndex==trackPos.fileIndex and current.line==trackPos.line:
  459. let col = trackPos.col
  460. if col >= current.col and col <= current.col+tokenLen-1:
  461. return true
  462. when defined(nimsuggest):
  463. # Since TLineInfo defined a == operator that doesn't include the column,
  464. # we map TLineInfo to a unique int here for this lookup table:
  465. proc infoToInt(info: TLineInfo): int64 =
  466. info.fileIndex.int64 + info.line.int64 shl 32 + info.col.int64 shl 48
  467. proc addNoDup(s: PSym; info: TLineInfo) =
  468. # ensure nothing gets too slow:
  469. if s.allUsages.len > 500: return
  470. let infoAsInt = info.infoToInt
  471. for infoB in s.allUsages:
  472. if infoB.infoToInt == infoAsInt: return
  473. s.allUsages.add(info)
  474. proc findUsages(g: ModuleGraph; info: TLineInfo; s: PSym; usageSym: var PSym) =
  475. if g.config.suggestVersion == 1:
  476. if usageSym == nil and isTracked(info, g.config.m.trackPos, s.name.s.len):
  477. usageSym = s
  478. suggestResult(g.config, symToSuggest(g, s, isLocal=false, ideUse, info, 100, PrefixMatch.None, false, 0))
  479. elif s == usageSym:
  480. if g.config.lastLineInfo != info:
  481. suggestResult(g.config, symToSuggest(g, s, isLocal=false, ideUse, info, 100, PrefixMatch.None, false, 0))
  482. g.config.lastLineInfo = info
  483. when defined(nimsuggest):
  484. proc listUsages*(g: ModuleGraph; s: PSym) =
  485. #echo "usages ", s.allUsages.len
  486. for info in s.allUsages:
  487. let x = if info == s.info and info.col == s.info.col: ideDef else: ideUse
  488. suggestResult(g.config, symToSuggest(g, s, isLocal=false, x, info, 100, PrefixMatch.None, false, 0))
  489. proc findDefinition(g: ModuleGraph; info: TLineInfo; s: PSym; usageSym: var PSym) =
  490. if s.isNil: return
  491. if isTracked(info, g.config.m.trackPos, s.name.s.len) or (s == usageSym and sfForward notin s.flags):
  492. suggestResult(g.config, symToSuggest(g, s, isLocal=false, ideDef, info, 100, PrefixMatch.None, false, 0, useSuppliedInfo = s == usageSym))
  493. if sfForward notin s.flags and g.config.suggestVersion < 3:
  494. suggestQuit()
  495. else:
  496. usageSym = s
  497. proc ensureIdx[T](x: var T, y: int) =
  498. if x.len <= y: x.setLen(y+1)
  499. proc ensureSeq[T](x: var seq[T]) =
  500. if x == nil: newSeq(x, 0)
  501. proc suggestSym*(g: ModuleGraph; info: TLineInfo; s: PSym; usageSym: var PSym; isDecl=true) {.inline.} =
  502. ## misnamed: should be 'symDeclared'
  503. let conf = g.config
  504. when defined(nimsuggest):
  505. g.suggestSymbols.mgetOrPut(info.fileIndex, @[]).add SymInfoPair(sym: s, info: info, isDecl: isDecl)
  506. if conf.suggestVersion == 0:
  507. if s.allUsages.len == 0:
  508. s.allUsages = @[info]
  509. else:
  510. s.addNoDup(info)
  511. if conf.ideCmd == ideUse:
  512. findUsages(g, info, s, usageSym)
  513. elif conf.ideCmd == ideDef:
  514. findDefinition(g, info, s, usageSym)
  515. elif conf.ideCmd == ideDus and s != nil:
  516. if isTracked(info, conf.m.trackPos, s.name.s.len):
  517. suggestResult(conf, symToSuggest(g, s, isLocal=false, ideDef, info, 100, PrefixMatch.None, false, 0))
  518. findUsages(g, info, s, usageSym)
  519. elif conf.ideCmd == ideHighlight and info.fileIndex == conf.m.trackPos.fileIndex:
  520. suggestResult(conf, symToSuggest(g, s, isLocal=false, ideHighlight, info, 100, PrefixMatch.None, false, 0))
  521. elif conf.ideCmd == ideOutline and isDecl:
  522. # if a module is included then the info we have is inside the include and
  523. # we need to walk up the owners until we find the outer most module,
  524. # which will be the last skModule prior to an skPackage.
  525. var
  526. parentFileIndex = info.fileIndex # assume we're in the correct module
  527. parentModule = s.owner
  528. while parentModule != nil and parentModule.kind == skModule:
  529. parentFileIndex = parentModule.info.fileIndex
  530. parentModule = parentModule.owner
  531. if parentFileIndex == conf.m.trackPos.fileIndex:
  532. suggestResult(conf, symToSuggest(g, s, isLocal=false, ideOutline, info, 100, PrefixMatch.None, false, 0))
  533. proc extractPragma(s: PSym): PNode =
  534. if s.kind in routineKinds:
  535. result = s.ast[pragmasPos]
  536. elif s.kind in {skType, skVar, skLet}:
  537. if s.ast != nil and s.ast.len > 0:
  538. if s.ast[0].kind == nkPragmaExpr and s.ast[0].len > 1:
  539. # s.ast = nkTypedef / nkPragmaExpr / [nkSym, nkPragma]
  540. result = s.ast[0][1]
  541. doAssert result == nil or result.kind == nkPragma
  542. proc warnAboutDeprecated(conf: ConfigRef; info: TLineInfo; s: PSym) =
  543. var pragmaNode: PNode
  544. pragmaNode = if s.kind == skEnumField: extractPragma(s.owner) else: extractPragma(s)
  545. let name =
  546. if s.kind == skEnumField and sfDeprecated notin s.flags: "enum '" & s.owner.name.s & "' which contains field '" & s.name.s & "'"
  547. else: s.name.s
  548. if pragmaNode != nil:
  549. for it in pragmaNode:
  550. if whichPragma(it) == wDeprecated and it.safeLen == 2 and
  551. it[1].kind in {nkStrLit..nkTripleStrLit}:
  552. message(conf, info, warnDeprecated, it[1].strVal & "; " & name & " is deprecated")
  553. return
  554. message(conf, info, warnDeprecated, name & " is deprecated")
  555. proc userError(conf: ConfigRef; info: TLineInfo; s: PSym) =
  556. let pragmaNode = extractPragma(s)
  557. template bail(prefix: string) =
  558. localError(conf, info, "$1usage of '$2' is an {.error.} defined at $3" %
  559. [prefix, s.name.s, toFileLineCol(conf, s.ast.info)])
  560. if pragmaNode != nil:
  561. for it in pragmaNode:
  562. if whichPragma(it) == wError and it.safeLen == 2 and
  563. it[1].kind in {nkStrLit..nkTripleStrLit}:
  564. bail(it[1].strVal & "; ")
  565. return
  566. bail("")
  567. proc markOwnerModuleAsUsed(c: PContext; s: PSym) =
  568. var module = s
  569. while module != nil and module.kind != skModule:
  570. module = module.owner
  571. if module != nil and module != c.module:
  572. var i = 0
  573. while i <= high(c.unusedImports):
  574. let candidate = c.unusedImports[i][0]
  575. if candidate == module or c.importModuleMap.getOrDefault(candidate.id, int.low) == module.id or
  576. c.exportIndirections.contains((candidate.id, s.id)):
  577. # mark it as used:
  578. c.unusedImports.del(i)
  579. else:
  580. inc i
  581. proc markUsed(c: PContext; info: TLineInfo; s: PSym) =
  582. let conf = c.config
  583. incl(s.flags, sfUsed)
  584. if s.kind == skEnumField and s.owner != nil:
  585. incl(s.owner.flags, sfUsed)
  586. if sfDeprecated in s.owner.flags:
  587. warnAboutDeprecated(conf, info, s)
  588. if {sfDeprecated, sfError} * s.flags != {}:
  589. if sfDeprecated in s.flags:
  590. if not (c.lastTLineInfo.line == info.line and
  591. c.lastTLineInfo.col == info.col):
  592. warnAboutDeprecated(conf, info, s)
  593. c.lastTLineInfo = info
  594. if sfError in s.flags: userError(conf, info, s)
  595. when defined(nimsuggest):
  596. suggestSym(c.graph, info, s, c.graph.usageSym, false)
  597. styleCheckUse(c, info, s)
  598. markOwnerModuleAsUsed(c, s)
  599. proc safeSemExpr*(c: PContext, n: PNode): PNode =
  600. # use only for idetools support!
  601. try:
  602. result = c.semExpr(c, n)
  603. except ERecoverableError:
  604. result = c.graph.emptyNode
  605. proc sugExpr(c: PContext, n: PNode, outputs: var Suggestions) =
  606. if n.kind == nkDotExpr:
  607. var obj = safeSemExpr(c, n[0])
  608. # it can happen that errnously we have collected the fieldname
  609. # of the next line, so we check the 'field' is actually on the same
  610. # line as the object to prevent this from happening:
  611. let prefix = if n.len == 2 and n[1].info.line == n[0].info.line and
  612. not c.config.m.trackPosAttached: n[1] else: nil
  613. suggestFieldAccess(c, obj, prefix, outputs)
  614. #if optIdeDebug in gGlobalOptions:
  615. # echo "expression ", renderTree(obj), " has type ", typeToString(obj.typ)
  616. #writeStackTrace()
  617. elif n.kind == nkIdent:
  618. let
  619. prefix = if c.config.m.trackPosAttached: nil else: n
  620. info = n.info
  621. wholeSymTab(filterSym(it, prefix, pm), ideSug)
  622. else:
  623. let prefix = if c.config.m.trackPosAttached: nil else: n
  624. suggestEverything(c, n, prefix, outputs)
  625. proc suggestExprNoCheck*(c: PContext, n: PNode) =
  626. # This keeps semExpr() from coming here recursively:
  627. if c.compilesContextId > 0: return
  628. inc(c.compilesContextId)
  629. var outputs: Suggestions = @[]
  630. if c.config.ideCmd == ideSug:
  631. sugExpr(c, n, outputs)
  632. elif c.config.ideCmd == ideCon:
  633. if n.kind in nkCallKinds:
  634. var a = copyNode(n)
  635. var x = safeSemExpr(c, n[0])
  636. if x.kind == nkEmpty or x.typ == nil: x = n[0]
  637. a.add x
  638. for i in 1..<n.len:
  639. # use as many typed arguments as possible:
  640. var x = safeSemExpr(c, n[i])
  641. if x.kind == nkEmpty or x.typ == nil: break
  642. a.add x
  643. suggestCall(c, a, n, outputs)
  644. elif n.kind in nkIdentKinds:
  645. var x = safeSemExpr(c, n)
  646. if x.kind == nkEmpty or x.typ == nil: x = n
  647. suggestVar(c, x, outputs)
  648. dec(c.compilesContextId)
  649. if outputs.len > 0 and c.config.ideCmd in {ideSug, ideCon, ideDef}:
  650. produceOutput(outputs, c.config)
  651. suggestQuit()
  652. proc suggestExpr*(c: PContext, n: PNode) =
  653. if exactEquals(c.config.m.trackPos, n.info): suggestExprNoCheck(c, n)
  654. proc suggestDecl*(c: PContext, n: PNode; s: PSym) =
  655. let attached = c.config.m.trackPosAttached
  656. if attached: inc(c.inTypeContext)
  657. defer:
  658. if attached: dec(c.inTypeContext)
  659. suggestExpr(c, n)
  660. proc suggestStmt*(c: PContext, n: PNode) =
  661. suggestExpr(c, n)
  662. proc suggestEnum*(c: PContext; n: PNode; t: PType) =
  663. var outputs: Suggestions = @[]
  664. suggestSymList(c, t.n, nil, n.info, outputs)
  665. produceOutput(outputs, c.config)
  666. if outputs.len > 0: suggestQuit()
  667. proc suggestSentinel*(c: PContext) =
  668. if c.config.ideCmd != ideSug or c.module.position != c.config.m.trackPos.fileIndex.int32: return
  669. if c.compilesContextId > 0: return
  670. inc(c.compilesContextId)
  671. var outputs: Suggestions = @[]
  672. # suggest everything:
  673. for (it, scopeN, isLocal) in allSyms(c):
  674. var pm: PrefixMatch
  675. if filterSymNoOpr(it, nil, pm):
  676. outputs.add(symToSuggest(c.graph, it, isLocal = isLocal, ideSug,
  677. newLineInfo(c.config.m.trackPos.fileIndex, 0, -1), it.getQuality,
  678. PrefixMatch.None, false, scopeN))
  679. dec(c.compilesContextId)
  680. produceOutput(outputs, c.config)
  681. when defined(nimsuggest):
  682. proc onDef(graph: ModuleGraph, s: PSym, info: TLineInfo) =
  683. if graph.config.suggestVersion >= 3 and info.exactEquals(s.info):
  684. suggestSym(graph, info, s, graph.usageSym)
  685. template getPContext(): untyped =
  686. when c is PContext: c
  687. else: c.c
  688. template onDef*(info: TLineInfo; s: PSym) =
  689. let c = getPContext()
  690. onDef(c.graph, s, info)