Skip to content

Commit 61adc15

Browse files
committed
add EcaServerMessages command
1 parent 62687fd commit 61adc15

File tree

2 files changed

+50
-7
lines changed

2 files changed

+50
-7
lines changed

lua/eca/commands.lua

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,52 @@ function M.setup()
157157
desc = "Restart ECA server",
158158
})
159159

160+
vim.api.nvim_create_user_command("EcaServerMessages", function()
161+
local has_snacks, snacks = pcall(require, "snacks")
162+
if not has_snacks then
163+
Logger.notify("snacks.nvim is not available", vim.log.levels.ERROR)
164+
return
165+
end
166+
167+
snacks.picker(
168+
---@type snacks.picker.Config
169+
{
170+
source = "eca messages",
171+
finder = function(opts, ctx)
172+
---@type snacks.picker.finder.Item[]
173+
local items = {}
174+
local eca = require("eca")
175+
if not eca or not eca.server then
176+
Logger.notify("ECA plugin is not available", vim.log.levels.ERROR)
177+
return items
178+
end
179+
180+
for msg in vim.iter(eca.server.messages) do
181+
print(msg)
182+
local decoded = vim.json.decode(msg.content)
183+
table.insert(items, {
184+
text = decoded.method,
185+
idx = decoded.id,
186+
preview = {
187+
text = vim.inspect(decoded),
188+
ft = "lua",
189+
},
190+
})
191+
end
192+
return items
193+
end,
194+
preview = "preview",
195+
format = "text",
196+
confirm = function(self, item, _)
197+
vim.fn.setreg("", item.preview.text)
198+
self:close()
199+
end,
200+
}
201+
)
202+
end, {
203+
desc = "Display Messages Sent to and Received by ECA server",
204+
})
205+
160206
vim.api.nvim_create_user_command("EcaLogs", function(opts)
161207
local Api = require("eca.api")
162208
local subcommand = opts.args and opts.args:match("%S+") or "show"

lua/eca/server.lua

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ local Logger = require("eca.logger")
1313
---@field on_stop function Callback when the server stops
1414
---Called when a notification is received(message without an ID)
1515
---@field on_notification fun(server: eca.Server, message: table)
16-
---@field capabilities eca.ServerCapabilities Server capabilities
1716
---@field private path_finder eca.PathFinder Server path finder
1817
---@field pending_requests {id: fun(err, data)} -- outgoing requests with callbacks
1918
local M = {}
@@ -50,7 +49,6 @@ function M.new(opts)
5049
path_finder = opts.path_finder,
5150
messages = {},
5251
pending_requests = {},
53-
capabilities = {},
5452
initialized = false,
5553
next_id = 0,
5654
}, { __index = M })
@@ -67,7 +65,7 @@ local function on_stdout(server)
6765
if #message.content ~= message.content_length then
6866
return
6967
end
70-
table.insert(messages, message)
68+
table.insert(server.messages, message)
7169
local msg = vim.json.decode(message.content)
7270
server:handle_message(msg)
7371
end)
@@ -171,20 +169,18 @@ function M:initialize()
171169
},
172170
},
173171
workspaceFolders = workspace_folders,
174-
}, function(err, result)
172+
}, function(err, _)
175173
if err then
176174
Logger.notify("Could not initialize server: " .. err, vim.log.levels.ERROR)
177175
return
178176
end
179-
if result then
180-
self.capabilities = result
181-
end
182177

183178
self:send_notification("initialized", {})
184179

185180
if self.on_initialize then
186181
self.on_initialize()
187182
end
183+
self.initialized = true
188184
end)
189185
end
190186

@@ -259,6 +255,7 @@ function M:send_request(method, params, callback)
259255
end
260256

261257
local json = vim.json.encode(message)
258+
table.insert(self.messages, { content = json, content_length = #json })
262259
local content = string.format("Content-Length: %d\r\n\r\n%s", #json, json)
263260
self.process:write(content)
264261
end

0 commit comments

Comments
 (0)