Skip to content

Commit

Permalink
fix(telescope): autmatically select telescope_files mode if list are …
Browse files Browse the repository at this point in the history
…files without locations. Fixes #466
  • Loading branch information
folke committed Jun 9, 2024
1 parent 806c504 commit 1ad6b14
Showing 1 changed file with 20 additions and 3 deletions.
23 changes: 20 additions & 3 deletions lua/trouble/sources/telescope.lua
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,21 @@ M.config = {
modes = {
telescope = {
desc = "Telescope results previously opened with `require('trouble.sources.telescope').open()`.",
-- events = { "BufEnter", "QuickFixCmdPost" },
source = "telescope",
title = "{hl:Title}Telescope{hl} {count}",
groups = {
{ "filename", format = "{file_icon} {filename} {count}" },
},
sort = { "filename", "pos" },
format = "{text:ts} {pos}",
},
telescope_files = {
desc = "Telescope results previously opened with `require('trouble.sources.telescope').open()`.",
source = "telescope",
title = "{hl:Title}Telescope{hl} {count}",
sort = { "filename", "pos" },
format = "{file_icon} {filename}",
},
},
}

Expand All @@ -52,6 +59,16 @@ 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 "telescope"
end
end
return "telescope_files"
end

-- Append the current telescope buffer to the trouble list.
---@param opts? trouble.Mode|string
function M.add(prompt_bufnr, opts)
Expand All @@ -71,15 +88,15 @@ function M.add(prompt_bufnr, opts)
table.insert(M.items, M.item(item))
end
end
Item.add_text(M.items, { mode = "after" })
-- Item.add_text(M.items, { mode = "after" })

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

0 comments on commit 1ad6b14

Please sign in to comment.