navigator.nim 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. #
  2. #
  3. # The Nim Compiler
  4. # (c) Copyright 2021 Andreas Rumpf
  5. #
  6. # See the file "copying.txt", included in this
  7. # distribution, for details about the copyright.
  8. #
  9. ## Supports the "nim check --ic:on --defusages:FILE,LINE,COL"
  10. ## IDE-like features. It uses the set of .rod files to accomplish
  11. ## its task. The set must cover a complete Nim project.
  12. import sets
  13. from os import nil
  14. from std/private/miscdollars import toLocation
  15. when defined(nimPreviewSlimSystem):
  16. import std/assertions
  17. import ".." / [ast, modulegraphs, msgs, options]
  18. import packed_ast, bitabs, ic
  19. type
  20. NavContext = object
  21. g: ModuleGraph
  22. thisModule: int32
  23. trackPos: PackedLineInfo
  24. alreadyEmitted: HashSet[string]
  25. outputSep: char # for easier testing, use short filenames and spaces instead of tabs.
  26. proc isTracked(current, trackPos: PackedLineInfo, tokenLen: int): bool =
  27. if current.file == trackPos.file and current.line == trackPos.line:
  28. let col = trackPos.col
  29. if col >= current.col and col < current.col+tokenLen:
  30. return true
  31. proc searchLocalSym(c: var NavContext; s: PackedSym; info: PackedLineInfo): bool =
  32. result = s.name != LitId(0) and
  33. isTracked(info, c.trackPos, c.g.packed[c.thisModule].fromDisk.strings[s.name].len)
  34. proc searchForeignSym(c: var NavContext; s: ItemId; info: PackedLineInfo): bool =
  35. let name = c.g.packed[s.module].fromDisk.syms[s.item].name
  36. result = name != LitId(0) and
  37. isTracked(info, c.trackPos, c.g.packed[s.module].fromDisk.strings[name].len)
  38. const
  39. EmptyItemId = ItemId(module: -1'i32, item: -1'i32)
  40. proc search(c: var NavContext; tree: PackedTree): ItemId =
  41. # We use the linear representation here directly:
  42. for i in 0..high(tree.nodes):
  43. case tree.nodes[i].kind
  44. of nkSym:
  45. let item = tree.nodes[i].operand
  46. if searchLocalSym(c, c.g.packed[c.thisModule].fromDisk.syms[item], tree.nodes[i].info):
  47. return ItemId(module: c.thisModule, item: item)
  48. of nkModuleRef:
  49. if tree.nodes[i].info.line == c.trackPos.line and tree.nodes[i].info.file == c.trackPos.file:
  50. let (n1, n2) = sons2(tree, NodePos i)
  51. assert n1.kind == nkInt32Lit
  52. assert n2.kind == nkInt32Lit
  53. let pId = PackedItemId(module: n1.litId, item: tree.nodes[n2.int].operand)
  54. let itemId = translateId(pId, c.g.packed, c.thisModule, c.g.config)
  55. if searchForeignSym(c, itemId, tree.nodes[i].info):
  56. return itemId
  57. else: discard
  58. return EmptyItemId
  59. proc isDecl(tree: PackedTree; n: NodePos): bool =
  60. # XXX This is not correct yet.
  61. const declarativeNodes = procDefs + {nkMacroDef, nkTemplateDef,
  62. nkLetSection, nkVarSection, nkUsingStmt, nkConstSection, nkTypeSection,
  63. nkIdentDefs, nkEnumTy, nkVarTuple}
  64. result = n.int >= 0 and tree[n.int].kind in declarativeNodes
  65. proc usage(c: var NavContext; info: PackedLineInfo; isDecl: bool) =
  66. var m = ""
  67. var file = c.g.packed[c.thisModule].fromDisk.strings[info.file]
  68. if c.outputSep == ' ':
  69. file = os.extractFilename file
  70. toLocation(m, file, info.line.int, info.col.int + ColOffset)
  71. if not c.alreadyEmitted.containsOrIncl(m):
  72. msgWriteln c.g.config, (if isDecl: "def" else: "usage") & c.outputSep & m
  73. proc list(c: var NavContext; tree: PackedTree; sym: ItemId) =
  74. for i in 0..high(tree.nodes):
  75. case tree.nodes[i].kind
  76. of nkSym:
  77. let item = tree.nodes[i].operand
  78. if sym.item == item and sym.module == c.thisModule:
  79. usage(c, tree.nodes[i].info, isDecl(tree, parent(NodePos i)))
  80. of nkModuleRef:
  81. let (n1, n2) = sons2(tree, NodePos i)
  82. assert n1.kind == nkInt32Lit
  83. assert n2.kind == nkInt32Lit
  84. let pId = PackedItemId(module: n1.litId, item: tree.nodes[n2.int].operand)
  85. let itemId = translateId(pId, c.g.packed, c.thisModule, c.g.config)
  86. if itemId.item == sym.item and sym.module == itemId.module:
  87. usage(c, tree.nodes[i].info, isDecl(tree, parent(NodePos i)))
  88. else: discard
  89. proc searchForIncludeFile(g: ModuleGraph; fullPath: string): int =
  90. for i in 0..high(g.packed):
  91. for k in 1..high(g.packed[i].fromDisk.includes):
  92. # we start from 1 because the first "include" file is
  93. # the module's filename.
  94. if os.cmpPaths(g.packed[i].fromDisk.strings[g.packed[i].fromDisk.includes[k][0]], fullPath) == 0:
  95. return i
  96. return -1
  97. proc nav(g: ModuleGraph) =
  98. # translate the track position to a packed position:
  99. let unpacked = g.config.m.trackPos
  100. var mid = unpacked.fileIndex.int
  101. let fullPath = toFullPath(g.config, unpacked.fileIndex)
  102. if g.packed[mid].status == undefined:
  103. # check if 'mid' is an include file of some other module:
  104. mid = searchForIncludeFile(g, fullPath)
  105. if mid < 0:
  106. localError(g.config, unpacked, "unknown file name: " & fullPath)
  107. return
  108. let fileId = g.packed[mid].fromDisk.strings.getKeyId(fullPath)
  109. if fileId == LitId(0):
  110. internalError(g.config, unpacked, "cannot find a valid file ID")
  111. return
  112. var c = NavContext(
  113. g: g,
  114. thisModule: int32 mid,
  115. trackPos: PackedLineInfo(line: unpacked.line, col: unpacked.col, file: fileId),
  116. outputSep: if isDefined(g.config, "nimIcNavigatorTests"): ' ' else: '\t'
  117. )
  118. var symId = search(c, g.packed[mid].fromDisk.topLevel)
  119. if symId == EmptyItemId:
  120. symId = search(c, g.packed[mid].fromDisk.bodies)
  121. if symId == EmptyItemId:
  122. localError(g.config, unpacked, "no symbol at this position")
  123. return
  124. for i in 0..high(g.packed):
  125. # case statement here to enforce exhaustive checks.
  126. case g.packed[i].status
  127. of undefined:
  128. discard "nothing to do"
  129. of loading:
  130. assert false, "cannot check integrity: Module still loading"
  131. of stored, storing, outdated, loaded:
  132. c.thisModule = int32 i
  133. list(c, g.packed[i].fromDisk.topLevel, symId)
  134. list(c, g.packed[i].fromDisk.bodies, symId)
  135. proc navDefinition*(g: ModuleGraph) = nav(g)
  136. proc navUsages*(g: ModuleGraph) = nav(g)
  137. proc navDefusages*(g: ModuleGraph) = nav(g)
  138. proc writeRodFiles*(g: ModuleGraph) =
  139. for i in 0..high(g.packed):
  140. case g.packed[i].status
  141. of undefined, loading, stored, loaded:
  142. discard "nothing to do"
  143. of storing, outdated:
  144. closeRodFile(g, g.packed[i].module)