feat(lsp): overwrite omnifunc/tagfunc set by ftplugin #22267

Problem:
Some built-in ftplugins set omnifunc/tagfunc/formatexpr which causes
lsp.lua:set_defaults() to skip setup of defaults for those filetypes.
For example the C++ ftplugin has:
    omnifunc=ccomplete#Complete
          Last set from /usr/share/nvim/runtime/ftplugin/c.vim line 30
so the changes done in #95c65a6b221fe6e1cf91e8322e7d7571dc511a71
will always be skipped for C++ files.

Solution:
Overwrite omnifunc/tagfunc/formatexpr options that were set by stock
ftplugin.

Fixes #21001
This commit is contained in:
Michal Liszcz
2023-03-09 15:12:56 +01:00
committed by GitHub
parent ce0fddf5ae
commit 9ef7297ef1
4 changed files with 153 additions and 6 deletions

View File

@@ -1112,19 +1112,43 @@ function lsp.start_client(config)
end
end
---@private
-- Determines whether the given option can be set by `set_defaults`.
local function is_empty_or_default(bufnr, option)
if vim.bo[bufnr][option] == '' then
return true
end
local old_bufnr = vim.fn.bufnr('')
local last_set_from = vim.fn.gettext('\n\tLast set from ')
local line = vim.fn.gettext(' line ')
vim.cmd.buffer(bufnr)
local scriptname = vim.fn
.execute('verbose set ' .. option .. '?')
:match(last_set_from .. '(.*)' .. line .. '%d+')
vim.cmd.buffer(old_bufnr)
if not scriptname then
return false
end
local vimruntime = vim.fn.getenv('VIMRUNTIME')
return vim.startswith(vim.fn.expand(scriptname), vim.fn.expand(vimruntime))
end
---@private
local function set_defaults(client, bufnr)
local capabilities = client.server_capabilities
if capabilities.definitionProvider and vim.bo[bufnr].tagfunc == '' then
if capabilities.definitionProvider and is_empty_or_default(bufnr, 'tagfunc') then
vim.bo[bufnr].tagfunc = 'v:lua.vim.lsp.tagfunc'
end
if capabilities.completionProvider and vim.bo[bufnr].omnifunc == '' then
if capabilities.completionProvider and is_empty_or_default(bufnr, 'omnifunc') then
vim.bo[bufnr].omnifunc = 'v:lua.vim.lsp.omnifunc'
end
if
capabilities.documentRangeFormattingProvider
and vim.bo[bufnr].formatprg == ''
and vim.bo[bufnr].formatexpr == ''
and is_empty_or_default(bufnr, 'formatprg')
and is_empty_or_default(bufnr, 'formatexpr')
then
vim.bo[bufnr].formatexpr = 'v:lua.vim.lsp.formatexpr()'
end