Skip to content

Commit 4805658

Browse files
committed
feat(recipes): add diagnostics configuration recipes page
1 parent 33ec834 commit 4805658

File tree

1 file changed

+144
-0
lines changed

1 file changed

+144
-0
lines changed
+144
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
---
2+
id: diagnostics
3+
title: Diagnostics Customizations
4+
---
5+
6+
Neovim has a powerful internal diagnostics system with many ways to render the diagnostics in a document. AstroNvim provides a nice interface to ease the configuration of diagnostics through the AstroCore plugin.
7+
8+
```lua title="lua/plugins/diagnostics.lua"
9+
return {
10+
"AstroNvim/astrocore",
11+
---@type AstroCoreOpts
12+
opts = {
13+
features = {
14+
-- toggle if diagnostics are enabled on startup
15+
diagnostics = true,
16+
},
17+
-- Configuration passed to `vim.diagnostic.config()`
18+
-- All available options can be found with `:h vim.diagnostic.Opts`
19+
diagnostics = {
20+
virtual_text = true,
21+
virtual_lines = false, -- Neovim v0.11+ only
22+
update_in_insert = false,
23+
underline = true,
24+
severity_sort = true,
25+
},
26+
},
27+
}
28+
```
29+
30+
### Using Both Virtual Lines and Virtual Text
31+
32+
AstroCore provides an option in the `features` table to allow controlling if diagnostic options are enabled on startup which allows for the user to configure their options for both `virtual_text` and `virtual_lines` and toggle between them at runtime using `<Leader>uv` and `<Leader>uV` respectively.
33+
34+
```lua title="lua/plugins/diagnostics.lua"
35+
return {
36+
"AstroNvim/astrocore",
37+
---@type AstroCoreOpts
38+
opts = {
39+
features = {
40+
-- toggle if diagnostics are enabled on startup
41+
diagnostics = {
42+
virtual_text = true,
43+
virtual_lines = false, -- disable one option on startup
44+
},
45+
},
46+
-- Configuration passed to `vim.diagnostic.config()`
47+
-- All available options can be found with `:h vim.diagnostic.Opts`
48+
diagnostics = {
49+
virtual_text = true,
50+
virtual_lines = true, -- Neovim v0.11+ only
51+
update_in_insert = false,
52+
underline = true,
53+
severity_sort = true,
54+
},
55+
},
56+
}
57+
```
58+
59+
### Virtual Line Current Line Only
60+
61+
:::tip
62+
63+
This is available in the [AstroCommunity](https://github.com/AstroNvim/astrocommunity/tree/main/lua/astrocommunity/recipes/diagnostic-virtual-lines-current-line)
64+
65+
```lua title="lua/community.lua" ins={3}
66+
return {
67+
"AstroNvim/astrocommunity",
68+
{ import = "astrocommunity.recipes.diagnostic-virtual-lines-current-line" },
69+
}
70+
```
71+
72+
:::
73+
74+
:::note
75+
76+
This recipe is based on the great [reddit post by @marjrohn](https://www.reddit.com/r/neovim/comments/1jpbc7s/disable_virtual_text_if_there_is_diagnostic_in/)
77+
78+
:::
79+
80+
Some users may like the new `virtual_lines` feature in Neovim 0.11+ but find it too intrusive showing all the time. A good work around for this is to show virtual text normally, but then show the virtual lines only on the current line if there is a diagnostic.
81+
82+
```lua title="lua/plugins/diagnostics.lua"
83+
local og_virt_text
84+
local og_virt_line
85+
return {
86+
"AstroNvim/astrocore",
87+
---@type AstroCoreOpts
88+
opts = {
89+
features = {
90+
diagnostics = true,
91+
},
92+
diagnostics = {
93+
virtual_text = true,
94+
virtual_lines = { current_line = true },
95+
underline = true,
96+
update_in_insert = false,
97+
},
98+
autocmds = {
99+
diagnostic_only_virtlines = {
100+
{
101+
event = { "CursorMoved", "DiagnosticChanged" },
102+
callback = function()
103+
if not require("astrocore.buffer").is_valid() then
104+
return
105+
end
106+
if og_virt_line == nil then
107+
og_virt_line = vim.diagnostic.config().virtual_lines
108+
end
109+
110+
-- ignore if virtual_lines.current_line is disabled
111+
if not (og_virt_line and og_virt_line.current_line) then
112+
if og_virt_text then
113+
vim.diagnostic.config({ virtual_text = og_virt_text })
114+
og_virt_text = nil
115+
end
116+
return
117+
end
118+
119+
if og_virt_text == nil then
120+
og_virt_text = vim.diagnostic.config().virtual_text
121+
end
122+
123+
local lnum = vim.api.nvim_win_get_cursor(0)[1] - 1
124+
125+
if vim.tbl_isempty(vim.diagnostic.get(0, { lnum = lnum })) then
126+
vim.diagnostic.config({ virtual_text = og_virt_text })
127+
else
128+
vim.diagnostic.config({ virtual_text = false })
129+
end
130+
end,
131+
},
132+
{
133+
event = "ModeChanged",
134+
callback = function()
135+
if require("astrocore.buffer").is_valid() then
136+
pcall(vim.diagnostic.show)
137+
end
138+
end,
139+
},
140+
},
141+
},
142+
},
143+
}
144+
```

0 commit comments

Comments
 (0)