nvim + pi / bi-directional communication
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081local M = {}
local config = require('pi-nvim.config')local state = require('pi-nvim.rpc.state')
---@return stringfunction M.get_data_dir() local cfg = config.get() if cfg.data_dir then return cfg.data_dir end return vim.fn.stdpath('data') .. '/pi-nvim'end
---@param cwd string---@return stringfunction M.cwd_hash(cwd) return vim.fn.sha256(cwd):sub(1, 8)end
---@param cwd string---@param pid number---@return stringfunction M.get_socket_path(cwd, pid) local dir = M.get_data_dir() local hash = M.cwd_hash(cwd) return string.format('%s/%s-%d.sock', dir, hash, pid)end
---@param cwd string---@param pid number---@return stringfunction M.get_lockfile_path(cwd, pid) local dir = M.get_data_dir() local hash = M.cwd_hash(cwd) return string.format('%s/%s-%d.json', dir, hash, pid)end
---@param socket_path string---@param cwd string---@param pid number---@return boolean success---@return string? errorfunction M.create(socket_path, cwd, pid) local dir = M.get_data_dir() vim.fn.mkdir(dir, 'p')
local lockfile_path = M.get_lockfile_path(cwd, pid) local data = vim.json.encode({ socket = socket_path, cwd = cwd, pid = pid, })
local ok = pcall(function() local f = io.open(lockfile_path, 'w') if f then f:write(data) f:close() end end)
if not ok then return false, 'Failed to write lockfile' end
state.set({ lockfile = lockfile_path }) return trueend
---@param cwd string---@param pid numberfunction M.remove(cwd, pid) local lockfile_path = M.get_lockfile_path(cwd, pid) local socket_path = M.get_socket_path(cwd, pid) vim.fn.delete(lockfile_path) vim.fn.delete(socket_path)end
return M