From 5151f635ca299dcb7aeb7e34d7c01fba07108b88 Mon Sep 17 00:00:00 2001 From: Siddhant Agarwal <68201519+siddhantdev@users.noreply.github.com> Date: Mon, 28 Jul 2025 10:10:04 +0530 Subject: feat: serverlist({peer=true}) returns peer addresses #34806 Problem: serverlist() only lists servers that were started by the current Nvim. Solution: Look for other Nvim servers in stdpath("run"). --- runtime/lua/vim/_core/server.lua | 33 +++++++++++++++++++++++++++++++++ runtime/lua/vim/_meta/vimfn.lua | 10 +++++++++- 2 files changed, 42 insertions(+), 1 deletion(-) create mode 100644 runtime/lua/vim/_core/server.lua (limited to 'runtime/lua/vim') diff --git a/runtime/lua/vim/_core/server.lua b/runtime/lua/vim/_core/server.lua new file mode 100644 index 0000000000..281a5705ea --- /dev/null +++ b/runtime/lua/vim/_core/server.lua @@ -0,0 +1,33 @@ +local M = {} + +--- Called by builtin serverlist(). Returns all running servers. +--- in stdpath("run"). Does not include named pipes or TCP servers. +--- +--- @param listed string[] Already listed servers +--- @return string[] A list of currently running servers in stdpath("run") +function M.serverlist(listed) + -- TODO: also get named pipes on Windows + local socket_paths = vim.fs.find(function(name, _) + return name:match('nvim.*') + end, { path = vim.fn.stdpath('run'), type = 'socket', limit = math.huge }) + + local running_sockets = {} + for _, socket in ipairs(socket_paths) do + -- Don't list servers twice + if not vim.list_contains(listed, socket) then + local ok, chan = pcall(vim.fn.sockconnect, 'pipe', socket, { rpc = true }) + if ok and chan then + -- Check that the server is responding + -- TODO: do we need a timeout or error handling here? + if vim.fn.rpcrequest(chan, 'nvim_get_chan_info', 0).id then + table.insert(running_sockets, socket) + end + vim.fn.chanclose(chan) + end + end + end + + return running_sockets +end + +return M diff --git a/runtime/lua/vim/_meta/vimfn.lua b/runtime/lua/vim/_meta/vimfn.lua index 76bc5e8410..ff800d7d87 100644 --- a/runtime/lua/vim/_meta/vimfn.lua +++ b/runtime/lua/vim/_meta/vimfn.lua @@ -7964,12 +7964,20 @@ function vim.fn.searchpos(pattern, flags, stopline, timeout, skip) end --- Returns a list of server addresses, or empty if all servers --- were stopped. |serverstart()| |serverstop()| +--- +--- The optional argument {opts} is a Dict and supports the following items: +--- +--- peer : If |TRUE|, servers not started by |serverstart()| +--- will also be returned. (default: |FALSE|) +--- Not supported on Windows yet. +--- --- Example: >vim --- echo serverlist() --- < --- +--- @param opts? table --- @return string[] -function vim.fn.serverlist() end +function vim.fn.serverlist(opts) end --- Opens a socket or named pipe at {address} and listens for --- |RPC| messages. Clients can send |API| commands to the -- cgit