Skip to content

Commit b367231

Browse files
committed
lsp sig and vim.diagnostics support
1 parent 5ca4040 commit b367231

File tree

7 files changed

+213
-43
lines changed

7 files changed

+213
-43
lines changed

lua/cosmic-ui/diagnostics/init.lua

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
local icons = require('cosmic-ui.icons')
2+
local utils = require('cosmic-ui.utils')
3+
local M = {}
4+
local function do_diagnostic_signs()
5+
local signs = {
6+
Error = icons.error .. ' ',
7+
Warn = icons.warn .. ' ',
8+
Hint = icons.hint .. ' ',
9+
Info = icons.info .. ' ',
10+
}
11+
12+
local t = vim.fn.sign_getdefined('DiagnosticSignWarn')
13+
if vim.tbl_isempty(t) then
14+
for type, icon in pairs(signs) do
15+
local hl = 'DiagnosticSign' .. type
16+
vim.fn.sign_define(hl, { text = icon, texthl = hl, numhl = '' })
17+
end
18+
end
19+
end
20+
21+
local function do_legacy_diagnostic_signs()
22+
local signs = {
23+
Error = icons.error .. ' ',
24+
Warning = icons.warn .. ' ',
25+
Hint = icons.hint .. ' ',
26+
Information = icons.info .. ' ',
27+
}
28+
local h = vim.fn.sign_getdefined('LspDiagnosticsSignWarn')
29+
if vim.tbl_isempty(h) then
30+
for type, icon in pairs(signs) do
31+
local hl = 'LspDiagnosticsSign' .. type
32+
vim.fn.sign_define(hl, { text = icon, texthl = hl, numhl = '' })
33+
end
34+
end
35+
end
36+
37+
M.init = function(diagnostic_opts)
38+
-- set up LSP signs
39+
do_diagnostic_signs()
40+
do_legacy_diagnostic_signs()
41+
42+
-- set up vim.diagnostics
43+
-- vim.diagnostic.config opts
44+
vim.diagnostic.config(diagnostic_opts)
45+
end
46+
47+
return M

lua/cosmic-ui/hover/init.lua

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
local M = {}
2+
3+
M.init = function(opts)
4+
vim.lsp.handlers['textDocument/hover'] = vim.lsp.with(opts.handler, opts.float)
5+
end
6+
7+
return M

lua/cosmic-ui/icons.lua

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
local icons = {
2+
rounded_left_filled = '',
3+
rounded_right_filled = '',
4+
arrow_left_filled = '', -- e0b2
5+
arrow_right_filled = '', -- e0b0
6+
arrow_left = '', -- e0b3
7+
arrow_right = '', -- e0b1
8+
ghost = '',
9+
cosmic = '💫',
10+
star = '',
11+
scorpio = '♏︎',
12+
rocket = '🚀',
13+
warn = '',
14+
info = '',
15+
error = '',
16+
hint = '',
17+
perf = '',
18+
note = '',
19+
branch = '',
20+
file = '',
21+
dotdotdot = '',
22+
information = '',
23+
symlink = '',
24+
line_number = '',
25+
debug = '',
26+
flame = '',
27+
check = '',
28+
trace = '',
29+
file1 = '',
30+
file2 = '',
31+
clock = '',
32+
word = '',
33+
git = {
34+
unstaged = '',
35+
staged = '',
36+
unmerged = '',
37+
renamed = '',
38+
untracked = '',
39+
deleted = '',
40+
ignored = '',
41+
},
42+
folder = {
43+
arrow_open = '',
44+
arrow_closed = '',
45+
default = '',
46+
open = '',
47+
empty = '',
48+
empty_open = '',
49+
symlink = '',
50+
symlink_open = '',
51+
},
52+
}
53+
54+
return icons

lua/cosmic-ui/init.lua

Lines changed: 64 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,72 @@
1+
local utils = require('cosmic-ui.utils')
12
local M = {}
23

3-
M.rename = function(popup_opts, opts)
4-
return require('cosmic-ui.rename')(popup_opts, opts)
5-
end
4+
local default_user_opts = {
5+
lsp_signature = {
6+
bind = true, -- This is mandatory, otherwise border config won't get registered.
7+
handler_opts = {
8+
border = 'rounded',
9+
},
10+
},
11+
icons = {
12+
warn = '',
13+
info = '',
14+
error = '',
15+
hint = '',
16+
},
17+
diagnostics = {
18+
underline = true,
19+
signs = true,
20+
update_in_insert = false,
21+
severity_sort = true,
22+
float = {
23+
header = false,
24+
source = 'always',
25+
border = 'rounded',
26+
},
27+
virtual_text = {
28+
spacing = 4,
29+
source = 'always',
30+
severity = {
31+
min = vim.diagnostic.severity.HINT,
32+
},
33+
},
34+
},
35+
hover = {
36+
handler = vim.lsp.handlers.hover,
37+
float = {
38+
border = 'rounded',
39+
},
40+
},
41+
signature_help = {
42+
handler = vim.lsp.handlers.signature_help,
43+
float = {
44+
border = 'rounded',
45+
},
46+
},
47+
}
648

7-
M.highlight = function(group, bg, fg, gui)
8-
if gui ~= nil and gui ~= '' then
9-
vim.api.nvim_command(('hi %s guibg=%s guifg=%s gui=%s'):format(group, bg, fg, gui))
10-
elseif bg == nil then
11-
vim.api.nvim_command(('hi %s guifg=%s'):format(group, fg))
49+
M.setup = function(user_opts)
50+
user_opts = utils.merge(default_user_opts, user_opts or {})
51+
52+
-- set up lsp_signature if enabled
53+
local ok, lsp_signature = pcall(require, 'lsp_signature')
54+
if ok and user_opts.signature_help ~= false then
55+
lsp_signature.setup(user_opts.lsp_signature)
1256
else
13-
vim.api.nvim_command(('hi %s guibg=%s guifg=%s'):format(group, bg, fg))
57+
-- set up signatureHelp
58+
require('cosmic-ui.signature-help').init(user_opts.signature_help)
1459
end
60+
61+
-- set up diagnostics
62+
require('cosmic-ui.diagnostics').init(user_opts.diagnostics)
63+
64+
-- set up hover
65+
require('cosmic-ui.hover').init(user_opts.hover)
66+
end
67+
68+
M.rename = function(popup_opts, opts)
69+
return require('cosmic-ui.rename')(popup_opts, opts)
1570
end
1671

1772
return M

lua/cosmic-ui/rename/handler.lua

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
return function(...)
2+
local result
3+
local method
4+
local err = select(1, ...)
5+
local is_new = not select(4, ...) or type(select(4, ...)) ~= 'number'
6+
if is_new then
7+
method = select(3, ...).method
8+
result = select(2, ...)
9+
else
10+
method = select(2, ...)
11+
result = select(3, ...)
12+
end
13+
14+
if err then
15+
vim.notify(("Error running LSP query '%s': %s"):format(method, err), vim.log.levels.ERROR, {
16+
title = 'Cosmic-UI',
17+
})
18+
return
19+
end
20+
21+
local new_word = ''
22+
if result and result.changes then
23+
local msg = {}
24+
for f, c in pairs(result.changes) do
25+
new_word = c[1].newText
26+
table.insert(msg, ('%d changes -> %s'):format(#c, utils.get_relative_path(f)))
27+
end
28+
local currName = vim.fn.expand('<cword>')
29+
vim.notify(msg, vim.log.levels.INFO, { title = ('Rename: %s -> %s'):format(currName, new_word) })
30+
end
31+
32+
vim.lsp.handlers[method](...)
33+
end

lua/cosmic-ui/rename.lua renamed to lua/cosmic-ui/rename/init.lua

Lines changed: 1 addition & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,6 @@
11
local lsp = vim.lsp
22
local utils = require('cosmic-ui.utils')
3-
4-
local function rename_handler(...)
5-
local result
6-
local method
7-
local err = select(1, ...)
8-
local is_new = not select(4, ...) or type(select(4, ...)) ~= 'number'
9-
if is_new then
10-
method = select(3, ...).method
11-
result = select(2, ...)
12-
else
13-
method = select(2, ...)
14-
result = select(3, ...)
15-
end
16-
17-
if err then
18-
vim.notify(("Error running LSP query '%s': %s"):format(method, err), vim.log.levels.ERROR, {
19-
title = 'Cosmic-UI',
20-
})
21-
return
22-
end
23-
24-
local new_word = ''
25-
if result and result.changes then
26-
local msg = {}
27-
for f, c in pairs(result.changes) do
28-
new_word = c[1].newText
29-
table.insert(msg, ('%d changes -> %s'):format(#c, utils.get_relative_path(f)))
30-
end
31-
local currName = vim.fn.expand('<cword>')
32-
vim.notify(msg, vim.log.levels.INFO, { title = ('Rename: %s -> %s'):format(currName, new_word) })
33-
end
34-
35-
vim.lsp.handlers[method](...)
36-
end
3+
local rename_handler = require('cosmic-ui.rename.handler')
374

385
local function rename(popup_opts, opts)
396
opts = opts or {}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
local M = {}
2+
3+
M.init = function(opts)
4+
vim.lsp.handlers['textDocument/signatureHelp'] = vim.lsp.with(opts.handler, opts.float)
5+
end
6+
7+
return M

0 commit comments

Comments
 (0)