Skip to content

Commit

Permalink
feat: added fzf-lua integration
Browse files Browse the repository at this point in the history
  • Loading branch information
folke committed Jun 9, 2024
1 parent 4deb811 commit d14323f
Show file tree
Hide file tree
Showing 2 changed files with 179 additions and 48 deletions.
110 changes: 62 additions & 48 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ A pretty list for showing diagnostics, references, telescope results, quickfix a
- quickfix list
- location list
- [Telescope](https://github.com/nvim-telescope/telescope.nvim) search results
- [fzf-lua](https://github.com/ibhagwan/fzf-lua) results

## 📰 What's new?

Expand Down Expand Up @@ -443,7 +444,7 @@ require("trouble").first(opts)
---@return trouble.View
require("trouble").focus(opts)

-- Fold close
-- Fold close
---@param opts? trouble.Mode | { new? : boolean } | string
---@return trouble.View
require("trouble").fold_close(opts)
Expand Down Expand Up @@ -473,7 +474,7 @@ require("trouble").fold_enable(opts)
---@return trouble.View
require("trouble").fold_more(opts)

-- Fold open
-- Fold open
---@param opts? trouble.Mode | { new? : boolean } | string
---@return trouble.View
require("trouble").fold_open(opts)
Expand All @@ -493,7 +494,7 @@ require("trouble").fold_open_recursive(opts)
---@return trouble.View
require("trouble").fold_reduce(opts)

-- Fold toggle
-- Fold toggle
---@param opts? trouble.Mode | { new? : boolean } | string
---@return trouble.View
require("trouble").fold_toggle(opts)
Expand Down Expand Up @@ -618,6 +619,19 @@ telescope.setup({

When you open telescope, you can now hit `<c-t>` to open the results in **Trouble**

### fzf-lua

You can easily open any search results in **Trouble**, by defining a custom action:

```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
```

When you open telescope, you can now hit `<c-t>` to open the results in **Trouble**

### Statusline Component

Example for [lualine.nvim](https://github.com/nvim-lualine/lualine.nvim):
Expand Down Expand Up @@ -653,51 +667,51 @@ The table below shows all the highlight groups defined for Trouble.

<!-- colors:start -->

| Highlight Group | Default Group | Description |
| --- | --- | --- |
| **TroubleCount** | ***TabLineSel*** | |
| **TroubleDirectory** | ***Directory*** | |
| **TroubleFileName** | ***Directory*** | |
| **TroubleIconArray** | ***@punctuation.bracket*** | |
| **TroubleIconBoolean** | ***@boolean*** | |
| **TroubleIconClass** | ***@type*** | |
| **TroubleIconConstant** | ***@constant*** | |
| **TroubleIconConstructor** | ***@constructor*** | |
| **TroubleIconDirectory** | ***Special*** | |
| **TroubleIconEnum** | ***@lsp.type.enum*** | |
| **TroubleIconEnumMember** | ***@lsp.type.enumMember*** | |
| **TroubleIconEvent** | ***Special*** | |
| **TroubleIconField** | ***@variable.member*** | |
| **TroubleIconFile** | ***Normal*** | |
| **TroubleIconFunction** | ***@function*** | |
| **TroubleIconInterface** | ***@lsp.type.interface*** | |
| **TroubleIconKey** | ***@lsp.type.keyword*** | |
| **TroubleIconMethod** | ***@function.method*** | |
| **TroubleIconModule** | ***@module*** | |
| **TroubleIconNamespace** | ***@module*** | |
| **TroubleIconNull** | ***@constant.builtin*** | |
| **TroubleIconNumber** | ***@number*** | |
| **TroubleIconObject** | ***@constant*** | |
| **TroubleIconOperator** | ***@operator*** | |
| **TroubleIconPackage** | ***@module*** | |
| **TroubleIconProperty** | ***@property*** | |
| **TroubleIconString** | ***@string*** | |
| **TroubleIconStruct** | ***@lsp.type.struct*** | |
| **TroubleIconTypeParameter** | ***@lsp.type.typeParameter*** | |
| **TroubleIconVariable** | ***@variable*** | |
| **TroubleIndent** | ***LineNr*** | |
| **TroubleIndentFoldClosed** | ***CursorLineNr*** | |
| **TroubleIndentFoldOpen** | ***TroubleIndent*** | |
| **TroubleIndentLast** | ***TroubleIndent*** | |
| **TroubleIndentMiddle** | ***TroubleIndent*** | |
| **TroubleIndentTop** | ***TroubleIndent*** | |
| **TroubleIndentWs** | ***TroubleIndent*** | |
| **TroubleNormal** | ***NormalFloat*** | |
| **TroubleNormalNC** | ***NormalFloat*** | |
| **TroublePos** | ***LineNr*** | |
| **TroublePreview** | ***Visual*** | |
| **TroubleSource** | ***Comment*** | |
| **TroubleText** | ***Normal*** | |
| Highlight Group | Default Group | Description |
| ---------------------------- | ----------------------------- | ----------- |
| **TroubleCount** | **_TabLineSel_** | |
| **TroubleDirectory** | **_Directory_** | |
| **TroubleFileName** | **_Directory_** | |
| **TroubleIconArray** | **_@punctuation.bracket_** | |
| **TroubleIconBoolean** | **_@boolean_** | |
| **TroubleIconClass** | **_@type_** | |
| **TroubleIconConstant** | **_@constant_** | |
| **TroubleIconConstructor** | **_@constructor_** | |
| **TroubleIconDirectory** | **_Special_** | |
| **TroubleIconEnum** | **_@lsp.type.enum_** | |
| **TroubleIconEnumMember** | **_@lsp.type.enumMember_** | |
| **TroubleIconEvent** | **_Special_** | |
| **TroubleIconField** | **_@variable.member_** | |
| **TroubleIconFile** | **_Normal_** | |
| **TroubleIconFunction** | **_@function_** | |
| **TroubleIconInterface** | **_@lsp.type.interface_** | |
| **TroubleIconKey** | **_@lsp.type.keyword_** | |
| **TroubleIconMethod** | **_@function.method_** | |
| **TroubleIconModule** | **_@module_** | |
| **TroubleIconNamespace** | **_@module_** | |
| **TroubleIconNull** | **_@constant.builtin_** | |
| **TroubleIconNumber** | **_@number_** | |
| **TroubleIconObject** | **_@constant_** | |
| **TroubleIconOperator** | **_@operator_** | |
| **TroubleIconPackage** | **_@module_** | |
| **TroubleIconProperty** | **_@property_** | |
| **TroubleIconString** | **_@string_** | |
| **TroubleIconStruct** | **_@lsp.type.struct_** | |
| **TroubleIconTypeParameter** | **_@lsp.type.typeParameter_** | |
| **TroubleIconVariable** | **_@variable_** | |
| **TroubleIndent** | **_LineNr_** | |
| **TroubleIndentFoldClosed** | **_CursorLineNr_** | |
| **TroubleIndentFoldOpen** | **_TroubleIndent_** | |
| **TroubleIndentLast** | **_TroubleIndent_** | |
| **TroubleIndentMiddle** | **_TroubleIndent_** | |
| **TroubleIndentTop** | **_TroubleIndent_** | |
| **TroubleIndentWs** | **_TroubleIndent_** | |
| **TroubleNormal** | **_NormalFloat_** | |
| **TroubleNormalNC** | **_NormalFloat_** | |
| **TroublePos** | **_LineNr_** | |
| **TroublePreview** | **_Visual_** | |
| **TroubleSource** | **_Comment_** | |
| **TroubleText** | **_Normal_** | |

<!-- colors:end -->

Expand Down
117 changes: 117 additions & 0 deletions lua/trouble/sources/fzf.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
---@diagnostic disable: inject-field
local Item = require("trouble.item")

---Represents an item in a Neovim quickfix/loclist.
---@class fzf.Item
---@field stripped string the fzf item without any highlighting.
---@field bufnr? number The buffer number of the item.
---@field bufname? string
---@field terminal? boolean
---@field path string
---@field uri? string
---@field line number 1-indexed line number
---@field col number 1-indexed column number

---@class fzf.Opts

---@class trouble.Source.fzf: trouble.Source
local M = {}

---@type trouble.Item[]
M.items = {}

M.config = {
modes = {
fzf = {
desc = "FzfLua results previously opened with `require('trouble.sources.fzf').open()`.",
source = "fzf",
groups = {
{ "cmd", format = "{hl:Title}fzf{hl} {cmd:Comment} {count}" },
{ "filename", format = "{file_icon} {filename} {count}" },
},
sort = { "filename", "pos" },
format = "{text:ts} {pos}",
},
fzf_files = {
desc = "FzfLua results previously opened with `require('trouble.sources.fzf').open()`.",
source = "fzf",
groups = {
{ "cmd", format = "{hl:Title}fzf{hl} {cmd:Comment} {count}" },
},
sort = { "filename", "pos" },
format = "{file_icon} {filename}",
},
},
}

---@param item fzf.Item
function M.item(item)
item.text = item.stripped:match(":%d+:%d?%d?%d?%d?:?(.*)$")
local word = item.text and item.text:sub(item.col):match("%S+")
return Item.new({
source = "fzf",
buf = item.bufnr,
filename = item.bufname or item.path or item.uri,
pos = { item.line, item.col - 1 },
end_pos = word and { item.line, item.col - 1 + #word } or nil,
item = item,
})
end

---@param cb trouble.Source.Callback
---@param _ctx trouble.Source.ctx)
function M.get(cb, _ctx)
cb(M.items)
end

-- Returns the mode based on the items.
function M.mode()
for _, item in ipairs(M.items) do
if item.text then
return "fzf"
end
end
return "fzf_files"
end

-- Append the current fzf buffer to the trouble list.
---@param selected string[]
---@param fzf_opts fzf.Opts
---@param opts? trouble.Mode|string
function M.add(selected, fzf_opts, opts)
local cmd = fzf_opts.__INFO.cmd
local path = require("fzf-lua.path")
for _, line in ipairs(selected) do
local item = M.item(path.entry_to_file(line, fzf_opts))
item.item.cmd = cmd
table.insert(M.items, item)
end

vim.schedule(function()
opts = opts or {}
if type(opts) == "string" then
opts = { mode = opts }
end
opts = vim.tbl_extend("force", { mode = M.mode() }, opts)
require("trouble").open(opts)
end)
end

-- Opens the current fzf buffer in the trouble list.
-- This will clear the existing items.
---@param selected string[]
---@param fzf_opts fzf.Opts
---@param opts? trouble.Mode|string
function M.open(selected, fzf_opts, opts)
M.items = {}
M.add(selected, fzf_opts, opts)
end

M.actions = {
open = M.open,
open_all = { fn = M.open, prefix = "select-all" },
add = M.add,
add_all = { fn = M.add, prefix = "select-all" },
}

return M

0 comments on commit d14323f

Please sign in to comment.