diff options
author | 最萌小汐 <sumneko@hotmail.com> | 2020-12-04 13:14:57 +0800 |
---|---|---|
committer | 最萌小汐 <sumneko@hotmail.com> | 2020-12-04 13:14:57 +0800 |
commit | a85872115adb2a9d3ec0f14ddf0edb46016a4ea6 (patch) | |
tree | 975f4b42592a8b9a3c3df68c497d08f147e4d8e8 | |
parent | 923fcd8fe3e5ce1f140446482394eb2eb7261d8d (diff) | |
download | lua-language-server-a85872115adb2a9d3ec0f14ddf0edb46016a4ea6.zip |
Split getkeyname and getkeytype
-rw-r--r-- | script/core/code-action.lua | 4 | ||||
-rw-r--r-- | script/core/completion.lua | 5 | ||||
-rw-r--r-- | script/core/diagnostics/circle-doc-class.lua | 2 | ||||
-rw-r--r-- | script/core/diagnostics/duplicate-doc-class.lua | 2 | ||||
-rw-r--r-- | script/core/diagnostics/duplicate-index.lua | 7 | ||||
-rw-r--r-- | script/core/diagnostics/lowercase-global.lua | 2 | ||||
-rw-r--r-- | script/core/diagnostics/undefined-global.lua | 2 | ||||
-rw-r--r-- | script/core/hover/arg.lua | 2 | ||||
-rw-r--r-- | script/core/hover/name.lua | 10 | ||||
-rw-r--r-- | script/core/hover/table.lua | 6 | ||||
-rw-r--r-- | script/parser/guide.lua | 106 | ||||
-rw-r--r-- | script/vm/getClass.lua | 8 | ||||
-rw-r--r-- | script/vm/getMeta.lua | 2 | ||||
-rw-r--r-- | script/vm/vm.lua | 14 |
14 files changed, 92 insertions, 80 deletions
diff --git a/script/core/code-action.lua b/script/core/code-action.lua index 982f620e..3a7f7374 100644 --- a/script/core/code-action.lua +++ b/script/core/code-action.lua @@ -72,7 +72,7 @@ local function solveUndefinedGlobal(uri, diag, results) return end - local name = guide.getName(source) + local name = guide.getKeyName(source) markGlobal(uri, name, results) end) @@ -93,7 +93,7 @@ local function solveLowercaseGlobal(uri, diag, results) return end - local name = guide.getName(source) + local name = guide.getKeyName(source) markGlobal(uri, name, results) end) end diff --git a/script/core/completion.lua b/script/core/completion.lua index 174bde8e..af3547b6 100644 --- a/script/core/completion.lua +++ b/script/core/completion.lua @@ -426,14 +426,13 @@ local function checkFieldOfRefs(refs, ast, word, start, offset, parent, oop, res local fields = {} local count = 0 for _, src in ipairs(refs) do - local key = vm.getKeyName(src) - if not key or key:sub(1, 1) ~= 's' then + local name = vm.getKeyName(src) + if not name or vm.getKeyType(src) ~= 'string' then goto CONTINUE end if isSameSource(ast, src, start) then goto CONTINUE end - local name = key:sub(3) if locals and locals[name] then goto CONTINUE end diff --git a/script/core/diagnostics/circle-doc-class.lua b/script/core/diagnostics/circle-doc-class.lua index 55179447..7ef76603 100644 --- a/script/core/diagnostics/circle-doc-class.lua +++ b/script/core/diagnostics/circle-doc-class.lua @@ -19,7 +19,7 @@ return function (uri, callback) if not doc.extends then goto CONTINUE end - local myName = guide.getName(doc) + local myName = guide.getKeyName(doc) local list = { doc } local mark = {} for i = 1, 999 do diff --git a/script/core/diagnostics/duplicate-doc-class.lua b/script/core/diagnostics/duplicate-doc-class.lua index 259c048b..4751d28e 100644 --- a/script/core/diagnostics/duplicate-doc-class.lua +++ b/script/core/diagnostics/duplicate-doc-class.lua @@ -18,7 +18,7 @@ return function (uri, callback) for _, doc in ipairs(state.ast.docs) do if doc.type == 'doc.class' or doc.type == 'doc.alias' then - local name = guide.getName(doc) + local name = guide.getKeyName(doc) if not cache[name] then local docs = vm.getDocTypes(name) cache[name] = {} diff --git a/script/core/diagnostics/duplicate-index.lua b/script/core/diagnostics/duplicate-index.lua index dabe1b3c..3cf4faef 100644 --- a/script/core/diagnostics/duplicate-index.lua +++ b/script/core/diagnostics/duplicate-index.lua @@ -26,8 +26,7 @@ return function (uri, callback) end for name, defs in pairs(mark) do - local sname = name:match '^.|(.+)$' - if #defs > 1 and sname then + if #defs > 1 and name then local related = {} for i = 1, #defs do local def = defs[i] @@ -43,7 +42,7 @@ return function (uri, callback) start = def.start, finish = def.finish, related = related, - message = lang.script('DIAG_DUPLICATE_INDEX', sname), + message = lang.script('DIAG_DUPLICATE_INDEX', name), level = define.DiagnosticSeverity.Hint, tags = { define.DiagnosticTag.Unnecessary }, } @@ -54,7 +53,7 @@ return function (uri, callback) start = def.start, finish = def.finish, related = related, - message = lang.script('DIAG_DUPLICATE_INDEX', sname), + message = lang.script('DIAG_DUPLICATE_INDEX', name), } end end diff --git a/script/core/diagnostics/lowercase-global.lua b/script/core/diagnostics/lowercase-global.lua index fe5d1eca..8c7ae793 100644 --- a/script/core/diagnostics/lowercase-global.lua +++ b/script/core/diagnostics/lowercase-global.lua @@ -29,7 +29,7 @@ return function (uri, callback) end guide.eachSourceType(ast.ast, 'setglobal', function (source) - local name = guide.getName(source) + local name = guide.getKeyName(source) if definedGlobal[name] then return end diff --git a/script/core/diagnostics/undefined-global.lua b/script/core/diagnostics/undefined-global.lua index 5163ae9a..eb225169 100644 --- a/script/core/diagnostics/undefined-global.lua +++ b/script/core/diagnostics/undefined-global.lua @@ -13,7 +13,7 @@ return function (uri, callback) -- 遍历全局变量,检查所有没有 set 模式的全局变量 guide.eachSourceType(ast.ast, 'getglobal', function (src) - local key = guide.getName(src) + local key = guide.getKeyName(src) if not key then return end diff --git a/script/core/hover/arg.lua b/script/core/hover/arg.lua index c0518993..bb77c3db 100644 --- a/script/core/hover/arg.lua +++ b/script/core/hover/arg.lua @@ -20,7 +20,7 @@ local function asFunction(source, oop) local args = {} for i = 1, #source.args do local arg = source.args[i] - local name = arg.name or guide.getName(arg) + local name = arg.name or guide.getKeyName(arg) if name then args[i] = ('%s%s: %s'):format( name, diff --git a/script/core/hover/name.lua b/script/core/hover/name.lua index 04499e79..645cdaa9 100644 --- a/script/core/hover/name.lua +++ b/script/core/hover/name.lua @@ -4,7 +4,7 @@ local vm = require 'vm' local buildName local function asLocal(source) - local name = guide.getName(source) + local name = guide.getKeyName(source) if not source.attrs then return name end @@ -21,8 +21,8 @@ local function asField(source, oop) if source.node.type ~= 'getglobal' then class = vm.getClass(source.node, 0) end - local node = class or guide.getName(source.node) or '?' - local method = guide.getName(source) + local node = class or guide.getKeyName(source.node) or '?' + local method = guide.getKeyName(source) if oop then return ('%s:%s'):format(node, method) else @@ -34,11 +34,11 @@ local function asTableField(source) if not source.field then return end - return guide.getName(source.field) + return guide.getKeyName(source.field) end local function asGlobal(source) - return guide.getName(source) + return guide.getKeyName(source) end local function asDocFunction(source) diff --git a/script/core/hover/table.lua b/script/core/hover/table.lua index b43151ea..9fd79a25 100644 --- a/script/core/hover/table.lua +++ b/script/core/hover/table.lua @@ -6,7 +6,7 @@ local lang = require 'language' local function getKey(src) local key = vm.getKeyName(src) - if not key or #key <= 2 then + if not key or #key <= 0 then if not src.index then return '[any]' end @@ -20,9 +20,7 @@ local function getKey(src) end return '[any]' end - local ktype = key:sub(1, 2) - key = key:sub(3) - if ktype == 's|' then + if guide.getKeyType(src) == 'string' then if key:match '^[%a_][%w_]*$' then return key else diff --git a/script/parser/guide.lua b/script/parser/guide.lua index df5c5c45..7df247f2 100644 --- a/script/parser/guide.lua +++ b/script/parser/guide.lua @@ -29,6 +29,8 @@ _ENV = nil local m = {} +m.ANY = {} + local blockTypes = { ['while'] = true, ['in'] = true, @@ -660,48 +662,58 @@ function m.lineRange(lines, row, ignoreNL) end end -function m.getNameOfLiteral(obj) +function m.getKeyTypeOfLiteral(obj) if not obj then return nil end local tp = obj.type - if tp == 'string' then - return obj[1] + if tp == 'field' + or tp == 'method' then + return 'string' + elseif tp == 'string' then + return 'string' + elseif tp == 'number' then + return 'number' + elseif tp == 'boolean' then + return 'boolean' end - return nil end -function m.getName(obj) +function m.getKeyType(obj) + if not obj then + return nil + end local tp = obj.type if tp == 'getglobal' or tp == 'setglobal' then - return obj[1] + return 'string' elseif tp == 'local' or tp == 'getlocal' or tp == 'setlocal' then - return obj[1] + return 'local' elseif tp == 'getfield' or tp == 'setfield' or tp == 'tablefield' then - return obj.field and obj.field[1] + return 'string' elseif tp == 'getmethod' or tp == 'setmethod' then - return obj.method and obj.method[1] + return 'string' elseif tp == 'getindex' or tp == 'setindex' or tp == 'tableindex' then - return m.getNameOfLiteral(obj.index) + return m.getKeyTypeOfLiteral(obj.index) elseif tp == 'field' - or tp == 'method' then - return obj[1] + or tp == 'method' + or tp == 'doc.see.field' then + return 'string' elseif tp == 'doc.class' then - return obj.class[1] + return 'string' elseif tp == 'doc.alias' then - return obj.alias[1] + return 'string' elseif tp == 'doc.field' then - return obj.field[1] + return 'string' end - return m.getNameOfLiteral(obj) + return m.getKeyTypeOfLiteral(obj) end function m.getKeyNameOfLiteral(obj) @@ -711,30 +723,22 @@ function m.getKeyNameOfLiteral(obj) local tp = obj.type if tp == 'field' or tp == 'method' then - return 's|' .. obj[1] + return obj[1] elseif tp == 'string' then local s = obj[1] if s then - return 's|' .. s - else - return 's' + return s end elseif tp == 'number' then local n = obj[1] if n then - return ('n|%s'):format(util.viewLiteral(obj[1])) - else - return 'n' + return ('%s'):format(util.viewLiteral(obj[1])) end elseif tp == 'boolean' then local b = obj[1] if b then - return 'b|' .. tostring(b) - else - return 'b' + return tostring(b) end - else - return 'x' end end @@ -745,21 +749,21 @@ function m.getKeyName(obj) local tp = obj.type if tp == 'getglobal' or tp == 'setglobal' then - return 's|' .. obj[1] + return obj[1] elseif tp == 'local' or tp == 'getlocal' or tp == 'setlocal' then - return 'l|' .. obj[1] + return obj[1] elseif tp == 'getfield' or tp == 'setfield' or tp == 'tablefield' then if obj.field then - return 's|' .. obj.field[1] + return obj.field[1] end elseif tp == 'getmethod' or tp == 'setmethod' then if obj.method then - return 's|' .. obj.method[1] + return obj.method[1] end elseif tp == 'getindex' or tp == 'setindex' @@ -768,15 +772,13 @@ function m.getKeyName(obj) elseif tp == 'field' or tp == 'method' or tp == 'doc.see.field' then - return 's|' .. obj[1] + return obj[1] elseif tp == 'doc.class' then - return 's|' .. obj.class[1] + return obj.class[1] elseif tp == 'doc.alias' then - return 's|' .. obj.alias[1] + return obj.alias[1] elseif tp == 'doc.field' then - return 's|' .. obj.field[1] - elseif tp == 'dummy' then - return 's|' .. obj[1] + return obj.field[1] end return m.getKeyNameOfLiteral(obj) end @@ -786,17 +788,17 @@ function m.getSimpleName(obj) local key = obj.args and obj.args[2] return m.getKeyName(key) elseif obj.type == 'table' then - return ('t|%p'):format(obj) + return ('%p'):format(obj) elseif obj.type == 'select' then - return ('v|%p'):format(obj) + return ('%p'):format(obj) elseif obj.type == 'string' then - return ('z|%p'):format(obj) + return ('%p'):format(obj) elseif obj.type == 'doc.class.name' or obj.type == 'doc.type.name' or obj.type == 'doc.see.name' then - return ('c|%s'):format(obj[1]) + return ('%s'):format(obj[1]) elseif obj.type == 'doc.class' then - return ('c|%s'):format(obj.class[1]) + return ('%s'):format(obj.class[1]) end return m.getKeyName(obj) end @@ -1078,11 +1080,11 @@ local function convertSimpleList(list) simple.node = c end end - simple[#simple+1] = m.getSimpleName(c) + simple[#simple+1] = m.getSimpleName(c) or m.ANY ::CONTINUE:: end if simple.mode == 'global' and #simple == 0 then - simple[1] = 's|_G' + simple[1] = '_G' simple.node = list[#list] end return simple @@ -1320,7 +1322,7 @@ function m.searchFields(status, obj, key) if not simple then return end - simple[#simple+1] = key and ('s|' .. key) or '*' + simple[#simple+1] = key or m.ANY m.searchSameFields(status, simple, 'field') m.cleanResults(status.results) end @@ -1450,7 +1452,7 @@ function m.checkSameSimpleByBindDocs(status, obj, start, queue, mode) elseif doc.type == 'doc.param' then -- function (x) 的情况 if obj.type == 'local' - and m.getName(obj) == doc.param[1] then + and m.getKeyName(obj) == doc.param[1] then if obj.parent.type == 'funcargs' or obj.parent.type == 'in' or obj.parent.type == 'loop' then @@ -1830,7 +1832,7 @@ function m.checkSameSimpleInCall(status, ref, start, queue, mode) end local function searchRawset(ref, results) - if m.getKeyName(ref) ~= 's|rawset' then + if m.getKeyName(ref) ~= 'rawset' then return end local call = ref.parent @@ -1849,7 +1851,7 @@ local function searchRawset(ref, results) end local function searchG(ref, results) - while ref and m.getKeyName(ref) == 's|_G' do + while ref and m.getKeyName(ref) == '_G' do results[#results+1] = ref ref = ref.next end @@ -2223,7 +2225,7 @@ function m.pushResult(status, mode, ref, simple) end function m.checkSameSimpleName(ref, sm) - if sm == '*' then + if sm == m.ANY then return true end if m.getSimpleName(ref) == sm then @@ -3078,7 +3080,7 @@ function m.inferCheckUpDoc(status, source) elseif doc.type == 'doc.param' then -- function (x) 的情况 if source.type == 'local' - and m.getName(source) == doc.param[1] then + and m.getKeyName(source) == doc.param[1] then if source.parent.type == 'funcargs' or source.parent.type == 'in' or source.parent.type == 'loop' then @@ -3849,7 +3851,7 @@ function m.cleanInfers(infers, obj) infers[#infers] = nil goto CONTINUE end - local key = ('%s|%p'):format(infer.type, infer.source) + local key = ('%p'):format(infer.type, infer.source) if mark[key] then infers[i] = infers[#infers] infers[#infers] = nil diff --git a/script/vm/getClass.lua b/script/vm/getClass.lua index 7ac18e18..d95bab40 100644 --- a/script/vm/getClass.lua +++ b/script/vm/getClass.lua @@ -32,10 +32,10 @@ local function getClass(source, classes, depth, deep) goto CONTINUE end local lkey = key:lower() - if lkey == 's|type' - or lkey == 's|__name' - or lkey == 's|name' - or lkey == 's|class' then + if lkey == 'type' + or lkey == '__name' + or lkey == 'name' + or lkey == 'class' then local value = guide.getObjectValue(src) if value and value.type == 'string' then classes[#classes+1] = value[1] diff --git a/script/vm/getMeta.lua b/script/vm/getMeta.lua index aebef1a7..67db3d11 100644 --- a/script/vm/getMeta.lua +++ b/script/vm/getMeta.lua @@ -35,7 +35,7 @@ end function vm.eachMetaValue(source, callback) vm.eachMeta(source, function (mt) for _, src in ipairs(vm.getFields(mt)) do - if vm.getKeyName(src) == 's|__index' then + if vm.getKeyName(src) == '__index' then if src.value then for _, valueSrc in ipairs(vm.getFields(src.value)) do callback(valueSrc) diff --git a/script/vm/vm.lua b/script/vm/vm.lua index e942d55e..9caab31f 100644 --- a/script/vm/vm.lua +++ b/script/vm/vm.lua @@ -115,6 +115,20 @@ function m.getKeyName(source) return guide.getKeyName(source) end +function m.getKeyType(source) + if not source then + return nil + end + if source.type == 'call' then + local special = m.getSpecial(source.node) + if special == 'rawset' + or special == 'rawget' then + return guide.getKeyTypeOfLiteral(source.args[2]) + end + end + return guide.getKeyType(source) +end + function m.mergeResults(a, b) for _, r in ipairs(b) do if not a[r] then |