Skip to content

Commit

Permalink
feat: lsp references, loclist and quickfix lists!
Browse files Browse the repository at this point in the history
  • Loading branch information
folke committed May 3, 2021
1 parent d965d22 commit 0b852c8
Show file tree
Hide file tree
Showing 13 changed files with 403 additions and 199 deletions.
13 changes: 13 additions & 0 deletions .lua-format
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# https://github.com/Koihik/LuaFormatter/blob/master/docs/Style-Config.md
#column_limit: 100
#indent_width: 4
#continuation_indent_width: 4
#use_tab: false
#chop_down_parameter: true
#chop_down_table: true
#chop_down_kv_table: true
#single_quote_to_double_quote: true
#spaces_inside_table_braces: true
#align_parameter: true
#keep_simple_control_block_one_line: true
#extra_sep_at_table_end: true
60 changes: 41 additions & 19 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@

# 🚦 LSP Trouble
# 🚦 Trouble

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

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

## ✨ Features

* pretty list of LSP Diagnostics
* pretty list of:
- LSP Diagnostics
- LSP references
- quickfix list
- location list
* automatically updates on new diagnostics
* toggle mode between **workspace** or **document**
* toggle **diagnostics** mode between **workspace** or **document**
* **interactive preview** in your last accessed window
* *cancel* preview or *jump* to the location
* configurable actions, signs, highlights,...
Expand Down Expand Up @@ -69,8 +73,8 @@ Trouble comes with the following defaults:
```lua
{
height = 10, -- height of the trouble list
icons = true, -- use dev-icons for filenames
mode = "workspace", -- "workspace" or "document"
icons = true, -- use devicons for filenames
mode = "lsp_workspace_diagnostics", -- "lsp_workspace_diagnostics", "lsp_document_diagnostics", "quickfix", "lsp_references", "loclist"
fold_open = "", -- icon used for open folds
fold_closed = "", -- icon used for closed folds
action_keys = { -- key mappings for actions in the trouble list
Expand All @@ -79,9 +83,9 @@ Trouble comes with the following defaults:
refresh = "r", -- manually refresh
jump = {"<cr>", "<tab>"}, -- jump to the diagnostic or open / close folds
jump_close = {"o"}, -- jump to the diagnostic and close the list
hover = "K", -- opens a small poup with the full multiline message
toggle_mode = "m", -- toggle between "workspace" and "document" mode
toggle_mode = "m", -- toggle between "workspace" and "document" diagnostics mode
toggle_preview = "P", -- toggle auto_preview
hover = "K", -- opens a small poup with the full multiline message
preview = "p", -- preview the diagnostic location
close_folds = {"zM", "zm"}, -- close all folds
open_folds = {"zR", "zr"}, -- open all folds
Expand All @@ -92,14 +96,15 @@ Trouble comes with the following defaults:
indent_lines = true, -- add an indent guide below the fold icons
auto_open = false, -- automatically open the list when you have diagnostics
auto_close = false, -- automatically close the list when you have no diagnostics
auto_preview = true, -- automatically preview the location of the diagnostic. <esc> to close preview and go back
auto_preview = true, -- automatyically preview the location of the diagnostic. <esc> to close preview and go back to last window
auto_fold = false, -- automatically fold a file trouble list at creation
signs = {
-- icons / text used for a diagnostic
error = "",
warning = "",
hint = "",
information = ""
information = "",
other = ""
},
use_lsp_diagnostic_signs = false -- enabling this will use the signs defined in your lsp client
}
Expand Down Expand Up @@ -130,27 +135,43 @@ Trouble comes with the following defaults:

Trouble comes with the following commands:

* **LspTroubleOpen**: open the list
* **LspTroubleWorkspaceOpen**: set mode to "workspace" and open the list
* **LspTroubleDocumentOpen**: set mode to "document" and open the list
* **LspTroubleClose**: close the list
* **LspTroubleToggle**: toggle the list
* **LspTroubleWorkspaceToggle**: set mode to "workspace" and toggle the list (remains open if mode changes)
* **LspTroubleDocumentToggle**: set mode to "document" and toggle the list (remains open if mode changes)
* **LspTroubleRefresh**: manually refresh
* `LspTrouble [provider]`: open the list
* `LspTroubleClose [provider]`: close the list
* `LspTroubleToggle [provider]`: toggle the list
* `LspTroubleRefresh`: manually refresh the active list

Example keybinding of `<leader>xx` that toggles the trouble list:
Example keybindings:

```vim
-- Vim Script
nnoremap <leader>xx <cmd>LspTroubleToggle<cr>
nnoremap <leader>xw <cmd>LspTroubleToggle lsp_workspace_diagnostics<cr>
nnoremap <leader>xd <cmd>LspTroubleToggle lsp_document_diagnostics<cr>
nnoremap <leader>xq <cmd>LspTroubleToggle quickfix<cr>
nnoremap <leader>xl <cmd>LspTroubleToggle loclist<cr>
nnoremap gR <cmd>LspTroubleToggle lsp_references<cr>
```

```lua
-- Lua
vim.api.nvim_set_keymap("n", "<leader>xx", "<cmd>LspTroubleToggle<cr>",
{silent = true, noremap = true}
)
vim.api.nvim_set_keymap("n", "<leader>xw", "<cmd>LspTroubleToggle lsp_workspace_diagnostics<cr>",
{silent = true, noremap = true}
)
vim.api.nvim_set_keymap("n", "<leader>xd", "<cmd>LspTroubleToggle lsp_document_diagnostics<cr>",
{silent = true, noremap = true}
)
vim.api.nvim_set_keymap("n", "<leader>xl", "<cmd>LspTroubleToggle loclist<cr>",
{silent = true, noremap = true}
)
vim.api.nvim_set_keymap("n", "<leader>xq", "<cmd>LspTroubleToggle quickfix<cr>",
{silent = true, noremap = true}
)
vim.api.nvim_set_keymap("n", "gR", "<cmd>LspTrouble lsp_references<cr>",
{silent = true, noremap = true}
)
```

## 🎨 Colors
Expand All @@ -172,6 +193,7 @@ The table below shows all the highlight groups defined for LSP Trouble with thei
| *LspTroubleIndent* | LineNr |
| *LspTroubleSource* | Comment |
| *LspTroubleSignHint* | LspDiagnosticsSignHint |
| *LspTroubleSignOther* | LspTroubleSignInformation |
| *LspTroubleFoldIcon* | CursorLineNr |
| *LspTroubleTextWarning* | LspTroubleText |
| *LspTroubleCode* | Comment |
Expand Down
3 changes: 2 additions & 1 deletion lua/trouble/colors.lua
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ local links = {
Normal = "Normal",
Count = "TabLineSel",
Preview = "Search",
Indent = "LineNr"
Indent = "LineNr",
SignOther = "LspTroubleSignInformation"
}

function M.setup()
Expand Down
20 changes: 16 additions & 4 deletions lua/trouble/config.lua
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
local M = {}

M.namespace = vim.api.nvim_create_namespace('LspTrouble')
M.namespace = vim.api.nvim_create_namespace("LspTrouble")

---@class Options
---@field buf number|nil
---@field win number|nil
local defaults = {
debug = true,
debug = false,
height = 10, -- height of the trouble list
icons = true, -- use devicons for filenames
mode = "workspace", -- "workspace" or "document"
mode = "lsp_workspace_diagnostics", -- "lsp_workspace_diagnostics", "lsp_document_diagnostics", "quickfix", "lsp_references", "loclist"
fold_open = "", -- icon used for open folds
fold_closed = "", -- icon used for closed folds
action_keys = { -- key mappings for actions in the trouble list
Expand Down Expand Up @@ -36,7 +38,8 @@ local defaults = {
error = "",
warning = "",
hint = "",
information = ""
information = "",
other = ""
},
use_lsp_diagnostic_signs = false -- enabling this will use the signs defined in your lsp client
}
Expand All @@ -47,6 +50,15 @@ M.options = {}
---@return Options
function M.setup(options)
M.options = vim.tbl_deep_extend("force", {}, defaults, options or {})
M.fix_mode(M.options)
end

function M.fix_mode(opts)
if opts.mode == "workspace" then
opts.mode = "lsp_workspace_diagnostics"
elseif opts.mode == "document" then
opts.mode = "lsp_document_diagnostics"
end
end

M.setup()
Expand Down
56 changes: 42 additions & 14 deletions lua/trouble/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ local View = require("trouble.view")
local config = require("trouble.config")
local colors = require("trouble.colors")
local util = require("trouble.util")
local lsp = require("trouble.lsp")

colors.setup()

Expand All @@ -19,24 +18,32 @@ end

function Trouble.close() if is_open() then view:close() end end

function Trouble.open(opts)
local function get_opts(opts)
opts = opts or {}
if type(opts) == "string" then opts = {mode = opts} end
config.fix_mode(opts)
return opts
end

function Trouble.open(opts)
opts = get_opts(opts)
if opts.mode and (opts.mode ~= config.options.mode) then
Trouble.action("toggle_mode")
config.options.mode = opts.mode
end
opts.focus = true

if is_open() then
view:focus()
Trouble.refresh(opts)
else
view = View.create(opts)
end
end

function Trouble.toggle(opts)
opts = opts or {}
opts = get_opts(opts)

if opts.mode and (opts.mode ~= config.options.mode) then
Trouble.action("toggle_mode")
config.options.mode = opts.mode
Trouble.open()
return
end
Expand Down Expand Up @@ -71,25 +78,46 @@ local updater = util.debounce(100, function()
end)

function Trouble.refresh(opts)
opts = opts or {}

-- dont do an update if this is an automated refresh from a different provider
if opts.auto then
if opts.provider == "diagnostics" and config.options.mode ==
"lsp_document_diagnostics" then
opts.provider = "lsp_document_diagnostics"
elseif opts.provider == "diagnostics" and config.options.mode ==
"lsp_workspace_diagnostics" then
opts.provider = "lsp_workspace_diagnostics"
elseif opts.provider == "qf" and config.options.mode == "quickfix" then
opts.provider = "quickfix"
elseif opts.provider == "qf" and config.options.mode == "loclist" then
opts.provider = "loclist"
end
if opts.provider ~= config.options.mode then return end
end

if is_open() then
if opts and opts.auto then
if opts.auto then
updater()
else
util.debug("refresh")
view:update(opts)
end
elseif opts.auto and config.options.auto_open then
local count = util.count(lsp.diagnostics())
if count > 0 then Trouble.open(opts) end
elseif opts.auto and config.options.auto_open and opts.mode ==
config.options.mode then
local items = require("trouble.providers").get(
vim.api.nvim_get_current_win(),
vim.api.nvim_get_current_buf(), config.options)
if #items > 0 then Trouble.open(opts) end
end
end

function Trouble.action(action)
if action == "toggle_mode" then
if config.options.mode == "document" then
config.options.mode = "workspace"
else
config.options.mode = "document"
if config.options.mode == "lsp_document_diagnostics" then
config.options.mode = "lsp_workspace_diagnostics"
elseif config.options.mode == "lsp_workspace_diagnostics" then
config.options.mode = "lsp_document_diagnostics"
end
action = "refresh"
end
Expand Down
96 changes: 0 additions & 96 deletions lua/trouble/lsp.lua

This file was deleted.

Loading

0 comments on commit 0b852c8

Please sign in to comment.