Skip to content

Commit fa518d4

Browse files
committed
fix #1057
1 parent cd6b80d commit fa518d4

File tree

6 files changed

+47
-33
lines changed

6 files changed

+47
-33
lines changed

changelog.md

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
* `CHG` hover: split `local` into `local` / `parameter` / `upvalue` / `self`.
66
* `CHG` hover: added parentheses to some words, such as `global` / `field` / `class`.
77
* `FIX` definition of `table<k, v>`
8+
* `FIX` [#1057](https://github.com/sumneko/lua-language-server/issues/1057)
89
* `FIX` runtime errors reported by telemetry, see [#1058](https://github.com/sumneko/lua-language-server/issues/1058)
910

1011
## 3.0.2

script/core/completion/completion.lua

+4-4
Original file line numberDiff line numberDiff line change
@@ -380,7 +380,7 @@ local function checkModule(state, word, position, results)
380380
goto CONTINUE
381381
end
382382
if targetSource.type == 'getlocal'
383-
and vm.isDeprecated(targetSource.node) then
383+
and vm.getDeprecated(targetSource.node) then
384384
goto CONTINUE
385385
end
386386
results[#results+1] = {
@@ -495,7 +495,7 @@ local function checkFieldThen(state, name, src, word, startPos, position, parent
495495
kind = kind,
496496
match = name:match '^[^(]+',
497497
insertText = name:match '^[^(]+',
498-
deprecated = vm.isDeprecated(src) or nil,
498+
deprecated = vm.getDeprecated(src) and true or nil,
499499
id = stack(function () ---@async
500500
return {
501501
detail = buildDetail(src),
@@ -526,7 +526,7 @@ local function checkFieldThen(state, name, src, word, startPos, position, parent
526526
results[#results+1] = {
527527
label = name,
528528
kind = kind,
529-
deprecated = vm.isDeprecated(src) or nil,
529+
deprecated = vm.getDeprecated(src) and true or nil,
530530
textEdit = textEdit,
531531
id = stack(function () ---@async
532532
return {
@@ -588,7 +588,7 @@ local function checkFieldOfRefs(refs, state, word, startPos, position, parent, o
588588
count = count + 1
589589
goto CONTINUE
590590
end
591-
if vm.isDeprecated(src) then
591+
if vm.getDeprecated(src) then
592592
goto CONTINUE
593593
end
594594
if guide.isSet(src) then

script/core/diagnostics/deprecated.lua

+10-16
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ local guide = require 'parser.guide'
55
local config = require 'config'
66
local define = require 'proto.define'
77
local await = require 'await'
8+
local util = require 'utility'
89

910
local types = {'getglobal', 'getfield', 'getindex', 'getmethod'}
1011
---@async
@@ -16,7 +17,6 @@ return function (uri, callback)
1617

1718
local dglobals = config.get(uri, 'Lua.diagnostics.globals')
1819
local rspecial = config.get(uri, 'Lua.runtime.special')
19-
local cache = {}
2020

2121
guide.eachSourceTypes(ast.ast, types, function (src) ---@async
2222
if src.type == 'getglobal' then
@@ -34,28 +34,17 @@ return function (uri, callback)
3434

3535
await.delay()
3636

37-
if not vm.isDeprecated(src, true) then
37+
local deprecated = vm.getDeprecated(src, true)
38+
if not deprecated then
3839
return
3940
end
4041

4142
await.delay()
4243

43-
local defs = vm.getDefs(src)
44-
local validVersions
45-
for _, def in ipairs(defs) do
46-
if def.bindDocs then
47-
for _, doc in ipairs(def.bindDocs) do
48-
if doc.type == 'doc.version' then
49-
validVersions = vm.getValidVersions(doc)
50-
break
51-
end
52-
end
53-
end
54-
end
55-
5644
local message = lang.script.DIAG_DEPRECATED
5745
local versions
58-
if validVersions then
46+
if deprecated.type == 'doc.version' then
47+
local validVersions = vm.getValidVersions(deprecated)
5948
versions = {}
6049
for version, valid in pairs(validVersions) do
6150
if valid then
@@ -71,6 +60,11 @@ return function (uri, callback)
7160
)
7261
end
7362
end
63+
if deprecated.type == 'doc.deprecated' then
64+
if deprecated.comment then
65+
message = ('%s(%s)'):format(message, util.trim(deprecated.comment.text))
66+
end
67+
end
7468

7569
callback {
7670
start = src.start,

script/utility.lua

+3-3
Original file line numberDiff line numberDiff line change
@@ -640,12 +640,12 @@ end
640640
---@return string
641641
function m.trim(str, mode)
642642
if mode == "left" then
643-
return str:gsub('^%s+', '')
643+
return (str:gsub('^%s+', ''))
644644
end
645645
if mode == "right" then
646-
return str:gsub('%s+$', '')
646+
return (str:gsub('%s+$', ''))
647647
end
648-
return str:match '^%s*(%S+)%s*$'
648+
return (str:match '^%s*(.-)%s*$')
649649
end
650650

651651
function m.expandPath(path)

script/vm/doc.lua

+21-10
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,8 @@ function vm.getValidVersions(doc)
8585
return valids
8686
end
8787

88-
local function isDeprecated(value)
88+
---@return parser.object?
89+
local function getDeprecated(value)
8990
if not value.bindDocs then
9091
return false
9192
end
@@ -94,34 +95,44 @@ local function isDeprecated(value)
9495
end
9596
for _, doc in ipairs(value.bindDocs) do
9697
if doc.type == 'doc.deprecated' then
97-
value._deprecated = true
98-
return true
98+
value._deprecated = doc
99+
return doc
99100
elseif doc.type == 'doc.version' then
100101
local valids = vm.getValidVersions(doc)
101102
if not valids[config.get(guide.getUri(value), 'Lua.runtime.version')] then
102-
value._deprecated = true
103-
return true
103+
value._deprecated = doc
104+
return doc
104105
end
105106
end
106107
end
107108
value._deprecated = false
108109
return false
109110
end
110111

111-
function vm.isDeprecated(value, deep)
112+
---@return parser.object?
113+
function vm.getDeprecated(value, deep)
112114
if deep then
113115
local defs = vm.getDefs(value)
114116
if #defs == 0 then
115117
return false
116118
end
119+
local deprecated = false
117120
for _, def in ipairs(defs) do
118-
if not isDeprecated(def) then
119-
return false
121+
if def.type == 'setglobal'
122+
or def.type == 'setfield'
123+
or def.type == 'setmethod'
124+
or def.type == 'setindex'
125+
or def.type == 'tablefield'
126+
or def.type == 'tableindex' then
127+
deprecated = getDeprecated(def)
128+
if not deprecated then
129+
return false
130+
end
120131
end
121132
end
122-
return true
133+
return deprecated
123134
else
124-
return isDeprecated(value)
135+
return getDeprecated(value)
125136
end
126137
end
127138

test/diagnostics/common.lua

+8
Original file line numberDiff line numberDiff line change
@@ -1387,3 +1387,11 @@ local value
13871387
value = '1'
13881388
value = value:gsub()
13891389
]]
1390+
1391+
TEST [[
1392+
T = {}
1393+
---@deprecated # comment
1394+
T.x = 1
1395+
1396+
print(<!T.x!>)
1397+
]]

0 commit comments

Comments
 (0)