| 1 | # Copyright 2012-2026, Paul Johnson (paul@pjcj.net) | ||
| 2 | |||
| 3 | # This software is free. It is licensed under the same terms as Perl itself. | ||
| 4 | |||
| 5 | # The latest version of this software should be available from my homepage: | ||
| 6 | # https://pjcj.net | ||
| 7 | |||
| 8 | package Devel::Cover::Report::Nvim; | ||
| 9 | |||
| 10 | use 5.20.0; | ||
| 11 | use warnings; | ||
| 12 | use feature "signatures"; | ||
| 13 | no warnings "experimental::signatures"; | ||
| 14 | |||
| 15 | our $VERSION; | ||
| 16 | |||
| 17 | BEGIN { | ||
| 18 | our $VERSION = '1.52'; # VERSION | ||
| 19 | } | ||
| 20 | |||
| 21 | use parent "Devel::Cover::Base::Editor"; | ||
| 22 | |||
| 23 | use Devel::Cover::Inc (); | ||
| 24 | |||
| 25 | BEGIN { $VERSION //= $Devel::Cover::Inc::VERSION } | ||
| 26 | |||
| 27 | use Getopt::Long qw( GetOptions ); | ||
| 28 | |||
| 29 | my %Templates; | ||
| 30 | |||
| 31 | sub get_options ($self, $opt) { | ||
| 32 | $opt->{outputfile} = "coverage.lua"; | ||
| 33 | die "Invalid command line options" | ||
| 34 | unless GetOptions($opt, qw( outputfile=s )); | ||
| 35 | } | ||
| 36 | |||
| 37 | sub template_name { "nvim" } | ||
| 38 | sub templates { \%Templates } | ||
| 39 | sub output_message ($self, $out) { "Neovim Lua script written to $out" } | ||
| 40 | |||
| 41 | $Templates{nvim} = <<'EOT'; | ||
| 42 | -- This file was generated by Devel::Cover Version [% version %] | ||
| 43 | -- Devel::Cover is copyright 2001-2026, Paul Johnson (paul@pjcj.net) | ||
| 44 | -- Devel::Cover is free. It is licensed under the same terms as Perl itself. | ||
| 45 | -- The latest version of Devel::Cover should be available from my homepage: | ||
| 46 | -- https://pjcj.net | ||
| 47 | |||
| 48 | [% FOREACH r = runs %] | ||
| 49 | -- Run: [% r.run %] | ||
| 50 | -- Perl version: [% r.perl %] | ||
| 51 | -- OS: [% r.OS %] | ||
| 52 | -- Start: [% r.start %] | ||
| 53 | -- Finish: [% r.finish %] | ||
| 54 | |||
| 55 | [% END %] | ||
| 56 | |||
| 57 | local M = {} | ||
| 58 | |||
| 59 | -- Configuration defaults - can be overridden via vim.g variables | ||
| 60 | local config = { | ||
| 61 | -- Colors | ||
| 62 | cover_fg = vim.g.devel_cover_fg or "Green", | ||
| 63 | error_fg = vim.g.devel_cover_error_fg or "Red", | ||
| 64 | cover_bg = vim.g.devel_cover_bg or nil, | ||
| 65 | error_bg = vim.g.devel_cover_error_bg or nil, | ||
| 66 | valid_bg = vim.g.devel_cover_valid_bg or nil, | ||
| 67 | old_bg = vim.g.devel_cover_old_bg or nil, | ||
| 68 | |||
| 69 | -- Sign text - can be customized | ||
| 70 | signs = vim.g.devel_cover_signs or { | ||
| 71 | pod = "P ", | ||
| 72 | subroutine = "R ", | ||
| 73 | statement = "S ", | ||
| 74 | branch = "B ", | ||
| 75 | condition = "C " | ||
| 76 | }, | ||
| 77 | |||
| 78 | -- Line highlight groups | ||
| 79 | linehl_cover = vim.g.devel_cover_linehl or "cov", | ||
| 80 | linehl_error = vim.g.devel_cover_linehl_error or "err", | ||
| 81 | |||
| 82 | -- Additional highlight options | ||
| 83 | cterm = vim.g.devel_cover_cterm or "bold", | ||
| 84 | gui = vim.g.devel_cover_gui or "bold", | ||
| 85 | |||
| 86 | -- Sign column configuration | ||
| 87 | sign_group = vim.g.devel_cover_sign_group or "DevelCover", | ||
| 88 | sign_priority = vim.g.devel_cover_sign_priority or 1, | ||
| 89 | } | ||
| 90 | |||
| 91 | -- Build highlight group definitions dynamically | ||
| 92 | local function create_highlight_groups() | ||
| 93 | local types = { "pod", "subroutine", "statement", "branch", "condition" } | ||
| 94 | |||
| 95 | for _, type_name in ipairs(types) do | ||
| 96 | -- Normal coverage highlight | ||
| 97 | local hl_opts = { | ||
| 98 | fg = config.cover_fg, | ||
| 99 | bold = config.cterm == "bold" or config.gui == "bold", | ||
| 100 | } | ||
| 101 | if config.cover_bg then | ||
| 102 | hl_opts.bg = config.cover_bg | ||
| 103 | end | ||
| 104 | vim.api.nvim_set_hl(0, "cov_" .. type_name, hl_opts) | ||
| 105 | |||
| 106 | -- Error coverage highlight | ||
| 107 | local err_hl_opts = { | ||
| 108 | fg = config.error_fg, | ||
| 109 | bold = config.cterm == "bold" or config.gui == "bold", | ||
| 110 | } | ||
| 111 | if config.error_bg then | ||
| 112 | err_hl_opts.bg = config.error_bg | ||
| 113 | end | ||
| 114 | vim.api.nvim_set_hl(0, "cov_" .. type_name .. "_error", err_hl_opts) | ||
| 115 | end | ||
| 116 | end | ||
| 117 | |||
| 118 | -- Create highlight groups | ||
| 119 | create_highlight_groups() | ||
| 120 | |||
| 121 | -- Create line highlight groups for covered/error lines | ||
| 122 | if config.cover_bg then | ||
| 123 | vim.api.nvim_set_hl(0, config.linehl_cover, { bg = config.cover_bg }) | ||
| 124 | end | ||
| 125 | if config.error_bg then | ||
| 126 | vim.api.nvim_set_hl(0, config.linehl_error, { bg = config.error_bg }) | ||
| 127 | end | ||
| 128 | |||
| 129 | -- Build sign definitions dynamically | ||
| 130 | local function create_sign_definitions() | ||
| 131 | local types = { "pod", "subroutine", "statement", "branch", "condition" } | ||
| 132 | |||
| 133 | for _, type_name in ipairs(types) do | ||
| 134 | -- Normal coverage sign | ||
| 135 | vim.fn.sign_define(type_name, { | ||
| 136 | text = config.signs[type_name], | ||
| 137 | linehl = config.linehl_cover, | ||
| 138 | texthl = "cov_" .. type_name | ||
| 139 | }) | ||
| 140 | |||
| 141 | -- Error coverage sign | ||
| 142 | vim.fn.sign_define(type_name .. "_error", { | ||
| 143 | text = config.signs[type_name], | ||
| 144 | linehl = config.linehl_error, | ||
| 145 | texthl = "cov_" .. type_name .. "_error" | ||
| 146 | }) | ||
| 147 | end | ||
| 148 | end | ||
| 149 | |||
| 150 | -- Create sign definitions | ||
| 151 | create_sign_definitions() | ||
| 152 | |||
| 153 | -- Functions for handling coverage age | ||
| 154 | function M.coverage_old(filename) | ||
| 155 | -- This function is called when coverage data is older than the file | ||
| 156 | if config.old_bg then | ||
| 157 | M.set_background(config.old_bg) | ||
| 158 | end | ||
| 159 | end | ||
| 160 | |||
| 161 | function M.coverage_valid(filename) | ||
| 162 | -- This function is called when coverage data is current | ||
| 163 | if config.valid_bg then | ||
| 164 | M.set_background(config.valid_bg) | ||
| 165 | end | ||
| 166 | end | ||
| 167 | |||
| 168 | -- Helper function to set background colors for all coverage types | ||
| 169 | function M.set_background(bg) | ||
| 170 | local types = { "pod", "subroutine", "statement", "branch", "condition" } | ||
| 171 | |||
| 172 | -- Update highlight groups for coverage sign characters with new background | ||
| 173 | for _, type_name in ipairs(types) do | ||
| 174 | -- Get existing highlight and create new options table for normal coverage | ||
| 175 | local hl = vim.api.nvim_get_hl(0, { name = "cov_" .. type_name }) | ||
| 176 | local new_hl = { | ||
| 177 | fg = hl.fg, | ||
| 178 | bg = bg, | ||
| 179 | bold = hl.bold, | ||
| 180 | italic = hl.italic, | ||
| 181 | underline = hl.underline, | ||
| 182 | } | ||
| 183 | vim.api.nvim_set_hl(0, "cov_" .. type_name, new_hl) | ||
| 184 | |||
| 185 | -- Get existing highlight and create new options table for error coverage | ||
| 186 | local err_hl = vim.api.nvim_get_hl(0, { name = "cov_" .. type_name .. "_error" }) | ||
| 187 | local new_err_hl = { | ||
| 188 | fg = err_hl.fg, | ||
| 189 | bg = bg, | ||
| 190 | bold = err_hl.bold, | ||
| 191 | italic = err_hl.italic, | ||
| 192 | underline = err_hl.underline, | ||
| 193 | } | ||
| 194 | vim.api.nvim_set_hl(0, "cov_" .. type_name .. "_error", new_err_hl) | ||
| 195 | end | ||
| 196 | |||
| 197 | -- Redefine all signs with updated texthl (no numhl to avoid affecting line numbers) | ||
| 198 | for _, type_name in ipairs(types) do | ||
| 199 | -- Normal coverage sign | ||
| 200 | vim.fn.sign_define(type_name, { | ||
| 201 | text = config.signs[type_name], | ||
| 202 | linehl = config.linehl_cover, | ||
| 203 | texthl = "cov_" .. type_name | ||
| 204 | }) | ||
| 205 | |||
| 206 | -- Error coverage sign | ||
| 207 | vim.fn.sign_define(type_name .. "_error", { | ||
| 208 | text = config.signs[type_name], | ||
| 209 | linehl = config.linehl_error, | ||
| 210 | texthl = "cov_" .. type_name .. "_error" | ||
| 211 | }) | ||
| 212 | end | ||
| 213 | |||
| 214 | -- Force a refresh by issuing redraw commands | ||
| 215 | vim.cmd("redraw!") | ||
| 216 | vim.cmd("redrawstatus!") | ||
| 217 | end | ||
| 218 | |||
| 219 | -- Load local configuration if it exists | ||
| 220 | local config_path = vim.fn.stdpath("config") .. "/lua/devel-cover.lua" | ||
| 221 | if vim.fn.filereadable(config_path) == 1 then | ||
| 222 | print("Reading local config from " .. config_path) | ||
| 223 | dofile(config_path) | ||
| 224 | end | ||
| 225 | |||
| 226 | local types = { | ||
| 227 | [%- FOREACH type = types -%] "[%- type -%]",[%- END -%] | ||
| 228 | [%- FOREACH type = types -%] "[%- type -%]_error",[%- END -%] | ||
| 229 | } | ||
| 230 | |||
| 231 | [%- MACRO criterion(file, crit, error) BLOCK %] | ||
| 232 | [% crit %][% error ? "_error" : "" %] = { | ||
| 233 | [%- criteria = cover.file("$file").$crit -%] | ||
| 234 | [%- FOREACH loc = criteria.items.nsort -%] | ||
| 235 | [%- cov = 0 -%] | ||
| 236 | [%- FOREACH l = criteria.location("$loc") -%] | ||
| 237 | [%- IF error ? l.error : l.covered -%] [% loc -%],[%- cov = 1; LAST; END -%] | ||
| 238 | [%- LAST IF cov; END -%] | ||
| 239 | [%- END -%] | ||
| 240 | }, | ||
| 241 | [%- END %] | ||
| 242 | |||
| 243 | local coverage = { | ||
| 244 | [% FOREACH file = files %] | ||
| 245 | ["[% file %]"] = { | ||
| 246 | [%- FOREACH type = types; criterion(file, type, 0); criterion(file, type, 1); END %] | ||
| 247 | }, | ||
| 248 | [% END %] | ||
| 249 | } | ||
| 250 | |||
| 251 | local cov_time = [% cov_time %] | ||
| 252 | |||
| 253 | -- Find coverage data for a given filename | ||
| 254 | local function coverage_for(filename) | ||
| 255 | local fn_len = string.len(filename) | ||
| 256 | for cf, _ in pairs(coverage) do | ||
| 257 | local f = string.gsub(cf, "^blib/", "") | ||
| 258 | local match_pos = string.find(filename, f .. "$") | ||
| 259 | if match_pos and match_pos <= fn_len then | ||
| 260 | return coverage[cf] | ||
| 261 | end | ||
| 262 | end | ||
| 263 | |||
| 264 | print("No coverage recorded for " .. filename) | ||
| 265 | return {} | ||
| 266 | end | ||
| 267 | |||
| 268 | local signs = {} | ||
| 269 | local sign_num = 1 | ||
| 270 | |||
| 271 | -- Remove coverage signs from a file | ||
| 272 | local function del_coverage_signs(filename) | ||
| 273 | if signs[filename] then | ||
| 274 | local s = signs[filename] | ||
| 275 | for line, id in pairs(s) do | ||
| 276 | vim.fn.sign_unplace(config.sign_group, { id = id }) | ||
| 277 | end | ||
| 278 | signs[filename] = {} | ||
| 279 | end | ||
| 280 | end | ||
| 281 | |||
| 282 | -- Add coverage signs to a file | ||
| 283 | local function add_coverage_signs(filename) | ||
| 284 | local cov = coverage_for(filename) | ||
| 285 | if vim.tbl_isempty(cov) then | ||
| 286 | return | ||
| 287 | end | ||
| 288 | |||
| 289 | local file_time = vim.fn.getftime(filename) | ||
| 290 | local needs_refresh = false | ||
| 291 | |||
| 292 | -- Check if we already have signs and background is changing | ||
| 293 | if signs[filename] and not vim.tbl_isempty(signs[filename]) then | ||
| 294 | needs_refresh = true | ||
| 295 | end | ||
| 296 | |||
| 297 | if file_time > cov_time then | ||
| 298 | print("File is newer than coverage run of " .. os.date("%c", cov_time)) | ||
| 299 | M.coverage_old(filename) | ||
| 300 | else | ||
| 301 | M.coverage_valid(filename) | ||
| 302 | end | ||
| 303 | |||
| 304 | -- If background changed and we had existing signs, refresh them | ||
| 305 | if needs_refresh then | ||
| 306 | del_coverage_signs(filename) | ||
| 307 | end | ||
| 308 | |||
| 309 | if not signs[filename] then | ||
| 310 | signs[filename] = {} | ||
| 311 | end | ||
| 312 | local s = signs[filename] | ||
| 313 | |||
| 314 | -- Process types in reverse order for proper priority | ||
| 315 | local reversed_types = {} | ||
| 316 | for i = #types, 1, -1 do | ||
| 317 | table.insert(reversed_types, types[i]) | ||
| 318 | end | ||
| 319 | |||
| 320 | for _, type_name in ipairs(reversed_types) do | ||
| 321 | if cov[type_name] then | ||
| 322 | for _, line in ipairs(cov[type_name]) do | ||
| 323 | if not s[line] then | ||
| 324 | local id = sign_num | ||
| 325 | sign_num = sign_num + 1 | ||
| 326 | s[line] = id | ||
| 327 | |||
| 328 | -- Validate parameters before placing sign | ||
| 329 | local bufnr = vim.fn.bufnr(filename) | ||
| 330 | if bufnr == -1 then | ||
| 331 | -- Buffer doesn't exist, skip this sign | ||
| 332 | goto continue | ||
| 333 | end | ||
| 334 | |||
| 335 | -- Validate line number | ||
| 336 | local line_count = vim.api.nvim_buf_line_count(bufnr) | ||
| 337 | if line > line_count then | ||
| 338 | -- Line doesn't exist in buffer, skip this sign | ||
| 339 | goto continue | ||
| 340 | end | ||
| 341 | |||
| 342 | -- Place the sign with buffer number instead of filename for better reliability | ||
| 343 | local success, err = pcall(vim.fn.sign_place, id, config.sign_group, type_name, bufnr, { lnum = line, priority = config.sign_priority }) | ||
| 344 | if not success then | ||
| 345 | print("Warning: Failed to place coverage sign: " .. err) | ||
| 346 | end | ||
| 347 | |||
| 348 | ::continue:: | ||
| 349 | end | ||
| 350 | end | ||
| 351 | end | ||
| 352 | end | ||
| 353 | end | ||
| 354 | |||
| 355 | |||
| 356 | -- User commands | ||
| 357 | vim.api.nvim_create_user_command("Cov", function() | ||
| 358 | add_coverage_signs(vim.fn.expand("%:p")) | ||
| 359 | end, {}) | ||
| 360 | |||
| 361 | vim.api.nvim_create_user_command("Uncov", function() | ||
| 362 | del_coverage_signs(vim.fn.expand("%:p")) | ||
| 363 | end, {}) | ||
| 364 | |||
| 365 | -- Clear all existing Devel::Cover signs from all buffers when loading new coverage data | ||
| 366 | -- This ensures old signs don't persist when coverage.lua is reloaded | ||
| 367 | local function clear_all_coverage_signs() | ||
| 368 | -- Get all existing signs in the Devel::Cover group across all buffers | ||
| 369 | vim.fn.sign_unplace(config.sign_group) | ||
| 370 | |||
| 371 | -- Clear the signs tracking table | ||
| 372 | signs = {} | ||
| 373 | sign_num = 1 | ||
| 374 | end | ||
| 375 | |||
| 376 | -- Clear old signs when script loads | ||
| 377 | clear_all_coverage_signs() | ||
| 378 | |||
| 379 | -- Auto commands for coverage display | ||
| 380 | local augroup = vim.api.nvim_create_augroup("devel-cover", { clear = true }) | ||
| 381 | |||
| 382 | -- Show signs automatically for all known files | ||
| 383 | for filename, _ in pairs(coverage) do | ||
| 384 | vim.api.nvim_create_autocmd("BufReadPost", { | ||
| 385 | group = augroup, | ||
| 386 | pattern = filename, | ||
| 387 | callback = function() | ||
| 388 | add_coverage_signs(vim.fn.expand("%:p")) | ||
| 389 | end | ||
| 390 | }) | ||
| 391 | |||
| 392 | local f = string.gsub(filename, "^blib/", "") | ||
| 393 | if filename ~= f then | ||
| 394 | vim.api.nvim_create_autocmd("BufReadPost", { | ||
| 395 | group = augroup, | ||
| 396 | pattern = f, | ||
| 397 | callback = function() | ||
| 398 | add_coverage_signs(vim.fn.expand("%:p")) | ||
| 399 | end | ||
| 400 | }) | ||
| 401 | end | ||
| 402 | end | ||
| 403 | |||
| 404 | -- Apply coverage to current buffer | ||
| 405 | add_coverage_signs(vim.fn.expand("%:p")) | ||
| 406 | |||
| 407 | return M | ||
| 408 | EOT | ||
| 409 | |||
| 410 | 1 | ||
| 411 | |||
| 412 | __END__ |