Skip to content

Configuration I recommend #3

@lucas-garcia-rubio

Description

@lucas-garcia-rubio

Before starting, I would like to say sorry for opening an issue for this. I know that the best place would be a "Discussion" section, but it seems this repository doesn't have one. So, here it goes:

That plugin is amazing and worked fine for me, but I missed one thing: jacoco reports at a high level, like total percent of coverage, by class, by method, etc.

Also, I love neovim because it is very fast and that plugin made my neovim a bit slower when I open a java filetype for the first time during a session. So, I came up with that solution:

return {
  'dsych/blanket.nvim',
  opts = {
    report_path = vim.fn.getcwd() .. '/target/site/jacoco/jacoco.xml',
    filetypes = 'java',
  },
  init = function()
    require('blanket').stop()
    local coverage = false
    local report_path = vim.fn.getcwd() .. '/target/site/jacoco/jacoco.xml'
    local report_index_html = vim.fn.getcwd() .. '/target/site/jacoco/index.html'

    local file_exists = function(path)
      local stat = vim.uv.fs_stat(path)
      return stat ~= nil
    end

    local toggle_coverage_report = function()
      if coverage == false then
        if file_exists(report_path) then
          require('blanket').start()
          coverage = true
        else
          vim.notify('jacoco.xml not found at: ' .. report_path .. '. Maybe you should run some tests :)', vim.log.levels.INFO)
        end
      else
        require('blanket').stop()
        coverage = false
      end
    end

    local open_jacoco_report = function()
      if file_exists(report_index_html) then
        vim.fn.system('xdg-open ' .. report_index_html)
      else
        vim.notify('index.html not found at: ' .. report_index_html .. '. Maybe you should run some tests :)', vim.log.levels.INFO)
      end
    end

    vim.api.nvim_create_autocmd('FileType', {
      pattern = { 'java' },
      callback = function()
        vim.keymap.set('n', '<leader>cr', toggle_coverage_report, { buffer = 0, desc = 'Toggle coverage report' })
      end,
    })

    vim.api.nvim_create_autocmd('FileType', {
      pattern = { 'java' },
      callback = function()
        vim.keymap.set('n', '<leader>jro', open_jacoco_report, { desc = 'Open jacoco report in current browser' })
      end,
    })
  end,
}

Basically, it starts up with line coverage disabled. Then it creates three functions: one to verify if Jacoco files exist (to prevent blanket.nvim from throwing errors at me), one to toggle line coverage, and one to open the Jacoco index.html report in my favorite browser. After that, I create keymaps to run these functions, and there you go. Now I only see line coverage when I need it, and if I need to analyze the coverage report, I can open it in my browser.

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