mirror of
https://github.com/neovim/neovim.git
synced 2025-10-26 12:27:24 +00:00
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>
This commit is contained in:
@@ -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 {}
|
||||
|
||||
Reference in New Issue
Block a user