diff options
author | 最萌小汐 <sumneko@hotmail.com> | 2020-09-17 20:00:19 +0800 |
---|---|---|
committer | 最萌小汐 <sumneko@hotmail.com> | 2020-09-17 20:00:19 +0800 |
commit | b6c59b00a307b7cb6396e96c65611a08ac3c3339 (patch) | |
tree | c9396a6689e72770f093a0a1f2183eb0be06af9b /script-beta | |
parent | 955e08617643e9961da0000e8bc270b0bd85047c (diff) | |
download | lua-language-server-b6c59b00a307b7cb6396e96c65611a08ac3c3339.zip |
支持自动变形为index
Diffstat (limited to 'script-beta')
-rw-r--r-- | script-beta/core/completion.lua | 47 | ||||
-rw-r--r-- | script-beta/proto/define.lua | 6 | ||||
-rw-r--r-- | script-beta/provider/provider.lua | 18 |
3 files changed, 62 insertions, 9 deletions
diff --git a/script-beta/core/completion.lua b/script-beta/core/completion.lua index 8bb13966..0dec6d3b 100644 --- a/script-beta/core/completion.lua +++ b/script-beta/core/completion.lua @@ -249,7 +249,7 @@ local function checkLocal(ast, word, offset, results) end end -local function checkFieldThen(ast, key, src, word, start, parent, oop, results) +local function checkFieldThen(ast, key, src, word, start, offset, parent, oop, results) local name = key:sub(3) if not matchKey(word, name) then return @@ -281,10 +281,39 @@ local function checkFieldThen(ast, key, src, word, start, parent, oop, results) if literal ~= nil then kind = ckind.Enum end + local textEdit, additionalTextEdits + if not name:match '^[%a_][%w_]*$' then + local nxt = parent.next + local dotPos + if nxt.type == 'setfield' + or nxt.type == 'getfield' + or nxt.type == 'tablefield' then + dotPos = nxt.dot.start + elseif nxt.type == 'getmethod' + or nxt.type == 'setmethod' then + dotPos = nxt.colon.start + end + textEdit = { + start = start + 1, + finish = offset, + newText = ('[%q]'):format(name), + } + if dotPos then + additionalTextEdits = { + { + start = dotPos, + finish = start, + newText = '', + } + } + end + end results[#results+1] = { - label = name, - kind = kind, - id = stack(function () + label = name, + kind = kind, + textEdit = textEdit, + additionalTextEdits = additionalTextEdits, + id = stack(function () return { detail = buildDetail(src), description = buildDesc(src), @@ -293,7 +322,7 @@ local function checkFieldThen(ast, key, src, word, start, parent, oop, results) } end -local function checkField(ast, word, start, parent, oop, results) +local function checkField(ast, word, start, offset, parent, oop, results) local fields = {} vm.eachField(parent, function (src) local key = vm.getKeyName(src) @@ -318,7 +347,7 @@ local function checkField(ast, word, start, parent, oop, results) end end) for key, src in util.sortPairs(fields) do - checkFieldThen(ast, key, src, word, start, parent, oop, results) + checkFieldThen(ast, key, src, word, start, offset, parent, oop, results) end end @@ -643,7 +672,7 @@ local function tryWord(ast, text, offset, results) local parent, oop = findParent(ast, text, start - 1) if parent then if not hasSpace then - checkField(ast, word, start, parent, oop, results) + checkField(ast, word, start, offset, parent, oop, results) end elseif isFuncArg(ast, offset) then checkProvideLocal(ast, word, start, results) @@ -660,7 +689,7 @@ local function tryWord(ast, text, offset, results) checkLocal(ast, word, start, results) checkTableField(ast, word, start, results) local env = guide.getLocal(ast.ast, '_ENV', start) - checkField(ast, word, start, env, false, results) + checkField(ast, word, start, offset, env, false, results) end end end @@ -682,7 +711,7 @@ local function trySymbol(ast, text, offset, results) or symbol == ':' then local parent, oop = findParent(ast, text, start) if parent then - checkField(ast, '', start, parent, oop, results) + checkField(ast, '', start, offset, parent, oop, results) end end end diff --git a/script-beta/proto/define.lua b/script-beta/proto/define.lua index 0dd781c1..328cac90 100644 --- a/script-beta/proto/define.lua +++ b/script-beta/proto/define.lua @@ -151,4 +151,10 @@ m.MessageType = { Log = 4, } +m.FileChangeType = { + Created = 1, + Changed = 2, + Deleted = 3, +} + return m diff --git a/script-beta/provider/provider.lua b/script-beta/provider/provider.lua index 23860f82..d908e917 100644 --- a/script-beta/provider/provider.lua +++ b/script-beta/provider/provider.lua @@ -119,6 +119,10 @@ proto.on('workspace/didChangeConfiguration', function () end) proto.on('workspace/didChangeWatchedFiles', function (params) + for _, change in ipairs(params.changes) do + if change.type == define.FileChangeType.Created then + end + end end) proto.on('textDocument/didOpen', function (params) @@ -340,6 +344,20 @@ proto.on('textDocument/completion', function (params) ), newText = res.textEdit.newText, }, + additionalTextEdits = res.additionalTextEdits and (function () + local t = {} + for j, edit in ipairs(res.additionalTextEdits) do + t[j] = { + range = define.range( + lines, + text, + edit.start, + edit.finish + ) + } + end + return t + end)(), documentation = res.description and { value = res.description, kind = 'markdown', |