From e3486e01e820b2d61bf28e57277479ee59ab94b9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9C=80=E8=90=8C=E5=B0=8F=E6=B1=90?= Date: Wed, 11 Dec 2019 17:01:22 +0800 Subject: =?UTF-8?q?=E4=BF=AE=E6=AD=A3=E6=9C=AA=E5=BC=95=E7=94=A8=E5=8F=98?= =?UTF-8?q?=E9=87=8F=E7=9A=84bug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- script-beta/core/completion.lua | 26 +++-------- script-beta/core/diagnostics/unused-local.lua | 65 +++++++++++++++++++++++---- script-beta/parser/ast.lua | 3 ++ test-beta/completion/init.lua | 4 ++ test-beta/diagnostics/init.lua | 25 ++++++++--- 5 files changed, 89 insertions(+), 34 deletions(-) diff --git a/script-beta/core/completion.lua b/script-beta/core/completion.lua index a19edb5e..b0f15f74 100644 --- a/script-beta/core/completion.lua +++ b/script-beta/core/completion.lua @@ -360,29 +360,15 @@ end]] end}, {'function', function (ast, text, start, results) if config.config.completion.keywordSnippet then - local spStart = skipSpace(text, start - 1) - local before = findWord(text, spStart) - if before == 'local' then - results[#results+1] = { - label = 'function ()', - kind = ckind.Snippet, - insertTextFormat = 2, - insertText = [[ -function ($1) - $0 -end]] - } - else - results[#results+1] = { - label = 'function ()', - kind = ckind.Snippet, - insertTextFormat = 2, - insertText = [[ + results[#results+1] = { + label = 'function ()', + kind = ckind.Snippet, + insertTextFormat = 2, + insertText = [[ function $1($2) $0 end]] - } - end + } end end}, {'goto'}, diff --git a/script-beta/core/diagnostics/unused-local.lua b/script-beta/core/diagnostics/unused-local.lua index 22b2e16b..294a86e5 100644 --- a/script-beta/core/diagnostics/unused-local.lua +++ b/script-beta/core/diagnostics/unused-local.lua @@ -7,19 +7,46 @@ local function hasGet(loc) if not loc.ref then return false end + local weak for _, ref in ipairs(loc.ref) do if ref.type == 'getlocal' then if not ref.next then - return true + return 'strong' end local nextType = ref.next.type if nextType ~= 'setmethod' and nextType ~= 'setfield' and nextType ~= 'setindex' then - return true + return 'strong' + else + weak = true end end end + if weak then + return 'weak' + else + return nil + end +end + +local function isMyTable(loc) + local value = loc.value + if value and value.type == 'table' then + return true + end + return false +end + +local function isClose(source) + if not source.attrs then + return false + end + for _, attr in ipairs(source.attrs) do + if attr[1] == 'close' then + return true + end + end return false end @@ -34,13 +61,33 @@ return function (uri, callback) or name == '_ENV' then return end - if not hasGet(source) then - callback { - start = source.start, - finish = source.finish, - tags = { define.DiagnosticTag.Unnecessary }, - message = lang.script('DIAG_UNUSED_LOCAL', name), - } + if isClose(source) then + return + end + local data = hasGet(source) + if data == 'strong' then + return + end + if data == 'weak' then + if not isMyTable(source) then + return + end + end + callback { + start = source.start, + finish = source.finish, + tags = { define.DiagnosticTag.Unnecessary }, + message = lang.script('DIAG_UNUSED_LOCAL', name), + } + if source.ref then + for _, ref in ipairs(source.ref) do + callback { + start = ref.start, + finish = ref.finish, + tags = { define.DiagnosticTag.Unnecessary }, + message = lang.script('DIAG_UNUSED_LOCAL', name), + } + end end end) end diff --git a/script-beta/parser/ast.lua b/script-beta/parser/ast.lua index 42bbb751..43335950 100644 --- a/script-beta/parser/ast.lua +++ b/script-beta/parser/ast.lua @@ -1077,6 +1077,9 @@ local Defs = { return tableUnpack(keys) end, LocalAttr = function (attrs) + if #attrs == 0 then + return nil + end for i = 1, #attrs do local attr = attrs[i] local attrAst = attr diff --git a/test-beta/completion/init.lua b/test-beta/completion/init.lua index e6f740c4..36e9b9e4 100644 --- a/test-beta/completion/init.lua +++ b/test-beta/completion/init.lua @@ -283,6 +283,10 @@ while true d$ label = 'do', kind = CompletionItemKind.Keyword, }, + { + label = 'do .. end', + kind = CompletionItemKind.Snippet, + }, } TEST [[ diff --git a/test-beta/diagnostics/init.lua b/test-beta/diagnostics/init.lua index 0a38bce9..9ed22b1f 100644 --- a/test-beta/diagnostics/init.lua +++ b/test-beta/diagnostics/init.lua @@ -69,12 +69,27 @@ TEST [[ local ]] +TEST [[ +local x +]] + TEST [[ local function x() end x() ]] +TEST [[ +return function (x) + x.a = 1 +end +]] + +TEST [[ +local = {} +.a = 1 +]] + TEST([[ @@ -91,7 +106,7 @@ local = TEST [[ local -x = + = ]] @@ -338,18 +353,18 @@ return mt TEST [[ local = {} -function mt:f() +function :f() end ]] TEST [[ local = {} -x.a = 1 +.a = 1 ]] TEST [[ local = {} -x['a'] = 1 +['a'] = 1 ]] TEST [[ @@ -541,7 +556,7 @@ end TEST [[ local = {} -t[1] = 1 +[1] = 1 ]] --TEST [[ -- cgit v1.2.3