summaryrefslogtreecommitdiff
path: root/script/core
diff options
context:
space:
mode:
Diffstat (limited to 'script/core')
-rw-r--r--script/core/completion.lua6
-rw-r--r--script/core/definition.lua2
-rw-r--r--script/core/diagnostics/deprecated.lua2
-rw-r--r--script/core/diagnostics/redundant-parameter.lua2
-rw-r--r--script/core/diagnostics/undefined-field.lua2
-rw-r--r--script/core/hint.lua2
-rw-r--r--script/core/hover/description.lua2
-rw-r--r--script/core/hover/init.lua4
-rw-r--r--script/core/hover/label.lua4
-rw-r--r--script/core/hover/table.lua177
-rw-r--r--script/core/infer.lua23
-rw-r--r--script/core/reference.lua2
-rw-r--r--script/core/rename.lua4
-rw-r--r--script/core/searcher.lua75
-rw-r--r--script/core/signature.lua2
15 files changed, 110 insertions, 199 deletions
diff --git a/script/core/completion.lua b/script/core/completion.lua
index 8a9e1f14..d2836101 100644
--- a/script/core/completion.lua
+++ b/script/core/completion.lua
@@ -149,7 +149,7 @@ local function getSnip(source)
if context <= 0 then
return nil
end
- local defs = vm.getRefs(source, 0)
+ local defs = vm.getRefs(source)
for _, def in ipairs(defs) do
def = searcher.getObjectValue(def) or def
if def ~= source and def.type == 'function' then
@@ -274,7 +274,7 @@ local function checkLocal(ast, word, offset, results)
goto CONTINUE
end
if vm.hasType(source, 'function') then
- for _, def in ipairs(vm.getDefs(source, 0)) do
+ for _, def in ipairs(vm.getDefs(source)) do
if def.type == 'function'
or def.type == 'doc.type.function' then
local funcLabel = name .. getParams(def, false)
@@ -1431,7 +1431,7 @@ local function tryCallArg(ast, text, offset, results)
if arg and arg.type == 'function' then
return
end
- local defs = vm.getDefs(call.node, 0)
+ local defs = vm.getDefs(call.node)
for _, def in ipairs(defs) do
def = searcher.getObjectValue(def) or def
local enums = getCallEnums(def, argIndex)
diff --git a/script/core/definition.lua b/script/core/definition.lua
index 973ce5f1..3ced05a2 100644
--- a/script/core/definition.lua
+++ b/script/core/definition.lua
@@ -128,7 +128,7 @@ return function (uri, offset)
end
end
- local defs = vm.getDefs(source, 0)
+ local defs = vm.getDefs(source)
local values = {}
for _, src in ipairs(defs) do
local value = searcher.getObjectValue(src)
diff --git a/script/core/diagnostics/deprecated.lua b/script/core/diagnostics/deprecated.lua
index 03475b65..a5822745 100644
--- a/script/core/diagnostics/deprecated.lua
+++ b/script/core/diagnostics/deprecated.lua
@@ -38,7 +38,7 @@ return function (uri, callback)
return
end
- local defs = vm.getDefs(src, 0)
+ local defs = vm.getDefs(src)
local validVersions
for _, def in ipairs(defs) do
if def.bindDocs then
diff --git a/script/core/diagnostics/redundant-parameter.lua b/script/core/diagnostics/redundant-parameter.lua
index b465dceb..eca7fc91 100644
--- a/script/core/diagnostics/redundant-parameter.lua
+++ b/script/core/diagnostics/redundant-parameter.lua
@@ -84,7 +84,7 @@ return function (uri, callback)
local funcArgs = cache[func]
if funcArgs == nil then
funcArgs = getFuncArgs(func) or false
- local refs = vm.getRefs(func, 0)
+ local refs = vm.getRefs(func)
for _, ref in ipairs(refs) do
cache[ref] = funcArgs
end
diff --git a/script/core/diagnostics/undefined-field.lua b/script/core/diagnostics/undefined-field.lua
index ffa70364..597dd8b9 100644
--- a/script/core/diagnostics/undefined-field.lua
+++ b/script/core/diagnostics/undefined-field.lua
@@ -18,7 +18,7 @@ return function (uri, callback)
if cache[src] == nil then
tracy.ZoneBeginN('undefined-field getInfers')
infers = vm.getInfers(src, 0) or false
- local refs = vm.getRefs(src, 0)
+ local refs = vm.getRefs(src)
for _, ref in ipairs(refs) do
cache[ref] = infers
end
diff --git a/script/core/hint.lua b/script/core/hint.lua
index 9c0d9cf0..43b8726e 100644
--- a/script/core/hint.lua
+++ b/script/core/hint.lua
@@ -107,7 +107,7 @@ local function paramName(uri, edits, start, finish)
if not hasLiteralArgInCall(source) then
return
end
- local defs = vm.getDefs(source.node, 0)
+ local defs = vm.getDefs(source.node)
if not defs then
return
end
diff --git a/script/core/hover/description.lua b/script/core/hover/description.lua
index 77c52de5..327ddbe1 100644
--- a/script/core/hover/description.lua
+++ b/script/core/hover/description.lua
@@ -126,7 +126,7 @@ local function getBindComment(source, docGroup, base)
end
local function tryDocClassComment(source)
- for _, def in ipairs(vm.getDefs(source, 0)) do
+ for _, def in ipairs(vm.getDefs(source)) do
if def.type == 'doc.class.name'
or def.type == 'doc.alias.name' then
local class = noder.getDocState(def)
diff --git a/script/core/hover/init.lua b/script/core/hover/init.lua
index ded3e9eb..ea86f785 100644
--- a/script/core/hover/init.lua
+++ b/script/core/hover/init.lua
@@ -25,7 +25,7 @@ local function getHoverAsValue(source)
local label = getLabel(source)
local desc = getDesc(source)
if not desc then
- local values = vm.getDefs(source, 0)
+ local values = vm.getDefs(source)
for _, def in ipairs(values) do
desc = getDesc(def)
if desc then
@@ -41,7 +41,7 @@ local function getHoverAsValue(source)
end
local function getHoverAsFunction(source)
- local values = vm.getDefs(source, 0)
+ local values = vm.getDefs(source)
local desc = getDesc(source)
local labels = {}
local defs = 0
diff --git a/script/core/hover/label.lua b/script/core/hover/label.lua
index b93d3016..de30d372 100644
--- a/script/core/hover/label.lua
+++ b/script/core/hover/label.lua
@@ -45,8 +45,8 @@ end
local function asValue(source, title)
local name = buildName(source)
- local type = infer.searchAndViewInfers(source, 0)
- local literal = infer.searchAndViewLiterals(source, 0)
+ local type = infer.searchAndViewInfers(source)
+ local literal = infer.searchAndViewLiterals(source)
local cont
if not infer.hasType(source, 'string', 0)
and not type:find('%[%]$')
diff --git a/script/core/hover/table.lua b/script/core/hover/table.lua
index 8974738d..bac0bd84 100644
--- a/script/core/hover/table.lua
+++ b/script/core/hover/table.lua
@@ -1,10 +1,11 @@
local vm = require 'vm'
local util = require 'utility'
-local searcher = require 'core.searcher'
+local searcher = require 'core.searcher'
local config = require 'config'
local lang = require 'language'
+local infer = require 'core.infer'
-local function getKey(src)
+local function formatKey(src)
local key = vm.getKeyName(src)
if not key or #key <= 0 then
if not src.index then
@@ -30,104 +31,21 @@ local function getKey(src)
return ('[%s]'):format(key)
end
-local function getFieldFull(src)
- local value = searcher.getObjectValue(src) or src
- local tp = vm.getInferType(value, 0)
- --local class = vm.getClass(src)
- local literal = vm.getInferLiteral(value)
- if type(literal) == 'string' and #literal >= 50 then
- literal = literal:sub(1, 47) .. '...'
- end
- return tp, literal
-end
-
-local function getFieldFast(src)
- if src.bindDocs then
- return getFieldFull(src)
- end
- local value = searcher.getObjectValue(src) or src
- if not value then
- return 'any'
- end
- if value.type == 'boolean' then
- return value.type, util.viewLiteral(value[1])
- end
- if value.type == 'number'
- or value.type == 'integer' then
- if math.tointeger(value[1]) then
- if config.config.runtime.version == 'Lua 5.3'
- or config.config.runtime.version == 'Lua 5.4' then
- return 'integer', util.viewLiteral(value[1])
- end
- end
- return value.type, util.viewLiteral(value[1])
- end
- if value.type == 'table'
- or value.type == 'function' then
- return value.type
- end
- if value.type == 'string' then
- local literal = value[1]
- if type(literal) == 'string' and #literal >= 50 then
- literal = literal:sub(1, 47) .. '...'
- end
- return value.type, util.viewLiteral(literal)
- end
- if value.type == 'doc.field' then
- return vm.getInferType(value)
- end
-end
-
-local function getField(src, timeUp, mark, key)
- if src.type == 'table'
- or src.type == 'function' then
- return nil
- end
- if src.parent then
- if src.type == 'string'
- or src.type == 'boolean'
- or src.type == 'number'
- or src.type == 'integer' then
- if src.parent.type == 'tableindex'
- or src.parent.type == 'setindex'
- or src.parent.type == 'getindex' then
- if src.parent.index == src then
- src = src.parent
- end
- end
- end
- end
- local tp, literal
- tp, literal = getFieldFast(src)
- if tp then
- return tp, literal
- end
- if timeUp or mark[key] then
- return nil
- end
- mark[key] = true
- tp, literal = getFieldFull(src)
- if tp then
- return tp, literal
- end
- return nil
-end
-
-local function buildAsHash(classes, literals, reachMax)
+local function buildAsHash(keyMap, inferMap, literalMap, reachMax)
local keys = {}
- for k in pairs(classes) do
+ for k in pairs(keyMap) do
keys[#keys+1] = k
end
table.sort(keys)
local lines = {}
lines[#lines+1] = '{'
for _, key in ipairs(keys) do
- local class = classes[key]
- local literal = literals[key]
- if literal then
- lines[#lines+1] = (' %s: %s = %s,'):format(key, class, literal)
+ local inferView = inferMap[key]
+ local literalView = literalMap[key]
+ if literalView then
+ lines[#lines+1] = (' %s: %s = %s,'):format(formatKey(key), inferView, literalView)
else
- lines[#lines+1] = (' %s: %s,'):format(key, class)
+ lines[#lines+1] = (' %s: %s,'):format(formatKey(key), inferView)
end
end
if reachMax then
@@ -137,23 +55,23 @@ local function buildAsHash(classes, literals, reachMax)
return table.concat(lines, '\n')
end
-local function buildAsConst(classes, literals, reachMax)
+local function buildAsConst(keyMap, inferMap, literalMap, reachMax)
local keys = {}
- for k in pairs(classes) do
+ for k in pairs(keyMap) do
keys[#keys+1] = k
end
table.sort(keys, function (a, b)
- return tonumber(literals[a]) < tonumber(literals[b])
+ return tonumber(literalMap[a]) < tonumber(literalMap[b])
end)
local lines = {}
lines[#lines+1] = '{'
for _, key in ipairs(keys) do
- local class = classes[key]
- local literal = literals[key]
- if literal then
- lines[#lines+1] = (' %s: %s = %s,'):format(key, class, literal)
+ local inferView = inferMap[key]
+ local literalView = literalMap[key]
+ if literalView then
+ lines[#lines+1] = (' %s: %s = %s,'):format(formatKey(key), inferView, literalView)
else
- lines[#lines+1] = (' %s: %s,'):format(key, class)
+ lines[#lines+1] = (' %s: %s,'):format(formatKey(key), inferView)
end
end
if reachMax then
@@ -163,45 +81,13 @@ local function buildAsConst(classes, literals, reachMax)
return table.concat(lines, '\n')
end
-local function mergeLiteral(literals)
- local results = {}
- local mark = {}
- for _, value in ipairs(literals) do
- if not mark[value] then
- mark[value] = true
- results[#results+1] = value
- end
- end
- if #results == 0 then
- return nil
- end
- table.sort(results)
- return table.concat(results, '|')
-end
-
-local function mergeTypes(types)
- local results = {}
- local mark = {
- -- 讲道理table的keyvalue不会是nil
- ['nil'] = true,
- }
- for _, tv in ipairs(types) do
- for tp in tv:gmatch '[^|]+' do
- if not mark[tp] then
- mark[tp] = true
- results[tp] = true
- end
- end
- end
- return searcher.mergeTypes(results)
-end
-
local function clearClasses(classes)
classes['[nil]'] = nil
classes['[any]'] = nil
classes['[string]'] = nil
end
+--[[
return function (source)
if config.config.hover.previewFields <= 0 then
return 'table'
@@ -271,3 +157,28 @@ return function (source)
end
return result
end
+--]]
+
+local function getKeyMap(fields)
+ local keys = {}
+ for _, field in ipairs(fields) do
+ local key = vm.getKeyName(field)
+ if key then
+ keys[key] = true
+ end
+ end
+ return keys
+end
+
+return function (source)
+ if config.config.hover.previewFields <= 0 then
+ return 'table'
+ end
+ local fields = vm.getFields(source)
+ local keyMap = getKeyMap(fields)
+ local inferMap = {}
+ local literalMap = {}
+ for key in pairs(keyMap) do
+ inferMap[key] = infer.searchAndViewInfers(source, key)
+ end
+end
diff --git a/script/core/infer.lua b/script/core/infer.lua
index 425992fc..cefb99e9 100644
--- a/script/core/infer.lua
+++ b/script/core/infer.lua
@@ -444,12 +444,13 @@ end
---搜索对象的推断类型
---@param source parser.guide.object
+---@param field? string
---@return string[]
-function m.searchInfers(source)
+function m.searchInfers(source, field)
if not source then
return nil
end
- local defs = searcher.requestDefinition(source)
+ local defs = searcher.requestDefinition(source, field)
local infers = {}
local mark = {}
mark[source] = true
@@ -482,9 +483,10 @@ end
---搜索对象的字面量值
---@param source parser.guide.object
+---@param field? string
---@return table
-function m.searchLiterals(source)
- local defs = searcher.requestDefinition(source)
+function m.searchLiterals(source, field)
+ local defs = searcher.requestDefinition(source, field)
local literals = {}
local mark = {}
mark[source] = true
@@ -498,11 +500,15 @@ function m.searchLiterals(source)
return literals
end
-function m.searchAndViewLiterals(source)
+---搜索并显示推断值
+---@param source parser.guide.object
+---@param field? string
+---@return string
+function m.searchAndViewLiterals(source, field)
if not source then
return nil
end
- local literals = m.searchLiterals(source)
+ local literals = m.searchLiterals(source, field)
local view = m.viewLiterals(literals)
return view
end
@@ -530,12 +536,13 @@ end
---搜索并显示推断类型
---@param source parser.guide.object
+---@param field? string
---@return string
-function m.searchAndViewInfers(source)
+function m.searchAndViewInfers(source, field)
if not source then
return 'any'
end
- local infers = m.searchInfers(source)
+ local infers = m.searchInfers(source, field)
local view = m.viewInfers(infers)
return view
end
diff --git a/script/core/reference.lua b/script/core/reference.lua
index ca067a75..c3f3b349 100644
--- a/script/core/reference.lua
+++ b/script/core/reference.lua
@@ -65,7 +65,7 @@ return function (uri, offset)
local metaSource = vm.isMetaFile(uri)
- local refs = vm.getRefs(source, 5)
+ local refs = vm.getRefs(source)
local values = {}
for _, src in ipairs(refs) do
local value = searcher.getObjectValue(src)
diff --git a/script/core/rename.lua b/script/core/rename.lua
index 0851f191..1b28be06 100644
--- a/script/core/rename.lua
+++ b/script/core/rename.lua
@@ -299,7 +299,7 @@ end
local function ofGlobal(source, newname, callback)
local key = searcher.getKeyName(source)
- for _, src in ipairs(vm.getRefs(source, 0)) do
+ for _, src in ipairs(vm.getRefs(source)) do
ofFieldThen(key, src, newname, callback)
end
end
@@ -308,7 +308,7 @@ local function ofLabel(source, newname, callback)
if not isValidName(newname) and not askForcing(newname)then
return false
end
- for _, src in ipairs(vm.getRefs(source, 0)) do
+ for _, src in ipairs(vm.getRefs(source)) do
callback(src, src.start, src.finish, newname)
end
end
diff --git a/script/core/searcher.lua b/script/core/searcher.lua
index 31715ca5..7835b213 100644
--- a/script/core/searcher.lua
+++ b/script/core/searcher.lua
@@ -8,22 +8,6 @@ local vm = require 'vm.vm'
local NONE = {'NONE'}
local LAST = {'LAST'}
-local function checkFunctionReturn(source)
- if source.parent
- and source.parent.type == 'return' then
- if source.parent.parent.type == 'main' then
- return 0
- elseif source.parent.parent.type == 'function' then
- for i = 1, #source.parent do
- if source.parent[i] == source then
- return i
- end
- end
- end
- end
- return nil
-end
-
local ignoredIDs = {
['dn:nil'] = true,
['dn:any'] = true,
@@ -52,8 +36,13 @@ function m.pushResult(status, mode, source, force)
return
end
local results = status.results
+ if results[source] then
+ return
+ end
+ results[source] = true
if force then
results[#results+1] = source
+ return
end
local parent = source.parent
if mode == 'def' then
@@ -135,7 +124,6 @@ function m.pushResult(status, mode, source, force)
results[#results+1] = source
end
end
- elseif mode == 'field' then
end
end
@@ -521,10 +509,10 @@ local function prepareSearch(source)
return uri, id
end
-local function getField(status, source)
+local function getField(status, source, mode)
if source.type == 'table' then
for _, field in ipairs(source) do
- status.results[#status.results+1] = field
+ m.pushResult(status, mode, field)
end
end
local field = source.next
@@ -535,7 +523,7 @@ local function getField(status, source)
or field.type == 'setfield'
or field.type == 'getindex'
or field.type == 'setindex' then
- status.results[#status.results+1] = field
+ m.pushResult(status, mode, field)
end
return
end
@@ -558,15 +546,21 @@ end
---@param status guide.status
---@param source parser.guide.object
---@param mode guide.searchmode
-function m.searchFields(status, source, mode)
+---@param field string
+function m.searchFields(status, source, mode, field)
local uri, id = prepareSearch(source)
if not id then
return
end
- local newStatus = m.status(status)
- m.searchRefsByID(newStatus, uri, id, mode)
- for _, def in ipairs(newStatus.results) do
- getField(status, def)
+ if field == '*' then
+ local newStatus = m.status(status)
+ m.searchRefsByID(newStatus, uri, id, mode)
+ for _, def in ipairs(newStatus.results) do
+ getField(status, def, mode)
+ end
+ else
+ local fullID = id .. noder.SPLIT_CHAR .. field
+ m.searchRefsByID(status, uri, fullID, mode)
end
end
@@ -590,35 +584,34 @@ end
--- 请求对象的引用
---@param obj parser.guide.object
+---@param field? string
---@return parser.guide.object[]
---@return integer
-function m.requestReference(obj)
+function m.requestReference(obj, field)
local status = m.status()
- -- 根据 field 搜索引用
- m.searchRefs(status, obj, 'ref')
- return status.results, 0
+ if field then
+ m.searchFields(status, obj, 'ref', field)
+ else
+ m.searchRefs(status, obj, 'ref')
+ end
+
+ return status.results
end
--- 请求对象的定义
---@param obj parser.guide.object
+---@param field? string
---@return parser.guide.object[]
---@return integer
-function m.requestDefinition(obj)
+function m.requestDefinition(obj, field)
local status = m.status()
- -- 根据 field 搜索引用
- m.searchRefs(status, obj, 'def')
- return status.results, 0
-end
-
---- 请求对象的field
-function m.requestFields(obj, key)
- if key then
- error('not support')
+ if field then
+ m.searchFields(status, obj, 'ref', field)
+ else
+ m.searchRefs(status, obj, 'def')
end
- local status = m.status()
- m.searchFields(status, obj, 'ref')
return status.results
end
diff --git a/script/core/signature.lua b/script/core/signature.lua
index 6886d151..d01557cd 100644
--- a/script/core/signature.lua
+++ b/script/core/signature.lua
@@ -96,7 +96,7 @@ local function makeSignatures(call, pos)
index = 1
end
local signs = {}
- local defs = vm.getDefs(node, 0)
+ local defs = vm.getDefs(node)
local mark = {}
for _, src in ipairs(defs) do
src = searcher.getObjectValue(src) or src