summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--README.md3
-rw-r--r--server/src/matcher/completion.lua20
-rw-r--r--server/src/matcher/vm.lua18
-rw-r--r--server/test/completion/init.lua30
-rw-r--r--server/test/crossfile/completion.lua16
5 files changed, 76 insertions, 11 deletions
diff --git a/README.md b/README.md
index bd4f2573..5350a68a 100644
--- a/README.md
+++ b/README.md
@@ -7,7 +7,7 @@
- [x] Goto Definition
- [x] Goto Implementation
- [x] Find References
-- [x] Type Inference
+- [x] Rough Type Inference
- [x] Hover
- [x] Diagnostics
- [x] Rename
@@ -18,6 +18,7 @@
- [ ] Syntax Check
- [ ] Multi Workspace
- [ ] Type Format
+- [ ] Accurate Type Inference
### Locale
diff --git a/server/src/matcher/completion.lua b/server/src/matcher/completion.lua
index 83f51044..16be7b4b 100644
--- a/server/src/matcher/completion.lua
+++ b/server/src/matcher/completion.lua
@@ -232,12 +232,26 @@ local function searchAsArg(vm, inCall, inString, callback)
end
for _, v in ipairs(results) do
if v ~= inString[1] then
- callback(v, CompletionItemKind.Module)
+ callback(v, CompletionItemKind.File)
end
end
end
end
+local function searchAsIndex(vm, pos, result, callback)
+ searchLocals(vm, pos, result.key, function (var)
+ callback(var, CompletionItemKind.Variable)
+ end)
+ for _, index in ipairs(vm.results.indexs) do
+ if matchKey(result.key, index) then
+ callback(index, CompletionItemKind.Property)
+ end
+ end
+ searchFields(result.key, vm.results.locals[1], nil, function (var)
+ callback(var, CompletionItemKind.Field)
+ end)
+end
+
local function findClosePos(vm, pos)
local curDis = math.maxinteger
local parent = nil
@@ -394,7 +408,9 @@ return function (vm, pos)
if result.type == 'local' then
searchAsGlobal(vm, pos, result, callback)
elseif result.type == 'field' then
- if result.parent and result.parent.value and result.parent.value.ENV == true then
+ if result.isIndex then
+ searchAsIndex(vm, pos, result, callback)
+ elseif result.parent and result.parent.value and result.parent.value.ENV == true then
searchAsGlobal(vm, pos, result, callback)
else
searchAsSuffix(result, callback)
diff --git a/server/src/matcher/vm.lua b/server/src/matcher/vm.lua
index e2ea5a15..00e4d11f 100644
--- a/server/src/matcher/vm.lua
+++ b/server/src/matcher/vm.lua
@@ -70,6 +70,14 @@ local function readOnly(t)
})
end
+local function insertOnce(tbl, key)
+ if tbl[key] then
+ return
+ end
+ tbl[key] = true
+ tbl[#tbl+1] = key
+end
+
local mt = {}
mt.__index = mt
@@ -190,7 +198,9 @@ function mt:buildTable(source)
else
if key.type == 'name' then
local index = key[1]
+ insertOnce(self.results.indexs, index)
local field = self:createField(tbl, index, key)
+ field.isIndex = true
if value.type == 'list' then
self:setValue(field, value[1], key)
else
@@ -216,6 +226,13 @@ function mt:buildTable(source)
local field = self:createField(tbl, n)
self:setValue(field, value)
end
+ -- 处理写了一半的 key = value,把name类的数组元素视为哈希键
+ if obj.type == 'name' then
+ local field = self.results.sources[obj]
+ if field then
+ field.isIndex = true
+ end
+ end
end
end
return tbl
@@ -1426,6 +1443,7 @@ local function compile(ast, lsp, uri)
calls = {},
sources= {},
strings= {},
+ indexs = {},
main = nil,
},
libraryValue = {},
diff --git a/server/test/completion/init.lua b/server/test/completion/init.lua
index face4216..9c2a878f 100644
--- a/server/test/completion/init.lua
+++ b/server/test/completion/init.lua
@@ -267,3 +267,33 @@ TEST [[
(EXISTS)
TEST 'local s = "a:@"' (nil)
+
+TEST [[
+local xxxx = {
+ xxyy = 1,
+ xxzz = 2,
+}
+
+local t = {
+ x@
+}
+]]
+{
+ {
+ label = 'xxxx',
+ kind = CompletionItemKind.Variable,
+ },
+ {
+ label = 'xxyy',
+ kind = CompletionItemKind.Property,
+ },
+ {
+ label = 'xxzz',
+ kind = CompletionItemKind.Property,
+ },
+ {
+ label = 'xpcall',
+ kind = CompletionItemKind.Function,
+ documentation = EXISTS,
+ }
+}
diff --git a/server/test/crossfile/completion.lua b/server/test/crossfile/completion.lua
index 7d4513ba..e35874b3 100644
--- a/server/test/crossfile/completion.lua
+++ b/server/test/crossfile/completion.lua
@@ -117,15 +117,15 @@ TEST {
completion = {
{
label = 'abc',
- kind = CompletionItemKind.Module,
+ kind = CompletionItemKind.File,
},
{
label = 'abc.aaa',
- kind = CompletionItemKind.Module,
+ kind = CompletionItemKind.File,
},
{
label = 'abcde',
- kind = CompletionItemKind.Module,
+ kind = CompletionItemKind.File,
},
}
}
@@ -143,7 +143,7 @@ TEST {
completion = {
{
label = 'abc',
- kind = CompletionItemKind.Module,
+ kind = CompletionItemKind.File,
},
}
}
@@ -165,7 +165,7 @@ TEST {
completion = {
{
label = 'abc.init',
- kind = CompletionItemKind.Module,
+ kind = CompletionItemKind.File,
},
}
}
@@ -187,7 +187,7 @@ TEST {
completion = {
{
label = 'abc.init',
- kind = CompletionItemKind.Module,
+ kind = CompletionItemKind.File,
},
}
}
@@ -209,11 +209,11 @@ TEST {
completion = {
{
label = 'x111',
- kind = CompletionItemKind.Module,
+ kind = CompletionItemKind.File,
},
{
label = 'x000',
- kind = CompletionItemKind.Module,
+ kind = CompletionItemKind.File,
},
}
}