summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--script/core/searcher.lua7
-rw-r--r--script/vm/docs.lua52
2 files changed, 58 insertions, 1 deletions
diff --git a/script/core/searcher.lua b/script/core/searcher.lua
index 095ff782..f1dd9508 100644
--- a/script/core/searcher.lua
+++ b/script/core/searcher.lua
@@ -6,6 +6,7 @@ local ws = require 'workspace'
local vm = require 'vm.vm'
local await = require 'await'
local globals = require 'vm.globals'
+local docs = require 'vm.docs'
local NONE = {'NONE'}
local LAST = {'LAST'}
@@ -446,8 +447,12 @@ function m.searchRefsByID(status, uri, expect, mode)
return
end
status.crossed[firstID] = true
+ local uris = docs.getUrisByID(firstID)
+ if not uris then
+ return
+ end
local tid = id .. (field or '')
- for guri in files.eachFile() do
+ for guri in pairs(uris) do
if not files.eq(uri, guri) then
crossSearch(status, guri, tid, mode)
end
diff --git a/script/vm/docs.lua b/script/vm/docs.lua
new file mode 100644
index 00000000..632c8c85
--- /dev/null
+++ b/script/vm/docs.lua
@@ -0,0 +1,52 @@
+local files = require 'files'
+local await = require 'await'
+local noder = require 'core.noder'
+
+local docsMap = {}
+local subscribeMap = {}
+
+local function popDocs(uri)
+ if not subscribeMap[uri] then
+ return
+ end
+ for id in pairs(subscribeMap[uri]) do
+ if docsMap[id] then
+ docsMap[id][uri] = nil
+ end
+ end
+ subscribeMap[uri] = nil
+end
+
+local function pushDocs(uri)
+ subscribeMap[uri] = {}
+ local state = files.getState(uri)
+ if not state then
+ return
+ end
+ local nodes = noder.compileNodes(state.ast)
+ for id in pairs(nodes) do
+ if id:sub(1, 3) == 'dn:' then
+ if not docsMap[id] then
+ docsMap[id] = {}
+ end
+ docsMap[id][uri] = true
+ subscribeMap[uri][id] = true
+ end
+ end
+end
+
+files.watch(function (ev, uri)
+ if ev == 'update' then
+ popDocs(uri)
+ await.delay()
+ pushDocs(uri)
+ end
+end)
+
+local m = {}
+
+function m.getUrisByID(id)
+ return docsMap[id]
+end
+
+return m