neovim

Neovim text editor
git clone https://git.dasho.dev/neovim.git
Log | Files | Refs | README

server.lua (1190B)


      1 -- For "--listen" and related functionality.
      2 
      3 local M = {}
      4 
      5 --- Called by builtin serverlist(). Returns all running servers in stdpath("run").
      6 ---
      7 --- - TODO: track TCP servers, somehow.
      8 --- - TODO: support Windows named pipes.
      9 ---
     10 --- @param listed string[] Already listed servers
     11 --- @return string[] # List of servers found on the current machine in stdpath("run").
     12 function M.serverlist(listed)
     13  local root = vim.fs.normalize(vim.fn.stdpath('run') .. '/..')
     14  local socket_paths = vim.fs.find(function(name, _)
     15    return name:match('nvim.*')
     16  end, { path = root, type = 'socket', limit = math.huge })
     17 
     18  local found = {} ---@type string[]
     19  for _, socket in ipairs(socket_paths) do
     20    -- Don't list servers twice
     21    if not vim.list_contains(listed, socket) then
     22      local ok, chan = pcall(vim.fn.sockconnect, 'pipe', socket, { rpc = true })
     23      if ok and chan then
     24        -- Check that the server is responding
     25        -- TODO: do we need a timeout or error handling here?
     26        if vim.fn.rpcrequest(chan, 'nvim_get_chan_info', 0).id then
     27          table.insert(found, socket)
     28        end
     29        vim.fn.chanclose(chan)
     30      end
     31    end
     32  end
     33 
     34  return found
     35 end
     36 
     37 return M