summaryrefslogtreecommitdiff
path: root/script/core/completion
diff options
context:
space:
mode:
author最萌小汐 <sumneko@hotmail.com>2021-11-23 21:38:48 +0800
committer最萌小汐 <sumneko@hotmail.com>2021-11-23 21:38:48 +0800
commit7c176eb6882abbe00b0a1285bbbb4ff2af897b13 (patch)
tree4d597c7227d2ca5f45863ffffec9ac238f54e04b /script/core/completion
parent7dc7385ba55bbab7be12f6de6263ddeb71257250 (diff)
downloadlua-language-server-7c176eb6882abbe00b0a1285bbbb4ff2af897b13.zip
cleanup
Diffstat (limited to 'script/core/completion')
-rw-r--r--script/core/completion/completion.lua50
-rw-r--r--script/core/completion/postfix.lua77
2 files changed, 107 insertions, 20 deletions
diff --git a/script/core/completion/completion.lua b/script/core/completion/completion.lua
index 4f666847..a766cccc 100644
--- a/script/core/completion/completion.lua
+++ b/script/core/completion/completion.lua
@@ -21,6 +21,7 @@ local guide = require 'parser.guide'
local infer = require 'core.infer'
local noder = require 'core.noder'
local await = require 'await'
+local postfix = require 'core.completion.postfix'
local DiagnosticModes = {
'disable-next-line',
@@ -2029,6 +2030,33 @@ local function clearCache()
end
---@async
+local function tryCompletions(state, position, triggerCharacter, results)
+ local text = state.lua
+ if not state then
+ local word = lookBackward.findWord(text, guide.positionToOffset(state, position))
+ if not word then
+ return
+ end
+ checkCommon(nil, word, position, results)
+ return
+ end
+ if getComment(state, position) then
+ tryluaDoc(state, position, results)
+ tryComment(state, position, results)
+ return
+ end
+ if postfix(state, position, results) then
+ return
+ end
+ trySpecial(state, position, results)
+ tryCallArg(state, position, results)
+ tryTable(state, position, results)
+ tryWord(state, position, triggerCharacter, results)
+ tryIndex(state, position, results)
+ trySymbol(state, position, results)
+end
+
+---@async
local function completion(uri, position, triggerCharacter)
tracy.ZoneBeginN 'completion cache'
local results = getCache(uri, position)
@@ -2039,29 +2067,11 @@ local function completion(uri, position, triggerCharacter)
await.delay()
tracy.ZoneBeginN 'completion #1'
local state = files.getState(uri)
- local text = files.getText(uri)
- local results = {}
+ results = {}
clearStack()
tracy.ZoneEnd()
tracy.ZoneBeginN 'completion #2'
- if state then
- if getComment(state, position) then
- tryluaDoc(state, position, results)
- tryComment(state, position, results)
- else
- trySpecial(state, position, results)
- tryCallArg(state, position, results)
- tryTable(state, position, results)
- tryWord(state, position, triggerCharacter, results)
- tryIndex(state, position, results)
- trySymbol(state, position, results)
- end
- else
- local word = lookBackward.findWord(text, guide.positionToOffset(state, position))
- if word then
- checkCommon(nil, word, position, results)
- end
- end
+ tryCompletions(state, position, triggerCharacter, results)
tracy.ZoneEnd()
if #results == 0 then
diff --git a/script/core/completion/postfix.lua b/script/core/completion/postfix.lua
new file mode 100644
index 00000000..8e21a86d
--- /dev/null
+++ b/script/core/completion/postfix.lua
@@ -0,0 +1,77 @@
+local guide = require 'parser.guide'
+local lookback = require 'core.look-backward'
+local matchKey = require 'core.matchkey'
+local define = require 'proto.define'
+
+local actions = {}
+
+local function register(key)
+ return function (data)
+ actions[#actions+1] = {
+ key = key,
+ data = data
+ }
+ end
+end
+
+register 'pcall' {
+ function (source, callback)
+ callback({
+ start = source.start,
+ finish = source.start,
+ newText = 'pcall(',
+ }, {
+ start = source.finish,
+ finish = source.finish,
+ newText = ')',
+ })
+ end
+}
+
+local accepts = {
+ ['getglobal'] = true,
+ ['getfield'] = true,
+ ['getindex'] = true,
+ ['getmethod'] = true,
+ ['call'] = true,
+}
+
+local function checkPostFix(state, word, wordPosition, position, results)
+ local source = guide.eachSourceContain(state.ast, wordPosition, function (source)
+ if accepts[source.type] then
+ return source
+ end
+ end)
+ for _, action in ipairs(actions) do
+ if matchKey(word, action.key) then
+ action.data[1](source, function (...)
+ results[#results+1] = {
+ label = action.key,
+ kind = define.CompletionItemKind.Event,
+ textEdit = {
+ start = wordPosition,
+ finish = position,
+ newText = '',
+ },
+ additionalTextEdits = { ... }
+ }
+ end)
+ end
+ end
+end
+
+return function (state, position, results)
+ local text = state.lua
+ local offset = guide.positionToOffset(state, position)
+ local word, newOffset = lookback.findWord(text, offset)
+ if newOffset then
+ offset = newOffset - 1
+ end
+ local symbol = text:sub(offset, offset)
+ if symbol == '@' then
+ local wordPosition = guide.offsetToPosition(state, offset - 1)
+ checkPostFix(state, word or '', wordPosition, position, results)
+ return true
+ end
+ return false
+end