diff options
-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 | ||||
-rw-r--r-- | test-beta/completion/init.lua | 55 |
4 files changed, 86 insertions, 40 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', diff --git a/test-beta/completion/init.lua b/test-beta/completion/init.lua index b0cfd4e2..cf79d795 100644 --- a/test-beta/completion/init.lua +++ b/test-beta/completion/init.lua @@ -34,9 +34,10 @@ end rawset(_G, 'TEST', true) local Cared = { - ['label'] = true, - ['kind'] = true, - ['textEdit'] = true, + ['label'] = true, + ['kind'] = true, + ['textEdit'] = true, + ['additionalTextEdits'] = true, } function TEST(script) @@ -58,7 +59,7 @@ function TEST(script) item[k] = v end for k in pairs(item) do - if not Cared[k] then + if not Cared[k] then item[k] = nil end end @@ -962,7 +963,6 @@ else$ } Cared['insertText'] = true - TEST [[ local xpcal xpcal$ @@ -1000,9 +1000,8 @@ mt:f$ insertText = 'f(${1:a: any}, ${2:b: any}, ${3:c: any})', }, } +Cared['insertText'] = false --- TODO -do return end TEST [[ local function f() if a then @@ -1011,39 +1010,32 @@ end ]] { { - label = 'select', - kind = CompletionItemKind.Function, - detail = EXISTS, + label = 'else', + kind = CompletionItemKind.Keyword, }, { - label = 'select()', + label = 'elseif', + kind = CompletionItemKind.Keyword, + }, + { + label = 'elseif .. then', kind = CompletionItemKind.Snippet, - detail = EXISTS, - insertText = EXISTS, }, { - label = 'setmetatable', + label = 'select', kind = CompletionItemKind.Function, - detail = EXISTS, }, { - label = 'setmetatable()', + label = 'select()', kind = CompletionItemKind.Snippet, - detail = EXISTS, - insertText = EXISTS, - }, - { - label = 'else', - kind = CompletionItemKind.Keyword, }, { - label = 'elseif', - kind = CompletionItemKind.Keyword, + label = 'setmetatable', + kind = CompletionItemKind.Function, }, { - label = 'elseif .. then', + label = 'setmetatable()', kind = CompletionItemKind.Snippet, - insertText = EXISTS, }, } @@ -1058,7 +1050,6 @@ t.$ { label = 'a.b.c', kind = CompletionItemKind.Field, - detail = EXISTS, textEdit = { start = 37, finish = 36, @@ -1066,14 +1057,16 @@ t.$ }, additionalTextEdits = { { - start = 36, - finish = 36, + start = 36, + finish = 36, newText = '', - } - } + }, + }, } } +-- TODO +do return end TEST [[ _ENV['z.b.c'] = {} |