summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
author最萌小汐 <sumneko@hotmail.com>2020-12-11 18:40:05 +0800
committer最萌小汐 <sumneko@hotmail.com>2020-12-11 18:40:05 +0800
commit87673cfad9e4fc32a2023c24a3d7d2248e8d2916 (patch)
tree486538d26e52b9f90b221bb8ffd9f17ee72b3ba3
parentb8c5d1208f9f276bd168c9a17a2b17105af09107 (diff)
downloadlua-language-server-87673cfad9e4fc32a2023c24a3d7d2248e8d2916.zip
completion: keyword considers expression
-rw-r--r--changelog.md11
-rw-r--r--script/core/completion.lua75
-rw-r--r--script/core/keyword.lua30
-rw-r--r--test/completion/init.lua36
4 files changed, 105 insertions, 47 deletions
diff --git a/changelog.md b/changelog.md
index 787cd755..737f5dbc 100644
--- a/changelog.md
+++ b/changelog.md
@@ -1,12 +1,13 @@
# changelog
## 1.6.0
-* `NEW` auto require local modules
-* `NEW` hover function by keyword `function`
+* `NEW` completion: auto require local modules
+* `NEW` hover: show function by keyword `function`
* `NEW` code action: swap params
-* `CHG` unbind the relative path between binaries and scripts
-* `CHG` `LuaDoc` also catchs `--` (no need `---`)
-* `CHG` rename doc
+* `CHG` standalone: unbind the relative path between binaries and scripts
+* `CHG` hover: `LuaDoc` also catchs `--` (no need `---`)
+* `CHG` rename: support doc
+* `CHG` completion: keyword considers expression
## 1.5.0
`2020-12-5`
diff --git a/script/core/completion.lua b/script/core/completion.lua
index 924a9691..931e97f5 100644
--- a/script/core/completion.lua
+++ b/script/core/completion.lua
@@ -35,6 +35,7 @@ end
local function resolveStack(id)
local callback = stacks[id]
if not callback then
+ log.warn('Unknown resolved id', id)
return nil
end
@@ -87,7 +88,9 @@ local function findSymbol(text, offset)
end
if char == '.'
or char == ':'
- or char == '(' then
+ or char == '('
+ or char == ','
+ or char == '=' then
return char, i
else
return nil
@@ -286,7 +289,7 @@ local function buildInsertRequire(ast, targetUri, stemName)
end
local path = furi.decode(targetUri)
local visiblePaths = rpath.getVisiblePath(path, config.config.runtime.path, true)
- if #visiblePaths == 0 then
+ if not visiblePaths or #visiblePaths == 0 then
return nil
end
table.sort(visiblePaths, function (a, b)
@@ -619,6 +622,8 @@ end
local function checkKeyWord(ast, text, start, word, hasSpace, afterLocal, results)
local snipType = config.config.completion.keywordSnippet
+ local symbol = findSymbol(text, start - 1)
+ local isExp = symbol == '(' or symbol == ',' or symbol == '='
for _, data in ipairs(keyWordMap) do
local key = data[1]
local eq
@@ -630,40 +635,50 @@ local function checkKeyWord(ast, text, start, word, hasSpace, afterLocal, result
if afterLocal and key ~= 'function' then
eq = false
end
- if eq then
- local replaced
- local extra
- if snipType == 'Both' or snipType == 'Replace' then
- local func = data[2]
- if func then
- replaced = func(hasSpace, results)
- extra = true
- end
+ if not eq then
+ goto CONTINUE
+ end
+ if isExp then
+ if key ~= 'nil'
+ and key ~= 'true'
+ and key ~= 'false'
+ and key ~= 'function' then
+ goto CONTINUE
end
- if snipType == 'Both' then
- replaced = false
+ end
+ local replaced
+ local extra
+ if snipType == 'Both' or snipType == 'Replace' then
+ local func = data[2]
+ if func then
+ replaced = func(hasSpace, isExp, results)
+ extra = true
end
- if not replaced then
- if not hasSpace then
- local item = {
- label = key,
- kind = define.CompletionItemKind.Keyword,
- }
- if extra then
- table.insert(results, #results, item)
- else
- results[#results+1] = item
- end
+ end
+ if snipType == 'Both' then
+ replaced = false
+ end
+ if not replaced then
+ if not hasSpace then
+ local item = {
+ label = key,
+ kind = define.CompletionItemKind.Keyword,
+ }
+ if extra then
+ table.insert(results, #results, item)
+ else
+ results[#results+1] = item
end
end
- local checkStop = data[3]
- if checkStop then
- local stop = checkStop(ast, start)
- if stop then
- return true
- end
+ end
+ local checkStop = data[3]
+ if checkStop then
+ local stop = checkStop(ast, start)
+ if stop then
+ return true
end
end
+ ::CONTINUE::
end
end
diff --git a/script/core/keyword.lua b/script/core/keyword.lua
index 1cbeb78d..cace541b 100644
--- a/script/core/keyword.lua
+++ b/script/core/keyword.lua
@@ -2,7 +2,7 @@ local define = require 'proto.define'
local guide = require 'parser.guide'
local keyWordMap = {
- {'do', function (hasSpace, results)
+ {'do', function (hasSpace, isExp, results)
if hasSpace then
results[#results+1] = {
label = 'do .. end',
@@ -38,7 +38,7 @@ end]],
{'and'},
{'break'},
{'else'},
- {'elseif', function (hasSpace, results)
+ {'elseif', function (hasSpace, isExp, results)
if hasSpace then
results[#results+1] = {
label = 'elseif .. then',
@@ -58,7 +58,7 @@ end]],
end},
{'end'},
{'false'},
- {'for', function (hasSpace, results)
+ {'for', function (hasSpace, isExp, results)
if hasSpace then
results[#results+1] = {
label = 'for .. in',
@@ -100,13 +100,16 @@ end]]
end
return true
end},
- {'function', function (hasSpace, results)
+ {'function', function (hasSpace, isExp, results)
if hasSpace then
results[#results+1] = {
label = 'function ()',
kind = define.CompletionItemKind.Snippet,
insertTextFormat = 2,
- insertText = [[
+ insertText = isExp and [[
+($1)
+ $0
+end]] or [[
$1($2)
$0
end]]
@@ -116,7 +119,10 @@ end]]
label = 'function ()',
kind = define.CompletionItemKind.Snippet,
insertTextFormat = 2,
- insertText = [[
+ insertText = isExp and [[
+function ($1)
+ $0
+end]] or [[
function $1($2)
$0
end]]
@@ -125,7 +131,7 @@ end]]
return true
end},
{'goto'},
- {'if', function (hasSpace, results)
+ {'if', function (hasSpace, isExp, results)
if hasSpace then
results[#results+1] = {
label = 'if .. then',
@@ -149,7 +155,7 @@ end]]
end
return true
end},
- {'in', function (hasSpace, results)
+ {'in', function (hasSpace, isExp, results)
if hasSpace then
results[#results+1] = {
label = 'in ..',
@@ -173,7 +179,7 @@ end]]
end
return true
end},
- {'local', function (hasSpace, results)
+ {'local', function (hasSpace, isExp, results)
if hasSpace then
results[#results+1] = {
label = 'local function',
@@ -200,7 +206,7 @@ end]]
{'nil'},
{'not'},
{'or'},
- {'repeat', function (hasSpace, results)
+ {'repeat', function (hasSpace, isExp, results)
if hasSpace then
results[#results+1] = {
label = 'repeat .. until',
@@ -221,7 +227,7 @@ until $1]]
end
return true
end},
- {'return', function (hasSpace, results)
+ {'return', function (hasSpace, isExp, results)
if not hasSpace then
results[#results+1] = {
label = 'do return end',
@@ -235,7 +241,7 @@ until $1]]
{'then'},
{'true'},
{'until'},
- {'while', function (hasSpace, results)
+ {'while', function (hasSpace, isExp, results)
if hasSpace then
results[#results+1] = {
label = 'while .. do',
diff --git a/test/completion/init.lua b/test/completion/init.lua
index 7a797df9..edea2978 100644
--- a/test/completion/init.lua
+++ b/test/completion/init.lua
@@ -1059,6 +1059,42 @@ mt:f$
insertText = 'f(${1:a: any}, ${2:b: any}, ${3:c: any})',
},
}
+
+TEST [[
+function$
+]]
+{
+ {
+ label = 'function',
+ kind = define.CompletionItemKind.Keyword,
+ },
+ {
+ label = 'function ()',
+ kind = define.CompletionItemKind.Snippet,
+ insertText = [[
+function $1($2)
+ $0
+end]],
+ },
+}
+
+TEST [[
+local t = function$
+]]
+{
+ {
+ label = 'function',
+ kind = define.CompletionItemKind.Keyword,
+ },
+ {
+ label = 'function ()',
+ kind = define.CompletionItemKind.Snippet,
+ insertText = [[
+function ($1)
+ $0
+end]],
+ },
+}
Cared['insertText'] = false
TEST [[