-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.lua
215 lines (180 loc) · 6.56 KB
/
main.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
local util = require('one.util')
local symbolMap = require('one.config').config.symbolMap
local M = { 'neovim/nvim-lspconfig' }
local SeverityMap = {
[vim.diagnostic.severity.ERROR] = symbolMap.ERROR,
[vim.diagnostic.severity.WARN] = symbolMap.WARN,
[vim.diagnostic.severity.INFO] = symbolMap.INFO,
[vim.diagnostic.severity.HINT] = symbolMap.HINT,
}
M.highlights = function(config)
local c = config.colors
return {
LspWindowBorder = { fg = c.cyan },
LspInfoTitle = { fg = c.lightGreen }, -- Client name
LspInfoList = { fg = c.lightGreen }, -- Server name list
LspInfoFiletype = { fg = c.purple }, -- `filetypes` area
LspInfoTip = { link = 'Comment' }, -- Tip
LspInfoBorder = { fg = c.blue }, -- Window border
DiagnosticUnnecessary = { fg = c.grey, undercurl = true },
DiagnosticError = { fg = c.red },
DiagnosticWarn = { fg = c.yellow },
DiagnosticInfo = { fg = c.blue },
DiagnosticHint = { fg = c.cyan },
DiagnosticOk = { fg = c.lightGreen },
DiagnosticSignError = { fg = c.red },
DiagnosticSignWarn = { fg = c.yellow },
DiagnosticSignInfo = { fg = c.blue },
DiagnosticSignHint = { fg = c.cyan },
DiagnosticSignOk = { fg = c.lightGreen },
DiagnosticUnderlineError = { fg = c.red, undercurl = true },
DiagnosticUnderlineWarn = { fg = c.yellow, undercurl = true },
DiagnosticUnderlineInfo = { fg = c.blue, undercurl = true },
DiagnosticUnderlineHint = { fg = c.cyan, undercurl = true },
DiagnosticUnderlineOk = { fg = c.lightGreen, undercurl = true },
}
end
M.defaultConfig = {
'lsp',
{
log = { level = 'WARN' }, -- 'TRACE', 'DEBUG', 'INFO', 'WARN', 'ERROR', 'OFF'
masonLspconfig = { automatic_installation = false },
cursorHoldUptime = 100, -- millisecond. see ":h updatetime"
-- Change lsp.setup(opts). Format: {['lsp_name'] = settings}
-- ":h lspconfig-all" or https://github.com/neovim/nvim-lspconfig/blob/master/doc/server_configurations.md
-- for LSP configs provided by nvim-lspconfig
setup = {
-- https://github.com/neovim/nvim-lspconfig/blob/master/doc/server_configurations.md#tsserver
tsserver = {
filetypes = { -- limit tsserver only .ts files
'typescript',
'typescriptreact',
'typescript.tsx',
},
},
-- https://github.com/neovim/nvim-lspconfig/blob/master/doc/server_configurations.md#clangd
clangd = {
cmd = { 'clangd', '--enable-config' }, -- allow .clangd file
-- Fix: multiple different client offset_encodings detected for buffer
-- https://github.com/jose-elias-alvarez/null-ls.nvim/issues/428#issuecomment-997226723
capabilities = { offsetEncoding = { 'utf-16' } },
},
-- sumneko_lua = {
-- -- You can write lsp.settings in lua. Or write it to lsp-settings/<lsp_name>.yaml file (See nlsp features).
-- -- If have the same key, the value in the YAML file will take precedence.
-- settings = { Lua = { diagnostics = { globals = {} } } },
-- },
},
diagnostic = { -- :h vim.diagnostic.config
virtual_text = false,
signs = true,
underline = true,
update_in_insert = false,
severity_sort = true,
float = {
wrap = true,
max_width = 120,
max_height = 20,
focusable = false,
source = false, -- (boolean or string) Show source in diagnostics. Values: 'always' or 'if_many'
prefix = ' ',
scope = 'cursor',
format = function(d)
return string.format('%s %s %s: [%s] %s', SeverityMap[d.severity], d.col, d.source, d.code,
d.message)
end,
close_events = { 'BufLeave', 'CursorMoved', 'InsertEnter', 'FocusLost' },
border = { -- See the border property of ":h nvim_open_win"
{ '╭', 'LspWindowBorder' },
{ '─', 'LspWindowBorder' },
{ '╮', 'LspWindowBorder' },
{ '│', 'LspWindowBorder' },
{ '╯', 'LspWindowBorder' },
{ '─', 'LspWindowBorder' },
{ '╰', 'LspWindowBorder' },
{ '│', 'LspWindowBorder' },
},
-- border = {
-- { '╔', 'LspWindowBorder' },
-- { '═', 'LspWindowBorder' },
-- { '╗', 'LspWindowBorder' },
-- { '║', 'LspWindowBorder' },
-- { '╝', 'LspWindowBorder' },
-- { '═', 'LspWindowBorder' },
-- { '╚', 'LspWindowBorder' },
-- { '║', 'LspWindowBorder' },
-- },
},
},
},
}
local function setDefaultBorder(conf)
local border = conf.diagnostic.float.border
require('lspconfig.ui.windows').default_options.border = border -- This line maybe not work
vim.lsp.handlers['textDocument/hover'] = vim.lsp.with(vim.lsp.handlers.hover, { border = border })
vim.lsp.handlers['textDocument/signatureHelp'] = vim.lsp.with(vim.lsp.handlers.signature_help,
{ border = border })
end
function M.config(config)
local conf = config.lsp
vim.o.updatetime = conf.cursorHoldUptime
vim.lsp.set_log_level(conf.log.level)
vim.diagnostic.config(conf.diagnostic)
setDefaultBorder(conf)
local masonLspconfig = require('mason-lspconfig')
masonLspconfig.setup(conf.masonLspconfig)
-- Newly installed LSP without having to restart Neovim!
masonLspconfig.setup_handlers {
-- The first entry (without a key) will be the default handler
-- and will be called for each installed server that doesn't have
-- a dedicated handler.
function(serverName) -- default handler (optional)
local lspconfig = require('lspconfig')
-- https://github.com/hrsh7th/cmp-nvim-lsp#capabilities
local capabilities = require('cmp_nvim_lsp').default_capabilities()
local opts = config.lsp.setup[serverName] or {}
if type(opts) == 'function' then opts = opts(lspconfig) or {} end
opts = util.merge({
-- Use LspAttach autocmd insteads of on_attach function
-- on_attach = function(client, bufnr) end,
capabilities = capabilities,
autostart = true,
flags = {
debounce_text_changes = 150, -- This is default in neovim 0.7+
},
}, opts)
-- :h lspconfig-setup
lspconfig[serverName].setup(opts)
end,
-- Next, you can provide a dedicated handler for specific servers.
-- For example, a handler override for the `rust_analyzer`:
-- ['rust_analyzer'] = function()
-- require('rust-tools').setup {}
-- end,
}
end
M.signs = function()
local map = {
Error = symbolMap.ERROR,
Warn = symbolMap.WARN,
Hint = symbolMap.HINT,
Info = symbolMap.INFO,
}
local signs = {}
for type, icon in pairs(map) do
local hl = 'DiagnosticSign' .. type
signs[hl] = { text = icon, texthl = hl, numhl = hl }
end
return signs
end
M.keymaps = require('one.plugins.lsp.keymaps')
M.autocmds = {
-- Show line diagnostics automatically in hover window
-- The CursorHold autocmd is triggered when updatetime.
CursorHold = {
callback = function()
vim.diagnostic.open_float()
end,
},
}
return M