Skip to content

Commit

Permalink
feat: Telescope support
Browse files Browse the repository at this point in the history
  • Loading branch information
folke committed May 3, 2021
1 parent 4c5fd8a commit 9c81e16
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 4 deletions.
25 changes: 24 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

# 🚦 Trouble

A pretty diagnostics, references, quickfix and location list to help you solve all the trouble your code is causing.
A pretty diagnostics, references, telescope and quickfix and location list to help you solve all the trouble your code is causing.

![LSP Trouble Screenshot](./media/shot.png)

Expand All @@ -12,6 +12,7 @@ A pretty diagnostics, references, quickfix and location list to help you solve a
- LSP references
- quickfix list
- location list
- Telescope search results
* automatically updates on new diagnostics
* toggle **diagnostics** mode between **workspace** or **document**
* **interactive preview** in your last accessed window
Expand Down Expand Up @@ -174,6 +175,28 @@ vim.api.nvim_set_keymap("n", "gR", "<cmd>LspTrouble lsp_references<cr>",
)
```

### Telescope

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

```lua
local actions = require("telescope.actions")
local trouble = require("trouble.providers.telescope")

local telescope = require("telescope")

telescope.setup {
defaults = {
mappings = {
i = { ["<c-t>"] = trouble.open_with_trouble },
n = { ["<c-t>"] = trouble.open_with_trouble },
},
},
}
```

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

## 🎨 Colors

The table below shows all the highlight groups defined for LSP Trouble with their default link.
Expand Down
8 changes: 5 additions & 3 deletions lua/trouble/providers/init.lua
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
local util = require("trouble.util")
local qf = require("trouble.providers.qf")
local M = {}

local telescope = require("trouble.providers.telescope")
local lsp = require("trouble.providers.lsp")

local M = {}

M.providers = {
lsp_workspace_diagnostics = lsp.diagnostics,
lsp_document_diagnostics = lsp.diagnostics,
lsp_references = lsp.references,
quickfix = qf.qflist,
loclist = qf.loclist
loclist = qf.loclist,
telescope = telescope.telescope
}

---@param options Options
Expand Down
41 changes: 41 additions & 0 deletions lua/trouble/providers/telescope.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
local M = {}

M.results = {}

function M.open_with_trouble(prompt_bufnr, mode)
local action_state = require('telescope.actions.state')
local actions = require('telescope.actions')
local util = require("trouble.util")
local picker = action_state.get_current_picker(prompt_bufnr)
local manager = picker.manager

M.results = {}
for item in manager:iter() do
local row = (item.lnum or 1) - 1
local col = (item.col or 1) - 1

if not item.bufnr then
item.bufnr = vim.fn.bufnr(item.filename, true)
end

local pitem = {
row = row,
col = col,
message = item.text,
severity = 0,
range = {
start = {line = row, character = col},
["end"] = {line = row, character = -1}
}
}

table.insert(M.results, util.process_item(pitem, item.bufnr))
end

actions.close(prompt_bufnr)
require("trouble").open({mode = "telescope"})
end

function M.telescope(win, buf, cb, options) cb(M.results) end

return M

0 comments on commit 9c81e16

Please sign in to comment.