summaryrefslogtreecommitdiff
path: root/script/method/textDocument/foldingRange.lua
diff options
context:
space:
mode:
Diffstat (limited to 'script/method/textDocument/foldingRange.lua')
-rw-r--r--script/method/textDocument/foldingRange.lua57
1 files changed, 57 insertions, 0 deletions
diff --git a/script/method/textDocument/foldingRange.lua b/script/method/textDocument/foldingRange.lua
new file mode 100644
index 00000000..0320b422
--- /dev/null
+++ b/script/method/textDocument/foldingRange.lua
@@ -0,0 +1,57 @@
+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,
+ }
+ if result.startLine >= result.endLine then
+ return nil
+ end
+ 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 comments = lsp:getComments(uri)
+ local ranges = core.foldingRange(vm, comments)
+ if not ranges then
+ response(nil)
+ return
+ end
+
+ local results = {}
+ for _, range in ipairs(ranges) do
+ results[#results+1] = convertRange(lines, range)
+ end
+
+ response(results)
+ end)
+ end
+end