How to extend (not override) default config?

I’m using nvim-lspconfig & I want to append a couple of filetypes to the default filetypes table of a specific LSP.

This overrides & I don’t want to override.

local lsp = require('lspconfig')

lsp.foo_lsp.setup {
  filetypes = {'filetype', 'filetype'}
}

I can’t do something like this for example, because each LSP is lazy loaded, so, this will throw

local lsp = require('lspconfig')
local configs = require('lspconfig/configs') 

configs.foo_lsp.default_config.filetypes = vim.tbl_extend('force', configs.foo_lsp.default_config.filetypes, { 'filetype', 'filetype' })

-- or 

lsp.foo_lsp.setup {
  filetypes = vim.tbl_extend('force', configs.foo_lsp.default_config.filetypes, { 'filetype', 'filetype' })
}

And I don’t want to copy the defaults because there is a chance that things will get out of sync if I duplicate this list. So what are my options if I want to do this?

1 Like

I recently looked into this myself (for exactly the same reason as you), and I concluded that it was not possible with the current API design.

If nobody else comes along with a solution, maybe file a feature request on their Github issue tracker. I will +1 it!

Yes you can do this, but not thru table.concat not what you think it does. filetypes is a table, table.concat returns a string not a table, what you want is table.insert. Here is an example with tsserver to add extra filetypes without messing with the default config:

local tsserver_filetypes = require('lspconfig/configs')['tsserver'].filetypes
table.insert(tsserver_filetypes, 'some_filetype')
table.insert(tsserver_filetypes, 'some_other_filetype')

require('lspconfig').tsserver.setup({
  filetypes = tsserver_filetypes,
})

@creativenull The point is not table.concat or table.insert, I can do it wit also with vim.tbl_extend, the main issue still remains. the code you shared will throw, you can try to run this

:lua print(require('lspconfig/configs')['tsserver'].filetypes)

Edit: I fixed the first post to use vim.tbl_extend to remove the confusion.

Ok did a little digging and seems like it’s not possible with the current implementation of how lspconfig setup works. Unless you want to call the setup() function per lsp twice, but that’s unintuitive.

But from what I know LSP servers are set for certain languages so it’s pretty rare for LSPs to change languages they support, given how scoped they are.

Which LSP server is this? If it supports a certain filetype and not in lspconfig you could make a PR to include it in the lspconfig repo.

The use case is if/when Nvim-Lspconfig changes, not the language server itself. And also just avoiding copy/paste in general.

1 Like

I don’t think lspconfig would be changing filetypes for LSPs ever so often, once a lsp is set it will only work for the languages it supports and lspconfig pretty much follows that (or I would assume so). The default config is where they are set and like I said, it’s very rare for lspconfig or LSPs in general to change their filetype.

So I think you should be fine in most cases setting up lspconfig without having the need to override the filetypes. But if you still want to be able to merge the default and your own filetypes together, then I guess there is no way to do that without copy/pasting, unfortunately. Sorry, wasn’t much of help on that domain.

The idea applies to any table in the default config that you would like to extend & not override. Not specific to filetypes. Filetypes maybe don’t change much but in each LSP but you have other configs that you might want to extend, some examples