suggest.nim 28 KB

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