diff options
author | TJ DeVries <devries.timothyj@gmail.com> | 2021-03-10 16:53:23 -0500 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-03-10 16:53:23 -0500 |
commit | 564dd7d8db110d134987f8619c3894d2149f8956 (patch) | |
tree | c640576e9074274a66982f26daecf72230653f57 /runtime/lua/vim/lsp.lua | |
parent | c29494b719d9706aa84758336b4c1e7ba6f86070 (diff) | |
download | rneovim-564dd7d8db110d134987f8619c3894d2149f8956.tar.gz rneovim-564dd7d8db110d134987f8619c3894d2149f8956.tar.bz2 rneovim-564dd7d8db110d134987f8619c3894d2149f8956.zip |
lsp: get_language_id (#14092)
* Allow specifying a languageId for a lsp
For some languages the filetype might not match the languageId the
language server accepts. In these cases the config for the language
server can contain a function which gets the current buffer and filetype
and returns a languageId. When it isn't provided the filetype is used
instead.
Example:
```lua
require'lspconfig'.sourcekit.setup{
get_language_id = function(bufnr, ft)
return 'swift'
end;
}
```
Closes #13093
* lsp: Change to get_language_id
Co-authored-by: Jan Dammshäuser <mail@jandamm.de>
Diffstat (limited to 'runtime/lua/vim/lsp.lua')
-rw-r--r-- | runtime/lua/vim/lsp.lua | 14 |
1 files changed, 12 insertions, 2 deletions
diff --git a/runtime/lua/vim/lsp.lua b/runtime/lua/vim/lsp.lua index c32413dc31..b8911f7f2f 100644 --- a/runtime/lua/vim/lsp.lua +++ b/runtime/lua/vim/lsp.lua @@ -228,6 +228,7 @@ local function validate_client_config(config) before_init = { config.before_init, "f", true }; offset_encoding = { config.offset_encoding, "s", true }; flags = { config.flags, "t", true }; + get_language_id = { config.get_language_id, "f", true }; } local cmd, cmd_args = lsp._cmd_parts(config.cmd) @@ -275,12 +276,13 @@ local function text_document_did_open_handler(bufnr, client) if not vim.api.nvim_buf_is_loaded(bufnr) then return end + local filetype = nvim_buf_get_option(bufnr, 'filetype') + local params = { textDocument = { version = 0; uri = vim.uri_from_bufnr(bufnr); - -- TODO make sure our filetypes are compatible with languageId names. - languageId = nvim_buf_get_option(bufnr, 'filetype'); + languageId = client.config.get_language_id(bufnr, filetype); text = buf_get_full_text(bufnr); } } @@ -407,6 +409,9 @@ end --- --@param name (string, default=client-id) Name in log messages. --- +--@param get_language_id function(bufnr, filetype) -> language ID as string. +--- Defaults to the filetype. +--- --@param offset_encoding (default="utf-16") One of "utf-8", "utf-16", --- or "utf-32" which is the encoding that the LSP server expects. Client does --- not verify this is correct. @@ -466,6 +471,11 @@ function lsp.start_client(config) config.flags = config.flags or {} config.settings = config.settings or {} + -- By default, get_language_id just returns the exact filetype it is passed. + -- It is possible to pass in something that will calculate a different filetype, + -- to be sent by the client. + config.get_language_id = config.get_language_id or function(_, filetype) return filetype end + local client_id = next_client_id() local handlers = config.handlers or {} |