diff options
author | 最萌小汐 <sumneko@hotmail.com> | 2021-11-02 17:59:54 +0800 |
---|---|---|
committer | 最萌小汐 <sumneko@hotmail.com> | 2021-11-02 17:59:54 +0800 |
commit | c16264eac3b25f6f2bce14b0504071787bd2bc54 (patch) | |
tree | 25a76c376b0bcb244e1281ff89afd5aa55600615 /script/core | |
parent | a1fc6ac2f84804d3dbbde2ce5df02907f496ce7f (diff) | |
download | lua-language-server-c16264eac3b25f6f2bce14b0504071787bd2bc54.zip |
#687 await-in-sync
Diffstat (limited to 'script/core')
-rw-r--r-- | script/core/code-action.lua | 40 | ||||
-rw-r--r-- | script/core/diagnostics/await-in-sync.lua | 26 |
2 files changed, 66 insertions, 0 deletions
diff --git a/script/core/code-action.lua b/script/core/code-action.lua index 8256107e..ad048c48 100644 --- a/script/core/code-action.lua +++ b/script/core/code-action.lua @@ -308,6 +308,44 @@ local function solveTrailingSpace(uri, diag, results) } end +local function solveAwaitInSync(uri, diag, results) + local state = files.getState(uri) + if not state then + return + end + local start, finish = converter.unpackRange(uri, diag.range) + local parentFunction + guide.eachSourceType(state.ast, 'function', function (source) + if source.start > finish + or source.finish < start then + return + end + if not parentFunction or parentFunction.start < source.start then + parentFunction = source + end + end) + if not parentFunction then + return + end + local row = guide.rowColOf(parentFunction.start) + local pos = guide.positionOf(row, 0) + results[#results+1] = { + title = lang.script.ACTION_MARK_ASYNC, + kind = 'quickfix', + edit = { + changes = { + [uri] = { + { + start = pos, + finish = pos, + newText = '---@async\n', + } + } + } + }, + } +end + local function solveDiagnostic(uri, diag, start, results) if diag.source == lang.script.DIAG_SYNTAX_CHECK then solveSyntax(uri, diag, results) @@ -326,6 +364,8 @@ local function solveDiagnostic(uri, diag, start, results) solveAmbiguity1(uri, diag, results) elseif diag.code == 'trailing-space' then solveTrailingSpace(uri, diag, results) + elseif diag.code == 'await-in-sync' then + solveAwaitInSync(uri, diag, results) end disableDiagnostic(uri, diag.code, start, results) end diff --git a/script/core/diagnostics/await-in-sync.lua b/script/core/diagnostics/await-in-sync.lua new file mode 100644 index 00000000..5fb6467d --- /dev/null +++ b/script/core/diagnostics/await-in-sync.lua @@ -0,0 +1,26 @@ +local files = require 'files' +local guide = require 'parser.guide' +local vm = require 'vm' +local lang = require 'language' + +return function (uri, callback) + local state = files.getState(uri) + if not state then + return + end + + guide.eachSourceType(state.ast, 'call', function (source) + local currentFunc = guide.getParentFunction(source) + if currentFunc and vm.isAsync(currentFunc) then + return + end + if not vm.isAsync(source.node, true) then + return + end + callback { + start = source.node.start, + finish = source.node.finish, + message = lang.script('DIAG_AWAIT_IN_SYNC'), + } + end) +end |