-- jdtls formats with an Eclipse formatter profile. The project keeps a flat .properties file -- (the one Spotless uses) in config/eclipse-formatter.properties, but jdtls wants an Eclipse -- XML export, so convert it to a cached XML file and point jdtls at that. local PROFILE = 'GTCS1332' ---@param root_dir string|nil ---@return string|nil local function formatter_url(root_dir) local props_path = (root_dir or vim.fn.getcwd()) .. '/config/eclipse-formatter.properties' local stat = vim.uv.fs_stat(props_path) if not stat then return nil end local xml_path = vim.fn.stdpath 'cache' .. '/jdtls-formatter-' .. vim.fn.sha256(props_path) .. '.xml' local xml_stat = vim.uv.fs_stat(xml_path) if not xml_stat or xml_stat.mtime.sec < stat.mtime.sec then local lines = { '', '', (''):format(PROFILE), } for line in io.lines(props_path) do local key, value = line:match '^%s*([^=%s]+)%s*=%s*(.-)%s*$' if key and not key:match '^#' then value = value:gsub('&', '&'):gsub('"', '"'):gsub('<', '<'):gsub('>', '>') table.insert(lines, (''):format(key, value)) end end table.insert(lines, '') table.insert(lines, '') local f = io.open(xml_path, 'w') if f then f:write(table.concat(lines, '\n')) f:close() end end return xml_path end return { -- client.settings is snapshotted when the client is constructed, so it has to be changed (and -- pushed to the server) here, after creation, rather than in before_init or config.settings. on_init = function(client) local xml_path = formatter_url(client.root_dir or (client.config and client.config.root_dir)) if not xml_path then return end client.settings = vim.tbl_deep_extend('force', client.settings or {}, { java = { format = { enabled = true, settings = { url = xml_path, profile = PROFILE } } }, }) client:notify('workspace/didChangeConfiguration', { settings = client.settings }) end, }