Skip to content

Commit

Permalink
big renames and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
tjdevries committed Nov 2, 2020
1 parent ea25fe3 commit 99124d7
Show file tree
Hide file tree
Showing 7 changed files with 190 additions and 71 deletions.
66 changes: 32 additions & 34 deletions runtime/doc/lsp.txt
Expand Up @@ -1020,9 +1020,8 @@ workspace_symbol({query}) *vim.lsp.buf.workspace_symbol()*
==============================================================================
Lua module: vim.lsp.diagnostic *lsp-diagnostic*

*vim.lsp.diagnostic.buf_clear_displayed_diagnostics()*
buf_clear_displayed_diagnostics({bufnr}, {client_id},
{diagnostic_ns}, {sign_ns})
*vim.lsp.diagnostic.clear()*
clear({bufnr}, {client_id}, {diagnostic_ns}, {sign_ns})
Clears the currently displayed diagnostics

Parameters: ~
Expand All @@ -1032,8 +1031,24 @@ buf_clear_displayed_diagnostics({bufnr}, {client_id},
namespace
{sign_ns} number|nil Associated sign namespace

*vim.lsp.diagnostic.buf_get_count()*
buf_get_count({bufnr}, {kind}, {client_id})
*vim.lsp.diagnostic.define_default_sign()*
define_default_sign({name}, {properties})
TODO: Documentation

*vim.lsp.diagnostic.display()*
display({diagnostics}, {bufnr}, {client_id}, {config})
TODO: Documentation

get({bufnr}, {client_id}) *vim.lsp.diagnostic.get()*
Return associated diagnostics for bufnr

Parameters: ~
{bufnr} number
{client_id} number|nil If nil, then return all of the
diagnostics.

*vim.lsp.diagnostic.get_count()*
get_count({bufnr}, {kind}, {client_id})
Get the counts for a particular severity kind

Useful for showing diagnostic counts in statusline. eg:
Expand All @@ -1059,34 +1074,6 @@ buf_get_count({bufnr}, {kind}, {client_id})
{kind} DiagnosticSeverity
{client_id} number the client id

*vim.lsp.diagnostic.buf_remove_schedule_display_on_insert_leave()*
buf_remove_schedule_display_on_insert_leave({bufnr}, {client_id})
Used in tandem with

For parameter description, see |M.display()|

*vim.lsp.diagnostic.buf_schedule_display_on_insert_leave()*
buf_schedule_display_on_insert_leave({bufnr}, {client_id}, {args})
Used to schedule diagnostic updates upon leaving insert mode.

For parameter description, see |M.display()|

*vim.lsp.diagnostic.define_default_sign()*
define_default_sign({name}, {properties})
TODO: Documentation

*vim.lsp.diagnostic.display()*
display({diagnostics}, {bufnr}, {client_id}, {config})
TODO: Documentation

get({bufnr}, {client_id}) *vim.lsp.diagnostic.get()*
Return associated diagnostics for bufnr

Parameters: ~
{bufnr} number
{client_id} number|nil If nil, then return all of the
diagnostics.

*vim.lsp.diagnostic.get_line_diagnostics()*
get_line_diagnostics({bufnr}, {line_nr}, {client_id})
Get the diagnostics by line
Expand Down Expand Up @@ -1160,7 +1147,18 @@ get_prev_pos({bufnr}, {client_id}, {cursor_position})

*vim.lsp.diagnostic.get_virtual_text_chunks_for_line()*
get_virtual_text_chunks_for_line({_}, {_}, {line_diagnostics})
TODO: Documentation
Default function to get text chunks to display using `nvim_buf_set_virtual_text` .

Parameters: ~
{bufnr} Buffer The buffer to display the
virtual text in
{line} number The line number to display the
virtual text on
{line_diagnostics} Diagnostic [] The diagnostics associated with the
line

Return: ~
chunks, as defined by |nvim_buf_set_virtual_text()|

*vim.lsp.diagnostic.goto_next()*
goto_next({bufnr}, {client_id}, {cursor_position})
Expand Down
31 changes: 31 additions & 0 deletions runtime/lua/vim/lsp.lua
Expand Up @@ -1223,6 +1223,37 @@ function lsp.with(handler, override_config)
end
end
--- Helper function to use when implementing a handler.
--- This will check that all of the keys in the user configuration
--- are valid keys and make sense to include for this handler.
---
--- Will error on invalid keys (i.e. keys that do not exist in the options)
function lsp._with_extend(name, options, user_config)
user_config = user_config or {}
local resulting_config = {}
for k, v in pairs(user_config) do
if options[k] == nil then
error(debug.traceback(string.format(
"Invalid option for `%s`: %s. Valid options are:\n%s",
name,
k,
vim.inspect(vim.tbl_keys(options))
)))
end
resulting_config[k] = v
end
for k, v in pairs(options) do
if resulting_config[k] == nil then
resulting_config[k] = v
end
end
return resulting_config
end
-- Define the LspDiagnostics signs if they're not defined already.
require('vim.lsp.diagnostic')._define_default_signs_and_highlights()
Expand Down
4 changes: 2 additions & 2 deletions runtime/lua/vim/lsp/buf.lua
Expand Up @@ -278,7 +278,7 @@ end
--@see https://microsoft.github.io/language-server-protocol/specifications/specification-current/#textDocument_codeAction
function M.code_action(context)
validate { context = { context, 't', true } }
context = context or { diagnostics = util.get_line_diagnostics() }
context = context or { diagnostics = vim.lsp.diagnostic.get_line_diagnostics() }
local params = util.make_range_params()
params.context = context
request('textDocument/codeAction', params)
Expand All @@ -293,7 +293,7 @@ end
---Defaults to the end of the last visual selection.
function M.range_code_action(context, start_pos, end_pos)
validate { context = { context, 't', true } }
context = context or { diagnostics = util.get_line_diagnostics() }
context = context or { diagnostics = vim.lsp.diagnostic.get_line_diagnostics() }
local params = util.make_given_range_params(start_pos, end_pos)
params.context = context
request('textDocument/codeAction', params)
Expand Down
26 changes: 16 additions & 10 deletions runtime/lua/vim/lsp/diagnostic.lua
Expand Up @@ -321,11 +321,11 @@ end
---@param bufnr number The buffer number
---@param kind DiagnosticSeverity
---@param client_id number the client id
function M.buf_get_count(bufnr, kind, client_id)
function M.get_count(bufnr, kind, client_id)
if client_id == nil then
local total = 0
for iter_client_id, _ in pairs(diagnostic_cache_counts[bufnr]) do
total = total + M.buf_get_count(bufnr, kind, iter_client_id)
total = total + M.get_count(bufnr, kind, iter_client_id)
end

return total
Expand Down Expand Up @@ -570,6 +570,11 @@ function M.set_virtual_text(diagnostics, bufnr, client_id, diagnostic_ns)
end
end

--- Default function to get text chunks to display using `nvim_buf_set_virtual_text`.
---@param bufnr Buffer The buffer to display the virtual text in
---@param line number The line number to display the virtual text on
---@param line_diagnostics Diagnostic[] The diagnostics associated with the line
---@return chunks, as defined by |nvim_buf_set_virtual_text()|
function M.get_virtual_text_chunks_for_line(_, _, line_diagnostics)
if #line_diagnostics == 0 then
return nil
Expand Down Expand Up @@ -638,7 +643,7 @@ end
---
--- Used to handle
--@private
function M._buf_on_insert_leave(bufnr, client_id)
function M._execute_scheduled_display(bufnr, client_id)
local args = table.remove(_bufs_waiting_to_update[bufnr], client_id)

M.display(nil, bufnr, client_id, args)
Expand All @@ -656,7 +661,7 @@ M.insert_leave_auto_cmds = { "InsertLeave", "CursorHoldI" }
--- Used to schedule diagnostic updates upon leaving insert mode.
---
--- For parameter description, see |M.display()|
function M.buf_schedule_display_on_insert_leave(bufnr, client_id, args)
function M._schedule_display(bufnr, client_id, args)
if _bufs_waiting_to_update[bufnr][client_id] then
_bufs_waiting_to_update[bufnr][client_id] = args
return
Expand All @@ -668,7 +673,7 @@ function M.buf_schedule_display_on_insert_leave(bufnr, client_id, args)
vim.cmd(" au!")
vim.cmd(
string.format(
[[autocmd %s <buffer=%s> :lua vim.lsp.diagnostic._buf_on_insert_leave(%s, %s)]],
[[autocmd %s <buffer=%s> :lua vim.lsp.diagnostic._execute_scheduled_display(%s, %s)]],
table.concat(M.insert_leave_auto_cmds, ","),
bufnr,
bufnr,
Expand All @@ -685,7 +690,7 @@ end
--- Used in tandem with
---
--- For parameter description, see |M.display()|
function M.buf_remove_schedule_display_on_insert_leave(bufnr, client_id)
function M._clear_scheduled_display(bufnr, client_id)
local key = make_augroup_key(bufnr, client_id)

if registered[key] then
Expand All @@ -697,6 +702,7 @@ function M.buf_remove_schedule_display_on_insert_leave(bufnr, client_id)
end
end
-- }}}

-- Diagnostic Private Highlight Utilies {{{
--- Get the severity highlight name
--@private
Expand Down Expand Up @@ -789,24 +795,24 @@ function M.on_publish_diagnostics(_, _, params, client_id, _, config)
end

function M.display(diagnostics, bufnr, client_id, config)
config = vim.tbl_deep_extend("keep", config or {}, {
config = vim.lsp._with_extend('vim.lsp.diagnostic.on_publish_diagnostics', {
underline = true,
virtual_text = true,
signs = true,
update_in_insert = false,
})
}, config)

if diagnostics == nil then
diagnostics = M.get(bufnr, client_id)
end

if config.update_in_insert then
M.buf_remove_schedule_display_on_insert_leave(bufnr, client_id)
M._clear_scheduled_display(bufnr, client_id)
else
local mode = vim.api.nvim_get_mode()

if string.sub(mode.mode, 1, 1) == 'i' then
M.buf_schedule_display_on_insert_leave(bufnr, client_id, config)
M._schedule_display(bufnr, client_id, config)
return
end
end
Expand Down
4 changes: 2 additions & 2 deletions runtime/lua/vim/lsp/util.lua
Expand Up @@ -1028,8 +1028,8 @@ do
--@deprecated
function M.buf_diagnostics_count(kind, client_id)
warn_once("buf_diagnostics_count is deprecated. Use 'vim.lsp.diagnostic.buf_get_count'")
return vim.lsp.diagnostic.buf_get_count(vim.api.nvim_get_current_buf(), client_id, kind)
warn_once("buf_diagnostics_count is deprecated. Use 'vim.lsp.diagnostic.get_count'")
return vim.lsp.diagnostic.get_count(vim.api.nvim_get_current_buf(), client_id, kind)
end
--@deprecated
Expand Down

0 comments on commit 99124d7

Please sign in to comment.