suggest.nim 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587
  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, prefixmatches, lineinfos, pathutils
  33. from wordrecg import wDeprecated, wError
  34. when defined(nimsuggest):
  35. import passes, tables # 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.sons[0])
  45. if result != nil: return
  46. if n.len > 1:
  47. result = findDocComment(n.sons[1])
  48. elif n.kind in {nkAsgn, nkFastAsgn} and n.len == 2:
  49. result = findDocComment(n.sons[1])
  50. proc extractDocComment(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(s.ast[bodyPos])
  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 scope
  63. cf prefix
  64. # when the first type matches, it's better when it's a generic match:
  65. cf quality
  66. cf contextFits
  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 symToSuggest(conf: ConfigRef; s: PSym, isLocal: bool, section: IdeCmd, info: TLineInfo;
  73. quality: range[0..100]; prefix: PrefixMatch;
  74. inTypeContext: bool; scope: int): Suggest =
  75. new(result)
  76. result.section = section
  77. result.quality = quality
  78. result.isGlobal = sfGlobal in s.flags
  79. result.tokenLen = s.name.s.len
  80. result.prefix = prefix
  81. result.contextFits = inTypeContext == (s.kind in {skType, skGenericParam})
  82. result.scope = scope
  83. result.name = addr s.name.s
  84. when defined(nimsuggest):
  85. result.globalUsages = s.allUsages.len
  86. var c = 0
  87. for u in s.allUsages:
  88. if u.fileIndex == info.fileIndex: inc c
  89. result.localUsages = c
  90. result.symkind = byte s.kind
  91. if optIdeTerse notin conf.globalOptions:
  92. result.qualifiedPath = @[]
  93. if not isLocal and s.kind != skModule:
  94. let ow = s.owner
  95. if ow != nil and ow.kind != skModule and ow.owner != nil:
  96. let ow2 = ow.owner
  97. result.qualifiedPath.add(ow2.origModuleName)
  98. if ow != nil:
  99. result.qualifiedPath.add(ow.origModuleName)
  100. result.qualifiedPath.add(s.name.s)
  101. if s.typ != nil:
  102. result.forth = typeToString(s.typ)
  103. else:
  104. result.forth = ""
  105. when defined(nimsuggest) and not defined(noDocgen) and not defined(leanCompiler):
  106. result.doc = s.extractDocComment
  107. let infox = if section in {ideUse, ideHighlight, ideOutline}: info else: s.info
  108. result.filePath = toFullPath(conf, infox)
  109. result.line = toLinenumber(infox)
  110. result.column = toColumn(infox)
  111. result.version = conf.suggestVersion
  112. proc `$`*(suggest: Suggest): string =
  113. result = $suggest.section
  114. result.add(sep)
  115. if suggest.section == ideHighlight:
  116. if suggest.symkind.TSymKind == skVar and suggest.isGlobal:
  117. result.add("skGlobalVar")
  118. elif suggest.symkind.TSymKind == skLet and suggest.isGlobal:
  119. result.add("skGlobalLet")
  120. else:
  121. result.add($suggest.symkind.TSymKind)
  122. result.add(sep)
  123. result.add($suggest.line)
  124. result.add(sep)
  125. result.add($suggest.column)
  126. result.add(sep)
  127. result.add($suggest.tokenLen)
  128. else:
  129. result.add($suggest.symkind.TSymKind)
  130. result.add(sep)
  131. if suggest.qualifiedPath.len != 0:
  132. result.add(suggest.qualifiedPath.join("."))
  133. result.add(sep)
  134. result.add(suggest.forth)
  135. result.add(sep)
  136. result.add(suggest.filePath)
  137. result.add(sep)
  138. result.add($suggest.line)
  139. result.add(sep)
  140. result.add($suggest.column)
  141. result.add(sep)
  142. when defined(nimsuggest) and not defined(noDocgen) and not defined(leanCompiler):
  143. result.add(suggest.doc.escape)
  144. if suggest.version == 0:
  145. result.add(sep)
  146. result.add($suggest.quality)
  147. if suggest.section == ideSug:
  148. result.add(sep)
  149. result.add($suggest.prefix)
  150. proc suggestResult(conf: ConfigRef; s: Suggest) =
  151. if not isNil(conf.suggestionResultHook):
  152. conf.suggestionResultHook(s)
  153. else:
  154. conf.suggestWriteln($s)
  155. proc produceOutput(a: var Suggestions; conf: ConfigRef) =
  156. if conf.ideCmd in {ideSug, ideCon}:
  157. a.sort cmpSuggestions
  158. when defined(debug):
  159. # debug code
  160. writeStackTrace()
  161. if a.len > conf.suggestMaxResults: a.setLen(conf.suggestMaxResults)
  162. if not isNil(conf.suggestionResultHook):
  163. for s in a:
  164. conf.suggestionResultHook(s)
  165. else:
  166. for s in a:
  167. conf.suggestWriteln($s)
  168. proc filterSym(s: PSym; prefix: PNode; res: var PrefixMatch): bool {.inline.} =
  169. proc prefixMatch(s: PSym; n: PNode): PrefixMatch =
  170. case n.kind
  171. of nkIdent: result = n.ident.s.prefixMatch(s.name.s)
  172. of nkSym: result = n.sym.name.s.prefixMatch(s.name.s)
  173. of nkOpenSymChoice, nkClosedSymChoice, nkAccQuoted:
  174. if n.len > 0:
  175. result = prefixMatch(s, n[0])
  176. else: discard
  177. if s.kind != skModule:
  178. if prefix != nil:
  179. res = prefixMatch(s, prefix)
  180. result = res != PrefixMatch.None
  181. else:
  182. result = true
  183. proc filterSymNoOpr(s: PSym; prefix: PNode; res: var PrefixMatch): bool {.inline.} =
  184. result = filterSym(s, prefix, res) and s.name.s[0] in lexer.SymChars and
  185. not isKeyword(s.name)
  186. proc fieldVisible*(c: PContext, f: PSym): bool {.inline.} =
  187. let fmoduleId = getModule(f).id
  188. result = sfExported in f.flags or fmoduleId == c.module.id
  189. for module in c.friendModules:
  190. if fmoduleId == module.id:
  191. result = true
  192. break
  193. proc suggestField(c: PContext, s: PSym; f: PNode; info: TLineInfo; outputs: var Suggestions) =
  194. var pm: PrefixMatch
  195. if filterSym(s, f, pm) and fieldVisible(c, s):
  196. outputs.add(symToSuggest(c.config, s, isLocal=true, ideSug, info, 100, pm, c.inTypeContext > 0, 0))
  197. proc getQuality(s: PSym): range[0..100] =
  198. if s.typ != nil and s.typ.len > 1:
  199. var exp = s.typ.sons[1].skipTypes({tyGenericInst, tyVar, tyLent, tyAlias, tySink})
  200. if exp.kind == tyVarargs: exp = elemType(exp)
  201. if exp.kind in {tyExpr, tyStmt, tyGenericParam, tyAnything}: return 50
  202. return 100
  203. template wholeSymTab(cond, section: untyped) =
  204. var isLocal = true
  205. var scopeN = 0
  206. for scope in walkScopes(c.currentScope):
  207. if scope == c.topLevelScope: isLocal = false
  208. dec scopeN
  209. for item in scope.symbols:
  210. let it {.inject.} = item
  211. var pm {.inject.}: PrefixMatch
  212. if cond:
  213. outputs.add(symToSuggest(c.config, it, isLocal = isLocal, section, info, getQuality(it),
  214. pm, c.inTypeContext > 0, scopeN))
  215. proc suggestSymList(c: PContext, list, f: PNode; info: TLineInfo, outputs: var Suggestions) =
  216. for i in countup(0, sonsLen(list) - 1):
  217. if list.sons[i].kind == nkSym:
  218. suggestField(c, list.sons[i].sym, f, info, outputs)
  219. #else: InternalError(list.info, "getSymFromList")
  220. proc suggestObject(c: PContext, n, f: PNode; info: TLineInfo, outputs: var Suggestions) =
  221. case n.kind
  222. of nkRecList:
  223. for i in countup(0, sonsLen(n)-1): suggestObject(c, n.sons[i], f, info, outputs)
  224. of nkRecCase:
  225. var L = sonsLen(n)
  226. if L > 0:
  227. suggestObject(c, n.sons[0], f, info, outputs)
  228. for i in countup(1, L-1): suggestObject(c, lastSon(n.sons[i]), f, info, outputs)
  229. of nkSym: suggestField(c, n.sym, f, info, outputs)
  230. else: discard
  231. proc nameFits(c: PContext, s: PSym, n: PNode): bool =
  232. var op = n.sons[0]
  233. if op.kind in {nkOpenSymChoice, nkClosedSymChoice}: op = op.sons[0]
  234. var opr: PIdent
  235. case op.kind
  236. of nkSym: opr = op.sym.name
  237. of nkIdent: opr = op.ident
  238. else: return false
  239. result = opr.id == s.name.id
  240. proc argsFit(c: PContext, candidate: PSym, n, nOrig: PNode): bool =
  241. case candidate.kind
  242. of OverloadableSyms:
  243. var m: TCandidate
  244. initCandidate(c, m, candidate, nil)
  245. sigmatch.partialMatch(c, n, nOrig, m)
  246. result = m.state != csNoMatch
  247. else:
  248. result = false
  249. proc suggestCall(c: PContext, n, nOrig: PNode, outputs: var Suggestions) =
  250. let info = n.info
  251. wholeSymTab(filterSym(it, nil, pm) and nameFits(c, it, n) and argsFit(c, it, n, nOrig),
  252. ideCon)
  253. proc typeFits(c: PContext, s: PSym, firstArg: PType): bool {.inline.} =
  254. if s.typ != nil and sonsLen(s.typ) > 1 and s.typ.sons[1] != nil:
  255. # special rule: if system and some weird generic match via 'tyExpr'
  256. # or 'tyGenericParam' we won't list it either to reduce the noise (nobody
  257. # wants 'system.`-|` as suggestion
  258. let m = s.getModule()
  259. if m != nil and sfSystemModule in m.flags:
  260. if s.kind == skType: return
  261. var exp = s.typ.sons[1].skipTypes({tyGenericInst, tyVar, tyLent, tyAlias, tySink})
  262. if exp.kind == tyVarargs: exp = elemType(exp)
  263. if exp.kind in {tyExpr, tyStmt, tyGenericParam, tyAnything}: return
  264. result = sigmatch.argtypeMatches(c, s.typ.sons[1], firstArg)
  265. proc suggestOperations(c: PContext, n, f: PNode, typ: PType, outputs: var Suggestions) =
  266. assert typ != nil
  267. let info = n.info
  268. wholeSymTab(filterSymNoOpr(it, f, pm) and typeFits(c, it, typ), ideSug)
  269. proc suggestEverything(c: PContext, n, f: PNode, outputs: var Suggestions) =
  270. # do not produce too many symbols:
  271. var isLocal = true
  272. var scopeN = 0
  273. for scope in walkScopes(c.currentScope):
  274. if scope == c.topLevelScope: isLocal = false
  275. dec scopeN
  276. for it in items(scope.symbols):
  277. var pm: PrefixMatch
  278. if filterSym(it, f, pm):
  279. outputs.add(symToSuggest(c.config, it, isLocal = isLocal, ideSug, n.info, 0, pm,
  280. c.inTypeContext > 0, scopeN))
  281. #if scope == c.topLevelScope and f.isNil: break
  282. proc suggestFieldAccess(c: PContext, n, field: PNode, outputs: var Suggestions) =
  283. # special code that deals with ``myObj.``. `n` is NOT the nkDotExpr-node, but
  284. # ``myObj``.
  285. var typ = n.typ
  286. var pm: PrefixMatch
  287. when defined(nimsuggest):
  288. if n.kind == nkSym and n.sym.kind == skError and c.config.suggestVersion == 0:
  289. # consider 'foo.|' where 'foo' is some not imported module.
  290. let fullPath = findModule(c.config, n.sym.name.s, toFullPath(c.config, n.info))
  291. if fullPath.isEmpty:
  292. # error: no known module name:
  293. typ = nil
  294. else:
  295. let m = c.graph.importModuleCallback(c.graph, c.module, fileInfoIdx(c.config, fullpath))
  296. if m == nil: typ = nil
  297. else:
  298. for it in items(n.sym.tab):
  299. if filterSym(it, field, pm):
  300. outputs.add(symToSuggest(c.config, it, isLocal=false, ideSug, n.info, 100, pm, c.inTypeContext > 0, -100))
  301. outputs.add(symToSuggest(c.config, m, isLocal=false, ideMod, n.info, 100, PrefixMatch.None,
  302. c.inTypeContext > 0, -99))
  303. if typ == nil:
  304. # a module symbol has no type for example:
  305. if n.kind == nkSym and n.sym.kind == skModule:
  306. if n.sym == c.module:
  307. # all symbols accessible, because we are in the current module:
  308. for it in items(c.topLevelScope.symbols):
  309. if filterSym(it, field, pm):
  310. outputs.add(symToSuggest(c.config, it, isLocal=false, ideSug, n.info, 100, pm, c.inTypeContext > 0, -99))
  311. else:
  312. for it in items(n.sym.tab):
  313. if filterSym(it, field, pm):
  314. outputs.add(symToSuggest(c.config, it, isLocal=false, ideSug, n.info, 100, pm, c.inTypeContext > 0, -99))
  315. else:
  316. # fallback:
  317. suggestEverything(c, n, field, outputs)
  318. elif typ.kind == tyEnum and n.kind == nkSym and n.sym.kind == skType:
  319. # look up if the identifier belongs to the enum:
  320. var t = typ
  321. while t != nil:
  322. suggestSymList(c, t.n, field, n.info, outputs)
  323. t = t.sons[0]
  324. suggestOperations(c, n, field, typ, outputs)
  325. else:
  326. let orig = typ # skipTypes(typ, {tyGenericInst, tyAlias, tySink})
  327. typ = skipTypes(typ, {tyGenericInst, tyVar, tyLent, tyPtr, tyRef, tyAlias, tySink})
  328. if typ.kind == tyObject:
  329. var t = typ
  330. while true:
  331. suggestObject(c, t.n, field, n.info, outputs)
  332. if t.sons[0] == nil: break
  333. t = skipTypes(t.sons[0], skipPtrs)
  334. elif typ.kind == tyTuple and typ.n != nil:
  335. suggestSymList(c, typ.n, field, n.info, outputs)
  336. suggestOperations(c, n, field, orig, outputs)
  337. if typ != orig:
  338. suggestOperations(c, n, field, typ, outputs)
  339. type
  340. TCheckPointResult* = enum
  341. cpNone, cpFuzzy, cpExact
  342. proc inCheckpoint*(current, trackPos: TLineInfo): TCheckPointResult =
  343. if current.fileIndex == trackPos.fileIndex:
  344. if current.line == trackPos.line and
  345. abs(current.col-trackPos.col) < 4:
  346. return cpExact
  347. if current.line >= trackPos.line:
  348. return cpFuzzy
  349. proc isTracked*(current, trackPos: TLineInfo, tokenLen: int): bool =
  350. if current.fileIndex==trackPos.fileIndex and current.line==trackPos.line:
  351. let col = trackPos.col
  352. if col >= current.col and col <= current.col+tokenLen-1:
  353. return true
  354. when defined(nimsuggest):
  355. # Since TLineInfo defined a == operator that doesn't include the column,
  356. # we map TLineInfo to a unique int here for this lookup table:
  357. proc infoToInt(info: TLineInfo): int64 =
  358. info.fileIndex.int64 + info.line.int64 shl 32 + info.col.int64 shl 48
  359. proc addNoDup(s: PSym; info: TLineInfo) =
  360. # ensure nothing gets too slow:
  361. if s.allUsages.len > 500: return
  362. let infoAsInt = info.infoToInt
  363. for infoB in s.allUsages:
  364. if infoB.infoToInt == infoAsInt: return
  365. s.allUsages.add(info)
  366. proc findUsages(conf: ConfigRef; info: TLineInfo; s: PSym; usageSym: var PSym) =
  367. if conf.suggestVersion == 1:
  368. if usageSym == nil and isTracked(info, conf.m.trackPos, s.name.s.len):
  369. usageSym = s
  370. suggestResult(conf, symToSuggest(conf, s, isLocal=false, ideUse, info, 100, PrefixMatch.None, false, 0))
  371. elif s == usageSym:
  372. if conf.lastLineInfo != info:
  373. suggestResult(conf, symToSuggest(conf, s, isLocal=false, ideUse, info, 100, PrefixMatch.None, false, 0))
  374. conf.lastLineInfo = info
  375. when defined(nimsuggest):
  376. proc listUsages*(conf: ConfigRef; s: PSym) =
  377. #echo "usages ", len(s.allUsages)
  378. for info in s.allUsages:
  379. let x = if info == s.info and info.col == s.info.col: ideDef else: ideUse
  380. suggestResult(conf, symToSuggest(conf, s, isLocal=false, x, info, 100, PrefixMatch.None, false, 0))
  381. proc findDefinition(conf: ConfigRef; info: TLineInfo; s: PSym) =
  382. if s.isNil: return
  383. if isTracked(info, conf.m.trackPos, s.name.s.len):
  384. suggestResult(conf, symToSuggest(conf, s, isLocal=false, ideDef, info, 100, PrefixMatch.None, false, 0))
  385. suggestQuit()
  386. proc ensureIdx[T](x: var T, y: int) =
  387. if x.len <= y: x.setLen(y+1)
  388. proc ensureSeq[T](x: var seq[T]) =
  389. if x == nil: newSeq(x, 0)
  390. proc suggestSym*(conf: ConfigRef; info: TLineInfo; s: PSym; usageSym: var PSym; isDecl=true) {.inline.} =
  391. ## misnamed: should be 'symDeclared'
  392. when defined(nimsuggest):
  393. if conf.suggestVersion == 0:
  394. if s.allUsages.len == 0:
  395. s.allUsages = @[info]
  396. else:
  397. s.addNoDup(info)
  398. if conf.ideCmd == ideUse:
  399. findUsages(conf, info, s, usageSym)
  400. elif conf.ideCmd == ideDef:
  401. findDefinition(conf, info, s)
  402. elif conf.ideCmd == ideDus and s != nil:
  403. if isTracked(info, conf.m.trackPos, s.name.s.len):
  404. suggestResult(conf, symToSuggest(conf, s, isLocal=false, ideDef, info, 100, PrefixMatch.None, false, 0))
  405. findUsages(conf, info, s, usageSym)
  406. elif conf.ideCmd == ideHighlight and info.fileIndex == conf.m.trackPos.fileIndex:
  407. suggestResult(conf, symToSuggest(conf, s, isLocal=false, ideHighlight, info, 100, PrefixMatch.None, false, 0))
  408. elif conf.ideCmd == ideOutline and info.fileIndex == conf.m.trackPos.fileIndex and
  409. isDecl:
  410. suggestResult(conf, symToSuggest(conf, s, isLocal=false, ideOutline, info, 100, PrefixMatch.None, false, 0))
  411. proc extractPragma(s: PSym): PNode =
  412. if s.kind in routineKinds:
  413. result = s.ast[pragmasPos]
  414. elif s.kind in {skType}:
  415. # s.ast = nkTypedef / nkPragmaExpr / [nkSym, nkPragma]
  416. result = s.ast[0][1]
  417. doAssert result == nil or result.kind == nkPragma
  418. proc warnAboutDeprecated(conf: ConfigRef; info: TLineInfo; s: PSym) =
  419. let pragmaNode = extractPragma(s)
  420. if pragmaNode != nil:
  421. for it in pragmaNode:
  422. if whichPragma(it) == wDeprecated and it.safeLen == 2 and
  423. it[1].kind in {nkStrLit..nkTripleStrLit}:
  424. message(conf, info, warnDeprecated, it[1].strVal & "; " & s.name.s)
  425. return
  426. message(conf, info, warnDeprecated, s.name.s)
  427. proc userError(conf: ConfigRef; info: TLineInfo; s: PSym) =
  428. let pragmaNode = extractPragma(s)
  429. if pragmaNode != nil:
  430. for it in pragmaNode:
  431. if whichPragma(it) == wError and it.safeLen == 2 and
  432. it[1].kind in {nkStrLit..nkTripleStrLit}:
  433. localError(conf, info, it[1].strVal & "; usage of '$1' is a user-defined error" % s.name.s)
  434. return
  435. localError(conf, info, "usage of '$1' is a user-defined error" % s.name.s)
  436. proc markUsed(conf: ConfigRef; info: TLineInfo; s: PSym; usageSym: var PSym) =
  437. incl(s.flags, sfUsed)
  438. if s.kind == skEnumField and s.owner != nil:
  439. incl(s.owner.flags, sfUsed)
  440. if {sfDeprecated, sfError} * s.flags != {}:
  441. if sfDeprecated in s.flags: warnAboutDeprecated(conf, info, s)
  442. if sfError in s.flags: userError(conf, info, s)
  443. when defined(nimsuggest):
  444. suggestSym(conf, info, s, usageSym, false)
  445. proc useSym*(conf: ConfigRef; sym: PSym; usageSym: var PSym): PNode =
  446. result = newSymNode(sym)
  447. markUsed(conf, result.info, sym, usageSym)
  448. proc safeSemExpr*(c: PContext, n: PNode): PNode =
  449. # use only for idetools support!
  450. try:
  451. result = c.semExpr(c, n)
  452. except ERecoverableError:
  453. result = c.graph.emptyNode
  454. proc sugExpr(c: PContext, n: PNode, outputs: var Suggestions) =
  455. if n.kind == nkDotExpr:
  456. var obj = safeSemExpr(c, n.sons[0])
  457. # it can happen that errnously we have collected the fieldname
  458. # of the next line, so we check the 'field' is actually on the same
  459. # line as the object to prevent this from happening:
  460. let prefix = if n.len == 2 and n[1].info.line == n[0].info.line and
  461. not c.config.m.trackPosAttached: n[1] else: nil
  462. suggestFieldAccess(c, obj, prefix, outputs)
  463. #if optIdeDebug in gGlobalOptions:
  464. # echo "expression ", renderTree(obj), " has type ", typeToString(obj.typ)
  465. #writeStackTrace()
  466. else:
  467. let prefix = if c.config.m.trackPosAttached: nil else: n
  468. suggestEverything(c, n, prefix, outputs)
  469. proc suggestExprNoCheck*(c: PContext, n: PNode) =
  470. # This keeps semExpr() from coming here recursively:
  471. if c.compilesContextId > 0: return
  472. inc(c.compilesContextId)
  473. var outputs: Suggestions = @[]
  474. if c.config.ideCmd == ideSug:
  475. sugExpr(c, n, outputs)
  476. elif c.config.ideCmd == ideCon:
  477. if n.kind in nkCallKinds:
  478. var a = copyNode(n)
  479. var x = safeSemExpr(c, n.sons[0])
  480. if x.kind == nkEmpty or x.typ == nil: x = n.sons[0]
  481. addSon(a, x)
  482. for i in 1..sonsLen(n)-1:
  483. # use as many typed arguments as possible:
  484. var x = safeSemExpr(c, n.sons[i])
  485. if x.kind == nkEmpty or x.typ == nil: break
  486. addSon(a, x)
  487. suggestCall(c, a, n, outputs)
  488. dec(c.compilesContextId)
  489. if outputs.len > 0 and c.config.ideCmd in {ideSug, ideCon, ideDef}:
  490. produceOutput(outputs, c.config)
  491. suggestQuit()
  492. proc suggestExpr*(c: PContext, n: PNode) =
  493. if exactEquals(c.config.m.trackPos, n.info): suggestExprNoCheck(c, n)
  494. proc suggestDecl*(c: PContext, n: PNode; s: PSym) =
  495. let attached = c.config.m.trackPosAttached
  496. if attached: inc(c.inTypeContext)
  497. defer:
  498. if attached: dec(c.inTypeContext)
  499. suggestExpr(c, n)
  500. proc suggestStmt*(c: PContext, n: PNode) =
  501. suggestExpr(c, n)
  502. proc suggestEnum*(c: PContext; n: PNode; t: PType) =
  503. var outputs: Suggestions = @[]
  504. suggestSymList(c, t.n, nil, n.info, outputs)
  505. produceOutput(outputs, c.config)
  506. if outputs.len > 0: suggestQuit()
  507. proc suggestSentinel*(c: PContext) =
  508. if c.config.ideCmd != ideSug or c.module.position != c.config.m.trackPos.fileIndex.int32: return
  509. if c.compilesContextId > 0: return
  510. inc(c.compilesContextId)
  511. var outputs: Suggestions = @[]
  512. # suggest everything:
  513. var isLocal = true
  514. var scopeN = 0
  515. for scope in walkScopes(c.currentScope):
  516. if scope == c.topLevelScope: isLocal = false
  517. dec scopeN
  518. for it in items(scope.symbols):
  519. var pm: PrefixMatch
  520. if filterSymNoOpr(it, nil, pm):
  521. outputs.add(symToSuggest(c.config, it, isLocal = isLocal, ideSug,
  522. newLineInfo(c.config.m.trackPos.fileIndex, -1, -1), 0,
  523. PrefixMatch.None, false, scopeN))
  524. dec(c.compilesContextId)
  525. produceOutput(outputs, c.config)