Skip to content

Commit 1458139

Browse files
committed
supports incremental text sync
1 parent c7a0b29 commit 1458139

File tree

3 files changed

+23
-10
lines changed

3 files changed

+23
-10
lines changed

script/proto/define.lua

+5
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,11 @@ function m.range(lines, text, offset1, offset2)
8989
end
9090

9191
--- convert `range` to `offsetStart` and `offsetFinish`
92+
---@param lines table
93+
---@param text string
94+
---@param range table
95+
---@return integer start
96+
---@return integer finish
9297
function m.unrange(lines, text, range)
9398
local start = m.offset(lines, text, range.start)
9499
local finish = m.offset(lines, text, range['end'])

script/provider/capability.lua

+2-2
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ function m.getIniter()
1919
textDocumentSync = {
2020
-- 打开关闭文本时通知
2121
openClose = true,
22-
-- 文本改变时完全通知 TODO 支持差量更新(2)
23-
change = 1,
22+
-- 文本增量更新
23+
change = 2,
2424
},
2525

2626
hoverProvider = true,

script/provider/provider.lua

+16-8
Original file line numberDiff line numberDiff line change
@@ -182,15 +182,23 @@ proto.on('textDocument/didClose', function (params)
182182
end)
183183

184184
proto.on('textDocument/didChange', function (params)
185-
local doc = params.textDocument
186-
local change = params.contentChanges
187-
local uri = doc.uri
188-
local text = change[1].text
189-
if files.isLua(uri) or files.isOpen(uri) then
190-
--log.debug('didChange:', uri)
191-
files.setText(uri, text)
192-
--log.debug('setText:', #text)
185+
local doc = params.textDocument
186+
local changes = params.contentChanges
187+
local uri = doc.uri
188+
if not files.isLua(uri) and not files.isOpen(uri) then
189+
return
193190
end
191+
local text = files.getText(uri) or ''
192+
for _, change in ipairs(changes) do
193+
if change.range then
194+
local lines = files.getLines(uri)
195+
local start, finish = define.unrange(lines, text, change.range)
196+
text = text:sub(1, start) .. change.text .. text:sub(finish + 1)
197+
else
198+
text = change.text
199+
end
200+
end
201+
files.setText(uri, text)
194202
end)
195203

196204
proto.on('textDocument/hover', function (params)

0 commit comments

Comments
 (0)