diff options
author | 最萌小汐 <sumneko@hotmail.com> | 2021-01-19 11:49:09 +0800 |
---|---|---|
committer | 最萌小汐 <sumneko@hotmail.com> | 2021-01-19 11:49:09 +0800 |
commit | 1458139721646236406825a03a21152d98753515 (patch) | |
tree | 99d931e0c103e5047d1a0eb8b502aa1670a04979 /script | |
parent | c7a0b295570e78a43f0819d9e0ba9a4c076b7e1a (diff) | |
download | lua-language-server-1458139721646236406825a03a21152d98753515.zip |
supports incremental text sync
Diffstat (limited to 'script')
-rw-r--r-- | script/proto/define.lua | 5 | ||||
-rw-r--r-- | script/provider/capability.lua | 4 | ||||
-rw-r--r-- | script/provider/provider.lua | 24 |
3 files changed, 23 insertions, 10 deletions
diff --git a/script/proto/define.lua b/script/proto/define.lua index f6f8e9cc..920571af 100644 --- a/script/proto/define.lua +++ b/script/proto/define.lua @@ -89,6 +89,11 @@ function m.range(lines, text, offset1, offset2) end --- convert `range` to `offsetStart` and `offsetFinish` +---@param lines table +---@param text string +---@param range table +---@return integer start +---@return integer finish function m.unrange(lines, text, range) local start = m.offset(lines, text, range.start) local finish = m.offset(lines, text, range['end']) diff --git a/script/provider/capability.lua b/script/provider/capability.lua index 0d24b120..9a8d4f9b 100644 --- a/script/provider/capability.lua +++ b/script/provider/capability.lua @@ -19,8 +19,8 @@ function m.getIniter() textDocumentSync = { -- 打开关闭文本时通知 openClose = true, - -- 文本改变时完全通知 TODO 支持差量更新(2) - change = 1, + -- 文本增量更新 + change = 2, }, hoverProvider = true, diff --git a/script/provider/provider.lua b/script/provider/provider.lua index fd8e113c..054e3ac3 100644 --- a/script/provider/provider.lua +++ b/script/provider/provider.lua @@ -182,15 +182,23 @@ proto.on('textDocument/didClose', function (params) end) proto.on('textDocument/didChange', function (params) - local doc = params.textDocument - local change = params.contentChanges - local uri = doc.uri - local text = change[1].text - if files.isLua(uri) or files.isOpen(uri) then - --log.debug('didChange:', uri) - files.setText(uri, text) - --log.debug('setText:', #text) + local doc = params.textDocument + local changes = params.contentChanges + local uri = doc.uri + if not files.isLua(uri) and not files.isOpen(uri) then + return end + local text = files.getText(uri) or '' + for _, change in ipairs(changes) do + if change.range then + local lines = files.getLines(uri) + local start, finish = define.unrange(lines, text, change.range) + text = text:sub(1, start) .. change.text .. text:sub(finish + 1) + else + text = change.text + end + end + files.setText(uri, text) end) proto.on('textDocument/hover', function (params) |