mirror of
https://github.com/neovim/neovim.git
synced 2026-08-28 10:01:49 +00:00
refactor(lsp): avoid using coroutine when parsing frames
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
local uv = vim.uv
|
||||
local log = require('vim.lsp.log')
|
||||
local strbuffer = require('vim._core.stringbuffer')
|
||||
|
||||
--- Interface for transport implementations.
|
||||
---
|
||||
@@ -182,34 +183,36 @@ function TransportConnect:terminate()
|
||||
end
|
||||
end
|
||||
|
||||
--- Create a message stream from a coroutine decoder.
|
||||
--- Create a message stream from a decoder.
|
||||
---
|
||||
--- The decoder receives transport data from `coroutine.yield()`
|
||||
--- and may yield a message body when a full message is available.
|
||||
--- A nil yield means it needs more transport data.
|
||||
--- Decoder errors are reported through `on_error`.
|
||||
--- The decoder consumes from the given string buffer
|
||||
--- and returns a message body when a full message is available.
|
||||
--- `nil` means it needs more transport data.
|
||||
--- decoder errors are reported through `on_error`.
|
||||
---
|
||||
---@class (private, exact) vim.MessageStream
|
||||
---@field private co thread
|
||||
---@field private decode fun()
|
||||
---@field private strbuf string.buffer
|
||||
---@field private decode fun(strbuf: string.buffer): string?
|
||||
---@field private on_read fun(err: string?, data: string?)
|
||||
---@field private on_error fun(err: any)
|
||||
---@field feed fun(self: vim.MessageStream, err: string?, data: string?)
|
||||
---@field encode fun(msg: string): string
|
||||
---@field new fun(decode: fun(), encode: (fun(msg: string): string), on_read: fun(err: string?, data: string?), on_error: fun(err: any)): vim.MessageStream
|
||||
---@field new fun(decode: (fun(strbuf: string.buffer): string?), encode: (fun(msg: string): string), on_read: fun(err: string?, data: string?), on_error: fun(err: any)): vim.MessageStream
|
||||
local MessageStream = {}
|
||||
|
||||
---@param decode fun(strbuf: string.buffer): string?
|
||||
---@param encode fun(msg: string): string
|
||||
---@param on_read fun(err: string?, data: string?)
|
||||
---@param on_error fun(err: any)
|
||||
---@return vim.MessageStream
|
||||
function MessageStream.new(decode, encode, on_read, on_error)
|
||||
local self = setmetatable({
|
||||
co = coroutine.create(decode),
|
||||
return setmetatable({
|
||||
strbuf = strbuffer.new(),
|
||||
decode = decode,
|
||||
on_read = on_read,
|
||||
on_error = on_error,
|
||||
encode = encode,
|
||||
}, { __index = MessageStream })
|
||||
|
||||
coroutine.resume(self.co)
|
||||
return self
|
||||
end
|
||||
|
||||
---@param err string?
|
||||
@@ -223,8 +226,10 @@ function MessageStream:feed(err, data)
|
||||
return
|
||||
end
|
||||
|
||||
self.strbuf:put(data)
|
||||
|
||||
while true do
|
||||
local ok, body = coroutine.resume(self.co, data)
|
||||
local ok, body = pcall(self.decode, self.strbuf)
|
||||
if not ok then
|
||||
self.on_error(body)
|
||||
return
|
||||
@@ -232,7 +237,6 @@ function MessageStream:feed(err, data)
|
||||
break
|
||||
end
|
||||
self.on_read(nil, body)
|
||||
data = ''
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
Reference in New Issue
Block a user