diff options
author | 最萌小汐 <sumneko@hotmail.com> | 2019-04-05 21:01:20 +0800 |
---|---|---|
committer | 最萌小汐 <sumneko@hotmail.com> | 2019-04-05 21:01:20 +0800 |
commit | 0408b1a9e2b0281b8afbe931e8bc934ee6e0c775 (patch) | |
tree | 908e0cc7b090bd18521128539a85ab779e6ba052 | |
parent | 243ffa67e5b3b094365c40b04a1a505a9e73da57 (diff) | |
download | lua-language-server-0408b1a9e2b0281b8afbe931e8bc934ee6e0c775.zip |
自动完成支持 *toclose
-rw-r--r-- | server/src/core/completion.lua | 36 | ||||
-rw-r--r-- | server/src/method/initialize.lua | 2 | ||||
-rw-r--r-- | server/src/method/textDocument/completion.lua | 4 | ||||
-rw-r--r-- | server/src/parser/ast.lua | 41 | ||||
-rw-r--r-- | server/src/parser/grammar.lua | 3 | ||||
-rw-r--r-- | server/test/completion/init.lua | 25 | ||||
-rw-r--r-- | server/test/crossfile/completion.lua | 2 | ||||
-rw-r--r-- | server/test/diagnostics/init.lua | 1 |
8 files changed, 86 insertions, 28 deletions
diff --git a/server/src/core/completion.lua b/server/src/core/completion.lua index a9652d8c..7da54412 100644 --- a/server/src/core/completion.lua +++ b/server/src/core/completion.lua @@ -1,6 +1,7 @@ local findSource = require 'core.find_source' local getFunctionHover = require 'core.hover.function' local getFunctionHoverAsLib = require 'core.hover.lib_function' +local config = require 'config' local CompletionItemKind = { Text = 1, @@ -30,7 +31,7 @@ local CompletionItemKind = { TypeParameter = 25, } -local KEYS = {'and', 'break', 'do', 'else', 'elseif', 'end', 'false', 'for', 'function', 'goto', 'if', 'in', 'local', 'nil', 'not', 'or', 'repeat', 'return', 'then', 'true', 'until', 'while', 'toclose'} +local KEYS = {'and', 'break', 'do', 'else', 'elseif', 'end', 'false', 'for', 'function', 'goto', 'if', 'in', 'local', 'nil', 'not', 'or', 'repeat', 'return', 'then', 'true', 'until', 'while'} local KEYMAP = {} for _, k in ipairs(KEYS) do KEYMAP[k] = true @@ -333,11 +334,14 @@ end local function searchAsLocal(vm, source, word, callback) searchCloseGlobal(vm, source, word, callback) - -- 特殊支持 local function if matchKey(word, 'function') then callback('function', nil, CompletionItemKind.Keyword) end + -- 特殊支持 local *toclose + if word == '' and config.config.runtime.version == 'Lua 5.4' then + callback('*toclose', nil, CompletionItemKind.Keyword) + end end local function searchAsArg(vm, source, word, callback) @@ -585,7 +589,30 @@ local function makeList(source, pos, word) end, list end -return function (vm, pos, word, oldText) +local function searchToclose(text, word, callback, pos) + if #word > 0 then + pos = pos - 1 + end + if text:sub(pos, pos) ~= '*' then + return false + end + if not matchKey(word, 'toclose') then + return false + end + for i = pos-1, 1, -1 do + if text:sub(i, i):match '[^%s%c]' then + if text:sub(i - #'local' + 1, i) == 'local' then + callback('toclose', nil, CompletionItemKind.Keyword) + return true + else + return false + end + end + end + return false +end + +return function (vm, text, pos, word, oldText) local source = findSource(vm, pos) or findSource(vm, pos-1) if not source then return nil @@ -597,6 +624,9 @@ return function (vm, pos, word, oldText) end end local callback, list = makeList(source, pos, word) + if searchToclose(text, word, callback, pos) then + return list + end searchSpecial(vm, source, word, callback, pos) searchCallArg(vm, source, word, callback, pos) searchSource(vm, source, word, callback) diff --git a/server/src/method/initialize.lua b/server/src/method/initialize.lua index abce7b94..e7c48545 100644 --- a/server/src/method/initialize.lua +++ b/server/src/method/initialize.lua @@ -1,5 +1,5 @@ local function allWords() - local str = [[abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789.:('"[,# ]] + local str = [[abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789.:('"[,#* ]] local list = {} for c in str:gmatch '.' do list[#list+1] = c diff --git a/server/src/method/textDocument/completion.lua b/server/src/method/textDocument/completion.lua index 8419036a..ed1d1c35 100644 --- a/server/src/method/textDocument/completion.lua +++ b/server/src/method/textDocument/completion.lua @@ -77,14 +77,14 @@ return function (lsp, params) end startPos = startPos or position - local items = core.completion(vm, startPos, word, oldText) + local items = core.completion(vm, text, startPos, word, oldText) if not items or #items == 0 then vm = lsp:loadVM(uri) if not vm then return nil end startPos = startPos or position - items = core.completion(vm, startPos, word) + items = core.completion(vm, text, startPos, word) if not items or #items == 0 then return nil end diff --git a/server/src/parser/ast.lua b/server/src/parser/ast.lua index 57d88b0c..a82e8d6e 100644 --- a/server/src/parser/ast.lua +++ b/server/src/parser/ast.lua @@ -327,7 +327,6 @@ local Defs = { max = '7FFFFFFF', } } - return '' end else if v < 0 or v > 0x10FFFF then @@ -341,8 +340,9 @@ local Defs = { max = '10FFFF', } } - return '' end + end + if v >= 0 and v <= 0x10FFFF then return utf8_char(v) end return '' @@ -736,10 +736,29 @@ local Defs = { keys, values, } end, - Local = function (keys, values) + ToClose = function (start) + if State.Version == 'Lua 5.4' then + return { + type = 'toclose', + start = start, + finish = start + #'*toclose' - 1, + } + end + pushError { + type = 'UNSUPPORT_SYMBOL', + start = start, + finish = start + #'*toclose' - 1, + version = 'Lua 5.4', + info = { + version = State.Version, + } + } + return nil + end, + Local = function (toclose, keys, values) return { type = 'local', - keys, values, + keys, values, toclose } end, DoBody = function (...) @@ -1238,20 +1257,6 @@ local Defs = { } return rtn, action end, - ToClose = function (start) - if State.Version == 'Lua 5.4' then - return - end - pushError { - type = 'UNSUPPORT_SYMBOL', - start = start, - finish = start + #'*toclose' - 1, - version = 'Lua 5.4', - info = { - version = State.Version, - } - } - end } return function (self, lua, mode, version) diff --git a/server/src/parser/grammar.lua b/server/src/parser/grammar.lua index a1be48db..e35b11a2 100644 --- a/server/src/parser/grammar.lua +++ b/server/src/parser/grammar.lua @@ -473,7 +473,8 @@ RepeatBody <- REPEAT BreakEnd NeedUntil DirtyExp -Local <- (LOCAL TOCLOSE? NameList (ASSIGN ExpList)?) +ToClose <- TOCLOSE / %nil +Local <- (LOCAL ToClose NameList (ASSIGN ExpList)?) -> Local Set <- (SimpleList ASSIGN ExpList?) -> Set diff --git a/server/test/completion/init.lua b/server/test/completion/init.lua index d17b4a83..ed68dec5 100644 --- a/server/test/completion/init.lua +++ b/server/test/completion/init.lua @@ -103,12 +103,12 @@ function TEST(script) return function (expect) local pos = script:find('@', 1, true) - 1 local new_script = script:gsub('@', ' ') - local ast = parser:ast(new_script, 'lua', 'Lua 5.3') + local ast = parser:ast(new_script, 'lua', 'Lua 5.4') local vm = buildVM(ast) assert(vm) local word = findWord(pos, new_script) local startPos = findStartPos(pos, new_script) - local result = core.completion(vm, startPos, word) + local result = core.completion(vm, new_script, startPos, word) if expect then assert(result) assert(eq(expect, result)) @@ -734,3 +734,24 @@ do end ]] (nil) + +require 'config' .config.runtime.version = 'Lua 5.4' +TEST [[ +local *@ +]] +{ + { + label = 'toclose', + kind = CompletionItemKind.Keyword, + } +} + +TEST [[ +local *tocl@ +]] +{ + { + label = 'toclose', + kind = CompletionItemKind.Keyword, + } +} diff --git a/server/test/crossfile/completion.lua b/server/test/crossfile/completion.lua index 5cce68ef..e89d013f 100644 --- a/server/test/crossfile/completion.lua +++ b/server/test/crossfile/completion.lua @@ -116,7 +116,7 @@ function TEST(data) assert(vm) local word = findWord(pos, mainBuf) local startPos = findStartPos(pos, mainBuf) or pos - local result = core.completion(vm, startPos, word) + local result = core.completion(vm, mainBuf, startPos, word) local expect = data.completion if expect then assert(result) diff --git a/server/test/diagnostics/init.lua b/server/test/diagnostics/init.lua index 00ec11bf..01795658 100644 --- a/server/test/diagnostics/init.lua +++ b/server/test/diagnostics/init.lua @@ -247,6 +247,7 @@ local f = load('') f(1, 2, 3) ]] +require 'config' .config.runtime.version = 'Lua 5.3' TEST [[ <!warn!>(1) ]] |