summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorAndreas Rumpf <rumpf_a@web.de>2016-11-06 11:19:59 +0100
committerAraq <rumpf_a@web.de>2016-11-06 17:33:33 +0100
commit2caf08855f99bbf816708a1497512d16b631289a (patch)
treee11e842ecc83f082b0377d57bbc04fb7279aeedf
parentfc7127153f102488f8ac4fcc2f14062eb1bac396 (diff)
downloadNim-2caf08855f99bbf816708a1497512d16b631289a.tar.gz
nimsuggest supports include files properly; added the compiler itself as a testcase
-rw-r--r--compiler/modulegraphs.nim18
-rw-r--r--compiler/modules.nim1
-rw-r--r--tools/nimsuggest/nimsuggest.nim10
-rw-r--r--tools/nimsuggest/tests/tinclude.nim7
4 files changed, 29 insertions, 7 deletions
diff --git a/compiler/modulegraphs.nim b/compiler/modulegraphs.nim
index 2c1671789..9a3caa663 100644
--- a/compiler/modulegraphs.nim
+++ b/compiler/modulegraphs.nim
@@ -25,7 +25,7 @@
 ## - Its dependent module stays the same.
 ##
 
-import ast, intsets
+import ast, intsets, tables
 
 type
   ModuleGraph* = ref object
@@ -34,6 +34,8 @@ type
     deps*: IntSet # the dependency graph or potentially its transitive closure.
     suggestMode*: bool # whether we are in nimsuggest mode or not.
     invalidTransitiveClosure: bool
+    inclToMod*: Table[int32, int32] # mapping of include file to the
+                                    # first module that included it
 
 {.this: g.}
 
@@ -42,11 +44,13 @@ proc newModuleGraph*(): ModuleGraph =
   initStrTable(result.packageSyms)
   result.deps = initIntSet()
   result.modules = @[]
+  result.inclToMod = initTable[int32, int32]()
 
 proc resetAllModules*(g: ModuleGraph) =
   initStrTable(packageSyms)
   deps = initIntSet()
   modules = @[]
+  inclToMod = initTable[int32, int32]()
 
 proc getModule*(g: ModuleGraph; fileIdx: int32): PSym =
   if fileIdx >= 0 and fileIdx < modules.len:
@@ -61,6 +65,18 @@ proc addDep*(g: ModuleGraph; m: PSym, dep: int32) =
     # this improve efficiency quite a lot:
     invalidTransitiveClosure = true
 
+proc addIncludeDep*(g: ModuleGraph; module, includeFile: int32) =
+  discard hasKeyOrPut(inclToMod, includeFile, module)
+
+proc parentModule*(g: ModuleGraph; fileIdx: int32): int32 =
+  ## returns 'fileIdx' if the file belonging to this index is
+  ## directly used as a module or else the module that first
+  ## references this include file.
+  if fileIdx >= 0 and fileIdx < modules.len and modules[fileIdx] != nil:
+    result = fileIdx
+  else:
+    result = inclToMod.getOrDefault(fileIdx)
+
 proc transitiveClosure(g: var IntSet; n: int) =
   # warshall's algorithm
   for k in 0..<n:
diff --git a/compiler/modules.nim b/compiler/modules.nim
index e901ba896..68df5f756 100644
--- a/compiler/modules.nim
+++ b/compiler/modules.nim
@@ -208,6 +208,7 @@ proc includeModule*(graph: ModuleGraph; s: PSym, fileIdx: int32;
                     cache: IdentCache): PNode {.procvar.} =
   result = syntaxes.parseFile(fileIdx, cache)
   graph.addDep(s, fileIdx)
+  graph.addIncludeDep(s.position.int32, fileIdx)
 
 proc compileSystemModule*(graph: ModuleGraph; cache: IdentCache) =
   if magicsys.systemModule == nil:
diff --git a/tools/nimsuggest/nimsuggest.nim b/tools/nimsuggest/nimsuggest.nim
index 6b7ee74e2..cc323e9a1 100644
--- a/tools/nimsuggest/nimsuggest.nim
+++ b/tools/nimsuggest/nimsuggest.nim
@@ -157,13 +157,11 @@ proc execute(cmd: IdeCmd, file, dirtyfile: string, line, col: int;
       dirtyfile.len == 0:
     discard "no need to recompile anything"
   else:
-    #resetModule dirtyIdx
-    #if dirtyIdx != gProjectMainIdx:
-    #  resetModule gProjectMainIdx
-    graph.markDirty dirtyIdx
-    graph.markClientsDirty dirtyIdx
+    let modIdx = graph.parentModule(dirtyIdx)
+    graph.markDirty modIdx
+    graph.markClientsDirty modIdx
     if gIdeCmd != ideMod:
-      graph.compileProject(cache, dirtyIdx)
+      graph.compileProject(cache, modIdx)
   if gIdeCmd in {ideUse, ideDus}:
     let u = if suggestVersion >= 2: graph.symFromInfo(gTrackPos) else: usageSym
     if u != nil:
diff --git a/tools/nimsuggest/tests/tinclude.nim b/tools/nimsuggest/tests/tinclude.nim
new file mode 100644
index 000000000..77492d745
--- /dev/null
+++ b/tools/nimsuggest/tests/tinclude.nim
@@ -0,0 +1,7 @@
+discard """
+$nimsuggest --tester compiler/nim.nim
+>def compiler/semexprs.nim:13:50
+def;;skType;;ast.PSym;;PSym;;*ast.nim;;668;;2;;"";;100
+>def compiler/semexprs.nim:13:50
+def;;skType;;ast.PSym;;PSym;;*ast.nim;;668;;2;;"";;100
+"""