Skip to content

Commit

Permalink
fix(diagnostic): fix option resolution in open_float (neovim#16229)
Browse files Browse the repository at this point in the history
  • Loading branch information
gpanders authored Nov 4, 2021
1 parent f26b391 commit fd34784
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 11 deletions.
22 changes: 11 additions & 11 deletions runtime/lua/vim/diagnostic.lua
Original file line number Diff line number Diff line change
Expand Up @@ -1154,6 +1154,17 @@ function M.open_float(bufnr, opts)
error("Invalid value for option 'scope'")
end

do
-- Resolve options with user settings from vim.diagnostic.config
-- Unlike the other decoration functions (e.g. set_virtual_text, set_signs, etc.) `open_float`
-- does not have a dedicated table for configuration options; instead, the options are mixed in
-- with its `opts` table which also includes "keyword" parameters. So we create a dedicated
-- options table that inherits missing keys from the global configuration before resolving.
local t = global_diagnostic_options.float
local float_opts = vim.tbl_extend("keep", opts, type(t) == "table" and t or {})
opts = get_resolved_options({ float = float_opts }, nil, bufnr).float
end

local diagnostics = M.get(bufnr, opts)
clamp_line_numbers(bufnr, diagnostics)

Expand Down Expand Up @@ -1184,17 +1195,6 @@ function M.open_float(bufnr, opts)
end
end

do
-- Resolve options with user settings from vim.diagnostic.config
-- Unlike the other decoration functions (e.g. set_virtual_text, set_signs, etc.) `open_float`
-- does not have a dedicated table for configuration options; instead, the options are mixed in
-- with its `opts` table which also includes "keyword" parameters. So we create a dedicated
-- options table that inherits missing keys from the global configuration before resolving.
local t = global_diagnostic_options.float
local float_opts = vim.tbl_extend("keep", opts, type(t) == "table" and t or {})
opts = get_resolved_options({ float = float_opts }, nil, bufnr).float
end

local lines = {}
local highlights = {}
local show_header = vim.F.if_nil(opts.show_header, true)
Expand Down
35 changes: 35 additions & 0 deletions test/functional/lua/diagnostic_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -1240,6 +1240,41 @@ describe('vim.diagnostic', function()
return lines
]])
end)

it('can filter by severity', function()
local count_diagnostics_with_severity = function(min_severity, max_severity)
return exec_lua([[
local min_severity, max_severity = ...
vim.diagnostic.config({
float = {
severity = {min=min_severity, max=max_severity},
},
})
vim.diagnostic.set(diagnostic_ns, diagnostic_bufnr, {
make_error("Syntax error", 0, 1, 0, 3),
make_info('Info', 0, 3, 0, 4),
make_error('Error', 0, 2, 0, 2),
make_warning('Warning', 0, 0, 0, 1),
})
local float_bufnr, winnr = vim.diagnostic.open_float(diagnostic_bufnr, { show_header = false })
if not float_bufnr then
return 0
end
local lines = vim.api.nvim_buf_get_lines(float_bufnr, 0, -1, false)
vim.api.nvim_win_close(winnr, true)
return #lines
]], min_severity, max_severity)
end

eq(2, count_diagnostics_with_severity("ERROR"))
eq(3, count_diagnostics_with_severity("WARN"))
eq(1, count_diagnostics_with_severity("WARN", "WARN"))
eq(4, count_diagnostics_with_severity("HINT"))
eq(0, count_diagnostics_with_severity("HINT", "HINT"))
end)
end)

describe('setloclist()', function()
Expand Down

0 comments on commit fd34784

Please sign in to comment.