Skip to content

Commit

Permalink
feat(fzf-lua): added smart open/add that will use selection or all wh…
Browse files Browse the repository at this point in the history
…en nothing selected.
  • Loading branch information
folke committed Jun 10, 2024
1 parent 75fad30 commit bed3c5b
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 4 deletions.
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -628,8 +628,7 @@ You can easily open any search results in **Trouble**, by defining a custom acti
```lua
local config = require("fzf-lua.config")
local actions = require("trouble.sources.fzf").actions
config.defaults.actions.files["ctrl-t"] = actions.open_all
config.defaults.actions.files["alt-t"] = actions.open
config.defaults.actions.files["ctrl-t"] = actions.open
```

When you open telescope, you can now hit `<c-t>` to open the results in **Trouble**
Expand Down
15 changes: 13 additions & 2 deletions lua/trouble/sources/fzf.lua
Original file line number Diff line number Diff line change
Expand Up @@ -107,10 +107,21 @@ function M.open(selected, fzf_opts, opts)
M.add(selected, fzf_opts, opts)
end

local smart_prefix = require("trouble.util").is_win() and "IF %FZF_SELECT_COUNT% LEQ 0 (echo select-all)"

This comment has been minimized.

Copy link
@ibhagwan

ibhagwan Jun 10, 2024

@folke, note the correct windows condition should be also wrapped in transform:

"transform(IF %FZF_SELECT_COUNT% LEQ 0 (echo select-all))"

This comment has been minimized.

Copy link
@folke

folke Jun 10, 2024

Author Owner

Fixed, thanks!

or "transform([ $FZF_SELECT_COUNT -eq 0 ] && echo select-all)"

M.actions = {
open = M.open,
-- Open selected or all items in the trouble list.
open = { fn = M.open, prefix = smart_prefix },
-- Open selected items in the trouble list.
open_selected = M.open,
-- Open all items in the trouble list.
open_all = { fn = M.open, prefix = "select-all" },
add = M.add,
-- Add selected or all items to the trouble list.
add = { fn = M.add, prefix = smart_prefix },
-- Add selected items to the trouble list.
add_selected = M.add,
-- Add all items to the trouble list.
add_all = { fn = M.add, prefix = "select-all" },
}

Expand Down
4 changes: 4 additions & 0 deletions lua/trouble/util.lua
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ function M.noautocmd(fn)
vim.o.eventignore = ei
end

function M.is_win()
return vim.uv.os_uname().sysname:find("Windows") ~= nil
end

---@param opts? {msg?: string}
function M.try(fn, opts)
local ok, err = pcall(fn)
Expand Down

0 comments on commit bed3c5b

Please sign in to comment.