Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Modifies read to handle large LS messages #52

Merged
merged 4 commits into from
Oct 11, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 15 additions & 4 deletions lua/typst-preview/servers/factory.lua
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,8 @@ end
---@param mode mode
---@param callback fun(server: Server)
function M.new(path, mode, callback)
local read_buffer = ''

spawn(path, mode, function(close, write, read, link)
---@type Server
local server = {
Expand All @@ -173,17 +175,26 @@ function M.new(path, mode, callback)

read(function(data)
vim.defer_fn(function()
while data:len() > 0 do
local s, _ = data:find '\n'
local event = assert(vim.json.decode(data:sub(1, s - 1)))
data = data:sub(s + 1, -1)
read_buffer = read_buffer .. data
local s, _ = read_buffer:find '\n'
while s ~= nil do
local event = assert(vim.json.decode(read_buffer:sub(1, s - 1)))

-- Make sure we keep the next message in the read buffer
read_buffer = read_buffer:sub(s + 1, -1)
s, _ = read_buffer:find '\n'

local listeners = server.listenerss[event.event]
if listeners ~= nil then
for _, listener in pairs(listeners) do
listener(event)
end
end
end

if read_buffer ~= '' then
utils.debug('Leaving for next read: '..read_buffer)
end
end, 0)
end)

Expand Down