summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
author最萌小汐 <sumneko@hotmail.com>2022-04-16 21:50:29 +0800
committer最萌小汐 <sumneko@hotmail.com>2022-04-16 21:50:29 +0800
commitfa518d4f9522b38aca9478ac494e9523cca475f3 (patch)
treeb82261ecb612e15c9fa9a8381839898dbab3149a
parentcd6b80d6126a785c4407e118c13388db537d6bf3 (diff)
downloadlua-language-server-fa518d4f9522b38aca9478ac494e9523cca475f3.zip
fix #1057
-rw-r--r--changelog.md1
-rw-r--r--script/core/completion/completion.lua8
-rw-r--r--script/core/diagnostics/deprecated.lua26
-rw-r--r--script/utility.lua6
-rw-r--r--script/vm/doc.lua31
-rw-r--r--test/diagnostics/common.lua8
6 files changed, 47 insertions, 33 deletions
diff --git a/changelog.md b/changelog.md
index 54a5676f..772be9e4 100644
--- a/changelog.md
+++ b/changelog.md
@@ -5,6 +5,7 @@
* `CHG` hover: split `local` into `local` / `parameter` / `upvalue` / `self`.
* `CHG` hover: added parentheses to some words, such as `global` / `field` / `class`.
* `FIX` definition of `table<k, v>`
+* `FIX` [#1057](https://github.com/sumneko/lua-language-server/issues/1057)
* `FIX` runtime errors reported by telemetry, see [#1058](https://github.com/sumneko/lua-language-server/issues/1058)
## 3.0.2
diff --git a/script/core/completion/completion.lua b/script/core/completion/completion.lua
index 75f75999..c257643d 100644
--- a/script/core/completion/completion.lua
+++ b/script/core/completion/completion.lua
@@ -380,7 +380,7 @@ local function checkModule(state, word, position, results)
goto CONTINUE
end
if targetSource.type == 'getlocal'
- and vm.isDeprecated(targetSource.node) then
+ and vm.getDeprecated(targetSource.node) then
goto CONTINUE
end
results[#results+1] = {
@@ -495,7 +495,7 @@ local function checkFieldThen(state, name, src, word, startPos, position, parent
kind = kind,
match = name:match '^[^(]+',
insertText = name:match '^[^(]+',
- deprecated = vm.isDeprecated(src) or nil,
+ deprecated = vm.getDeprecated(src) and true or nil,
id = stack(function () ---@async
return {
detail = buildDetail(src),
@@ -526,7 +526,7 @@ local function checkFieldThen(state, name, src, word, startPos, position, parent
results[#results+1] = {
label = name,
kind = kind,
- deprecated = vm.isDeprecated(src) or nil,
+ deprecated = vm.getDeprecated(src) and true or nil,
textEdit = textEdit,
id = stack(function () ---@async
return {
@@ -588,7 +588,7 @@ local function checkFieldOfRefs(refs, state, word, startPos, position, parent, o
count = count + 1
goto CONTINUE
end
- if vm.isDeprecated(src) then
+ if vm.getDeprecated(src) then
goto CONTINUE
end
if guide.isSet(src) then
diff --git a/script/core/diagnostics/deprecated.lua b/script/core/diagnostics/deprecated.lua
index a5d623d2..27920c43 100644
--- a/script/core/diagnostics/deprecated.lua
+++ b/script/core/diagnostics/deprecated.lua
@@ -5,6 +5,7 @@ local guide = require 'parser.guide'
local config = require 'config'
local define = require 'proto.define'
local await = require 'await'
+local util = require 'utility'
local types = {'getglobal', 'getfield', 'getindex', 'getmethod'}
---@async
@@ -16,7 +17,6 @@ return function (uri, callback)
local dglobals = config.get(uri, 'Lua.diagnostics.globals')
local rspecial = config.get(uri, 'Lua.runtime.special')
- local cache = {}
guide.eachSourceTypes(ast.ast, types, function (src) ---@async
if src.type == 'getglobal' then
@@ -34,28 +34,17 @@ return function (uri, callback)
await.delay()
- if not vm.isDeprecated(src, true) then
+ local deprecated = vm.getDeprecated(src, true)
+ if not deprecated then
return
end
await.delay()
- local defs = vm.getDefs(src)
- local validVersions
- for _, def in ipairs(defs) do
- if def.bindDocs then
- for _, doc in ipairs(def.bindDocs) do
- if doc.type == 'doc.version' then
- validVersions = vm.getValidVersions(doc)
- break
- end
- end
- end
- end
-
local message = lang.script.DIAG_DEPRECATED
local versions
- if validVersions then
+ if deprecated.type == 'doc.version' then
+ local validVersions = vm.getValidVersions(deprecated)
versions = {}
for version, valid in pairs(validVersions) do
if valid then
@@ -71,6 +60,11 @@ return function (uri, callback)
)
end
end
+ if deprecated.type == 'doc.deprecated' then
+ if deprecated.comment then
+ message = ('%s(%s)'):format(message, util.trim(deprecated.comment.text))
+ end
+ end
callback {
start = src.start,
diff --git a/script/utility.lua b/script/utility.lua
index f302e706..5a52e417 100644
--- a/script/utility.lua
+++ b/script/utility.lua
@@ -640,12 +640,12 @@ end
---@return string
function m.trim(str, mode)
if mode == "left" then
- return str:gsub('^%s+', '')
+ return (str:gsub('^%s+', ''))
end
if mode == "right" then
- return str:gsub('%s+$', '')
+ return (str:gsub('%s+$', ''))
end
- return str:match '^%s*(%S+)%s*$'
+ return (str:match '^%s*(.-)%s*$')
end
function m.expandPath(path)
diff --git a/script/vm/doc.lua b/script/vm/doc.lua
index 1367bbc2..5a92a103 100644
--- a/script/vm/doc.lua
+++ b/script/vm/doc.lua
@@ -85,7 +85,8 @@ function vm.getValidVersions(doc)
return valids
end
-local function isDeprecated(value)
+---@return parser.object?
+local function getDeprecated(value)
if not value.bindDocs then
return false
end
@@ -94,13 +95,13 @@ local function isDeprecated(value)
end
for _, doc in ipairs(value.bindDocs) do
if doc.type == 'doc.deprecated' then
- value._deprecated = true
- return true
+ value._deprecated = doc
+ return doc
elseif doc.type == 'doc.version' then
local valids = vm.getValidVersions(doc)
if not valids[config.get(guide.getUri(value), 'Lua.runtime.version')] then
- value._deprecated = true
- return true
+ value._deprecated = doc
+ return doc
end
end
end
@@ -108,20 +109,30 @@ local function isDeprecated(value)
return false
end
-function vm.isDeprecated(value, deep)
+---@return parser.object?
+function vm.getDeprecated(value, deep)
if deep then
local defs = vm.getDefs(value)
if #defs == 0 then
return false
end
+ local deprecated = false
for _, def in ipairs(defs) do
- if not isDeprecated(def) then
- return false
+ if def.type == 'setglobal'
+ or def.type == 'setfield'
+ or def.type == 'setmethod'
+ or def.type == 'setindex'
+ or def.type == 'tablefield'
+ or def.type == 'tableindex' then
+ deprecated = getDeprecated(def)
+ if not deprecated then
+ return false
+ end
end
end
- return true
+ return deprecated
else
- return isDeprecated(value)
+ return getDeprecated(value)
end
end
diff --git a/test/diagnostics/common.lua b/test/diagnostics/common.lua
index 6aa1dd6a..56ad0d59 100644
--- a/test/diagnostics/common.lua
+++ b/test/diagnostics/common.lua
@@ -1387,3 +1387,11 @@ local value
value = '1'
value = value:gsub()
]]
+
+TEST [[
+T = {}
+---@deprecated # comment
+T.x = 1
+
+print(<!T.x!>)
+]]