summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--changelog.md1
-rw-r--r--script/parser/guide.lua2
-rw-r--r--script/vm/compiler.lua84
-rw-r--r--test/tclient/init.lua1
-rw-r--r--test/tclient/tests/hover-pairs.lua40
5 files changed, 90 insertions, 38 deletions
diff --git a/changelog.md b/changelog.md
index 22e7d1b3..2048c043 100644
--- a/changelog.md
+++ b/changelog.md
@@ -4,6 +4,7 @@
* `FIX` [#1213](https://github.com/sumneko/lua-language-server/issues/1213)
* `FIX` [#1215](https://github.com/sumneko/lua-language-server/issues/1215)
* `FIX` [#1217](https://github.com/sumneko/lua-language-server/issues/1217)
+* `FIX` [#1218](https://github.com/sumneko/lua-language-server/issues/1218)
## 3.3.0
`2022-6-15`
diff --git a/script/parser/guide.lua b/script/parser/guide.lua
index 2894f673..28a37dac 100644
--- a/script/parser/guide.lua
+++ b/script/parser/guide.lua
@@ -13,6 +13,8 @@ local type = type
---@field args parser.object[]
---@field locals parser.object[]
---@field returns parser.object[]
+---@field exps parser.object[]
+---@field keys parser.object[]
---@field uri uri
---@field start integer
---@field finish integer
diff --git a/script/vm/compiler.lua b/script/vm/compiler.lua
index 5bdb1d3c..29de39ae 100644
--- a/script/vm/compiler.lua
+++ b/script/vm/compiler.lua
@@ -846,6 +846,44 @@ function vm.compileCallArg(arg, call, index)
return vm.getNode(arg)
end
+---@class parser.object
+---@field _iterator? table
+---@field _iterArgs? table
+---@field _iterVars? table<parser.object, vm.node>
+
+---@param source parser.object
+local function compileForVars(source)
+ if source._iterator then
+ return
+ end
+ -- for k, v in pairs(t) do
+ --> for k, v in iterator, status, initValue do
+ --> local k, v = iterator(status, initValue)
+ source._iterator = {
+ type = 'dummyfunc',
+ parent = source,
+ }
+ source._iterArgs = {{},{}}
+ source._iterVars = {}
+ -- iterator
+ selectNode(source._iterator, source.exps, 1)
+ -- status
+ selectNode(source._iterArgs[1], source.exps, 2)
+ -- initValue
+ selectNode(source._iterArgs[2], source.exps, 3)
+ if source.keys then
+ for i, loc in ipairs(source.keys) do
+ local node = getReturn(source._iterator, i, source._iterArgs)
+ if node then
+ if i == 1 then
+ node:removeOptional()
+ end
+ source._iterVars[loc] = node
+ end
+ end
+ end
+end
+
---@param source parser.object
---@return vm.node
local function compileLocal(source)
@@ -917,12 +955,18 @@ local function compileLocal(source)
end
-- for x in ... do
if source.parent.type == 'in' then
- vm.compileNode(source.parent)
+ compileForVars(source.parent)
+ local keyNode = source.parent._iterVars[source]
+ if keyNode then
+ vm.setNode(source, keyNode)
+ end
end
-- for x = ... do
if source.parent.type == 'loop' then
- vm.compileNode(source.parent)
+ if source.parent.loc == source then
+ vm.setNode(source, vm.declareGlobal('type', 'integer'))
+ end
end
vm.getNode(source):setData('hasDefined', hasMarkDoc or hasMarkParam or hasMarkValue)
@@ -1411,42 +1455,6 @@ local compilerSwitch = util.switch()
end
vm.setNode(source, node)
end)
- : case 'in'
- : call(function (source)
- if not source._iterator then
- -- for k, v in pairs(t) do
- --> for k, v in iterator, status, initValue do
- --> local k, v = iterator(status, initValue)
- source._iterator = {
- type = 'dummyfunc',
- parent = source,
- }
- source._iterArgs = {{},{}}
- end
- -- iterator
- selectNode(source._iterator, source.exps, 1)
- -- status
- selectNode(source._iterArgs[1], source.exps, 2)
- -- initValue
- selectNode(source._iterArgs[2], source.exps, 3)
- if source.keys then
- for i, loc in ipairs(source.keys) do
- local node = getReturn(source._iterator, i, source._iterArgs)
- if node then
- if i == 1 then
- node:removeOptional()
- end
- vm.setNode(loc, node)
- end
- end
- end
- end)
- : case 'loop'
- : call(function (source)
- if source.loc then
- vm.setNode(source.loc, vm.declareGlobal('type', 'integer'))
- end
- end)
: case 'doc.type'
: call(function (source)
for _, typeUnit in ipairs(source.types) do
diff --git a/test/tclient/init.lua b/test/tclient/init.lua
index e8d3963e..04640b79 100644
--- a/test/tclient/init.lua
+++ b/test/tclient/init.lua
@@ -6,3 +6,4 @@ require 'tclient.tests.load-library'
require 'tclient.tests.files-associations'
require 'tclient.tests.resolve-completion'
require 'tclient.tests.performance-jass-common'
+require 'tclient.tests.hover-pairs'
diff --git a/test/tclient/tests/hover-pairs.lua b/test/tclient/tests/hover-pairs.lua
new file mode 100644
index 00000000..d7f6f103
--- /dev/null
+++ b/test/tclient/tests/hover-pairs.lua
@@ -0,0 +1,40 @@
+local lclient = require 'lclient'
+local ws = require 'workspace'
+local await = require 'await'
+
+---@async
+lclient():start(function (client)
+ client:registerFakers()
+ client:initialize()
+
+ client:notify('textDocument/didOpen', {
+ textDocument = {
+ uri = 'file://test.lua',
+ languageId = 'lua',
+ version = 0,
+ text = [[
+local t ---@type table<string, number>
+for key, value in pairs(t) do
+ print(key, value) --key or value is unknown
+end
+]]
+ }
+ })
+
+ ws.awaitReady()
+
+ await.sleep(0.1)
+
+ local hover1 = client:awaitRequest('textDocument/hover', {
+ textDocument = { uri = 'file://test.lua' },
+ position = { line = 2, character = 11 },
+ })
+
+ local hover2 = client:awaitRequest('textDocument/hover', {
+ textDocument = { uri = 'file://test.lua' },
+ position = { line = 2, character = 17 },
+ })
+
+ assert(hover1.contents.value:find 'string')
+ assert(hover2.contents.value:find 'number')
+end)