navigator.nim 6.0 KB

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