diff options
author | 最萌小汐 <sumneko@hotmail.com> | 2022-03-02 16:34:55 +0800 |
---|---|---|
committer | 最萌小汐 <sumneko@hotmail.com> | 2022-03-02 16:34:55 +0800 |
commit | 25d3e051095ff1f1b7da58e591b2455a25d19328 (patch) | |
tree | 262b756f7d045fa1c080e23d3234f94a9f2a9acb /script/core/completion | |
parent | 6aa49faddea1c572140b84a72a8bfa850d9143fd (diff) | |
download | lua-language-server-25d3e051095ff1f1b7da58e591b2455a25d19328.zip |
improve completion
Diffstat (limited to 'script/core/completion')
-rw-r--r-- | script/core/completion/completion.lua | 5 | ||||
-rw-r--r-- | script/core/completion/keyword.lua | 329 |
2 files changed, 331 insertions, 3 deletions
diff --git a/script/core/completion/completion.lua b/script/core/completion/completion.lua index 2c3a354d..969de95a 100644 --- a/script/core/completion/completion.lua +++ b/script/core/completion/completion.lua @@ -11,7 +11,7 @@ local config = require 'config' local util = require 'utility' local markdown = require 'provider.markdown' local parser = require 'parser' -local keyWordMap = require 'core.keyword' +local keyWordMap = require 'core.completion.keyword' local workspace = require 'workspace' local furi = require 'file-uri' local rpath = require 'workspace.require-path' @@ -723,7 +723,7 @@ local function checkKeyWord(state, start, position, word, hasSpace, afterLocal, local text = state.lua local snipType = config.get(state.uri, 'Lua.completion.keywordSnippet') local symbol = lookBackward.findSymbol(text, guide.positionToOffset(state, start)) - local isExp = symbol == '(' or symbol == ',' or symbol == '=' + local isExp = symbol == '(' or symbol == ',' or symbol == '=' or symbol == '[' local info = { hasSpace = hasSpace, isExp = isExp, @@ -1473,7 +1473,6 @@ local function checkTableLiteralField(state, position, tbl, fields, results) results[#results+1] = { label = guide.getKeyName(field), kind = define.CompletionItemKind.Property, - insertText = ('%s = $0'):format(guide.getKeyName(field)), id = stack(function () ---@async return { detail = buildDetail(field), diff --git a/script/core/completion/keyword.lua b/script/core/completion/keyword.lua new file mode 100644 index 00000000..5558106a --- /dev/null +++ b/script/core/completion/keyword.lua @@ -0,0 +1,329 @@ +local define = require 'proto.define' +local files = require 'files' +local guide = require 'parser.guide' + +local keyWordMap = { + { 'do', function(info, results) + if info.hasSpace then + results[#results+1] = { + label = 'do .. end', + kind = define.CompletionItemKind.Snippet, + insertTextFormat = 2, + insertText = [[$0 end]], + } + else + results[#results+1] = { + label = 'do .. end', + kind = define.CompletionItemKind.Snippet, + insertTextFormat = 2, + insertText = "\z +do\ +\t$0\ +end", + } + end + return true + end, function(info) + return guide.eachSourceContain(info.state.ast, info.start, function(source) + if source.type == 'while' + or source.type == 'in' + or source.type == 'loop' then + if source.finish - info.start <= 2 then + return true + end + end + end) + end }, + { 'and' }, + { 'break' }, + { 'else' }, + { 'elseif', function(info, results) + local offset = guide.positionToOffset(info.state, info.position) + if info.text:find('^%s*then', offset + 1) + or info.text:find('^%s*do', offset + 1) then + return false + end + if info.hasSpace then + results[#results+1] = { + label = 'elseif .. then', + kind = define.CompletionItemKind.Snippet, + insertTextFormat = 2, + insertText = [[$1 then]], + } + else + results[#results+1] = { + label = 'elseif .. then', + kind = define.CompletionItemKind.Snippet, + insertTextFormat = 2, + insertText = [[elseif $1 then]], + } + end + return true + end }, + { 'end' }, + { 'false' }, + { 'for', function(info, results) + if info.hasSpace then + results[#results+1] = { + label = 'for .. ipairs', + kind = define.CompletionItemKind.Snippet, + insertTextFormat = 2, + insertText = "\z +${1:index}, ${2:value} in ipairs(${3:t}) do\ +\t$0\ +end" + } + results[#results+1] = { + label = 'for .. pairs', + kind = define.CompletionItemKind.Snippet, + insertTextFormat = 2, + insertText = "\z +${1:key}, ${2:value} in pairs(${3:t}) do\ +\t$0\ +end" + } + results[#results+1] = { + label = 'for i = ..', + kind = define.CompletionItemKind.Snippet, + insertTextFormat = 2, + insertText = "\z +${1:i} = ${2:1}, ${3:10, 1} do\ +\t$0\ +end" + } + else + results[#results+1] = { + label = 'for .. ipairs', + kind = define.CompletionItemKind.Snippet, + insertTextFormat = 2, + insertText = "\z +for ${1:index}, ${2:value} in ipairs(${3:t}) do\ +\t$0\ +end" + } + results[#results+1] = { + label = 'for .. pairs', + kind = define.CompletionItemKind.Snippet, + insertTextFormat = 2, + insertText = "\z +for ${1:key}, ${2:value} in pairs(${3:t}) do\ +\t$0\ +end" + } + results[#results+1] = { + label = 'for i = ..', + kind = define.CompletionItemKind.Snippet, + insertTextFormat = 2, + insertText = "\z +for ${1:i} = ${2:1}, ${3:10, 1} do\ +\t$0\ +end" + } + end + return true + end }, + { 'function', function(info, results) + if info.hasSpace then + results[#results+1] = { + label = 'function ()', + kind = define.CompletionItemKind.Snippet, + insertTextFormat = 2, + insertText = info.isExp and "\z +($1)\ +\t$0\ +end" or "\z +$1($2)\ +\t$0\ +end" + } + else + results[#results+1] = { + label = 'function ()', + kind = define.CompletionItemKind.Snippet, + insertTextFormat = 2, + insertText = info.isExp and "\z +function ($1)\ +\t$0\ +end" or "\z +function $1($2)\ +\t$0\ +end" + } + end + return true + end }, + { 'goto' }, + { 'if', function(info, results) + local offset = guide.positionToOffset(info.state, info.position) + if info.text:find('^%s*then', offset + 1) + or info.text:find('^%s*do', offset + 1) then + return false + end + if info.hasSpace then + results[#results+1] = { + label = 'if .. then', + kind = define.CompletionItemKind.Snippet, + insertTextFormat = 2, + insertText = "\z +$1 then\ +\t$0\ +end" + } + else + results[#results+1] = { + label = 'if .. then', + kind = define.CompletionItemKind.Snippet, + insertTextFormat = 2, + insertText = "\z +if $1 then\ +\t$0\ +end" + } + end + return true + end }, + { 'in', function(info, results) + local offset = guide.positionToOffset(info.state, info.position) + if info.text:find('^%s*then', offset + 1) + or info.text:find('^%s*do', offset + 1) then + return false + end + if info.hasSpace then + results[#results+1] = { + label = 'in ..', + kind = define.CompletionItemKind.Snippet, + insertTextFormat = 2, + insertText = "\z +${1:pairs(${2:t})} do\ +\t$0\ +end" + } + else + results[#results+1] = { + label = 'in ..', + kind = define.CompletionItemKind.Snippet, + insertTextFormat = 2, + insertText = "\z +in ${1:pairs(${2:t})} do\ +\t$0\ +end" + } + end + return true + end }, + { 'local', function(info, results) + if info.hasSpace then + results[#results+1] = { + label = 'local function', + kind = define.CompletionItemKind.Snippet, + insertTextFormat = 2, + insertText = "\z +function $1($2)\ +\t$0\ +end" + } + else + results[#results+1] = { + label = 'local function', + kind = define.CompletionItemKind.Snippet, + insertTextFormat = 2, + insertText = "\z +local function $1($2)\ +\t$0\ +end" + } + end + return false + end }, + { 'nil' }, + { 'not' }, + { 'or' }, + { 'repeat', function(info, results) + if info.hasSpace then + results[#results+1] = { + label = 'repeat .. until', + kind = define.CompletionItemKind.Snippet, + insertTextFormat = 2, + insertText = [[$0 until $1]] + } + else + results[#results+1] = { + label = 'repeat .. until', + kind = define.CompletionItemKind.Snippet, + insertTextFormat = 2, + insertText = "\z +repeat\ +\t$0\ +until $1" + } + end + return true + end }, + { 'return', function(info, results) + if not info.hasSpace then + results[#results+1] = { + label = 'do return end', + kind = define.CompletionItemKind.Snippet, + insertTextFormat = 2, + insertText = [[do return $1end]] + } + end + return false + end }, + { 'then', function(info, results) + local startOffset = guide.positionToOffset(info.state, info.start) + local pos, first = info.text:match('%S+%s+()(%S+)', startOffset + 1) + if first == 'end' + or first == 'else' + or first == 'elseif' then + local startRow = guide.rowColOf(info.start) + local finishPosition = guide.offsetToPosition(info.state, pos) + local finishRow = guide.rowColOf(finishPosition) + local startSp = info.text:match('^%s*', info.state.lines[startRow]) + local finishSp = info.text:match('^%s*', info.state.lines[finishRow]) + if startSp == finishSp then + return false + end + end + if not info.hasSpace then + results[#results+1] = { + label = 'then .. end', + kind = define.CompletionItemKind.Snippet, + insertTextFormat = 2, + insertText = '\z +then\ +\t$0\ +end' + } + end + return true + end }, + { 'true' }, + { 'until' }, + { 'while', function(info, results) + if info.hasSpace then + results[#results+1] = { + label = 'while .. do', + kind = define.CompletionItemKind.Snippet, + insertTextFormat = 2, + insertText = "\z +${1:true} do\ +\t$0\ +end" + } + else + results[#results+1] = { + label = 'while .. do', + kind = define.CompletionItemKind.Snippet, + insertTextFormat = 2, + insertText = "\z +while ${1:true} do\ +\t$0\ +end" + } + end + return true + end }, +} + +return keyWordMap |