Skip to content

Commit

Permalink
feat(open_cmd): add open_cmd option to toggle() and open() APIs
Browse files Browse the repository at this point in the history
With this PR, now one could do:

```lua
require("quicker").toggle({ open_cmd = "botright copen" })
require("quicker").open({ open_cmd = "botright copen" })
```

An alternative to this PR is using the following autocmd.

```lua
local init = function()
  vim.api.nvim_create_autocmd("FileType", {
    desc = "Open quickfix list globally",
    group = vim.api.nvim_create_augroup("quickfix_toggle", { clear = true }),
    pattern = "qf",
    command = "wincmd J",
  })
end
```
  • Loading branch information
serranomorante committed Sep 11, 2024
1 parent 308088e commit 607c44d
Showing 1 changed file with 7 additions and 4 deletions.
11 changes: 7 additions & 4 deletions lua/quicker/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -102,16 +102,18 @@ end
---@field height? integer Height of the quickfix window when opened. Defaults to number of items in the list.
---@field min_height? integer Minimum height of the quickfix window. Default 4.
---@field max_height? integer Maximum height of the quickfix window. Default 10.
---@field open_cmd? nil|string Custom command to open quickfix or loclist windows.

---Toggle the quickfix or loclist window.
---@param opts? quicker.OpenOpts
M.toggle = function(opts)
---@type {loclist: boolean, focus: boolean, height?: integer, min_height: integer, max_height: integer}
---@type {loclist: boolean, focus: boolean, height?: integer, min_height: integer, max_height: integer, open_cmd: nil|string}
opts = vim.tbl_deep_extend("keep", opts or {}, {
loclist = false,
focus = false,
min_height = 4,
max_height = 10,
open_cmd = nil,
})
local loclist_win = opts.loclist and 0 or nil
if M.is_open(loclist_win) then
Expand All @@ -124,23 +126,24 @@ end
---Open the quickfix or loclist window.
---@param opts? quicker.OpenOpts
M.open = function(opts)
---@type {loclist: boolean, focus: boolean, height?: integer, min_height: integer, max_height: integer}
---@type {loclist: boolean, focus: boolean, height?: integer, min_height: integer, max_height: integer, open_cmd: nil|string}
opts = vim.tbl_deep_extend("keep", opts or {}, {
loclist = false,
focus = false,
min_height = 4,
max_height = 10,
open_cmd = nil,
})
local height
if opts.loclist then
local ok, err = pcall(vim.cmd.lopen)
local ok, err = pcall(vim.cmd[opts.open_cmd or "lopen"])
if not ok then
vim.notify(err, vim.log.levels.ERROR)
return
end
height = #vim.fn.getloclist(0)
else
vim.cmd.copen()
vim.cmd(opts.open_cmd or "copen")
height = #vim.fn.getqflist()
end

Expand Down

0 comments on commit 607c44d

Please sign in to comment.