summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--script-beta/core/completion.lua29
-rw-r--r--script-beta/core/definition.lua23
-rw-r--r--script-beta/core/find-source.lua13
-rw-r--r--script-beta/core/highlight.lua10
-rw-r--r--script-beta/core/hover/init.lua41
-rw-r--r--script-beta/core/reference.lua25
-rw-r--r--script-beta/core/rename.lua80
-rw-r--r--script-beta/vm/vm.lua21
8 files changed, 140 insertions, 102 deletions
diff --git a/script-beta/core/completion.lua b/script-beta/core/completion.lua
index c8965808..90f5fbb2 100644
--- a/script-beta/core/completion.lua
+++ b/script-beta/core/completion.lua
@@ -1,17 +1,18 @@
-local ckind = require 'define.CompletionItemKind'
-local files = require 'files'
-local guide = require 'parser.guide'
-local matchKey = require 'core.matchKey'
-local vm = require 'vm'
-local library = require 'library'
-local getLabel = require 'core.hover.label'
-local getName = require 'core.hover.name'
-local getArg = require 'core.hover.arg'
-local getDesc = require 'core.hover.description'
-local getHover = require 'core.hover'
-local config = require 'config'
-local util = require 'utility'
-local markdown = require 'provider.markdown'
+local ckind = require 'define.CompletionItemKind'
+local files = require 'files'
+local guide = require 'parser.guide'
+local matchKey = require 'core.matchKey'
+local vm = require 'vm'
+local library = require 'library'
+local getLabel = require 'core.hover.label'
+local getName = require 'core.hover.name'
+local getArg = require 'core.hover.arg'
+local getDesc = require 'core.hover.description'
+local getHover = require 'core.hover'
+local config = require 'config'
+local util = require 'utility'
+local markdown = require 'provider.markdown'
+local findSource = require 'core.find-source'
local stackID = 0
local stacks = {}
diff --git a/script-beta/core/definition.lua b/script-beta/core/definition.lua
index 02383ee6..8784eebd 100644
--- a/script-beta/core/definition.lua
+++ b/script-beta/core/definition.lua
@@ -1,7 +1,8 @@
-local guide = require 'parser.guide'
-local workspace = require 'workspace'
-local files = require 'files'
-local vm = require 'vm'
+local guide = require 'parser.guide'
+local workspace = require 'workspace'
+local files = require 'files'
+local vm = require 'vm'
+local findSource = require 'core.find-source'
local accept = {
['local'] = true,
@@ -18,18 +19,6 @@ local accept = {
['getglobal'] = true,
}
-local function findSource(ast, offset)
- local len = 999
- local result
- guide.eachSourceContain(ast.ast, offset, function (source)
- if source.finish - source.start < len and accept[source.type] then
- result = source
- len = source.finish - source.start
- end
- end)
- return result
-end
-
local function checkRequire(source, offset)
if source.type ~= 'call' then
return nil
@@ -65,7 +54,7 @@ return function (uri, offset)
return nil
end
- local source = findSource(ast, offset)
+ local source = findSource(ast, offset, accept)
if not source then
return nil
end
diff --git a/script-beta/core/find-source.lua b/script-beta/core/find-source.lua
new file mode 100644
index 00000000..2e42ca82
--- /dev/null
+++ b/script-beta/core/find-source.lua
@@ -0,0 +1,13 @@
+local guide = require 'parser.guide'
+
+return function (ast, offset, accept)
+ local len = 999
+ local result
+ guide.eachSourceContain(ast.ast, offset, function (source)
+ if source.finish - source.start < len and accept[source.type] then
+ result = source
+ len = source.finish - source.start
+ end
+ end)
+ return result
+end
diff --git a/script-beta/core/highlight.lua b/script-beta/core/highlight.lua
index 2d63f54e..5c5f1dc5 100644
--- a/script-beta/core/highlight.lua
+++ b/script-beta/core/highlight.lua
@@ -1,7 +1,8 @@
-local guide = require 'parser.guide'
-local files = require 'files'
-local vm = require 'vm'
-local define = require 'proto.define'
+local guide = require 'parser.guide'
+local files = require 'files'
+local vm = require 'vm'
+local define = require 'proto.define'
+local findSource = require 'core.find-source'
local function ofLocal(source, callback)
callback(source)
@@ -162,6 +163,7 @@ return function (uri, offset)
local text = files.getText(uri)
local results = {}
local mark = {}
+
guide.eachSourceContain(ast.ast, offset, function (source)
find(source, uri, function (target)
local kind
diff --git a/script-beta/core/hover/init.lua b/script-beta/core/hover/init.lua
index 38f6fa88..e56b009e 100644
--- a/script-beta/core/hover/init.lua
+++ b/script-beta/core/hover/init.lua
@@ -1,9 +1,10 @@
-local files = require 'files'
-local guide = require 'parser.guide'
-local vm = require 'vm'
-local getLabel = require 'core.hover.label'
-local getDesc = require 'core.hover.description'
-local util = require 'utility'
+local files = require 'files'
+local guide = require 'parser.guide'
+local vm = require 'vm'
+local getLabel = require 'core.hover.label'
+local getDesc = require 'core.hover.description'
+local util = require 'utility'
+local findSource = require 'core.find-source'
local function getHoverAsFunction(source)
local values = vm.getValue(source)
@@ -81,23 +82,27 @@ local function getHover(source)
end
end
+local accept = {
+ ['local'] = true,
+ ['setlocal'] = true,
+ ['getlocal'] = true,
+ ['setglobal'] = true,
+ ['getglobal'] = true,
+ ['field'] = true,
+ ['method'] = true,
+ ['string'] = true,
+}
+
local function getHoverByUri(uri, offset)
local ast = files.getAst(uri)
if not ast then
return nil
end
- local hover = guide.eachSourceContain(ast.ast, offset, function (source)
- if source.type == 'local'
- or source.type == 'setlocal'
- or source.type == 'getlocal'
- or source.type == 'setglobal'
- or source.type == 'getglobal'
- or source.type == 'field'
- or source.type == 'method'
- or source.type == 'string' then
- return getHover(source)
- end
- end)
+ local source = findSource(ast, offset, accept)
+ if not source then
+ return nil
+ end
+ local hover = getHover(source)
return hover
end
diff --git a/script-beta/core/reference.lua b/script-beta/core/reference.lua
index 98872d9c..73e1130e 100644
--- a/script-beta/core/reference.lua
+++ b/script-beta/core/reference.lua
@@ -1,5 +1,6 @@
-local guide = require 'parser.guide'
-local files = require 'files'
+local guide = require 'parser.guide'
+local files = require 'files'
+local findSource = require 'core.find-source'
local function isValidFunction(source, offset)
-- 必须点在 `function` 这个单词上才能查找函数引用
@@ -22,31 +23,19 @@ local accept = {
['function'] = true,
}
-local function findSource(ast, offset)
- local len = 999
- local result
- guide.eachSourceContain(ast.ast, offset, function (source)
- if source.finish - source.start < len and accept[source.type] then
- result = source
- len = source.finish - source.start
- end
- end)
- if result.type == 'function' and not isValidFunction(result, offset) and not TEST then
- return nil
- end
- return result
-end
-
return function (uri, offset)
local ast = files.getAst(uri)
if not ast then
return nil
end
- local source = findSource(ast, offset)
+ local source = findSource(ast, offset, accept)
if not source then
return nil
end
+ if source.type == 'function' and not isValidFunction(source, offset) and not TEST then
+ return nil
+ end
local results = {}
local refs = guide.requestReference(source)
diff --git a/script-beta/core/rename.lua b/script-beta/core/rename.lua
index 0bfaa95f..116393be 100644
--- a/script-beta/core/rename.lua
+++ b/script-beta/core/rename.lua
@@ -1,9 +1,10 @@
-local files = require 'files'
-local vm = require 'vm'
-local guide = require 'parser.guide'
-local proto = require 'proto'
-local define = require 'proto.define'
-local util = require 'utility'
+local files = require 'files'
+local vm = require 'vm'
+local guide = require 'parser.guide'
+local proto = require 'proto'
+local define = require 'proto.define'
+local util = require 'utility'
+local findSource = require 'core.find-source'
local Forcing
@@ -325,6 +326,20 @@ local function prepareRename(source)
return nil
end
+local accept = {
+ ['label'] = true,
+ ['goto'] = true,
+ ['local'] = true,
+ ['setlocal'] = true,
+ ['getlocal'] = true,
+ ['field'] = true,
+ ['method'] = true,
+ ['tablefield'] = true,
+ ['setglobal'] = true,
+ ['getglobal'] = true,
+ ['string'] = true,
+}
+
local m = {}
function m.rename(uri, pos, newname)
@@ -332,22 +347,24 @@ function m.rename(uri, pos, newname)
if not ast then
return nil
end
+ local source = findSource(ast, pos, accept)
+ if not source then
+ return nil
+ end
local results = {}
local mark = {}
- guide.eachSourceContain(ast.ast, pos, function(source)
- rename(source, newname, function (target, start, finish, text)
- if mark[start] then
- return
- end
- mark[start] = true
- results[#results+1] = {
- start = start,
- finish = finish,
- text = text,
- uri = guide.getRoot(target).uri,
- }
- end)
+ rename(source, newname, function (target, start, finish, text)
+ if mark[start] then
+ return
+ end
+ mark[start] = true
+ results[#results+1] = {
+ start = start,
+ finish = finish,
+ text = text,
+ uri = guide.getRoot(target).uri,
+ }
end)
if Forcing == false then
@@ -371,20 +388,21 @@ function m.prepareRename(uri, pos)
if not ast then
return nil
end
+ local source = findSource(ast, pos, accept)
+ if not source then
+ return
+ end
- local result
- guide.eachSourceContain(ast.ast, pos, function(source)
- local res, text = prepareRename(source)
- if res then
- result = {
- start = source.start,
- finish = source.finish,
- text = text,
- }
- end
- end)
+ local res, text = prepareRename(source)
+ if not res then
+ return nil
+ end
- return result
+ return {
+ start = source.start,
+ finish = source.finish,
+ text = text,
+ }
end
return m
diff --git a/script-beta/vm/vm.lua b/script-beta/vm/vm.lua
index 556de3df..93641f86 100644
--- a/script-beta/vm/vm.lua
+++ b/script-beta/vm/vm.lua
@@ -136,6 +136,27 @@ function m.getKeyName(source)
return guide.getKeyName(source)
end
+function m.eachDef(source, callback)
+ local results = guide.requestDefinition(source)
+ for i = 1, #results do
+ callback(results[i])
+ end
+end
+
+function m.eachRef(source, callback)
+ local results = guide.requestReference(source)
+ for i = 1, #results do
+ callback(results[i])
+ end
+end
+
+function m.eachField(source, callback)
+ local results = guide.requestFields(source)
+ for i = 1, #results do
+ callback(results[i])
+ end
+end
+
m.cacheTracker = setmetatable({}, { __mode = 'kv' })
--- 刷新缓存