2021-04-19 02:56:32 +00:00
|
|
|
local vim, api = vim, vim.api
|
2022-01-17 05:03:48 +00:00
|
|
|
local lsp = require('vim.lsp')
|
2021-04-19 02:56:32 +00:00
|
|
|
|
2022-01-17 05:03:48 +00:00
|
|
|
local util = require('navigator.util')
|
2021-04-19 02:56:32 +00:00
|
|
|
local log = util.log
|
2021-05-21 11:39:46 +00:00
|
|
|
local trace = util.trace
|
2022-07-16 23:26:26 +00:00
|
|
|
_NG_Attached = {}
|
2021-04-19 02:56:32 +00:00
|
|
|
local M = {}
|
|
|
|
|
|
|
|
M.on_attach = function(client, bufnr)
|
2022-01-17 05:03:48 +00:00
|
|
|
bufnr = bufnr or 0
|
|
|
|
|
|
|
|
if bufnr == 0 then
|
2022-04-28 09:56:18 +00:00
|
|
|
vim.notify('no bufnr provided from LSP ' .. client.name, vim.log.levels.DEBUG)
|
2022-01-17 05:03:48 +00:00
|
|
|
end
|
2021-05-27 01:10:32 +00:00
|
|
|
local uri = vim.uri_from_bufnr(bufnr)
|
2021-09-19 02:42:34 +00:00
|
|
|
|
2022-01-17 05:03:48 +00:00
|
|
|
if uri == 'file://' or uri == 'file:///' or #uri < 11 then
|
|
|
|
log('skip for float buffer', uri)
|
|
|
|
return { error = 'invalid file', result = nil }
|
2021-05-27 01:10:32 +00:00
|
|
|
end
|
2021-10-14 03:55:40 +00:00
|
|
|
|
2022-01-17 05:03:48 +00:00
|
|
|
log('attaching: ', bufnr, client.name, uri)
|
2021-10-14 03:55:40 +00:00
|
|
|
|
|
|
|
trace(client)
|
2022-07-16 23:26:26 +00:00
|
|
|
_NG_Attached[client.name] = true
|
2021-06-17 23:24:02 +00:00
|
|
|
|
2021-05-28 00:10:28 +00:00
|
|
|
-- add highlight for Lspxxx
|
2022-01-17 05:03:48 +00:00
|
|
|
require('navigator.lspclient.highlight').add_highlight()
|
|
|
|
require('navigator.lspclient.highlight').diagnositc_config_sign()
|
|
|
|
api.nvim_buf_set_option(bufnr, 'omnifunc', 'v:lua.vim.lsp.omnifunc')
|
2021-04-19 02:56:32 +00:00
|
|
|
|
2022-01-17 05:03:48 +00:00
|
|
|
require('navigator.lspclient.mapping').setup({
|
2021-05-17 03:15:15 +00:00
|
|
|
client = client,
|
|
|
|
bufnr = bufnr,
|
|
|
|
})
|
2021-05-08 04:54:37 +00:00
|
|
|
|
2022-05-28 09:37:15 +00:00
|
|
|
if client.server_capabilities.documentHighlightProvider == true then
|
2022-07-29 09:02:43 +00:00
|
|
|
trace('attaching doc highlight: ', bufnr, client.name)
|
|
|
|
vim.defer_fn(function()
|
|
|
|
require('navigator.dochighlight').documentHighlight(bufnr)
|
2022-11-24 02:42:15 +00:00
|
|
|
end, 50) -- allow a bit time for it to settle down
|
2022-07-29 09:02:43 +00:00
|
|
|
else
|
|
|
|
log('skip doc highlight: ', bufnr, client.name)
|
2021-04-19 02:56:32 +00:00
|
|
|
end
|
|
|
|
|
2022-01-17 05:03:48 +00:00
|
|
|
require('navigator.lspclient.lspkind').init()
|
2021-04-19 02:56:32 +00:00
|
|
|
|
2022-01-17 05:03:48 +00:00
|
|
|
local config = require('navigator').config_values()
|
|
|
|
trace(client.name, 'navigator on attach')
|
2021-05-23 04:10:49 +00:00
|
|
|
if config.on_attach ~= nil then
|
2022-01-17 05:03:48 +00:00
|
|
|
log(client.name, 'customized attach for all clients')
|
2021-05-23 04:10:49 +00:00
|
|
|
config.on_attach(client, bufnr)
|
|
|
|
end
|
2022-03-10 01:25:31 +00:00
|
|
|
if config.lsp and config.lsp[client.name] then
|
|
|
|
if type(config.lsp[client.name]) == 'function' then
|
|
|
|
local attach = config.lsp[client.name]().on_attach
|
|
|
|
if attach then
|
|
|
|
attach(client, bufnr)
|
|
|
|
end
|
|
|
|
elseif config.lsp[client.name].on_attach ~= nil then
|
|
|
|
log(client.name, 'customized attach for this client')
|
|
|
|
log('lsp client specific attach for', client.name)
|
|
|
|
config.lsp[client.name].on_attach(client, bufnr)
|
|
|
|
end
|
2021-05-23 04:10:49 +00:00
|
|
|
end
|
2021-06-17 14:49:13 +00:00
|
|
|
|
2022-11-24 02:42:15 +00:00
|
|
|
--- if code lens enabled
|
|
|
|
if _NgConfigValues.lsp.code_lens_action.enable then
|
|
|
|
if client.server_capabilities.codeLensProvider then
|
|
|
|
require('navigator.codelens').setup(bufnr)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-10-13 09:27:46 +00:00
|
|
|
if _NgConfigValues.lsp.code_action.enable then
|
2022-11-24 02:52:02 +00:00
|
|
|
if client.server_capabilities.codeActionProvider and client.name ~= 'null-ls' then
|
2022-11-26 01:15:42 +00:00
|
|
|
trace('code action enabled for client', client.server_capabilities.codeActionProvider)
|
2022-07-27 08:17:07 +00:00
|
|
|
api.nvim_create_autocmd({ 'CursorHold', 'CursorHoldI' }, {
|
2022-11-24 02:42:15 +00:00
|
|
|
group = api.nvim_create_augroup('NGCodeActGroup_' .. tostring(bufnr), {}),
|
2022-07-27 08:17:07 +00:00
|
|
|
buffer = bufnr,
|
|
|
|
callback = function()
|
2022-07-31 05:49:00 +00:00
|
|
|
require('navigator.codeAction').code_action_prompt(bufnr)
|
2022-07-27 08:17:07 +00:00
|
|
|
end,
|
|
|
|
})
|
2021-10-13 09:27:46 +00:00
|
|
|
end
|
|
|
|
end
|
2021-04-19 02:56:32 +00:00
|
|
|
end
|
|
|
|
|
2021-05-28 00:10:28 +00:00
|
|
|
-- M.setup = function(cfg)
|
|
|
|
-- return M
|
|
|
|
-- end
|
2021-04-19 02:56:32 +00:00
|
|
|
|
|
|
|
return M
|