diff options
author | sumneko <sumneko@hotmail.com> | 2019-05-22 18:12:58 +0800 |
---|---|---|
committer | sumneko <sumneko@hotmail.com> | 2019-05-22 18:12:58 +0800 |
commit | 40b6da834d649828cea1a9f59b69657264a8c3c5 (patch) | |
tree | 950888727a0016047d812b21832302511f863502 /server | |
parent | a69fc879a80f05c7bf27e98d14462bb0a0cbde38 (diff) | |
download | lua-language-server-40b6da834d649828cea1a9f59b69657264a8c3c5.zip |
自动处理引号
Diffstat (limited to 'server')
-rw-r--r-- | server/src/core/completion.lua | 37 | ||||
-rw-r--r-- | server/src/parser/ast.lua | 5 | ||||
-rw-r--r-- | server/src/parser/grammar.lua | 4 | ||||
-rw-r--r-- | server/test/crossfile/completion.lua | 66 |
4 files changed, 99 insertions, 13 deletions
diff --git a/server/src/core/completion.lua b/server/src/core/completion.lua index ecb7a90c..8f481b4f 100644 --- a/server/src/core/completion.lua +++ b/server/src/core/completion.lua @@ -518,24 +518,43 @@ local function searchSource(vm, source, word, callback, pos) end end -local function buildTextEdit(start, finish, str) +local function buildTextEdit(start, finish, str, quo) + local text, lquo, rquo + if quo == '"' then + text = str:gsub('\r', '\\r'):gsub('\n', '\\n'):gsub('"', '\\"') + lquo = quo + rquo = quo + elseif quo == "'" then + text = str:gsub('\r', '\\r'):gsub('\n', '\\n'):gsub("'", "\\'") + lquo = quo + rquo = quo + else + lquo = quo + rquo = ']' .. lquo:sub(2, -2) .. ']' + while str:find(rquo, 1, true) do + lquo = '[=' .. quo:sub(2) + rquo = ']' .. lquo:sub(2, -2) .. ']' + end + text = str + end return { label = str, + filterText = str, textEdit = { - start = start + 1, - finish = finish - 1, - newText = ('%q'):format(str), + start = start + #quo, + finish = finish - #quo, + newText = text, }, additionalTextEdits = { { start = start, - finish = start, - newText = '', + finish = start + #quo - 1, + newText = lquo, }, { - start = finish, + start = finish - #quo + 1, finish = finish, - newText = '', + newText = rquo, }, } } @@ -556,7 +575,7 @@ local function searchInRequire(vm, select, source, callback) return end for _, str in ipairs(list) do - local data = buildTextEdit(source.start, source.finish, str) + local data = buildTextEdit(source.start, source.finish, str, source[2]) data.documentation = map[str] callback(str, nil, CompletionItemKind.Reference, data) end diff --git a/server/src/parser/ast.lua b/server/src/parser/ast.lua index cb3597fd..ac97d512 100644 --- a/server/src/parser/ast.lua +++ b/server/src/parser/ast.lua @@ -263,12 +263,13 @@ local Defs = { } end end, - String = function (start, str, finish) + String = function (start, quote, str, finish) return { type = 'string', start = start, finish = finish - 1, [1] = str, + [2] = quote, } end, LongString = function (beforeEq, afterEq, str, missPos) @@ -282,7 +283,7 @@ local Defs = { } } end - return str + return '[' .. ('='):rep(afterEq-beforeEq) .. '[', str end, Char10 = function (char) char = tonumber(char) diff --git a/server/src/parser/grammar.lua b/server/src/parser/grammar.lua index 455a71ca..54b03e24 100644 --- a/server/src/parser/grammar.lua +++ b/server/src/parser/grammar.lua @@ -242,10 +242,10 @@ Boolean <- Sp ({} -> True) TRUE grammar 'String' [[ String <- Sp ({} StringDef {}) -> String -StringDef <- '"' +StringDef <- {'"'} {~(Esc / !%nl !'"' .)*~} -> 1 ('"' / {} -> MissQuote1) - / "'" + / {"'"} {~(Esc / !%nl !"'" .)*~} -> 1 ("'" / {} -> MissQuote2) / ('[' {} {:eq: '='* :} {} '[' diff --git a/server/test/crossfile/completion.lua b/server/test/crossfile/completion.lua index 2b38f42a..adfd3cfd 100644 --- a/server/test/crossfile/completion.lua +++ b/server/test/crossfile/completion.lua @@ -123,6 +123,7 @@ TEST { completion = { { label = 'abc', + filterText = 'abc', kind = CompletionItemKind.Reference, documentation = 'abc.lua', textEdit = EXISTS, @@ -130,6 +131,7 @@ TEST { }, { label = 'abc.aaa', + filterText = 'abc.aaa', kind = CompletionItemKind.Reference, documentation = 'abc/aaa.lua', textEdit = EXISTS, @@ -137,6 +139,7 @@ TEST { }, { label = 'abcde', + filterText = 'abcde', kind = CompletionItemKind.Reference, documentation = 'xxx/abcde.lua', textEdit = EXISTS, @@ -158,6 +161,7 @@ TEST { completion = { { label = 'abc', + filterText = 'abc', kind = CompletionItemKind.Reference, documentation = 'abc.lua', textEdit = EXISTS, @@ -183,6 +187,7 @@ TEST { completion = { { label = 'ABCD', + filterText = 'ABCD', kind = CompletionItemKind.Reference, documentation = 'ABCD.lua', textEdit = EXISTS, @@ -190,6 +195,7 @@ TEST { }, { label = 'abc', + filterText = 'abc', kind = CompletionItemKind.Reference, documentation = 'abc.lua', textEdit = EXISTS, @@ -217,6 +223,7 @@ TEST { completion = { { label = 'abc', + filterText = 'abc', kind = CompletionItemKind.Reference, documentation = 'abc.lua', textEdit = EXISTS, @@ -244,6 +251,7 @@ TEST { completion = { { label = 'abc', + filterText = 'abc', kind = CompletionItemKind.Reference, documentation = 'abc.lua', textEdit = EXISTS, @@ -251,6 +259,7 @@ TEST { }, { label = 'abc.init', + filterText = 'abc.init', kind = CompletionItemKind.Reference, documentation = 'abc/init.lua', textEdit = EXISTS, @@ -276,6 +285,7 @@ TEST { completion = { { label = 'abc', + filterText = 'abc', kind = CompletionItemKind.Reference, documentation = 'abc/init.lua', textEdit = EXISTS, @@ -283,6 +293,7 @@ TEST { }, { label = 'abc.bbc', + filterText = 'abc.bbc', kind = CompletionItemKind.Reference, documentation = 'abc/bbc.lua', textEdit = EXISTS, @@ -290,6 +301,7 @@ TEST { }, { label = 'abc.init', + filterText = 'abc.init', kind = CompletionItemKind.Reference, documentation = 'abc/init.lua', textEdit = EXISTS, @@ -315,6 +327,7 @@ TEST { completion = { { label = 'abc.init', + filterText = 'abc.init', kind = CompletionItemKind.Reference, documentation = 'abc/init.lua', textEdit = EXISTS, @@ -340,6 +353,7 @@ TEST { completion = { { label = 'abc.init', + filterText = 'abc.init', kind = CompletionItemKind.Reference, documentation = 'abc/init.lua', textEdit = EXISTS, @@ -365,6 +379,7 @@ TEST { completion = { { label = 'core.core', + filterText = 'core.core', kind = CompletionItemKind.Reference, documentation = 'core/core.lua', textEdit = EXISTS, @@ -390,6 +405,7 @@ TEST { completion = { { label = 'x000', + filterText = 'x000', kind = CompletionItemKind.Reference, documentation = 'x000.lua', textEdit = EXISTS, @@ -397,6 +413,7 @@ TEST { }, { label = 'x111', + filterText = 'x111', kind = CompletionItemKind.Reference, documentation = 'abc/x111.lua', textEdit = EXISTS, @@ -546,6 +563,7 @@ TEST { completion = { { label = 'xxx', + filterText = 'xxx', kind = CompletionItemKind.Reference, documentation = 'xxx.lua', textEdit = EXISTS, @@ -553,3 +571,51 @@ TEST { }, } } + +TEST { + { + path = [[xx'xx.lua]], + content = '' + }, + { + path = 'main.lua', + content = [[ + require 'xx$' + ]], + main = true, + }, + completion = { + { + label = [[xx'xx]], + filterText = [[xx'xx]], + kind = CompletionItemKind.Reference, + documentation = [[xx'xx.lua]], + textEdit = EXISTS, + additionalTextEdits = EXISTS, + }, + } +} + +TEST { + { + path = [[xx]=]xx.lua]], + content = '' + }, + { + path = 'main.lua', + content = [[ + require [=[xx$]=]' + ]], + main = true, + }, + completion = { + { + label = [[xx]=]xx]], + filterText = [[xx]=]xx]], + kind = CompletionItemKind.Reference, + documentation = [[xx]=]xx.lua]], + textEdit = EXISTS, + additionalTextEdits = EXISTS, + }, + } +} |