refactor(diagnostic): group local functions together

This commit is contained in:
Gregory Anders 2021-09-17 19:57:14 -06:00
parent 938ed458e2
commit 15d501ff7a

View File

@ -426,6 +426,70 @@ local function set_list(loclist, opts)
end
end
---@private
local function next_diagnostic(position, search_forward, bufnr, opts, namespace)
position[1] = position[1] - 1
bufnr = bufnr or vim.api.nvim_get_current_buf()
local wrap = vim.F.if_nil(opts.wrap, true)
local line_count = vim.api.nvim_buf_line_count(bufnr)
opts.namespace = namespace
for i = 0, line_count do
local offset = i * (search_forward and 1 or -1)
local lnum = position[1] + offset
if lnum < 0 or lnum >= line_count then
if not wrap then
return
end
lnum = (lnum + line_count) % line_count
end
opts.lnum = lnum
local line_diagnostics = M.get(bufnr, opts)
if line_diagnostics and not vim.tbl_isempty(line_diagnostics) then
local sort_diagnostics, is_next
if search_forward then
sort_diagnostics = function(a, b) return a.col < b.col end
is_next = function(diagnostic) return diagnostic.col > position[2] end
else
sort_diagnostics = function(a, b) return a.col > b.col end
is_next = function(diagnostic) return diagnostic.col < position[2] end
end
table.sort(line_diagnostics, sort_diagnostics)
if i == 0 then
for _, v in pairs(line_diagnostics) do
if is_next(v) then
return v
end
end
else
return line_diagnostics[1]
end
end
end
end
---@private
local function diagnostic_move_pos(opts, pos)
opts = opts or {}
local enable_popup = vim.F.if_nil(opts.enable_popup, true)
local win_id = opts.win_id or vim.api.nvim_get_current_win()
if not pos then
vim.api.nvim_echo({{"No more valid diagnostics to move to", "WarningMsg"}}, true, {})
return
end
vim.api.nvim_win_set_cursor(win_id, {pos[1] + 1, pos[2]})
if enable_popup then
-- This is a bit weird... I'm surprised that we need to wait til the next tick to do this.
vim.schedule(function()
M.show_position_diagnostics(opts.popup_opts, vim.api.nvim_win_get_buf(win_id))
end)
end
end
-- }}}
-- Public API {{{
@ -596,70 +660,6 @@ function M.get(bufnr, opts)
return diagnostics
end
-- Diagnostic Movements {{{
local next_diagnostic = function(position, search_forward, bufnr, opts, namespace)
position[1] = position[1] - 1
bufnr = bufnr or vim.api.nvim_get_current_buf()
local wrap = vim.F.if_nil(opts.wrap, true)
local line_count = vim.api.nvim_buf_line_count(bufnr)
opts.namespace = namespace
for i = 0, line_count do
local offset = i * (search_forward and 1 or -1)
local lnum = position[1] + offset
if lnum < 0 or lnum >= line_count then
if not wrap then
return
end
lnum = (lnum + line_count) % line_count
end
opts.lnum = lnum
local line_diagnostics = M.get(bufnr, opts)
if line_diagnostics and not vim.tbl_isempty(line_diagnostics) then
local sort_diagnostics, is_next
if search_forward then
sort_diagnostics = function(a, b) return a.col < b.col end
is_next = function(diagnostic) return diagnostic.col > position[2] end
else
sort_diagnostics = function(a, b) return a.col > b.col end
is_next = function(diagnostic) return diagnostic.col < position[2] end
end
table.sort(line_diagnostics, sort_diagnostics)
if i == 0 then
for _, v in pairs(line_diagnostics) do
if is_next(v) then
return v
end
end
else
return line_diagnostics[1]
end
end
end
end
---@private
local function diagnostic_move_pos(opts, pos)
opts = opts or {}
local enable_popup = vim.F.if_nil(opts.enable_popup, true)
local win_id = opts.win_id or vim.api.nvim_get_current_win()
if not pos then
vim.api.nvim_echo({{"No more valid diagnostics to move to", "WarningMsg"}}, true, {})
return
end
vim.api.nvim_win_set_cursor(win_id, {pos[1] + 1, pos[2]})
if enable_popup then
-- This is a bit weird... I'm surprised that we need to wait til the next tick to do this.
vim.schedule(function()
M.show_position_diagnostics(opts.popup_opts, vim.api.nvim_win_get_buf(win_id))
end)
end
end
--- Get the previous diagnostic closest to the cursor position.
---
---@param opts table See |vim.diagnostic.goto_next()|