From 5d5b068d5bdd2fe3787eb619097a5af08ef929e2 Mon Sep 17 00:00:00 2001 From: Jesse-Bakker Date: Tue, 11 Feb 2020 07:53:14 +0100 Subject: [PATCH] LSP: Refine formatting tabSize #11834 Use the logic explained in the softtabstop help section for defining the tabSize parameter in formatting requests. This means that: - if softtabstop is 0, tabstop is used - if softtabstop < 0, shiftwidth is used - if softtabstop > 0, softtabstop is used When inserting spaces instead of tabs, softtabstop is used in vim. Therefor it would be more logical to use it when formatting instead of the current tabstop. --- runtime/lua/vim/lsp/buf.lua | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/runtime/lua/vim/lsp/buf.lua b/runtime/lua/vim/lsp/buf.lua index a6a05fb095..19deb5df45 100644 --- a/runtime/lua/vim/lsp/buf.lua +++ b/runtime/lua/vim/lsp/buf.lua @@ -68,8 +68,9 @@ end function M.formatting(options) validate { options = {options, 't', true} } + local sts = vim.bo.softtabstop; options = vim.tbl_extend('keep', options or {}, { - tabSize = vim.bo.tabstop; + tabSize = (sts > 0 and sts) or (sts < 0 and vim.bo.shiftwidth) or vim.bo.tabstop; insertSpaces = vim.bo.expandtab; }) local params = { @@ -85,8 +86,9 @@ function M.range_formatting(options, start_pos, end_pos) start_pos = {start_pos, 't', true}; end_pos = {end_pos, 't', true}; } + local sts = vim.bo.softtabstop; options = vim.tbl_extend('keep', options or {}, { - tabSize = vim.bo.tabstop; + tabSize = (sts > 0 and sts) or (sts < 0 and vim.bo.shiftwidth) or vim.bo.tabstop; insertSpaces = vim.bo.expandtab; }) local A = list_extend({}, start_pos or api.nvim_buf_get_mark(0, '<'))