suggest.nim 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987
  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 prefixmatches, suggestsymdb
  33. from wordrecg import wDeprecated, wError, wAddr, wYield
  34. import std/[algorithm, sets, parseutils, tables, os]
  35. when defined(nimsuggest):
  36. import pathutils # importer
  37. const
  38. sep = '\t'
  39. type
  40. ImportContext = object
  41. isMultiImport: bool # True if we're in a [...] context
  42. baseDir: string # e.g., "folder/" in "import folder/[..."
  43. partialModule: string # The actual module name being typed
  44. #template sectionSuggest(): expr = "##begin\n" & getStackTrace() & "##end\n"
  45. template origModuleName(m: PSym): string = m.name.s
  46. proc findDocComment(n: PNode): PNode =
  47. if n == nil: return nil
  48. if n.comment.len > 0: return n
  49. if n.kind in {nkStmtList, nkStmtListExpr, nkObjectTy, nkRecList} and n.len > 0:
  50. result = findDocComment(n[0])
  51. if result != nil: return
  52. if n.len > 1:
  53. result = findDocComment(n[1])
  54. elif n.kind in {nkAsgn, nkFastAsgn, nkSinkAsgn} and n.len == 2:
  55. result = findDocComment(n[1])
  56. else:
  57. result = nil
  58. proc extractDocComment(g: ModuleGraph; s: PSym): string =
  59. var n = findDocComment(s.ast)
  60. if n.isNil and s.kind in routineKinds and s.ast != nil:
  61. n = findDocComment(getBody(g, s))
  62. if not n.isNil:
  63. result = n.comment.replace("\n##", "\n").strip
  64. else:
  65. result = ""
  66. proc cmpSuggestions(a, b: Suggest): int =
  67. template cf(field) {.dirty.} =
  68. result = b.field.int - a.field.int
  69. if result != 0: return result
  70. cf prefix
  71. cf contextFits
  72. cf scope
  73. # when the first type matches, it's better when it's a generic match:
  74. cf quality
  75. cf localUsages
  76. cf globalUsages
  77. # if all is equal, sort alphabetically for deterministic output,
  78. # independent of hashing order:
  79. result = cmp(a.name[], b.name[])
  80. proc scanForTrailingAsterisk(line: string, start: int): int =
  81. result = 0
  82. while start+result < line.len and line[start+result] in {' ', '\t'}:
  83. inc result
  84. if start+result < line.len and line[start+result] == '*':
  85. inc result
  86. else:
  87. result = 0
  88. proc getTokenLenFromSource(conf: ConfigRef; ident: string; info: TLineInfo; skipTrailingAsterisk: bool = false): int =
  89. let
  90. line = sourceLine(conf, info)
  91. column = toColumn(info)
  92. proc isOpeningBacktick(col: int): bool =
  93. if col >= 0 and col < line.len:
  94. if line[col] == '`':
  95. not isOpeningBacktick(col - 1)
  96. else:
  97. isOpeningBacktick(col - 1)
  98. else:
  99. false
  100. if column > line.len:
  101. result = 0
  102. elif column > 0 and line[column - 1] == '`' and isOpeningBacktick(column - 1):
  103. result = skipUntil(line, '`', column)
  104. if cmpIgnoreStyle(line[column..column + result - 1], ident) != 0:
  105. result = 0
  106. elif column >= 0 and line[column] == '`' and isOpeningBacktick(column):
  107. result = skipUntil(line, '`', column + 1) + 2
  108. if cmpIgnoreStyle(line[column + 1..column + result - 2], ident) != 0:
  109. result = 0
  110. elif ident[0] in linter.Letters and ident[^1] != '=':
  111. result = identLen(line, column)
  112. if cmpIgnoreStyle(line[column..column + result - 1], ident[0..min(result-1,len(ident)-1)]) != 0:
  113. result = 0
  114. if skipTrailingAsterisk and result > 0:
  115. result += scanForTrailingAsterisk(line, column + result)
  116. else:
  117. var sourceIdent: string = ""
  118. result = parseWhile(line, sourceIdent,
  119. OpChars + {'[', '(', '{', ']', ')', '}'}, column)
  120. if ident[^1] == '=' and ident[0] in linter.Letters:
  121. if sourceIdent != "=":
  122. result = 0
  123. elif sourceIdent.len > ident.len and sourceIdent[0..ident.high] == ident:
  124. result = ident.len
  125. elif sourceIdent != ident:
  126. result = 0
  127. proc symToSuggest*(g: ModuleGraph; s: PSym, isLocal: bool, section: IdeCmd, info: TLineInfo;
  128. quality: range[0..100]; prefix: PrefixMatch;
  129. inTypeContext: bool; scope: int;
  130. useSuppliedInfo = false,
  131. endLine: uint16 = 0,
  132. endCol = 0, extractDocs = true): Suggest =
  133. new(result)
  134. result.section = section
  135. result.quality = quality
  136. result.isGlobal = sfGlobal in s.flags
  137. result.prefix = prefix
  138. result.contextFits = inTypeContext == (s.kind in {skType, skGenericParam})
  139. result.scope = scope
  140. result.name = addr s.name.s
  141. when defined(nimsuggest):
  142. result.globalUsages = s.allUsages.len
  143. var c = 0
  144. for u in s.allUsages:
  145. if u.fileIndex == info.fileIndex: inc c
  146. result.localUsages = c
  147. result.symkind = byte s.kind
  148. if optIdeTerse notin g.config.globalOptions:
  149. result.qualifiedPath = @[]
  150. if not isLocal and s.kind != skModule:
  151. let ow = s.owner
  152. if ow != nil and ow.kind != skModule and ow.owner != nil:
  153. let ow2 = ow.owner
  154. result.qualifiedPath.add(ow2.origModuleName)
  155. if ow != nil:
  156. result.qualifiedPath.add(ow.origModuleName)
  157. if s.name.s[0] in OpChars + {'[', '{', '('} or
  158. s.name.id in ord(wAddr)..ord(wYield):
  159. result.qualifiedPath.add('`' & s.name.s & '`')
  160. else:
  161. result.qualifiedPath.add(s.name.s)
  162. if s.typ != nil:
  163. if section == ideInlayHints:
  164. result.forth = typeToString(s.typ, preferInlayHint)
  165. else:
  166. result.forth = typeToString(s.typ, preferInferredEffects)
  167. else:
  168. result.forth = ""
  169. when defined(nimsuggest) and not defined(noDocgen) and not defined(leanCompiler):
  170. if extractDocs:
  171. result.doc = extractDocComment(g, s)
  172. if s.kind == skModule and s.ast.len != 0 and section != ideHighlight:
  173. result.filePath = toFullPath(g.config, s.ast[0].info)
  174. result.line = 1
  175. result.column = 0
  176. result.tokenLen = 0
  177. else:
  178. let infox =
  179. if useSuppliedInfo or section in {ideUse, ideHighlight, ideOutline, ideDeclaration}:
  180. info
  181. else:
  182. s.info
  183. result.filePath = toFullPath(g.config, infox)
  184. result.line = toLinenumber(infox)
  185. result.column = toColumn(infox)
  186. result.tokenLen = if section notin {ideHighlight, ideInlayHints}:
  187. s.name.s.len
  188. else:
  189. getTokenLenFromSource(g.config, s.name.s, infox, section == ideInlayHints)
  190. result.version = g.config.suggestVersion
  191. result.endLine = endLine
  192. result.endCol = endCol
  193. proc `$`*(suggest: SuggestInlayHint): string =
  194. result = $suggest.kind
  195. result.add(sep)
  196. result.add($suggest.line)
  197. result.add(sep)
  198. result.add($suggest.column)
  199. result.add(sep)
  200. result.add(suggest.label)
  201. result.add(sep)
  202. result.add($suggest.paddingLeft)
  203. result.add(sep)
  204. result.add($suggest.paddingRight)
  205. result.add(sep)
  206. result.add($suggest.allowInsert)
  207. result.add(sep)
  208. result.add(suggest.tooltip)
  209. proc `$`*(suggest: Suggest): string =
  210. if suggest.section == ideInlayHints:
  211. result = $suggest.inlayHintInfo
  212. else:
  213. result = $suggest.section
  214. result.add(sep)
  215. if suggest.section == ideHighlight:
  216. if suggest.symkind.TSymKind == skVar and suggest.isGlobal:
  217. result.add("skGlobalVar")
  218. elif suggest.symkind.TSymKind == skLet and suggest.isGlobal:
  219. result.add("skGlobalLet")
  220. else:
  221. result.add($suggest.symkind.TSymKind)
  222. result.add(sep)
  223. result.add($suggest.line)
  224. result.add(sep)
  225. result.add($suggest.column)
  226. result.add(sep)
  227. result.add($suggest.tokenLen)
  228. else:
  229. result.add($suggest.symkind.TSymKind)
  230. result.add(sep)
  231. if suggest.qualifiedPath.len != 0:
  232. result.add(suggest.qualifiedPath.join("."))
  233. result.add(sep)
  234. result.add(suggest.forth)
  235. result.add(sep)
  236. result.add(suggest.filePath)
  237. result.add(sep)
  238. result.add($suggest.line)
  239. result.add(sep)
  240. result.add($suggest.column)
  241. result.add(sep)
  242. when defined(nimsuggest) and not defined(noDocgen) and not defined(leanCompiler):
  243. result.add(suggest.doc.escape)
  244. if suggest.version == 0 or suggest.version == 3:
  245. result.add(sep)
  246. result.add($suggest.quality)
  247. if suggest.section == ideSug:
  248. result.add(sep)
  249. result.add($suggest.prefix)
  250. if (suggest.version == 3 and suggest.section in {ideOutline, ideExpand}):
  251. result.add(sep)
  252. result.add($suggest.endLine)
  253. result.add(sep)
  254. result.add($suggest.endCol)
  255. proc suggestToSuggestInlayTypeHint*(sug: Suggest): SuggestInlayHint =
  256. SuggestInlayHint(
  257. kind: sihkType,
  258. line: sug.line,
  259. column: sug.column + sug.tokenLen,
  260. label: ": " & sug.forth,
  261. paddingLeft: false,
  262. paddingRight: false,
  263. allowInsert: true,
  264. tooltip: ""
  265. )
  266. proc suggestToSuggestInlayExceptionHintLeft*(sug: Suggest, propagatedExceptions: seq[PType]): SuggestInlayHint =
  267. SuggestInlayHint(
  268. kind: sihkException,
  269. line: sug.line,
  270. column: sug.column,
  271. label: "try ",
  272. paddingLeft: false,
  273. paddingRight: false,
  274. allowInsert: false,
  275. tooltip: "propagated exceptions: " & $propagatedExceptions
  276. )
  277. proc suggestToSuggestInlayExceptionHintRight*(sug: Suggest, propagatedExceptions: seq[PType]): SuggestInlayHint =
  278. SuggestInlayHint(
  279. kind: sihkException,
  280. line: sug.line,
  281. column: sug.column + sug.tokenLen,
  282. label: "!",
  283. paddingLeft: false,
  284. paddingRight: false,
  285. allowInsert: false,
  286. tooltip: "propagated exceptions: " & $propagatedExceptions
  287. )
  288. proc suggestResult*(conf: ConfigRef; s: Suggest) =
  289. if not isNil(conf.suggestionResultHook):
  290. conf.suggestionResultHook(s)
  291. else:
  292. conf.suggestWriteln($s)
  293. proc produceOutput(a: var Suggestions; conf: ConfigRef) =
  294. if conf.ideCmd in {ideSug, ideCon}:
  295. a.sort cmpSuggestions
  296. when defined(debug):
  297. # debug code
  298. writeStackTrace()
  299. if a.len > conf.suggestMaxResults: a.setLen(conf.suggestMaxResults)
  300. if not isNil(conf.suggestionResultHook):
  301. for s in a:
  302. conf.suggestionResultHook(s)
  303. else:
  304. for s in a:
  305. conf.suggestWriteln($s)
  306. proc filterSym(s: PSym; prefix: PNode; res: var PrefixMatch): bool {.inline.} =
  307. proc prefixMatch(s: PSym; n: PNode): PrefixMatch =
  308. case n.kind
  309. of nkIdent: result = n.ident.s.prefixMatch(s.name.s)
  310. of nkSym: result = n.sym.name.s.prefixMatch(s.name.s)
  311. of nkOpenSymChoice, nkClosedSymChoice, nkAccQuoted:
  312. if n.len > 0:
  313. result = prefixMatch(s, n[0])
  314. else:
  315. result = default(PrefixMatch)
  316. else: result = default(PrefixMatch)
  317. if s.kind != skModule:
  318. if prefix != nil:
  319. res = prefixMatch(s, prefix)
  320. result = res != PrefixMatch.None
  321. else:
  322. result = true
  323. else:
  324. result = false
  325. proc filterSymNoOpr(s: PSym; prefix: PNode; res: var PrefixMatch): bool {.inline.} =
  326. result = filterSym(s, prefix, res) and s.name.s[0] in lexer.SymChars and
  327. not isKeyword(s.name)
  328. proc fieldVisible*(c: PContext, f: PSym): bool {.inline.} =
  329. let fmoduleId = getModule(f).id
  330. result = sfExported in f.flags or fmoduleId == c.module.id
  331. if not result:
  332. for module in c.friendModules:
  333. if fmoduleId == module.id: return true
  334. if f.kind == skField:
  335. var symObj = f.owner.typ.toObjectFromRefPtrGeneric.sym
  336. assert symObj != nil
  337. for scope in allScopes(c.currentScope):
  338. for sym in scope.allowPrivateAccess:
  339. if symObj.id == sym.id: return true
  340. proc getQuality(s: PSym): range[0..100] =
  341. result = 100
  342. if s.typ != nil and s.typ.paramsLen > 0:
  343. var exp = s.typ.firstParamType.skipTypes({tyGenericInst, tyVar, tyLent, tyAlias, tySink})
  344. if exp.kind == tyVarargs: exp = elemType(exp)
  345. if exp.kind in {tyUntyped, tyTyped, tyGenericParam, tyAnything}: result = 50
  346. # penalize deprecated symbols
  347. if sfDeprecated in s.flags:
  348. result = result - 5
  349. proc suggestField(c: PContext, s: PSym; f: PNode; info: TLineInfo; outputs: var Suggestions) =
  350. var pm: PrefixMatch = default(PrefixMatch)
  351. if filterSym(s, f, pm) and fieldVisible(c, s):
  352. outputs.add(symToSuggest(c.graph, s, isLocal=true, ideSug, info,
  353. s.getQuality, pm, c.inTypeContext > 0, 0))
  354. template wholeSymTab(cond, section: untyped) {.dirty.} =
  355. for (item, scopeN, isLocal) in uniqueSyms(c):
  356. let it = item
  357. var pm: PrefixMatch = default(PrefixMatch)
  358. if cond:
  359. outputs.add(symToSuggest(c.graph, it, isLocal = isLocal, section, info, getQuality(it),
  360. pm, c.inTypeContext > 0, scopeN))
  361. proc suggestSymList(c: PContext, list, f: PNode; info: TLineInfo, outputs: var Suggestions) =
  362. for i in 0..<list.len:
  363. if list[i].kind == nkSym:
  364. suggestField(c, list[i].sym, f, info, outputs)
  365. #else: InternalError(list.info, "getSymFromList")
  366. proc suggestObject(c: PContext, n, f: PNode; info: TLineInfo, outputs: var Suggestions) =
  367. case n.kind
  368. of nkRecList:
  369. for i in 0..<n.len: suggestObject(c, n[i], f, info, outputs)
  370. of nkRecCase:
  371. if n.len > 0:
  372. suggestObject(c, n[0], f, info, outputs)
  373. for i in 1..<n.len: suggestObject(c, lastSon(n[i]), f, info, outputs)
  374. of nkSym: suggestField(c, n.sym, f, info, outputs)
  375. else: discard
  376. proc nameFits(c: PContext, s: PSym, n: PNode): bool =
  377. var op = if n.kind in nkCallKinds: n[0] else: n
  378. if op.kind in {nkOpenSymChoice, nkClosedSymChoice}: op = op[0]
  379. if op.kind == nkDotExpr: op = op[1]
  380. var opr: PIdent
  381. case op.kind
  382. of nkSym: opr = op.sym.name
  383. of nkIdent: opr = op.ident
  384. else: return false
  385. result = opr.id == s.name.id
  386. proc argsFit(c: PContext, candidate: PSym, n, nOrig: PNode): bool =
  387. case candidate.kind
  388. of OverloadableSyms:
  389. var m = newCandidate(c, candidate, nil)
  390. sigmatch.partialMatch(c, n, nOrig, m)
  391. result = m.state != csNoMatch
  392. else:
  393. result = false
  394. proc suggestCall(c: PContext, n, nOrig: PNode, outputs: var Suggestions) =
  395. let info = n.info
  396. wholeSymTab(filterSym(it, nil, pm) and nameFits(c, it, n) and argsFit(c, it, n, nOrig),
  397. ideCon)
  398. proc suggestVar(c: PContext, n: PNode, outputs: var Suggestions) =
  399. let info = n.info
  400. wholeSymTab(nameFits(c, it, n), ideCon)
  401. proc typeFits(c: PContext, s: PSym, firstArg: PType): bool {.inline.} =
  402. if s.typ != nil and s.typ.paramsLen > 0 and s.typ.firstParamType != nil:
  403. # special rule: if system and some weird generic match via 'tyUntyped'
  404. # or 'tyGenericParam' we won't list it either to reduce the noise (nobody
  405. # wants 'system.`-|` as suggestion
  406. let m = s.getModule()
  407. if m != nil and sfSystemModule in m.flags:
  408. if s.kind == skType: return
  409. var exp = s.typ.firstParamType.skipTypes({tyGenericInst, tyVar, tyLent, tyAlias, tySink})
  410. if exp.kind == tyVarargs: exp = elemType(exp)
  411. if exp.kind in {tyUntyped, tyTyped, tyGenericParam, tyAnything}: return
  412. result = sigmatch.argtypeMatches(c, s.typ.firstParamType, firstArg)
  413. else:
  414. result = false
  415. proc suggestOperations(c: PContext, n, f: PNode, typ: PType, outputs: var Suggestions) =
  416. assert typ != nil
  417. let info = n.info
  418. wholeSymTab(filterSymNoOpr(it, f, pm) and typeFits(c, it, typ), ideSug)
  419. proc suggestEverything(c: PContext, n, f: PNode, outputs: var Suggestions) =
  420. # do not produce too many symbols:
  421. for (it, scopeN, isLocal) in uniqueSyms(c):
  422. var pm: PrefixMatch = default(PrefixMatch)
  423. if filterSym(it, f, pm):
  424. outputs.add(symToSuggest(c.graph, it, isLocal = isLocal, ideSug, n.info,
  425. it.getQuality, pm, c.inTypeContext > 0, scopeN))
  426. proc suggestFieldAccess(c: PContext, n, field: PNode, outputs: var Suggestions) =
  427. # special code that deals with ``myObj.``. `n` is NOT the nkDotExpr-node, but
  428. # ``myObj``.
  429. var typ = n.typ
  430. var pm: PrefixMatch = default(PrefixMatch)
  431. when defined(nimsuggest):
  432. if n.kind == nkSym and n.sym.kind == skError and c.config.suggestVersion == 0:
  433. # consider 'foo.|' where 'foo' is some not imported module.
  434. let fullPath = findModule(c.config, n.sym.name.s, toFullPath(c.config, n.info))
  435. if fullPath.isEmpty:
  436. # error: no known module name:
  437. typ = nil
  438. else:
  439. let m = c.graph.importModuleCallback(c.graph, c.module, fileInfoIdx(c.config, fullPath))
  440. if m == nil: typ = nil
  441. else:
  442. for it in allSyms(c.graph, n.sym):
  443. if filterSym(it, field, pm):
  444. outputs.add(symToSuggest(c.graph, it, isLocal=false, ideSug,
  445. n.info, it.getQuality, pm,
  446. c.inTypeContext > 0, -100))
  447. outputs.add(symToSuggest(c.graph, m, isLocal=false, ideMod, n.info,
  448. 100, PrefixMatch.None, c.inTypeContext > 0,
  449. -99))
  450. if typ == nil:
  451. # a module symbol has no type for example:
  452. if n.kind == nkSym and n.sym.kind == skModule:
  453. if n.sym == c.module:
  454. # all symbols accessible, because we are in the current module:
  455. for it in items(c.topLevelScope.symbols):
  456. if filterSym(it, field, pm):
  457. outputs.add(symToSuggest(c.graph, it, isLocal=false, ideSug,
  458. n.info, it.getQuality, pm,
  459. c.inTypeContext > 0, -99))
  460. else:
  461. for it in allSyms(c.graph, n.sym):
  462. if filterSym(it, field, pm):
  463. outputs.add(symToSuggest(c.graph, it, isLocal=false, ideSug,
  464. n.info, it.getQuality, pm,
  465. c.inTypeContext > 0, -99))
  466. else:
  467. # fallback:
  468. suggestEverything(c, n, field, outputs)
  469. else:
  470. let orig = typ
  471. typ = skipTypes(orig, {tyTypeDesc, tyGenericInst, tyVar, tyLent, tyPtr, tyRef, tyAlias, tySink, tyOwned})
  472. if typ.kind == tyEnum and n.kind == nkSym and n.sym.kind == skType:
  473. # look up if the identifier belongs to the enum:
  474. var t = typ
  475. while t != nil:
  476. suggestSymList(c, t.n, field, n.info, outputs)
  477. t = t.baseClass
  478. elif typ.kind == tyObject:
  479. var t = typ
  480. while true:
  481. suggestObject(c, t.n, field, n.info, outputs)
  482. if t.baseClass == nil: break
  483. t = skipTypes(t.baseClass, skipPtrs)
  484. elif typ.kind == tyTuple and typ.n != nil:
  485. # All tuple fields are in scope
  486. # So go through each field and add it to the suggestions (If it passes the filter)
  487. for node in typ.n:
  488. if node.kind == nkSym:
  489. let s = node.sym
  490. var pm: PrefixMatch = default(PrefixMatch)
  491. if filterSym(s, field, pm):
  492. outputs.add(symToSuggest(c.graph, s, isLocal=true, ideSug, n.info,
  493. s.getQuality, pm, c.inTypeContext > 0, 0))
  494. suggestOperations(c, n, field, orig, outputs)
  495. if typ != orig:
  496. suggestOperations(c, n, field, typ, outputs)
  497. type
  498. TCheckPointResult* = enum
  499. cpNone, cpFuzzy, cpExact
  500. proc inCheckpoint*(current, trackPos: TLineInfo): TCheckPointResult =
  501. if current.fileIndex == trackPos.fileIndex:
  502. result = cpNone
  503. if current.line == trackPos.line and
  504. abs(current.col-trackPos.col) < 4:
  505. return cpExact
  506. if current.line >= trackPos.line:
  507. return cpFuzzy
  508. else:
  509. result = cpNone
  510. proc isTracked*(current, trackPos: TLineInfo, tokenLen: int): bool =
  511. if current.fileIndex==trackPos.fileIndex and current.line==trackPos.line:
  512. let col = trackPos.col
  513. if col >= current.col and col <= current.col+tokenLen-1:
  514. result = true
  515. else:
  516. result = false
  517. else:
  518. result = false
  519. proc isTracked*(current, trackPos: TinyLineInfo, tokenLen: int): bool =
  520. if current.line==trackPos.line:
  521. let col = trackPos.col
  522. if col >= current.col and col <= current.col+tokenLen-1:
  523. result = true
  524. else:
  525. result = false
  526. else:
  527. result = false
  528. when defined(nimsuggest):
  529. # Since TLineInfo defined a == operator that doesn't include the column,
  530. # we map TLineInfo to a unique int here for this lookup table:
  531. proc infoToInt(info: TLineInfo): int64 =
  532. info.fileIndex.int64 + info.line.int64 shl 32 + info.col.int64 shl 48
  533. proc addNoDup(s: PSym; info: TLineInfo) =
  534. # ensure nothing gets too slow:
  535. if s.allUsages.len > 500: return
  536. let infoAsInt = info.infoToInt
  537. for infoB in s.allUsages:
  538. if infoB.infoToInt == infoAsInt: return
  539. s.allUsages.add(info)
  540. proc findUsages(g: ModuleGraph; info: TLineInfo; s: PSym; usageSym: var PSym) =
  541. if g.config.suggestVersion == 1:
  542. if usageSym == nil and isTracked(info, g.config.m.trackPos, s.name.s.len):
  543. usageSym = s
  544. suggestResult(g.config, symToSuggest(g, s, isLocal=false, ideUse, info, 100, PrefixMatch.None, false, 0))
  545. elif s == usageSym:
  546. if g.config.lastLineInfo != info:
  547. suggestResult(g.config, symToSuggest(g, s, isLocal=false, ideUse, info, 100, PrefixMatch.None, false, 0))
  548. g.config.lastLineInfo = info
  549. when defined(nimsuggest):
  550. proc listUsages*(g: ModuleGraph; s: PSym) =
  551. #echo "usages ", s.allUsages.len
  552. for info in s.allUsages:
  553. let x = if info == s.info and info.col == s.info.col: ideDef else: ideUse
  554. suggestResult(g.config, symToSuggest(g, s, isLocal=false, x, info, 100, PrefixMatch.None, false, 0))
  555. proc findDefinition(g: ModuleGraph; info: TLineInfo; s: PSym; usageSym: var PSym) =
  556. if s.isNil: return
  557. if isTracked(info, g.config.m.trackPos, s.name.s.len) or (s == usageSym and sfForward notin s.flags):
  558. suggestResult(g.config, symToSuggest(g, s, isLocal=false, ideDef, info, 100, PrefixMatch.None, false, 0, useSuppliedInfo = s == usageSym))
  559. if sfForward notin s.flags and g.config.suggestVersion < 3:
  560. suggestQuit()
  561. else:
  562. usageSym = s
  563. proc ensureIdx[T](x: var T, y: int) =
  564. if x.len <= y: x.setLen(y+1)
  565. proc ensureSeq[T](x: var seq[T]) =
  566. if x == nil: newSeq(x, 0)
  567. proc suggestSym*(g: ModuleGraph; info: TLineInfo; s: PSym; usageSym: var PSym; isDecl=true; isGenericInstance=false) {.inline.} =
  568. ## misnamed: should be 'symDeclared'
  569. let conf = g.config
  570. when defined(nimsuggest):
  571. if optIdeExceptionInlayHints in conf.globalOptions or not isGenericInstance:
  572. g.suggestSymbols.add SymInfoPair(sym: s, info: info, isDecl: isDecl, isGenericInstance: isGenericInstance), optIdeExceptionInlayHints in g.config.globalOptions
  573. if not isGenericInstance:
  574. if conf.suggestVersion == 0:
  575. if s.allUsages.len == 0:
  576. s.allUsages = @[info]
  577. else:
  578. s.addNoDup(info)
  579. if conf.ideCmd == ideUse:
  580. findUsages(g, info, s, usageSym)
  581. elif conf.ideCmd == ideDef:
  582. findDefinition(g, info, s, usageSym)
  583. elif conf.ideCmd == ideDus and s != nil:
  584. if isTracked(info, conf.m.trackPos, s.name.s.len):
  585. suggestResult(conf, symToSuggest(g, s, isLocal=false, ideDef, info, 100, PrefixMatch.None, false, 0))
  586. findUsages(g, info, s, usageSym)
  587. elif conf.ideCmd == ideHighlight and info.fileIndex == conf.m.trackPos.fileIndex:
  588. suggestResult(conf, symToSuggest(g, s, isLocal=false, ideHighlight, info, 100, PrefixMatch.None, false, 0))
  589. elif conf.ideCmd == ideOutline and isDecl:
  590. # if a module is included then the info we have is inside the include and
  591. # we need to walk up the owners until we find the outer most module,
  592. # which will be the last skModule prior to an skPackage.
  593. var
  594. parentFileIndex = info.fileIndex # assume we're in the correct module
  595. parentModule = s.owner
  596. while parentModule != nil and parentModule.kind == skModule:
  597. parentFileIndex = parentModule.info.fileIndex
  598. parentModule = parentModule.owner
  599. if parentFileIndex == conf.m.trackPos.fileIndex:
  600. suggestResult(conf, symToSuggest(g, s, isLocal=false, ideOutline, info, 100, PrefixMatch.None, false, 0))
  601. proc warnAboutDeprecated(conf: ConfigRef; info: TLineInfo; s: PSym) =
  602. var pragmaNode: PNode
  603. pragmaNode = if s.kind == skEnumField: extractPragma(s.owner) else: extractPragma(s)
  604. let name =
  605. if s.kind == skEnumField and sfDeprecated notin s.flags: "enum '" & s.owner.name.s & "' which contains field '" & s.name.s & "'"
  606. else: s.name.s
  607. if pragmaNode != nil:
  608. for it in pragmaNode:
  609. if whichPragma(it) == wDeprecated and it.safeLen == 2 and
  610. it[1].kind in {nkStrLit..nkTripleStrLit}:
  611. message(conf, info, warnDeprecated, it[1].strVal & "; " & name & " is deprecated")
  612. return
  613. message(conf, info, warnDeprecated, name & " is deprecated")
  614. proc userError(conf: ConfigRef; info: TLineInfo; s: PSym) =
  615. let pragmaNode = extractPragma(s)
  616. template bail(prefix: string) =
  617. localError(conf, info, "$1usage of '$2' is an {.error.} defined at $3" %
  618. [prefix, s.name.s, toFileLineCol(conf, s.ast.info)])
  619. if pragmaNode != nil:
  620. for it in pragmaNode:
  621. if whichPragma(it) == wError and it.safeLen == 2 and
  622. it[1].kind in {nkStrLit..nkTripleStrLit}:
  623. bail(it[1].strVal & "; ")
  624. return
  625. bail("")
  626. proc markOwnerModuleAsUsed(c: PContext; s: PSym) =
  627. var module = s
  628. while module != nil and module.kind != skModule:
  629. module = module.owner
  630. if module != nil and module != c.module:
  631. var i = 0
  632. while i <= high(c.unusedImports):
  633. let candidate = c.unusedImports[i][0]
  634. if candidate == module or c.importModuleMap.getOrDefault(candidate.id, int.low) == module.id or
  635. c.exportIndirections.contains((candidate.id, s.id)):
  636. # mark it as used:
  637. c.unusedImports.del(i)
  638. else:
  639. inc i
  640. proc markUsed(c: PContext; info: TLineInfo; s: PSym; checkStyle = true; isGenericInstance = false) =
  641. if not isGenericInstance:
  642. let conf = c.config
  643. incl(s.flags, sfUsed)
  644. if s.kind == skEnumField and s.owner != nil:
  645. incl(s.owner.flags, sfUsed)
  646. if sfDeprecated in s.owner.flags:
  647. warnAboutDeprecated(conf, info, s)
  648. if {sfDeprecated, sfError} * s.flags != {}:
  649. if sfDeprecated in s.flags:
  650. if not (c.lastTLineInfo.line == info.line and
  651. c.lastTLineInfo.col == info.col):
  652. warnAboutDeprecated(conf, info, s)
  653. c.lastTLineInfo = info
  654. if sfError in s.flags: userError(conf, info, s)
  655. when defined(nimsuggest):
  656. suggestSym(c.graph, info, s, c.graph.usageSym, isDecl = false, isGenericInstance = isGenericInstance)
  657. if not isGenericInstance:
  658. if checkStyle:
  659. styleCheckUse(c, info, s)
  660. markOwnerModuleAsUsed(c, s)
  661. proc safeSemExpr*(c: PContext, n: PNode): PNode =
  662. # use only for idetools support!
  663. try:
  664. result = c.semExpr(c, n)
  665. except ERecoverableError:
  666. result = c.graph.emptyNode
  667. proc sugExpr(c: PContext, n: PNode, outputs: var Suggestions) =
  668. if n.kind == nkDotExpr:
  669. var obj = safeSemExpr(c, n[0])
  670. # it can happen that errnously we have collected the fieldname
  671. # of the next line, so we check the 'field' is actually on the same
  672. # line as the object to prevent this from happening:
  673. let prefix = if n.len == 2 and n[1].info.line == n[0].info.line and
  674. not c.config.m.trackPosAttached: n[1] else: nil
  675. suggestFieldAccess(c, obj, prefix, outputs)
  676. #if optIdeDebug in gGlobalOptions:
  677. # echo "expression ", renderTree(obj), " has type ", typeToString(obj.typ)
  678. #writeStackTrace()
  679. elif n.kind == nkIdent:
  680. let
  681. prefix = if c.config.m.trackPosAttached: nil else: n
  682. info = n.info
  683. wholeSymTab(filterSym(it, prefix, pm), ideSug)
  684. else:
  685. let prefix = if c.config.m.trackPosAttached: nil else: n
  686. suggestEverything(c, n, prefix, outputs)
  687. proc extractImportContextFromAst(n: PNode, cursorCol: int): ImportContext =
  688. result = ImportContext()
  689. if n.kind != nkImportStmt: return
  690. for child in n:
  691. case child.kind
  692. of nkIdent:
  693. # Single import, e.g. import foo
  694. if child.info.col <= cursorCol:
  695. result.baseDir = ""
  696. result.partialModule = child.ident.s
  697. result.isMultiImport = false
  698. of nkInfix:
  699. # Directory or multi-import, e.g. import std/[os, strutils]
  700. if child.len == 3 and child[0].kind == nkIdent and child[0].ident.s == "/":
  701. let dir = child[1].ident.s
  702. if child[2].kind == nkBracket:
  703. result.baseDir = dir
  704. result.isMultiImport = true
  705. for modNode in child[2]:
  706. if modNode.kind == nkIdent and modNode.info.col <= cursorCol:
  707. result.partialModule = modNode.ident.s
  708. elif child[2].kind == nkIdent:
  709. if child[2].info.col <= cursorCol:
  710. result.baseDir = dir
  711. result.partialModule = child[2].ident.s
  712. result.isMultiImport = false
  713. else:
  714. discard
  715. proc findModuleFile(c: PContext, partialPath: string): seq[string] =
  716. result = @[]
  717. let currentModuleDir = parentDir(toFullPath(c.config, FileIndex(c.module.position)))
  718. proc tryAddModule(path, baseName: string) =
  719. if fileExists(path & ".nim"):
  720. result.add(baseName)
  721. proc addModulesFromDir(dir, file: string; result: var seq[string]) =
  722. if dirExists(dir):
  723. for kind, path in walkDir(dir):
  724. if kind in {pcFile, pcDir}:
  725. let (_, name, ext) = splitFile(path)
  726. if kind == pcFile:
  727. if ext == ".nim" and name.startsWith(file):
  728. result.add(name)
  729. proc collectImportModulesFromDir(dir: string, result: var seq[string]) =
  730. for kind, path in walkDir(dir):
  731. if kind in {pcFile, pcDir}:
  732. let (_, name, ext) = splitFile(path)
  733. if kind == pcFile:
  734. if ext == ".nim" and name.startsWith(partialPath):
  735. result.add(name)
  736. else:
  737. if name.startsWith(partialPath):
  738. result.add(name)
  739. if '/' in partialPath:
  740. let parts = partialPath.split('/')
  741. let dir = parts[0]
  742. let file = parts[1]
  743. addModulesFromDir(currentModuleDir / dir, file, result)
  744. for searchPath in c.config.searchPaths:
  745. let searchDir = searchPath.string / dir
  746. addModulesFromDir(searchDir, file, result)
  747. else:
  748. collectImportModulesFromDir(currentModuleDir, result)
  749. for searchPath in c.config.searchPaths:
  750. collectImportModulesFromDir(searchPath.string, result)
  751. proc suggestModuleNames(c: PContext, n: PNode) =
  752. var suggestions: Suggestions = @[]
  753. let partialPath = if n.kind == nkIdent: n.ident.s else: ""
  754. proc addModuleSuggestion(path: string) =
  755. var suggest = Suggest(
  756. section: ideSug,
  757. qualifiedPath: @[path],
  758. name: addr path,
  759. filePath: path,
  760. line: n.info.line.int,
  761. column: n.info.col.int,
  762. doc: "",
  763. quality: 100,
  764. contextFits: true,
  765. prefix: if partialPath.len > 0: prefixMatch(path, partialPath)
  766. else: PrefixMatch.None,
  767. symkind: byte skModule
  768. )
  769. suggestions.add(suggest)
  770. let importCtx = extractImportContextFromAst(n, c.config.m.trackPos.col)
  771. var searchPath = ""
  772. if importCtx.baseDir.len > 0:
  773. searchPath = importCtx.baseDir & "/"
  774. let possibleModules = findModuleFile(c, searchPath & importCtx.partialModule)
  775. for moduleName in possibleModules:
  776. if moduleName != c.module.name.s:
  777. addModuleSuggestion(moduleName)
  778. produceOutput(suggestions, c.config)
  779. suggestQuit()
  780. proc findImportStmtOnLine(n: PNode, line: uint16): PNode =
  781. if n.kind in {nkImportStmt, nkFromStmt} and n.info.line == line:
  782. return n
  783. for i in 0..<n.safeLen:
  784. let res = findImportStmtOnLine(n[i], line)
  785. if res != nil: return res
  786. return nil
  787. template trySuggestModuleNames*(c: PContext, n: PNode) =
  788. if c.config.ideCmd == ideSug:
  789. let importNode = findImportStmtOnLine(n, c.config.m.trackPos.line)
  790. if importNode != nil:
  791. suggestModuleNames(c, importNode)
  792. proc suggestExprNoCheck*(c: PContext, n: PNode) =
  793. # This keeps semExpr() from coming here recursively:
  794. if c.compilesContextId > 0: return
  795. inc(c.compilesContextId)
  796. var outputs: Suggestions = @[]
  797. if c.config.ideCmd == ideSug:
  798. sugExpr(c, n, outputs)
  799. elif c.config.ideCmd == ideCon:
  800. if n.kind in nkCallKinds:
  801. var a = copyNode(n)
  802. var x = safeSemExpr(c, n[0])
  803. if x.kind == nkEmpty or x.typ == nil: x = n[0]
  804. a.add x
  805. for i in 1..<n.len:
  806. # use as many typed arguments as possible:
  807. var x = safeSemExpr(c, n[i])
  808. if x.kind == nkEmpty or x.typ == nil: break
  809. a.add x
  810. suggestCall(c, a, n, outputs)
  811. elif n.kind in nkIdentKinds:
  812. var x = safeSemExpr(c, n)
  813. if x.kind == nkEmpty or x.typ == nil: x = n
  814. suggestVar(c, x, outputs)
  815. dec(c.compilesContextId)
  816. if outputs.len > 0 and c.config.ideCmd in {ideSug, ideCon, ideDef}:
  817. produceOutput(outputs, c.config)
  818. suggestQuit()
  819. proc suggestExpr*(c: PContext, n: PNode) =
  820. if exactEquals(c.config.m.trackPos, n.info): suggestExprNoCheck(c, n)
  821. proc suggestDecl*(c: PContext, n: PNode; s: PSym) =
  822. let attached = c.config.m.trackPosAttached
  823. if attached: inc(c.inTypeContext)
  824. defer:
  825. if attached: dec(c.inTypeContext)
  826. # If user is typing out an enum field, then don't provide suggestions
  827. if s.kind == skEnumField and c.config.cmd == cmdIdeTools and exactEquals(c.config.m.trackPos, n.info):
  828. suggestQuit()
  829. suggestExpr(c, n)
  830. proc suggestStmt*(c: PContext, n: PNode) =
  831. suggestExpr(c, n)
  832. proc suggestEnum*(c: PContext; n: PNode; t: PType) =
  833. var outputs: Suggestions = @[]
  834. suggestSymList(c, t.n, nil, n.info, outputs)
  835. produceOutput(outputs, c.config)
  836. if outputs.len > 0: suggestQuit()
  837. proc suggestPragmas*(c: PContext, n: PNode) =
  838. ## Suggests anything that might be a pragma
  839. ## - template that has {.pragma.}
  840. ## - macros
  841. ## - user pragmas
  842. let info = n.info
  843. var outputs: Suggestions = @[]
  844. # First filter for template/macros
  845. wholeSymTab(filterSym(it, n, pm) and
  846. (sfCustomPragma in it.flags or it.kind == skMacro),
  847. ideSug)
  848. # Now show suggestions for user pragmas
  849. for pragma in c.userPragmas:
  850. var pm = default(PrefixMatch)
  851. if filterSym(pragma, n, pm):
  852. outputs &= symToSuggest(c.graph, pragma, isLocal=true, ideSug, info,
  853. pragma.getQuality, pm, c.inTypeContext > 0, 0,
  854. extractDocs=false)
  855. produceOutput(outputs, c.config)
  856. if outputs.len > 0:
  857. suggestQuit()
  858. template trySuggestPragmas*(c: PContext, n: PNode) =
  859. ## Runs [suggestPragmas] when compiling nimsuggest and
  860. ## we are querying the node
  861. when defined(nimsuggest):
  862. let tmp = n
  863. if c.config.ideCmd == ideSug and exactEquals(c.config.m.trackPos, tmp.info):
  864. suggestPragmas(c, tmp)
  865. proc suggestSentinel*(c: PContext) =
  866. if c.config.ideCmd != ideSug or c.module.position != c.config.m.trackPos.fileIndex.int32: return
  867. if c.compilesContextId > 0: return
  868. inc(c.compilesContextId)
  869. var outputs: Suggestions = @[]
  870. # suggest everything:
  871. for (it, scopeN, isLocal) in uniqueSyms(c):
  872. var pm: PrefixMatch = default(PrefixMatch)
  873. if filterSymNoOpr(it, nil, pm):
  874. outputs.add(symToSuggest(c.graph, it, isLocal = isLocal, ideSug,
  875. newLineInfo(c.config.m.trackPos.fileIndex, 0, -1), it.getQuality,
  876. PrefixMatch.None, false, scopeN))
  877. dec(c.compilesContextId)
  878. produceOutput(outputs, c.config)
  879. when defined(nimsuggest):
  880. proc onDef(graph: ModuleGraph, s: PSym, info: TLineInfo) =
  881. if graph.config.suggestVersion >= 3 and info.exactEquals(s.info):
  882. suggestSym(graph, info, s, graph.usageSym)
  883. template getPContext(): untyped =
  884. when c is PContext: c
  885. else: c.c
  886. template onDef*(info: TLineInfo; s: PSym) =
  887. let c = getPContext()
  888. onDef(c.graph, s, info)