Skip to content

Commit

Permalink
fix(lsp): set tabSize from 'shiftwidth', not 'softtabstop' (#17787)
Browse files Browse the repository at this point in the history
The use of 'softtabstop' to set tabSize was introduced in 5d5b068,
replacing 'tabstop'.  If we look past the name tabSize and at the actual
purpose of the field, it's the indentation width used when formatting.
This corresponds to the Vim option 'shiftwidth', not 'softtabstop'.
The latter has the comparatively mundane purpose of controlling what
happens when you hit the tab key (and even this is incomplete, as it
fails to account for 'smarttab').
  • Loading branch information
tpope authored Mar 20, 2022
1 parent 4637389 commit af427de
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 15 deletions.
6 changes: 3 additions & 3 deletions runtime/doc/lsp.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1458,17 +1458,17 @@ extract_completion_items({result})
https://microsoft.github.io/language-server-protocol/specification#textDocument_completion

get_effective_tabstop({bufnr}) *vim.lsp.util.get_effective_tabstop()*
Returns visual width of tabstop.
Returns indentation size.

Parameters: ~
{bufnr} (optional, number): Buffer handle, defaults to
current

Return: ~
(number) tabstop visual width
(number) indentation size

See also: ~
|softtabstop|
|shiftwidth|

*vim.lsp.util.jump_to_location()*
jump_to_location({location}, {offset_encoding})
Expand Down
10 changes: 5 additions & 5 deletions runtime/lua/vim/lsp/util.lua
Original file line number Diff line number Diff line change
Expand Up @@ -1894,16 +1894,16 @@ end
function M.make_workspace_params(added, removed)
return { event = { added = added; removed = removed; } }
end
--- Returns visual width of tabstop.
--- Returns indentation size.
---
---@see |softtabstop|
---@see |shiftwidth|
---@param bufnr (optional, number): Buffer handle, defaults to current
---@returns (number) tabstop visual width
---@returns (number) indentation size
function M.get_effective_tabstop(bufnr)
validate { bufnr = {bufnr, 'n', true} }
local bo = bufnr and vim.bo[bufnr] or vim.bo
local sts = bo.softtabstop
return (sts > 0 and sts) or (sts < 0 and bo.shiftwidth) or bo.tabstop
local sw = bo.shiftwidth
return (sw == 0 and bo.tabstop) or sw
end

--- Creates a `DocumentFormattingParams` object for the current buffer and cursor position.
Expand Down
12 changes: 5 additions & 7 deletions test/functional/plugin/lsp_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2374,18 +2374,16 @@ describe('LSP', function()
end)

describe('lsp.util.get_effective_tabstop', function()
local function test_tabstop(tabsize, softtabstop)
local function test_tabstop(tabsize, shiftwidth)
exec_lua(string.format([[
vim.api.nvim_buf_set_option(0, 'softtabstop', %d)
vim.api.nvim_buf_set_option(0, 'shiftwidth', %d)
vim.api.nvim_buf_set_option(0, 'tabstop', 2)
vim.api.nvim_buf_set_option(0, 'shiftwidth', 3)
]], softtabstop))
]], shiftwidth))
eq(tabsize, exec_lua('return vim.lsp.util.get_effective_tabstop()'))
end

it('with softtabstop = 1', function() test_tabstop(1, 1) end)
it('with softtabstop = 0', function() test_tabstop(2, 0) end)
it('with softtabstop = -1', function() test_tabstop(3, -1) end)
it('with shiftwidth = 1', function() test_tabstop(1, 1) end)
it('with shiftwidth = 0', function() test_tabstop(2, 0) end)
end)

describe('vim.lsp.buf.outgoing_calls', function()
Expand Down

0 comments on commit af427de

Please sign in to comment.