|
| 1 | +--- Health check for claudecode.nvim, run via :checkhealth claudecode |
| 2 | +--- Verifies prerequisites (Neovim version, Claude CLI, terminal provider) |
| 3 | +--- and reports live integration state (WebSocket server, lock file, |
| 4 | +--- Claude connection) without launching anything. |
| 5 | +---@module 'claudecode.health' |
| 6 | +local M = {} |
| 7 | + |
| 8 | +-- vim.health gained start/ok/warn/error in Neovim 0.10; older versions use report_* variants. |
| 9 | +local health = vim.health or require("health") |
| 10 | +local start = health.start or health.report_start |
| 11 | +local ok = health.ok or health.report_ok |
| 12 | +local warn = health.warn or health.report_warn |
| 13 | +local error_ = health.error or health.report_error |
| 14 | +local info = health.info or health.report_info or ok |
| 15 | + |
| 16 | +---Extracts the executable (first token) from a command string. |
| 17 | +---@param cmd string |
| 18 | +---@return string executable |
| 19 | +local function executable_of(cmd) |
| 20 | + assert(type(cmd) == "string" and cmd ~= "", "cmd must be a non-empty string") |
| 21 | + return cmd:match("^(%S+)") or cmd |
| 22 | +end |
| 23 | + |
| 24 | +local function check_neovim() |
| 25 | + if vim.fn.has("nvim-0.8.0") == 1 then |
| 26 | + ok("Neovim >= 0.8.0") |
| 27 | + else |
| 28 | + error_("Neovim >= 0.8.0 is required") |
| 29 | + end |
| 30 | +end |
| 31 | + |
| 32 | +---@param claudecode table The main plugin module |
| 33 | +local function check_setup(claudecode) |
| 34 | + if claudecode.state.initialized then |
| 35 | + ok("claudecode.nvim " .. claudecode.version:string() .. " is set up") |
| 36 | + return true |
| 37 | + end |
| 38 | + error_("setup() has not been called", { 'Call require("claudecode").setup() (or use your plugin manager\'s opts)' }) |
| 39 | + return false |
| 40 | +end |
| 41 | + |
| 42 | +---@param config table The merged plugin config |
| 43 | +local function check_cli(config) |
| 44 | + local terminal_cmd = config.terminal_cmd |
| 45 | + local cmd = (terminal_cmd and terminal_cmd ~= "") and terminal_cmd or "claude" |
| 46 | + local exe = executable_of(cmd) |
| 47 | + |
| 48 | + if vim.fn.executable(exe) ~= 1 then |
| 49 | + error_(("Claude CLI not found: '%s' is not executable"):format(exe), { |
| 50 | + "Install Claude Code: https://docs.anthropic.com/en/docs/claude-code", |
| 51 | + "Or set `terminal_cmd` in setup() to the full path of the CLI", |
| 52 | + }) |
| 53 | + return |
| 54 | + end |
| 55 | + |
| 56 | + ok(("Claude CLI found: %s (%s)"):format(exe, vim.fn.exepath(exe))) |
| 57 | + |
| 58 | + local version_ok, output = pcall(vim.fn.system, { exe, "--version" }) |
| 59 | + if version_ok and vim.v.shell_error == 0 then |
| 60 | + info("CLI version: " .. vim.trim(output)) |
| 61 | + else |
| 62 | + warn(("'%s --version' failed; the configured command may not be the Claude CLI"):format(exe)) |
| 63 | + end |
| 64 | +end |
| 65 | + |
| 66 | +---@param config table The merged plugin config |
| 67 | +local function check_terminal_provider(config) |
| 68 | + local provider = config.terminal and config.terminal.provider or "auto" |
| 69 | + if type(provider) == "table" then |
| 70 | + info("Terminal provider: custom (table)") |
| 71 | + return |
| 72 | + end |
| 73 | + |
| 74 | + if provider == "auto" or provider == "snacks" then |
| 75 | + local has_snacks = pcall(require, "snacks") |
| 76 | + if has_snacks then |
| 77 | + ok(("Terminal provider '%s': snacks.nvim available"):format(provider)) |
| 78 | + elseif provider == "snacks" then |
| 79 | + error_("Terminal provider 'snacks' configured but snacks.nvim is not installed") |
| 80 | + else |
| 81 | + ok("Terminal provider 'auto': snacks.nvim not installed, will fall back to native terminal") |
| 82 | + end |
| 83 | + elseif provider == "external" then |
| 84 | + local cmd = config.terminal.provider_opts and config.terminal.provider_opts.external_terminal_cmd |
| 85 | + if cmd and (type(cmd) == "function" or cmd:find("%%s")) then |
| 86 | + ok("Terminal provider 'external' configured") |
| 87 | + else |
| 88 | + error_("Terminal provider 'external' requires provider_opts.external_terminal_cmd containing '%s'") |
| 89 | + end |
| 90 | + else |
| 91 | + ok(("Terminal provider: %s"):format(provider)) |
| 92 | + end |
| 93 | +end |
| 94 | + |
| 95 | +---@param claudecode table The main plugin module |
| 96 | +local function check_server(claudecode) |
| 97 | + local server = require("claudecode.server.init") |
| 98 | + local status = server.get_status() |
| 99 | + |
| 100 | + if not status.running then |
| 101 | + warn("WebSocket server is not running", { |
| 102 | + "The server starts automatically when auto_start = true (default)", |
| 103 | + "Or start it manually with :ClaudeCodeStart", |
| 104 | + }) |
| 105 | + return |
| 106 | + end |
| 107 | + |
| 108 | + ok(("WebSocket server running on port %d"):format(status.port)) |
| 109 | + |
| 110 | + local lockfile = require("claudecode.lockfile") |
| 111 | + local lock_path = lockfile.lock_dir .. "/" .. tostring(status.port) .. ".lock" |
| 112 | + if vim.fn.filereadable(lock_path) == 1 then |
| 113 | + ok("Lock file present: " .. lock_path) |
| 114 | + else |
| 115 | + error_("Lock file missing: " .. lock_path, { |
| 116 | + "Claude discovers this Neovim instance through the lock file", |
| 117 | + "Restart the integration with :ClaudeCodeStop and :ClaudeCodeStart", |
| 118 | + }) |
| 119 | + end |
| 120 | + |
| 121 | + if claudecode.is_claude_connected() then |
| 122 | + ok(("Claude Code is connected (%d client(s))"):format(status.client_count)) |
| 123 | + else |
| 124 | + info("No Claude Code client connected yet (launch one with :ClaudeCode)") |
| 125 | + end |
| 126 | +end |
| 127 | + |
| 128 | +function M.check() |
| 129 | + start("claudecode.nvim") |
| 130 | + |
| 131 | + check_neovim() |
| 132 | + |
| 133 | + local loaded, claudecode = pcall(require, "claudecode") |
| 134 | + if not loaded then |
| 135 | + error_("Could not load claudecode module: " .. tostring(claudecode)) |
| 136 | + return |
| 137 | + end |
| 138 | + |
| 139 | + if not check_setup(claudecode) then |
| 140 | + return |
| 141 | + end |
| 142 | + |
| 143 | + check_cli(claudecode.state.config) |
| 144 | + check_terminal_provider(claudecode.state.config) |
| 145 | + check_server(claudecode) |
| 146 | +end |
| 147 | + |
| 148 | +return M |
0 commit comments