Skip to content

Commit

Permalink
Add workspace/configuration handler that recognizes insertSpaces/tabSize
Browse files Browse the repository at this point in the history
If using a eclipse.jdt.ls version that includes
eclipse-jdtls/eclipse.jdt.ls#1657 this will finally
make code-actions respect the clients shiftwidth/expandtab options and
no longer include tabs when the user uses spaces.

The alternative to overriding the handler would have been to document
that users should include these options in their `config.settings`, but
given that there is no good reason that I can think of where you don't
want to respond with your local editor settings it seems justified to
include this out of the box instead of requiring every user to add this.
  • Loading branch information
mfussenegger committed Feb 24, 2021
1 parent df23263 commit 373fb66
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions lua/jdtls/setup.lua
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,34 @@ local extendedClientCapabilities = {
};


local function tabsize(bufnr)
local get_option = api.nvim_buf_get_option
local sts = get_option(bufnr, 'softtabstop')
return (
(sts > 0 and sts)
or (sts < 0 and get_option(bufnr, 'shiftwidth'))
or get_option(bufnr, 'tabstop')
)
end


local function configuration_handler(err, method, params, client_id, bufnr, config)
local client = vim.lsp.get_client_by_id(client_id)
-- This isn't done in start_or_attach because a user could use a plugin like editorconfig to configue tabsize/spaces
-- That plugin may run after `start_or_attach` which is why we defer the setting lookup.
-- This ensures the language-server will use the latest version of the options
client.config.settings = vim.tbl_deep_extend('keep', client.config.settings or {}, {
java = {
format = {
insertSpaces = api.nvim_buf_get_option(bufnr, 'expandtab'),
tabSize = tabsize(bufnr),
}
}
})
return vim.lsp.handlers['workspace/configuration'](err, method, params, client_id, bufnr, config)
end


local function start_or_attach(config)
assert(config, 'config is required')
assert(
Expand Down Expand Up @@ -82,6 +110,7 @@ local function start_or_attach(config)
)
config.handlers = config.handlers or {}
config.handlers['language/status'] = config.handlers['language/status'] or status_callback
config.handlers['workspace/configuration'] = config.handlers['workspace/configuration'] or configuration_handler
config.capabilities = config.capabilities or lsp.protocol.make_client_capabilities()
config.capabilities.textDocument.codeAction = {
dynamicRegistration = false;
Expand Down

0 comments on commit 373fb66

Please sign in to comment.