summaryrefslogtreecommitdiff
path: root/server
diff options
context:
space:
mode:
author最萌小汐 <sumneko@hotmail.com>2019-11-13 21:32:39 +0800
committer最萌小汐 <sumneko@hotmail.com>2019-11-13 21:32:39 +0800
commite720a254720017de162a9fff4194bce307e08520 (patch)
tree3e6b88b445b4b52806aca9db629e996ebf7dec9d /server
parent1294d2c7919d24b698aed6fa3ad780fd9d0af4f3 (diff)
downloadlua-language-server-e720a254720017de162a9fff4194bce307e08520.zip
折行支持
Diffstat (limited to 'server')
-rw-r--r--server/src/core/folding_range.lua54
-rw-r--r--server/src/core/init.lua3
-rw-r--r--server/src/method/init.lua1
-rw-r--r--server/src/method/initialize.lua1
-rw-r--r--server/src/method/textDocument/documentSymbol.lua1
-rw-r--r--server/src/method/textDocument/foldingRange.lua55
-rw-r--r--server/src/vm/vm.lua1
7 files changed, 114 insertions, 2 deletions
diff --git a/server/src/core/folding_range.lua b/server/src/core/folding_range.lua
new file mode 100644
index 00000000..72e5b2ff
--- /dev/null
+++ b/server/src/core/folding_range.lua
@@ -0,0 +1,54 @@
+local foldingType = {
+ ['function'] = {'region', 'end', },
+ ['localfunction'] = {'region', 'end', },
+ ['do'] = {'region', 'end', },
+ ['if'] = {'region', 'end', },
+ ['loop'] = {'region', 'end', },
+ ['in'] = {'region', 'end', },
+ ['while'] = {'region', 'end', },
+ ['repeat'] = {'region', 'until',},
+ ['table'] = {'region', '}', },
+}
+
+return function (vm)
+ local result = {}
+ vm:eachSource(function (source)
+ local tp = source.type
+ local data = foldingType[tp]
+ if not data then
+ return
+ end
+ if data[1] == 'region' then
+ local start = source.start
+ local finish = source.finish
+ if data[2] == 'until' then
+ if #source > 0 then
+ finish = source[#source].finish
+ else
+ finish = start + #'repeat'
+ end
+ finish = vm.text:find('until', finish, true) or finish
+ end
+ result[#result+1] = {
+ start = start,
+ finish = finish,
+ kind = data[1],
+ }
+ if tp == 'if' then
+ for i = 2, #source do
+ local block = source[i]
+ local nblock = source[i+1]
+ result[#result+1] = {
+ start = block.start,
+ finish = nblock and nblock.start or finish,
+ kind = data[1],
+ }
+ end
+ end
+ end
+ end)
+ if #result == 0 then
+ return nil
+ end
+ return result
+end
diff --git a/server/src/core/init.lua b/server/src/core/init.lua
index 81982cef..213dbaca 100644
--- a/server/src/core/init.lua
+++ b/server/src/core/init.lua
@@ -12,7 +12,8 @@ local api = {
documentSymbol = require 'core.document_symbol',
global = require 'core.global',
highlight = require 'core.highlight',
- codeAction = require 'core.code_action'
+ codeAction = require 'core.code_action',
+ foldingRange = require 'core.folding_range',
}
return api
diff --git a/server/src/method/init.lua b/server/src/method/init.lua
index 7e81eb00..8827768b 100644
--- a/server/src/method/init.lua
+++ b/server/src/method/init.lua
@@ -16,6 +16,7 @@ init 'textDocument/didChange'
init 'textDocument/didClose'
init 'textDocument/documentHighlight'
init 'textDocument/documentSymbol'
+init 'textDocument/foldingRange'
init 'textDocument/hover'
init 'textDocument/implementation'
init 'textDocument/onTypeFormatting'
diff --git a/server/src/method/initialize.lua b/server/src/method/initialize.lua
index 8bc47fdd..02a96695 100644
--- a/server/src/method/initialize.lua
+++ b/server/src/method/initialize.lua
@@ -18,6 +18,7 @@ return function (lsp)
documentSymbolProvider = true,
documentHighlightProvider = true,
codeActionProvider = true,
+ foldingRangeProvider = true,
signatureHelpProvider = {
triggerCharacters = { '(', ',' },
},
diff --git a/server/src/method/textDocument/documentSymbol.lua b/server/src/method/textDocument/documentSymbol.lua
index 664041e6..a4b0c3b7 100644
--- a/server/src/method/textDocument/documentSymbol.lua
+++ b/server/src/method/textDocument/documentSymbol.lua
@@ -68,6 +68,5 @@ return function (lsp, params)
response(symbols)
end)
- timerCache[uri]:onTimer()
end
end
diff --git a/server/src/method/textDocument/foldingRange.lua b/server/src/method/textDocument/foldingRange.lua
new file mode 100644
index 00000000..0ca94d66
--- /dev/null
+++ b/server/src/method/textDocument/foldingRange.lua
@@ -0,0 +1,55 @@
+local core = require 'core'
+
+local timerCache = {}
+
+local function convertRange(lines, range)
+ local start_row, start_col = lines:rowcol(range.start)
+ local finish_row, finish_col = lines:rowcol(range.finish)
+ local result = {
+ startLine = start_row - 1,
+ endLine = finish_row - 2,
+ kind = range.kind,
+ }
+ return result
+end
+
+return function (lsp, params)
+ local uri = params.textDocument.uri
+ if timerCache[uri] then
+ timerCache[uri]:remove()
+ timerCache[uri] = nil
+ end
+
+ return function (response)
+ local clock = os.clock()
+ timerCache[uri] = ac.loop(0.1, function (t)
+ local vm, lines = lsp:getVM(uri)
+ if not vm then
+ if os.clock() - clock > 10 then
+ t:remove()
+ timerCache[uri] = nil
+ response(nil)
+ end
+ return
+ end
+
+ t:remove()
+ timerCache[uri] = nil
+
+ local ranges = core.foldingRange(vm)
+ if not ranges then
+ response(nil)
+ return
+ end
+
+ local results = {}
+ for _, range in ipairs(ranges) do
+ results[#results+1] = convertRange(lines, range)
+ end
+
+ log.debug(vm.text)
+ log.debug(table.dump(results))
+ response(results)
+ end)
+ end
+end
diff --git a/server/src/vm/vm.lua b/server/src/vm/vm.lua
index 9ec2bccd..36ad78c9 100644
--- a/server/src/vm/vm.lua
+++ b/server/src/vm/vm.lua
@@ -740,6 +740,7 @@ function mt:getMultiByList(list)
end
function mt:doDo(action)
+ self:instantSource(action)
self:scopePush(action)
self:doActions(action)
self:scopePop()