summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
author最萌小汐 <sumneko@hotmail.com>2019-12-11 17:01:22 +0800
committer最萌小汐 <sumneko@hotmail.com>2019-12-11 17:01:22 +0800
commite3486e01e820b2d61bf28e57277479ee59ab94b9 (patch)
treefd023532fc7c8b785a9f33083e0b9ec908f15214
parentee4396dce24c8fd004d1a53a80328cf573809af9 (diff)
downloadlua-language-server-e3486e01e820b2d61bf28e57277479ee59ab94b9.zip
修正未引用变量的bug
-rw-r--r--script-beta/core/completion.lua26
-rw-r--r--script-beta/core/diagnostics/unused-local.lua65
-rw-r--r--script-beta/parser/ast.lua3
-rw-r--r--test-beta/completion/init.lua4
-rw-r--r--test-beta/diagnostics/init.lua25
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
@@ -70,11 +70,26 @@ local <!x!>
]]
TEST [[
+local x <close>
+]]
+
+TEST [[
local function x()
end
x()
]]
+TEST [[
+return function (x)
+ x.a = 1
+end
+]]
+
+TEST [[
+local <!t!> = {}
+<!t!>.a = 1
+]]
+
TEST([[
<!local function x()
end!>
@@ -91,7 +106,7 @@ local <!x!> = <!function () end!>
TEST [[
local <!x!>
-x = <!function () end!>
+<!x!> = <!function () end!>
]]
@@ -338,18 +353,18 @@ return mt
TEST [[
local <!mt!> = {}
-function mt:f()
+function <!mt!>:f()
end
]]
TEST [[
local <!x!> = {}
-x.a = 1
+<!x!>.a = 1
]]
TEST [[
local <!x!> = {}
-x['a'] = 1
+<!x!>['a'] = 1
]]
TEST [[
@@ -541,7 +556,7 @@ end
TEST [[
local <!t!> = {}
-t[1] = 1
+<!t!>[1] = 1
]]
--TEST [[