diff options
author | Gregory Anders <greg@gpanders.com> | 2021-11-15 08:56:10 -0700 |
---|---|---|
committer | Gregory Anders <greg@gpanders.com> | 2021-11-15 09:12:27 -0700 |
commit | 63413bd0478ebbb2aa5fc90b2533a04d8b785bbc (patch) | |
tree | 928fbbd8e1979078da4986ce278be77b2e01e4a4 | |
parent | cc488376221e3792a2c69d1507bdfe405eaebc73 (diff) | |
download | rneovim-63413bd0478ebbb2aa5fc90b2533a04d8b785bbc.tar.gz rneovim-63413bd0478ebbb2aa5fc90b2533a04d8b785bbc.tar.bz2 rneovim-63413bd0478ebbb2aa5fc90b2533a04d8b785bbc.zip |
refactor(diagnostic)!: rename 'show_header' to 'header'
Rename the `show_header` option in `open_float` to simply `header` and
allow users to specify both the header string as well as the highlight
group.
-rw-r--r-- | runtime/doc/diagnostic.txt | 15 | ||||
-rw-r--r-- | runtime/lua/vim/diagnostic.lua | 28 | ||||
-rw-r--r-- | test/functional/lua/diagnostic_spec.lua | 76 |
3 files changed, 88 insertions, 31 deletions
diff --git a/runtime/doc/diagnostic.txt b/runtime/doc/diagnostic.txt index efcc90703f..6ada27edd0 100644 --- a/runtime/doc/diagnostic.txt +++ b/runtime/doc/diagnostic.txt @@ -375,8 +375,11 @@ config({opts}, {namespace}) *vim.diagnostic.config()* • float: Options for floating windows: • severity: See |diagnostic-severity|. - • show_header: (boolean, default true) Show - "Diagnostics:" header + • header: (string or table) String to use + as the header for the floating window. If + a table, it is interpreted as a [text, + hl_group] tuple. Defaults to + "Diagnostics:". • source: (string) Include the diagnostic source in the message. One of "always" or "if_many". @@ -620,9 +623,11 @@ open_float({bufnr}, {opts}) *vim.diagnostic.open_float()* |vim.diagnostic.config()|. • severity: See |diagnostic-severity|. Overrides the setting from |vim.diagnostic.config()|. - • show_header: (boolean, default true) Show - "Diagnostics:" header. Overrides the setting - from |vim.diagnostic.config()|. + • header: (string or table) String to use as the + header for the floating window. If a table, it + is interpreted as a [text, hl_group] tuple. + Overrides the setting from + |vim.diagnostic.config()|. • source: (string) Include the diagnostic source in the message. One of "always" or "if_many". Overrides the setting from diff --git a/runtime/lua/vim/diagnostic.lua b/runtime/lua/vim/diagnostic.lua index c5cb08fb2d..80efa20f27 100644 --- a/runtime/lua/vim/diagnostic.lua +++ b/runtime/lua/vim/diagnostic.lua @@ -534,7 +534,9 @@ end --- its severity. Otherwise, all signs use the same priority. --- - float: Options for floating windows: --- * severity: See |diagnostic-severity|. ---- * show_header: (boolean, default true) Show "Diagnostics:" header +--- * header: (string or table) String to use as the header for the floating +--- window. If a table, it is interpreted as a [text, hl_group] tuple. +--- Defaults to "Diagnostics:". --- * source: (string) Include the diagnostic source in --- the message. One of "always" or "if_many". --- * format: (function) A function that takes a diagnostic as input and returns a @@ -1146,8 +1148,9 @@ end --- from |vim.diagnostic.config()|. --- - severity: See |diagnostic-severity|. Overrides the setting from --- |vim.diagnostic.config()|. ---- - show_header: (boolean, default true) Show "Diagnostics:" header. Overrides the ---- setting from |vim.diagnostic.config()|. +--- - header: (string or table) String to use as the header for the floating window. If a +--- table, it is interpreted as a [text, hl_group] tuple. Overrides the setting +--- from |vim.diagnostic.config()|. --- - source: (string) Include the diagnostic source in the message. One of "always" or --- "if_many". Overrides the setting from |vim.diagnostic.config()|. --- - format: (function) A function that takes a diagnostic as input and returns a @@ -1225,10 +1228,21 @@ function M.open_float(bufnr, opts) local lines = {} local highlights = {} - local show_header = vim.F.if_nil(opts.show_header, true) - if show_header then - table.insert(lines, "Diagnostics:") - table.insert(highlights, {0, "Bold"}) + local header = if_nil(opts.header, "Diagnostics:") + if header then + vim.validate { header = { header, function(v) + return type(v) == "string" or type(v) == "table" + end, "'string' or 'table'" } } + if type(header) == "table" then + -- Don't insert any lines for an empty string + if string.len(if_nil(header[1], "")) > 0 then + table.insert(lines, header[1]) + table.insert(highlights, {0, header[2] or "Bold"}) + end + elseif #header > 0 then + table.insert(lines, header) + table.insert(highlights, {0, "Bold"}) + end end if opts.format then diff --git a/test/functional/lua/diagnostic_spec.lua b/test/functional/lua/diagnostic_spec.lua index 0ac831ab7f..bd513208e8 100644 --- a/test/functional/lua/diagnostic_spec.lua +++ b/test/functional/lua/diagnostic_spec.lua @@ -1032,6 +1032,44 @@ describe('vim.diagnostic', function() end) describe('open_float()', function() + it('can display a header', function() + eq({'Diagnostics:', '1. Syntax error'}, exec_lua [[ + local diagnostics = { + make_error("Syntax error", 0, 1, 0, 3), + } + vim.api.nvim_win_set_buf(0, diagnostic_bufnr) + vim.diagnostic.set(diagnostic_ns, diagnostic_bufnr, diagnostics) + local float_bufnr, winnr = vim.diagnostic.open_float() + local lines = vim.api.nvim_buf_get_lines(float_bufnr, 0, -1, false) + vim.api.nvim_win_close(winnr, true) + return lines + ]]) + + eq({"We're no strangers to love...", '1. Syntax error'}, exec_lua [[ + local diagnostics = { + make_error("Syntax error", 0, 1, 0, 3), + } + vim.api.nvim_win_set_buf(0, diagnostic_bufnr) + vim.diagnostic.set(diagnostic_ns, diagnostic_bufnr, diagnostics) + local float_bufnr, winnr = vim.diagnostic.open_float(0, {header = "We're no strangers to love..."}) + local lines = vim.api.nvim_buf_get_lines(float_bufnr, 0, -1, false) + vim.api.nvim_win_close(winnr, true) + return lines + ]]) + + eq({'You know the rules', '1. Syntax error'}, exec_lua [[ + local diagnostics = { + make_error("Syntax error", 0, 1, 0, 3), + } + vim.api.nvim_win_set_buf(0, diagnostic_bufnr) + vim.diagnostic.set(diagnostic_ns, diagnostic_bufnr, diagnostics) + local float_bufnr, winnr = vim.diagnostic.open_float(0, {header = {'You know the rules', 'Search'}}) + local lines = vim.api.nvim_buf_get_lines(float_bufnr, 0, -1, false) + vim.api.nvim_win_close(winnr, true) + return lines + ]]) + end) + it('can show diagnostics from the whole buffer', function() eq({'1. Syntax error', '2. Some warning'}, exec_lua [[ local diagnostics = { @@ -1040,7 +1078,7 @@ describe('vim.diagnostic', function() } vim.api.nvim_win_set_buf(0, diagnostic_bufnr) vim.diagnostic.set(diagnostic_ns, diagnostic_bufnr, diagnostics) - local float_bufnr, winnr = vim.diagnostic.open_float(0, {show_header = false}) + local float_bufnr, winnr = vim.diagnostic.open_float(0, {header = false}) local lines = vim.api.nvim_buf_get_lines(float_bufnr, 0, -1, false) vim.api.nvim_win_close(winnr, true) return lines @@ -1057,7 +1095,7 @@ describe('vim.diagnostic', function() vim.api.nvim_win_set_buf(0, diagnostic_bufnr) vim.diagnostic.set(diagnostic_ns, diagnostic_bufnr, diagnostics) vim.api.nvim_win_set_cursor(0, {2, 1}) - local float_bufnr, winnr = vim.diagnostic.open_float(0, {show_header=false, scope="line"}) + local float_bufnr, winnr = vim.diagnostic.open_float(0, {header=false, scope="line"}) local lines = vim.api.nvim_buf_get_lines(float_bufnr, 0, -1, false) vim.api.nvim_win_close(winnr, true) return lines @@ -1072,7 +1110,7 @@ describe('vim.diagnostic', function() vim.api.nvim_win_set_buf(0, diagnostic_bufnr) vim.diagnostic.set(diagnostic_ns, diagnostic_bufnr, diagnostics) vim.api.nvim_win_set_cursor(0, {1, 1}) - local float_bufnr, winnr = vim.diagnostic.open_float(0, {show_header=false, scope="line", pos=1}) + local float_bufnr, winnr = vim.diagnostic.open_float(0, {header=false, scope="line", pos=1}) local lines = vim.api.nvim_buf_get_lines(float_bufnr, 0, -1, false) vim.api.nvim_win_close(winnr, true) return lines @@ -1089,7 +1127,7 @@ describe('vim.diagnostic', function() vim.api.nvim_win_set_buf(0, diagnostic_bufnr) vim.diagnostic.set(diagnostic_ns, diagnostic_bufnr, diagnostics) vim.api.nvim_win_set_cursor(0, {2, 2}) - local float_bufnr, winnr = vim.diagnostic.open_float(0, {show_header=false, scope="cursor"}) + local float_bufnr, winnr = vim.diagnostic.open_float(0, {header=false, scope="cursor"}) local lines = vim.api.nvim_buf_get_lines(float_bufnr, 0, -1, false) vim.api.nvim_win_close(winnr, true) return lines @@ -1104,7 +1142,7 @@ describe('vim.diagnostic', function() vim.api.nvim_win_set_buf(0, diagnostic_bufnr) vim.diagnostic.set(diagnostic_ns, diagnostic_bufnr, diagnostics) vim.api.nvim_win_set_cursor(0, {1, 1}) - local float_bufnr, winnr = vim.diagnostic.open_float(0, {show_header=false, scope="cursor", pos={1,3}}) + local float_bufnr, winnr = vim.diagnostic.open_float(0, {header=false, scope="cursor", pos={1,3}}) local lines = vim.api.nvim_buf_get_lines(float_bufnr, 0, -1, false) vim.api.nvim_win_close(winnr, true) return lines @@ -1119,7 +1157,7 @@ describe('vim.diagnostic', function() vim.api.nvim_win_set_buf(0, diagnostic_bufnr) vim.diagnostic.set(diagnostic_ns, diagnostic_bufnr, diagnostics) vim.api.nvim_win_set_cursor(0, {1, 1}) - local float_bufnr, winnr = vim.diagnostic.open_float(0, {show_header=false, scope="cursor", pos={0,first_line_len}}) + local float_bufnr, winnr = vim.diagnostic.open_float(0, {header=false, scope="cursor", pos={0,first_line_len}}) local lines = vim.api.nvim_buf_get_lines(float_bufnr, 0, -1, false) vim.api.nvim_win_close(winnr, true) return lines @@ -1189,7 +1227,7 @@ describe('vim.diagnostic', function() } vim.api.nvim_win_set_buf(0, diagnostic_bufnr) vim.diagnostic.set(diagnostic_ns, diagnostic_bufnr, diagnostics) - local float_bufnr, winnr = vim.diagnostic.open_float(diagnostic_bufnr, {show_header = false}) + local float_bufnr, winnr = vim.diagnostic.open_float(diagnostic_bufnr, {header = false}) local lines = vim.api.nvim_buf_get_lines(float_bufnr, 0, -1, false) vim.api.nvim_win_close(winnr, true) return #lines @@ -1203,7 +1241,7 @@ describe('vim.diagnostic', function() } vim.api.nvim_win_set_buf(0, diagnostic_bufnr) vim.diagnostic.set(diagnostic_ns, diagnostic_bufnr, diagnostics) - local float_bufnr, winnr = vim.diagnostic.open_float(diagnostic_bufnr, {show_header = false, scope = "line", pos = 5}) + local float_bufnr, winnr = vim.diagnostic.open_float(diagnostic_bufnr, {header = false, scope = "line", pos = 5}) local lines = vim.api.nvim_buf_get_lines(float_bufnr, 0, -1, false) vim.api.nvim_win_close(winnr, true) return #lines @@ -1219,7 +1257,7 @@ describe('vim.diagnostic', function() } vim.diagnostic.set(diagnostic_ns, diagnostic_bufnr, diagnostics) local float_bufnr, winnr = vim.diagnostic.open_float(diagnostic_bufnr, { - show_header = false, + header = false, source = "if_many", }) local lines = vim.api.nvim_buf_get_lines(float_bufnr, 0, -1, false) @@ -1229,7 +1267,7 @@ describe('vim.diagnostic', function() eq({"1. source x: Syntax error"}, exec_lua [[ local float_bufnr, winnr = vim.diagnostic.open_float(diagnostic_bufnr, { - show_header = false, + header = false, source = "always", }) local lines = vim.api.nvim_buf_get_lines(float_bufnr, 0, -1, false) @@ -1244,7 +1282,7 @@ describe('vim.diagnostic', function() } vim.diagnostic.set(diagnostic_ns, diagnostic_bufnr, diagnostics) local float_bufnr, winnr = vim.diagnostic.open_float(diagnostic_bufnr, { - show_header = false, + header = false, source = "if_many", }) local lines = vim.api.nvim_buf_get_lines(float_bufnr, 0, -1, false) @@ -1268,7 +1306,7 @@ describe('vim.diagnostic', function() vim.diagnostic.config({severity_sort = false}) - local float_bufnr, winnr = vim.diagnostic.open_float(diagnostic_bufnr, { show_header = false }) + local float_bufnr, winnr = vim.diagnostic.open_float(diagnostic_bufnr, { header = false }) local lines = vim.api.nvim_buf_get_lines(float_bufnr, 0, -1, false) vim.api.nvim_win_close(winnr, true) return lines @@ -1276,7 +1314,7 @@ describe('vim.diagnostic', function() eq({"1. Syntax error", "2. Error", "3. Warning", "4. Info"}, exec_lua [[ vim.diagnostic.config({severity_sort = true}) - local float_bufnr, winnr = vim.diagnostic.open_float(diagnostic_bufnr, { show_header = false }) + local float_bufnr, winnr = vim.diagnostic.open_float(diagnostic_bufnr, { header = false }) local lines = vim.api.nvim_buf_get_lines(float_bufnr, 0, -1, false) vim.api.nvim_win_close(winnr, true) return lines @@ -1284,7 +1322,7 @@ describe('vim.diagnostic', function() eq({"1. Info", "2. Warning", "3. Error", "4. Syntax error"}, exec_lua [[ vim.diagnostic.config({severity_sort = { reverse = true } }) - local float_bufnr, winnr = vim.diagnostic.open_float(diagnostic_bufnr, { show_header = false }) + local float_bufnr, winnr = vim.diagnostic.open_float(diagnostic_bufnr, { header = false }) local lines = vim.api.nvim_buf_get_lines(float_bufnr, 0, -1, false) vim.api.nvim_win_close(winnr, true) return lines @@ -1308,7 +1346,7 @@ describe('vim.diagnostic', function() make_warning('Warning', 0, 0, 0, 1), }) - local float_bufnr, winnr = vim.diagnostic.open_float(diagnostic_bufnr, { show_header = false }) + local float_bufnr, winnr = vim.diagnostic.open_float(diagnostic_bufnr, { header = false }) if not float_bufnr then return 0 end @@ -1335,7 +1373,7 @@ describe('vim.diagnostic', function() } vim.api.nvim_win_set_buf(0, diagnostic_bufnr) vim.diagnostic.set(diagnostic_ns, diagnostic_bufnr, diagnostics) - local float_bufnr, winnr = vim.diagnostic.open_float(0, {show_header = false, number = "always"}) + local float_bufnr, winnr = vim.diagnostic.open_float(0, {header = false}) local lines = vim.api.nvim_buf_get_lines(float_bufnr, 0, -1, false) vim.api.nvim_win_close(winnr, true) return lines @@ -1348,7 +1386,7 @@ describe('vim.diagnostic', function() } vim.api.nvim_win_set_buf(0, diagnostic_bufnr) vim.diagnostic.set(diagnostic_ns, diagnostic_bufnr, diagnostics) - local float_bufnr, winnr = vim.diagnostic.open_float(0, {show_header = false, prefix = ""}) + local float_bufnr, winnr = vim.diagnostic.open_float(0, {header = false, prefix = ""}) local lines = vim.api.nvim_buf_get_lines(float_bufnr, 0, -1, false) vim.api.nvim_win_close(winnr, true) return lines @@ -1362,7 +1400,7 @@ describe('vim.diagnostic', function() vim.api.nvim_win_set_buf(0, diagnostic_bufnr) vim.diagnostic.set(diagnostic_ns, diagnostic_bufnr, diagnostics) local float_bufnr, winnr = vim.diagnostic.open_float(0, { - show_header = false, + header = false, prefix = function(_, i, total) -- Only show a number if there is more than one diagnostic if total > 1 then @@ -1383,7 +1421,7 @@ describe('vim.diagnostic', function() vim.api.nvim_win_set_buf(0, diagnostic_bufnr) vim.diagnostic.set(diagnostic_ns, diagnostic_bufnr, diagnostics) local float_bufnr, winnr = vim.diagnostic.open_float(0, { - show_header = false, + header = false, prefix = function(_, i, total) -- Only show a number if there is more than one diagnostic if total > 1 then |