feat(server): add v:useractive, use it in serverlist(info=true) #39423

Problem:
When showing the :connect menu, it is useful to know which servers
are most-recently active. But we don't have a good way to detect that.

Solution:
- Introduce `v:useractive`.
- Include this timestamp in `serverlist({info=true})`.

Co-authored-by: Justin M. Keyes <justinkz@gmail.com>
Signed-off-by: Szymon Wilczek <swilczek.lx@gmail.com>
This commit is contained in:
Szymon Wilczek
2026-05-07 10:47:51 -04:00
committed by GitHub
co-authored by Justin M. Keyes
parent 97a557bd1e
commit 3639f7a867
12 changed files with 82 additions and 21 deletions
+9 -1
View File
@@ -6,8 +6,9 @@ local M = {}
---
---@class vim.ServerInfo
---@field addr string Server address (socket path, named pipe, or TCP host:port).
---@field pid integer? PID of the Nvim process owning this server (nil if unreachable).
---@field pid integer PID of the Nvim process.
---@field own boolean True if this server belongs to the current Nvim instance.
---@field active integer Timestamp of last user activity.
--- @param opts? table Options:
--- - opts.peer (boolean): If true, also discover peer servers.
@@ -42,6 +43,7 @@ function M.serverlist(opts, addrs)
end, { path = root, type = 'socket', limit = math.huge })
local peer_pids = {} ---@type table<string, integer>
local peer_active = {} ---@type table<string, integer>
for _, socket in ipairs(socket_paths) do
if not own_set[socket] and not vim.list_contains(addrs, socket) then
@@ -57,6 +59,10 @@ function M.serverlist(opts, addrs)
if ok_pid and type(pid) == 'number' then
peer_pids[socket] = pid
end
local ok_la, la = pcall(vim.fn.rpcrequest, chan, 'nvim_get_vvar', 'useractive')
if ok_la and type(la) == 'number' then
peer_active[socket] = la
end
end
end
pcall(vim.fn.chanclose, chan)
@@ -69,6 +75,7 @@ function M.serverlist(opts, addrs)
end
local self_pid = vim.fn.getpid()
local self_active = vim.v.useractive
local result = {} ---@type vim.ServerInfo[]
for _, addr in ipairs(addrs) do
local own = own_set[addr] == true
@@ -76,6 +83,7 @@ function M.serverlist(opts, addrs)
addr = addr,
pid = own and self_pid or peer_pids[addr],
own = own,
active = own and self_active or peer_active[addr],
})
end
return result