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

read lines #168

Merged
merged 2 commits into from
Apr 1, 2024
Merged
Show file tree
Hide file tree
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
21 changes: 9 additions & 12 deletions lua/tabnine/binary.lua
Original file line number Diff line number Diff line change
Expand Up @@ -75,20 +75,17 @@ function TabnineBinary:start()
uv.read_stop(self.stdout)
end)

uv.read_start(
utils.read_lines_start(
self.stdout,
vim.schedule_wrap(function(error, chunk)
if chunk then
for _, line in pairs(utils.str_to_lines(chunk)) do
local callback = table.remove(self.callbacks)
if not callback.cancelled then
local decoded = vim.json.decode(line, { luanil = { object = true, array = true } })
callback.callback(decoded)
end
end
elseif error then
print("tabnine binary read_start error", error)
vim.schedule_wrap(function(line)
local callback = table.remove(self.callbacks)
if not callback.cancelled then
local decoded = vim.json.decode(line, { luanil = { object = true, array = true } })
callback.callback(decoded)
end
end),
vim.schedule_wrap(function(error)
print("tabnine binary read_start error", error)
end)
)
end
Expand Down
40 changes: 19 additions & 21 deletions lua/tabnine/chat/binary.lua
Original file line number Diff line number Diff line change
Expand Up @@ -76,28 +76,26 @@ function ChatBinary:start()
end)
)

self.stdout:read_start(vim.schedule_wrap(function(error, chunk)
if chunk then
for _, line in pairs(utils.str_to_lines(chunk)) do
local message = vim.json.decode(line, { luanil = { object = true, array = true } })
local handler = self.registry[message.command]
if handler then
handler(message.data, function(payload)
-- if payload then
self:post_message({
id = message.id,
payload = payload,
})
-- end
end)
else
self:post_message({ id = message.id, error = "not_implemented" })
end
utils.read_lines_start(
self.stdout,
vim.schedule_wrap(function(line)
local message = vim.json.decode(line, { luanil = { object = true, array = true } })
local handler = self.registry[message.command]
if handler then
handler(message.data, function(payload)
self:post_message({
id = message.id,
payload = payload,
})
end)
else
self:post_message({ id = message.id, error = "not_implemented" })
end
elseif error then
print("chat binary read_start error", error)
end
end))
end),
vim.schedule_wrap(function(error)
print("error reading chat binary", error)
end)
)
end

function ChatBinary:new(o)
Expand Down
17 changes: 17 additions & 0 deletions lua/tabnine/utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -157,4 +157,21 @@ function M.read_file_into_buffer(file_path)
return bufnr
end

function M.read_lines_start(stream, on_line, on_error)
local buffer = ""
stream:read_start(function(error, chunk)
buffer = buffer .. chunk
if error then
on_error(error)
return
end
while true do
local start_pos = buffer:find("\n")
if not start_pos then break end
on_line(buffer:sub(1, start_pos - 1))
buffer = buffer:sub(start_pos + 1)
end
end)
end

return M
Loading