Skip to content

Commit

Permalink
Add convenience on_attach function and default statusline module
Browse files Browse the repository at this point in the history
  • Loading branch information
wbthomason committed May 25, 2020
1 parent e0af53b commit 8fb7cc4
Show file tree
Hide file tree
Showing 2 changed files with 111 additions and 0 deletions.
26 changes: 26 additions & 0 deletions lua/lsp-status.lua
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ local extension_callbacks = {
-- Find current enclosing function
local current_function = require('lsp-status/current_function')

-- Out-of-the-box statusline component
local statusline = require('lsp-status/statusline')

local function configure(config)
_config = vim.tbl_extend('keep', config, _config, default_config)
Expand All @@ -32,6 +34,28 @@ local function configure(config)
current_function._init(messages, _config)
end

local function on_attach(client)
-- Register the client for messages
messaging.register_client(client.name)

-- Set up autocommands to refresh the statusline when information changes
vim.api.nvim_command('augroup lsp_aucmds')
vim.api.nvim_command('au! * <buffer>')
vim.api.nvim_command('au User LspDiagnosticsChanged redrawstatus!')
vim.api.nvim_command('au User LspMessageUpdate redrawstatus!')
vim.api.nvim_command('au User LspStatusUpdate redrawstatus!')
vim.api.nvim_command('augroup END')

-- If the client is a documentSymbolProvider, set up an autocommand
-- to update the containing symbol
if client.resolved_capabilities.document_symbol then
vim.api.nvim_command('augroup lsp_aucmds')
vim.api.nvim_command(
'au CursorHold <buffer> lua require("lsp-status").update_current_function()'
)
vim.api.nvim_command('augroup END')
end
end

configure(_config)

Expand All @@ -43,6 +67,8 @@ local M = {
register_client = messaging.register_client,
extensions = extension_callbacks,
config = configure,
on_attach = on_attach,
status = statusline,
}

return M
85 changes: 85 additions & 0 deletions lua/lsp-status/statusline.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
vim.g.indicator_errors = ''
vim.g.indicator_warnings = ''
vim.g.indicator_info = '🛈'
vim.g.indicator_hint = ''
vim.g.indicator_ok = ''
vim.g.spinner_frames = { '', '', '', '', '', '', '', '' }

local diagnostics = require('lsp-status/diagnostics')
local messages = require('lsp-status/messaging').messages
local aliases = {
pyls_ms = 'MPLS',
}

local function statusline_lsp()
if #vim.lsp.buf_get_clients() == 0 then
return ''
end

local buf_diagnostics = diagnostics()
local buf_messages = messages()
local only_hint = true
local some_diagnostics = false
local status_parts = {}
if buf_diagnostics.errors and buf_diagnostics.errors > 0 then
table.insert(status_parts, vim.g.indicator_errors .. ' ' .. buf_diagnostics.errors)
only_hint = false
some_diagnostics = true
end

if buf_diagnostics.warnings and buf_diagnostics.warnings > 0 then
table.insert(status_parts, vim.g.indicator_warnings .. ' ' .. buf_diagnostics.warnings)
only_hint = false
some_diagnostics = true
end

if buf_diagnostics.info and buf_diagnostics.info > 0 then
table.insert(status_parts, vim.g.indicator_info .. ' ' .. buf_diagnostics.info)
only_hint = false
some_diagnostics = true
end

if buf_diagnostics.hints and buf_diagnostics.hints > 0 then
table.insert(status_parts, vim.g.indicator_hint .. ' ' .. buf_diagnostics.hints)
some_diagnostics = true
end

local msgs = {}
for _, msg in ipairs(buf_messages) do
local name = aliases[msg.name] or msg.name
local client_name = '[' .. name .. ']'
if msg.progress then
local contents = msg.title
if msg.message then
contents = contents .. ' ' .. msg.message
end

if msg.percentage then
contents = contents .. ' (' .. msg.percentage .. ')'
end

if msg.spinner then
contents = vim.g.spinner_frames[(msg.spinner % #vim.g.spinner_frames) + 1] .. ' ' .. contents
end

table.insert(msgs, client_name .. ' ' .. contents)
else
table.insert(msgs, client_name .. ' ' .. msg.content)
end
end

local base_status = vim.trim(table.concat(status_parts, ' ') .. ' ' .. table.concat(msgs, ' '))
local symbol = ' 🇻' .. ((some_diagnostics and only_hint) and '' or ' ')
local current_function = vim.b.lsp_current_function
if current_function and current_function ~= '' then
symbol = symbol .. '(' .. current_function .. ') '
end

if base_status ~= '' then
return symbol .. base_status .. ' '
end

return symbol .. vim.g.indicator_ok .. ' '
end

return statusline_lsp

0 comments on commit 8fb7cc4

Please sign in to comment.