Skip to content

Commit

Permalink
read lines
Browse files Browse the repository at this point in the history
  • Loading branch information
amirbilu committed Mar 30, 2024
1 parent 463b3e4 commit 743058f
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 21 deletions.
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

0 comments on commit 743058f

Please sign in to comment.