summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
author最萌小汐 <sumneko@hotmail.com>2019-07-23 15:40:25 +0800
committer最萌小汐 <sumneko@hotmail.com>2019-07-23 15:40:25 +0800
commit7ca97acc2a6cdd1bb99cb3bc7942aa392b858b61 (patch)
treeb125a2cf1e8990f70c7260afcf4c6c2fb19fe1c0
parentbbe054ceeb542330732a510ca87dfe45c63b30f1 (diff)
downloadlua-language-server-7ca97acc2a6cdd1bb99cb3bc7942aa392b858b61.zip
根据缩进补上end
-rw-r--r--server/src/core/code_action.lua66
1 files changed, 66 insertions, 0 deletions
diff --git a/server/src/core/code_action.lua b/server/src/core/code_action.lua
index 9f3e0338..7291e3a7 100644
--- a/server/src/core/code_action.lua
+++ b/server/src/core/code_action.lua
@@ -246,6 +246,67 @@ local function solveSyntaxByFix(uri, err, lines, callback)
}
end
+local function findEndPosition(err, lines, row, endrow)
+ if endrow == row then
+ return {
+ newText = ' end',
+ range = {
+ start = {
+ line = row - 1,
+ character = 999999,
+ },
+ ['end'] = {
+ line = row - 1,
+ character = 999999,
+ }
+ }
+ }
+ else
+ local l = lines[row]
+ return {
+ newText = ('\t'):rep(l.tab) .. (' '):rep(l.sp) .. 'end\n',
+ range = {
+ start = {
+ line = endrow,
+ character = 0,
+ },
+ ['end'] = {
+ line = endrow,
+ character = 0,
+ }
+ }
+ }
+ end
+end
+
+local function solveSyntaxByAddEnd(uri, err, lines, callback)
+ local row = lines:rowcol(err.start)
+ local line = lines[row]
+ if not line then
+ return nil
+ end
+ local sp = line.sp + line.tab * 4
+ for i = row + 1, #lines do
+ local nl = lines[i]
+ local lsp = nl.sp + nl.tab * 4
+ if lsp <= sp then
+ callback {
+ title = lang.script['ACTION_ADD_END'],
+ kind = 'quickfix',
+ edit = {
+ changes = {
+ [uri] = {
+ findEndPosition(err, lines, row, i - 1)
+ }
+ }
+ }
+ }
+ return
+ end
+ end
+ return nil
+end
+
---@param lsp LSP
---@param uri uri
---@param data table
@@ -269,6 +330,9 @@ local function solveSyntax(lsp, uri, data, callback)
if err.type == 'ACTION_AFTER_BREAK' or err.type == 'ACTION_AFTER_RETURN' then
solveSyntaxByAddDoEnd(uri, data, callback)
end
+ if err.type == 'MISS_END' then
+ solveSyntaxByAddEnd(uri, err, lines, callback)
+ end
if err.fix then
solveSyntaxByFix(uri, err, lines, callback)
end
@@ -326,5 +390,7 @@ return function (lsp, uri, diagnostics, range)
end
end
+ log.debug(table.dump(results))
+
return results
end