Skip to content

Coverage data is not automatically loaded when running Coverage* commands #35

@lukepafford

Description

@lukepafford

Description

First off, pretty damn nifty plugin. I Just started ramping up with NeoVim, and I try to create a "toggle" mapping for every plugin that contains its own "on/off" actions.

If I want to turn on the coverage report in my code files, I have to first run CoverageLoad, and then run CoverageToggle.
This is two commands, rather than one. Ok, not the end of the world, let's just map these two commands to a single mapping:

keys = {
  { "<leader>cot", 
    function()
      local cov = require("coverage")
      cov.load()
      cov.toggle()
    end, desc = "Coverage Toggle"
  },
}

Unfortunately, this doesn't work. I suspect the reason is because CoverageLoad is performing IO, and CoverageToggle finishes running before CoverageLoad completes. I have created a workaround that waits half a second, and produces consistent results:

-- My hacky-ish nvim-coverage.lua config for lazy.nvim
local function waitUntilLoaded()
    local coverageLoaded = false

    return function(cov, f)
        if not coverageLoaded then
            cov.load()
            coverageLoaded = true

            local timer = vim.loop.new_timer()
            timer:start(500, 0, function()
                vim.schedule(function()
                    f()
                end)
            end
            )
        else
            f()
        end
    end
end


WaitUntilLoaded = waitUntilLoaded()

local function toggle(cov)
    return WaitUntilLoaded(cov, cov.toggle)
end

local function summary(cov)
    return WaitUntilLoaded(cov, cov.summary)
end

return {
    "andythigpen/nvim-coverage",
    lazy = true,
    dependencies = {
        'nvim-lua/plenary.nvim',
        'nvim-neotest/neotest',
    },
    keys = {
        { "<leader>cos", function() local cov = require("coverage") summary(cov) end, desc = "Coverage Summary" },
        { "<leader>cot", function() local cov = require("coverage") toggle(cov) end, desc = "Coverage Toggle"},
    },
    config = function()
        require("coverage").setup({
            auto_reload = true,
        })
    end
}

While this works for me, this feels like something that nvim-coverage should do natively, and with a less hacky solution than just sleeping for half a second.

You would probably want to emit an event when CoverageLoad completes, and Coverage* commands would listen for that event (I don't know how this is done in the NeoVim API, I'm just a newborn lua scripter 🥲)

Acceptance Criteria

  • nvim-coverage automatically loads coverage data when performing Coverage commands for the first time

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions