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 5a04332
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 configuration_handler(err, _, params, client_id, bufnr, config)
assert(not err, vim.inspect(err))
local client = vim.lsp.get_client_by_id(client_id)
if not client then
vim.notify('LSP[id=' .. client_id .. '] client has shut down after sending the message', vim.log.levels.ERROR)
return
end

local result = {}
for _, item in ipairs(params.items or {}) do
if item.section == 'java.format.insertSpaces' then
table.insert(result, vim.bo.expandtab)
elseif item.section == 'java.format.tabSize' then
local sts = vim.bo.softtabstop
local tab_size = (sts > 0 and sts) or (sts < 0 and vim.bo.shiftwidth) or vim.bo.tabstop
table.insert(result, tab_size)
else
local value = vim.lsp.util.lookup_section(client.config.settings, item.section) or vim.NIL
if value == vim.NIL and item.section == '' then
value = client.config.settings or vim.NIL
end
table.insert(result, value)
end
end
return result
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 5a04332

Please sign in to comment.