feat: add an action to add the current file to the qflist upon leaving Canola #281

Closed
opened 2026-03-30 17:05:19 +00:00 by llakala · 3 comments
llakala commented 2026-03-30 17:05:19 +00:00

Prerequisites

Problem

Currently, the only quickfix integration offered by Canola is send_to_qflist - but this sends all the files on the screen to the qflist. Sometimes, it's useful to only add one or two files to the qflist. The workaround for this is a custom search pattern, but I think that's rather silly. I'm not typing /foo.txt\|bar.txt\|baz.txt every time!

Proposed solution

An action could be added that adds the currently hovered entry to the qflist upon existing Canola. I only created this issue because I actually already have an action that does this, and thought upstreaming it would be nice:

local function add_to_qflist()
  local message = {}
  local dir = canola.get_current_dir()
  local qf_entries = {}

  table.insert(message, "Adding '")

  local function add_file(entry, index)
    if entry and entry.type == "file" then
      if index == 1 then
        table.insert(message, entry.name)
      else
        table.insert(message, ", " .. entry.name)
      end
      local qf_entry = {
        filename = dir .. entry.name,
        lnum = 1,
        col = 1,
        text = entry.name,
      }
      table.insert(qf_entries, qf_entry)
    end
  end

  if not utils.is_visual_mode() then
    local entry = canola.get_cursor_entry()
    add_file(entry, 1)
  else
    local range = utils.get_visual_range()
    if range == nil then
      return
    end
    for i = range.start_lnum, range.end_lnum do
      local entry = canola.get_entry_on_line(0, i)
      add_file(entry, i - range.start_lnum + 1)
    end
  end

  table.insert(message, "' to qflist")
  vim.print(table.concat(message))

  if #qf_entries == 0 then
    vim.notify("[canola] No entries found to send to quickfix", vim.log.levels.WARN)
    return
  end

  vim.api.nvim_exec_autocmds("QuickFixCmdPre", {})
  local qf_title = "canola files"
  vim.fn.setqflist({}, "a", { title = qf_title, items = qf_entries })

  vim.g.open_qf_on_quit = true -- I have this handled in the canola ftplugin
end

And the ftplugin contains this bit of code:

vim.api.nvim_create_autocmd("BufUnload", {
  buffer = 0,
  group = vim.api.nvim_create_augroup("OpenQuickfixIfChanged", {}),
  callback = function()
    -- I have an action that sets this, so when we exit canola, we can run it
    if vim.g.open_qf_on_quit then
      vim.cmd.copen()
      vim.g.open_qf_on_quit = false
    end
  end,
})

Before upstreaming, this needs some polish. A few things off the top of my head

  • If Canola provided a custom autocmd event for exiting the window, we could just use that rather than using the ftplugin as a workaround
  • This should take a parameter to chosoe between the qflist or the loclist (this is trivial, I just didn't need it)
  • Prettier logging than just printing Adding 'canola.txt' to qflist would be nice. Maybe there's a way to display the qflist in a buffer without switching to it, so we can see the changes in real time?

Anyways, I don't have the energy to PR a polished version of this at the moment. I thought I'd create the issue to establish the demand, and see if you're interested in getting this over the finish line.

Alternatives considered

This doesn't have to be upstreamed - I'm happy with my custom action. I just thought it would be a nice thing to offer for everyone.

### Prerequisites - [x] I have searched [existing issues](https://github.com/barrettruth/canola.nvim/issues) ### Problem Currently, the only quickfix integration offered by Canola is `send_to_qflist` - but this sends all the files on the screen to the qflist. Sometimes, it's useful to only add one or two files to the qflist. The workaround for this is a custom search pattern, but I think that's rather silly. I'm not typing `/foo.txt\|bar.txt\|baz.txt` every time! ### Proposed solution An action could be added that adds the currently hovered entry to the qflist upon existing Canola. I only created this issue because I actually already have an action that does this, and thought upstreaming it would be nice: ```lua local function add_to_qflist() local message = {} local dir = canola.get_current_dir() local qf_entries = {} table.insert(message, "Adding '") local function add_file(entry, index) if entry and entry.type == "file" then if index == 1 then table.insert(message, entry.name) else table.insert(message, ", " .. entry.name) end local qf_entry = { filename = dir .. entry.name, lnum = 1, col = 1, text = entry.name, } table.insert(qf_entries, qf_entry) end end if not utils.is_visual_mode() then local entry = canola.get_cursor_entry() add_file(entry, 1) else local range = utils.get_visual_range() if range == nil then return end for i = range.start_lnum, range.end_lnum do local entry = canola.get_entry_on_line(0, i) add_file(entry, i - range.start_lnum + 1) end end table.insert(message, "' to qflist") vim.print(table.concat(message)) if #qf_entries == 0 then vim.notify("[canola] No entries found to send to quickfix", vim.log.levels.WARN) return end vim.api.nvim_exec_autocmds("QuickFixCmdPre", {}) local qf_title = "canola files" vim.fn.setqflist({}, "a", { title = qf_title, items = qf_entries }) vim.g.open_qf_on_quit = true -- I have this handled in the canola ftplugin end ``` And the ftplugin contains this bit of code: ```lua vim.api.nvim_create_autocmd("BufUnload", { buffer = 0, group = vim.api.nvim_create_augroup("OpenQuickfixIfChanged", {}), callback = function() -- I have an action that sets this, so when we exit canola, we can run it if vim.g.open_qf_on_quit then vim.cmd.copen() vim.g.open_qf_on_quit = false end end, }) ``` Before upstreaming, this needs some polish. A few things off the top of my head - If Canola provided a custom autocmd event for exiting the window, we could just use that rather than using the ftplugin as a workaround - This should take a parameter to chosoe between the qflist or the loclist (this is trivial, I just didn't need it) - Prettier logging than just printing `Adding 'canola.txt' to qflist` would be nice. Maybe there's a way to display the qflist in a buffer without switching to it, so we can see the changes in real time? Anyways, I don't have the energy to PR a polished version of this at the moment. I thought I'd create the issue to establish the demand, and see if you're interested in getting this over the finish line. ### Alternatives considered This doesn't have to be upstreamed - I'm happy with my custom action. I just thought it would be a nice thing to offer for everyone.
barrettruth commented 2026-03-30 22:52:39 +00:00

see #284. the open qfl on leave is too-specific to your workflow to warrant that.

you can now use add_to_qfl for adding singular entries

see #284. the open qfl on leave is too-specific to your workflow to warrant that. you can now use `add_to_qfl` for adding singular entries
llakala commented 2026-03-31 01:20:06 +00:00

The open qfl on leave is too-specific to your workflow to warrant that.

Reasonable - I'll take what I can get. Thanks!

> The open qfl on leave is too-specific to your workflow to warrant that. Reasonable - I'll take what I can get. Thanks!
barrettruth commented 2026-03-31 03:56:30 +00:00

thanks for using :DDD

thanks for using :DDD
Sign in to join this conversation.
No description provided.