suggest.nim 20 KB

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