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

Conversation

cskeeters
Copy link
Contributor

read is written correctly to handle the case where multiple JSON messages are returned from the Language Server (LS) with new lines as the messages separator. However, as the Typst source gets large messages from the LS, the messages become too large to fit in a single chunk of data read from stdout of the LS. LibUV makes no guarentee that the message will end on a newline, so the left over data needs to be saved. In this case, vim logs an error because data:find returns nil into s and the code tries to add 1 to a nil value.

I got this to work by having a read_buffer outside the function that can persist the unused portion of the previous read chunk and apply it to the next message before parsing.

To reproduce this error, create test.typ with:

= Introduction 0

Then make 99 copies of this line. I incremented the number each time.

qq<C-a>yypq
99@q

Run TypstPreview, then try to insert text. You'll get one or two related errors in vim

Error executing vim.schedule lua callback: ...typst-preview.nvim/lua/typst-preview/servers/factory.lua:
180: attempt to perform arithmetic on local 's' (a nil value)
stack traceback:
        ...typst-preview.nvim/lua/typst-preview/servers/factory.lua:180: in function ''
        vim/_editor.lua: in function ''
        vim/_editor.lua: in function <vim/_editor.lua:0>

This cutoff may vary by OS. I'm on Mac OS Sonoma 14.5.

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
print('Leaving for next read: '..read_buffer)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This print statement should be removed from the final version.

Copy link
Owner

@chomosuke chomosuke left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lgtm. Thank you for your work!!

@chomosuke chomosuke merged commit a53a136 into chomosuke:master Oct 11, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants