Slotos: 3 Expose lsp config and use nvim_create_user_command Expose lsp config and use nvim_create_user_command Expose lsp config and use nvim_create_user_command 4 files changed, 135 insertions(+), 71 deletions(-)
Applied, thanks!
Thanks, if I'm understanding this correctly this lets the user call `require("clangd_extensions").prepare()` and then lspconfig's setup whenever they want?
That's exactly it. I'd updated the README to mention and explain `prepare` usage. README.md | 11 ++++-- lua/clangd_extensions/init.lua | 65 ++++++++++++++++++++++------------ 2 files changed, 51 insertions(+), 25 deletions(-) diff --git a/README.md b/README.md index 62d2613..49ff7c7 100644 --- a/README.md +++ b/README.md @@ -4,8 +4,15 @@ Install this plugin using any plugin/package manager or see [`:h packages`](https://neovim.io/doc/user/repeat.html#packages) ## Configuration: -This is the default config, you can call `require("clangd_extensions").setup()` with no arguments if you don't want to make changes. -Remove `require'lspconfig'.clangd.setup{}` from your config, this will be called by clangd_extensions.nvim. Use the `server` field below to customize `setup{}`. + +Calling `setup` will instruct `clangd_extensions` to configure lsp via lspconfig automatically. So if you use it, remove `require'lspconfig'.clangd.setup{}` from your config. Use the `server` config field to customize lspconfig settings. + +If you prefer to integrate `clangd_extensions` into your own LSP setup, `require("clangd_extensions").prepare()` applies passed configuration to `clangd_extensions` and returns lspconfig configuration table for further processing. Any customisations passed to `server` config field will be present in this returned configuration. + +### Default configuration + +You can call `require("clangd_extensions").setup()` or `require("clangd_extensions").prepare()` with no arguments if you don't want to make changes. + ```lua require("clangd_extensions").setup { server = { diff --git a/lua/clangd_extensions/init.lua b/lua/clangd_extensions/init.lua index 8ffba05..e729705 100644 --- a/lua/clangd_extensions/init.lua +++ b/lua/clangd_extensions/init.lua @@ -1,28 +1,17 @@ -local config = require("clangd_extensions.config") - local M = {} M.hint_aucmd_set_up = false -local commands = [[ -if !exists(':ClangdAST') - - function s:memuse_compl(_a,_b,_c) - return ['expand_preamble'] - endfunction - - command ClangdSetInlayHints lua require('clangd_extensions.inlay_hints').set_inlay_hints() - command ClangdDisableInlayHints lua require('clangd_extensions.inlay_hints').disable_inlay_hints() - command ClangdToggleInlayHints lua require('clangd_extensions.inlay_hints').toggle_inlay_hints() - command -range ClangdAST lua require('clangd_extensions.ast').display_ast(<line1>, <line2>) - command ClangdTypeHierarchy lua require('clangd_extensions.type_hierarchy').show_hierarchy() - command ClangdSymbolInfo lua require('clangd_extensions.symbol_info').show_symbol_info() - command -nargs=? -complete=customlist,s:memuse_compl ClangdMemoryUsage lua require('clangd_extensions.memory_usage').show_memory_usage('<args>' == 'expand_preamble') - -endif -]] - function M.setup(opts) + local lsp_config = M.prepare(opts) + + -- Call lspconfig setup + require("lspconfig").clangd.setup(lsp_config) +end + +function M.prepare(opts) + local config = require("clangd_extensions.config") + -- Set up extensions, get lspconfig opts config.setup(opts) -- Set up autocommands for inlay hints @@ -35,13 +24,43 @@ function M.setup(opts) require("clangd_extensions.inlay_hints").setup_autocmd() require("clangd_extensions.inlay_hints").set_inlay_hints() end - vim.cmd(commands) + M.setup_comands() end - -- Call lspconfig setup - require("lspconfig").clangd.setup(config.options.server) -- Set up AST state stuff require("clangd_extensions.ast").init() + + return config.options.server +end + +function M.setup_comands() + -- nvim_get_commands would work here, but we don't need _all_ the commands, just an existence check + if vim.fn.exists(':ClangdAST') == 0 then + local inlay_hints = require('clangd_extensions.inlay_hints') + local ast = require('clangd_extensions.ast') + local type_hierarchy = require('clangd_extensions.type_hierarchy') + local symbol_info = require('clangd_extensions.symbol_info') + local memory_usage = require('clangd_extensions.memory_usage') + + vim.api.nvim_create_user_command('ClangdSetInlayHints', inlay_hints.set_inlay_hints, { nargs = 0 }) + vim.api.nvim_create_user_command('ClangdDisableInlayHints', inlay_hints.disable_inlay_hints, { nargs = 0 }) + vim.api.nvim_create_user_command('ClangdToggleInlayHints', inlay_hints.toggle_inlay_hints, { nargs = 0 }) + vim.api.nvim_create_user_command('ClangAST', function(opts) ast.display_ast(opts.line1, opts.line2) end, { range = true, nargs = 0 }) + vim.api.nvim_create_user_command('ClangdTypeHierarchy', type_hierarchy.show_hierarchy, { nargs = 0 }) + vim.api.nvim_create_user_command('ClangdSymbolInfo', symbol_info.show_symbol_info, { nargs = 0 }) + vim.api.nvim_create_user_command( + 'ClangdMemoryUsage', + function(opts) + memory_usage.show_memory_usage(opts.args == 'expand_preamble') + end, + { + nargs = '?', + complete = function(_,_,_) + return {'expand_preamble'} + end + } + ) + end end return
If not, can you also change the readme explaining this?
Copy & paste the following snippet into your terminal to import this patchset into git:
curl -s https://lists.sr.ht/~p00f/public-inbox/patches/36753/mbox | git am -3Learn more about email & git
From: Slotos <slotos@gmail.com> --- lua/clangd_extensions/init.lua | 65 ++++++++++++++++++++++------------ 1 file changed, 42 insertions(+), 23 deletions(-) diff --git a/lua/clangd_extensions/init.lua b/lua/clangd_extensions/init.lua index 8ffba05..2e854ab 100644 --- a/lua/clangd_extensions/init.lua +++ b/lua/clangd_extensions/init.lua @@ -1,28 +1,17 @@ -local config = require("clangd_extensions.config") - local M = {} M.hint_aucmd_set_up = false -local commands = [[ -if !exists(':ClangdAST') - - function s:memuse_compl(_a,_b,_c) - return ['expand_preamble'] - endfunction - - command ClangdSetInlayHints lua require('clangd_extensions.inlay_hints').set_inlay_hints() - command ClangdDisableInlayHints lua require('clangd_extensions.inlay_hints').disable_inlay_hints() - command ClangdToggleInlayHints lua require('clangd_extensions.inlay_hints').toggle_inlay_hints() - command -range ClangdAST lua require('clangd_extensions.ast').display_ast(<line1>, <line2>) - command ClangdTypeHierarchy lua require('clangd_extensions.type_hierarchy').show_hierarchy() - command ClangdSymbolInfo lua require('clangd_extensions.symbol_info').show_symbol_info() - command -nargs=? -complete=customlist,s:memuse_compl ClangdMemoryUsage lua require('clangd_extensions.memory_usage').show_memory_usage('<args>' == 'expand_preamble') - -endif -]] - function M.setup(opts) + local lsp_config = M.prepare(opts) + + -- Call lspconfig setup + require("lspconfig").clangd.setup(lsp_config) +end + +function M.prepare(opts) + local config = require("clangd_extensions.config") + -- Set up extensions, get lspconfig opts config.setup(opts) -- Set up autocommands for inlay hints @@ -35,13 +24,43 @@ function M.setup(opts) require("clangd_extensions.inlay_hints").setup_autocmd() require("clangd_extensions.inlay_hints").set_inlay_hints() end - vim.cmd(commands) + M.setup_comands() end - -- Call lspconfig setup - require("lspconfig").clangd.setup(config.options.server) -- Set up AST state stuff require("clangd_extensions.ast").init() + + return config.options.server +end + +function M.setup_comands() + -- nvim_get_commands would work here, but we don't need _all_ the commands, just an existence check + if vim.fn.exists(':ClangdAST') ~= 0 then + local inlay_hints = require('clangd_extensions.inlay_hints') + local ast = require('clangd_extensions.ast') + local type_hierarchy = require('clangd_extensions.type_hierarchy') + local symbol_info = require('clangd_extensions.symbol_info') + local memory_usage = require('clangd_extensions.memory_usage') + + vim.api.nvim_create_user_command('ClangdSetInlayHints', inlay_hints.set_inlay_hints) + vim.api.nvim_create_user_command('ClangdDisableInlayHints', inlay_hints.disable_inlay_hints) + vim.api.nvim_create_user_command('ClangdToggleInlayHints', inlay_hints.toggle_inlay_hints) + vim.api.nvim_create_user_command('ClangAST', function(opts) ast.display_ast(opts.line1, opts.line2) end, { range = true }) + vim.api.nvim_create_user_command('ClangdTypeHierarchy', type_hierarchy.show_hierarchy) + vim.api.nvim_create_user_command('ClangdSymbolInfo', symbol_info.show_symbol_info) + vim.api.nvim_create_user_command( + 'ClangdMemoryUsage', + function(opts) + memory_usage.show_memory_usage(opts.args == 'expand_preamble') + end, + { + nargs = '?', + complete = function(_,_,_) + return {'expand_preamble'} + end + } + ) + end end return M -- 2.38.1
The idea behind this change is to separate lspconfig call and preparation for the call. This would allow users to choose whether to skip clangd setup entirely in favor of this plugin, or incorporate it by preparing and using returned configuration in their own setup routine. --- Generally speaking, with new https://neovim.io/doc/user/lsp.html#LspAttach autocommand, the plugin could avoid any need for setup for default configuration at all. But that requires handling of late setups, autocommand registration after LSP server start etc. Thus a simpler and somewhat crude approach. First patch had a logical mistake that I managed to not test for - it checked for absence of previously set command and skipped setting it if it was set. This version fixes it and seems to work just fine. lua/clangd_extensions/init.lua | 65 ++++++++++++++++++++++------------ 1 file changed, 42 insertions(+), 23 deletions(-) diff --git a/lua/clangd_extensions/init.lua b/lua/clangd_extensions/init.lua index 8ffba05..e729705 100644 --- a/lua/clangd_extensions/init.lua +++ b/lua/clangd_extensions/init.lua @@ -1,28 +1,17 @@ -local config = require("clangd_extensions.config") - local M = {} M.hint_aucmd_set_up = false -local commands = [[ -if !exists(':ClangdAST') - - function s:memuse_compl(_a,_b,_c) - return ['expand_preamble'] - endfunction - - command ClangdSetInlayHints lua require('clangd_extensions.inlay_hints').set_inlay_hints() - command ClangdDisableInlayHints lua require('clangd_extensions.inlay_hints').disable_inlay_hints() - command ClangdToggleInlayHints lua require('clangd_extensions.inlay_hints').toggle_inlay_hints() - command -range ClangdAST lua require('clangd_extensions.ast').display_ast(<line1>, <line2>) - command ClangdTypeHierarchy lua require('clangd_extensions.type_hierarchy').show_hierarchy() - command ClangdSymbolInfo lua require('clangd_extensions.symbol_info').show_symbol_info() - command -nargs=? -complete=customlist,s:memuse_compl ClangdMemoryUsage lua require('clangd_extensions.memory_usage').show_memory_usage('<args>' == 'expand_preamble') - -endif -]] - function M.setup(opts) + local lsp_config = M.prepare(opts) + + -- Call lspconfig setup + require("lspconfig").clangd.setup(lsp_config) +end + +function M.prepare(opts) + local config = require("clangd_extensions.config") + -- Set up extensions, get lspconfig opts config.setup(opts) -- Set up autocommands for inlay hints @@ -35,13 +24,43 @@ function M.setup(opts) require("clangd_extensions.inlay_hints").setup_autocmd() require("clangd_extensions.inlay_hints").set_inlay_hints() end - vim.cmd(commands) + M.setup_comands() end - -- Call lspconfig setup - require("lspconfig").clangd.setup(config.options.server) -- Set up AST state stuff require("clangd_extensions.ast").init() + + return config.options.server +end + +function M.setup_comands() + -- nvim_get_commands would work here, but we don't need _all_ the commands, just an existence check + if vim.fn.exists(':ClangdAST') == 0 then + local inlay_hints = require('clangd_extensions.inlay_hints') + local ast = require('clangd_extensions.ast') + local type_hierarchy = require('clangd_extensions.type_hierarchy') + local symbol_info = require('clangd_extensions.symbol_info') + local memory_usage = require('clangd_extensions.memory_usage') + + vim.api.nvim_create_user_command('ClangdSetInlayHints', inlay_hints.set_inlay_hints, { nargs = 0 }) + vim.api.nvim_create_user_command('ClangdDisableInlayHints', inlay_hints.disable_inlay_hints, { nargs = 0 }) + vim.api.nvim_create_user_command('ClangdToggleInlayHints', inlay_hints.toggle_inlay_hints, { nargs = 0 }) + vim.api.nvim_create_user_command('ClangAST', function(opts) ast.display_ast(opts.line1, opts.line2) end, { range = true, nargs = 0 }) + vim.api.nvim_create_user_command('ClangdTypeHierarchy', type_hierarchy.show_hierarchy, { nargs = 0 }) + vim.api.nvim_create_user_command('ClangdSymbolInfo', symbol_info.show_symbol_info, { nargs = 0 }) + vim.api.nvim_create_user_command( + 'ClangdMemoryUsage', + function(opts) + memory_usage.show_memory_usage(opts.args == 'expand_preamble') + end, + { + nargs = '?', + complete = function(_,_,_) + return {'expand_preamble'} + end + } + ) + end end return M -- 2.38.1
Applied, thanks!
Thanks, if I'm understanding this correctly this lets the user call `require("clangd_extensions").prepare()` and then lspconfig's setup whenever they want? If not, can you also change the readme explaining this?
The idea behind this change is to separate lspconfig call and preparation for the call. This would allow users to choose whether to skip clangd setup entirely in favor of this plugin, or incorporate it by preparing and using returned configuration in their own setup routine. --- > this lets the user call `require("clangd_extensions").prepare()` and then lspconfig's setup whenever they want? That's exactly it. I'd updated the README to mention and explain `prepare` usage. README.md | 11 ++++-- lua/clangd_extensions/init.lua | 65 ++++++++++++++++++++++------------ 2 files changed, 51 insertions(+), 25 deletions(-) diff --git a/README.md b/README.md index 62d2613..49ff7c7 100644 --- a/README.md +++ b/README.md @@ -4,8 +4,15 @@ Install this plugin using any plugin/package manager or see [`:h packages`](https://neovim.io/doc/user/repeat.html#packages) ## Configuration: -This is the default config, you can call `require("clangd_extensions").setup()` with no arguments if you don't want to make changes. -Remove `require'lspconfig'.clangd.setup{}` from your config, this will be called by clangd_extensions.nvim. Use the `server` field below to customize `setup{}`. + +Calling `setup` will instruct `clangd_extensions` to configure lsp via lspconfig automatically. So if you use it, remove `require'lspconfig'.clangd.setup{}` from your config. Use the `server` config field to customize lspconfig settings. + +If you prefer to integrate `clangd_extensions` into your own LSP setup, `require("clangd_extensions").prepare()` applies passed configuration to `clangd_extensions` and returns lspconfig configuration table for further processing. Any customisations passed to `server` config field will be present in this returned configuration. + +### Default configuration + +You can call `require("clangd_extensions").setup()` or `require("clangd_extensions").prepare()` with no arguments if you don't want to make changes. + ```lua require("clangd_extensions").setup { server = { diff --git a/lua/clangd_extensions/init.lua b/lua/clangd_extensions/init.lua index 8ffba05..e729705 100644 --- a/lua/clangd_extensions/init.lua +++ b/lua/clangd_extensions/init.lua @@ -1,28 +1,17 @@ -local config = require("clangd_extensions.config") - local M = {} M.hint_aucmd_set_up = false -local commands = [[ -if !exists(':ClangdAST') - - function s:memuse_compl(_a,_b,_c) - return ['expand_preamble'] - endfunction - - command ClangdSetInlayHints lua require('clangd_extensions.inlay_hints').set_inlay_hints() - command ClangdDisableInlayHints lua require('clangd_extensions.inlay_hints').disable_inlay_hints() - command ClangdToggleInlayHints lua require('clangd_extensions.inlay_hints').toggle_inlay_hints() - command -range ClangdAST lua require('clangd_extensions.ast').display_ast(<line1>, <line2>) - command ClangdTypeHierarchy lua require('clangd_extensions.type_hierarchy').show_hierarchy() - command ClangdSymbolInfo lua require('clangd_extensions.symbol_info').show_symbol_info() - command -nargs=? -complete=customlist,s:memuse_compl ClangdMemoryUsage lua require('clangd_extensions.memory_usage').show_memory_usage('<args>' == 'expand_preamble') - -endif -]] - function M.setup(opts) + local lsp_config = M.prepare(opts) + + -- Call lspconfig setup + require("lspconfig").clangd.setup(lsp_config) +end + +function M.prepare(opts) + local config = require("clangd_extensions.config") + -- Set up extensions, get lspconfig opts config.setup(opts) -- Set up autocommands for inlay hints @@ -35,13 +24,43 @@ function M.setup(opts) require("clangd_extensions.inlay_hints").setup_autocmd() require("clangd_extensions.inlay_hints").set_inlay_hints() end - vim.cmd(commands) + M.setup_comands() end - -- Call lspconfig setup - require("lspconfig").clangd.setup(config.options.server) -- Set up AST state stuff require("clangd_extensions.ast").init() + + return config.options.server +end + +function M.setup_comands() + -- nvim_get_commands would work here, but we don't need _all_ the commands, just an existence check + if vim.fn.exists(':ClangdAST') == 0 then + local inlay_hints = require('clangd_extensions.inlay_hints') + local ast = require('clangd_extensions.ast') + local type_hierarchy = require('clangd_extensions.type_hierarchy') + local symbol_info = require('clangd_extensions.symbol_info') + local memory_usage = require('clangd_extensions.memory_usage') + + vim.api.nvim_create_user_command('ClangdSetInlayHints', inlay_hints.set_inlay_hints, { nargs = 0 }) + vim.api.nvim_create_user_command('ClangdDisableInlayHints', inlay_hints.disable_inlay_hints, { nargs = 0 }) + vim.api.nvim_create_user_command('ClangdToggleInlayHints', inlay_hints.toggle_inlay_hints, { nargs = 0 }) + vim.api.nvim_create_user_command('ClangAST', function(opts) ast.display_ast(opts.line1, opts.line2) end, { range = true, nargs = 0 }) + vim.api.nvim_create_user_command('ClangdTypeHierarchy', type_hierarchy.show_hierarchy, { nargs = 0 }) + vim.api.nvim_create_user_command('ClangdSymbolInfo', symbol_info.show_symbol_info, { nargs = 0 }) + vim.api.nvim_create_user_command( + 'ClangdMemoryUsage', + function(opts) + memory_usage.show_memory_usage(opts.args == 'expand_preamble') + end, + { + nargs = '?', + complete = function(_,_,_) + return {'expand_preamble'} + end + } + ) + end end return M -- 2.38.1