have to default to global shiftwidth

This commit is contained in:
Rustum Zia 2026-02-26 15:51:00 +01:00
parent cd1cd54849
commit ed14c4d855

View file

@ -107,8 +107,11 @@ end, function(bufnr, root, lang)
return tostring(bufnr) .. root:id() .. '_' .. lang
end)
---@param bufnr integer
---@param lang string
---@return integer
local get_language_shiftwidth = memoize(function(bufnr, lang)
local buffer_shiftwidth = vim.bo.shiftwidth
---@type integer
local global_shiftwidth = vim.go.shiftwidth
-- See :h 'shiftwidth': If set to 0, should use tabstop (0 is not the default value,
-- but users may rely on this behavior)
@ -119,16 +122,19 @@ local get_language_shiftwidth = memoize(function(bufnr, lang)
global_shiftwidth = vim.bo.tabstop
end
---@type integer|nil
local lang_shiftwidth = nil
-- Note: get_filetypes(lang) always includes `lang` in the returned array of filetypes even if
-- `lang` is not a filetype
---@type string[]
local filetypes = vim.treesitter.language.get_filetypes(lang)
for _, ft in ipairs(filetypes) do
-- filetype.get_option will default to the global value for the option
-- if (1) there is no local equivalent set, or (2) the filetype does not exist
---@type integer
local filetype_shiftwidth = vim.filetype.get_option(ft, 'shiftwidth')
if filetype_shiftwidth == 0 then
filetype_shiftwidth = vim.filetype.get_option(ft, 'tabstop')
filetype_shiftwidth = vim.filetype.get_option(ft, 'tabstop')--[[@as integer]]
end
if filetype_shiftwidth ~= global_shiftwidth then
lang_shiftwidth = filetype_shiftwidth
@ -137,7 +143,7 @@ local get_language_shiftwidth = memoize(function(bufnr, lang)
-- if lang_shiftwidth is nil, then it is either unset OR it is the same as
-- global_shiftwidth
return lang_shiftwidth or global_shiftwidth or buffer_shiftwidth
return lang_shiftwidth or global_shiftwidth
end, function(bufnr, lang)
return tostring(bufnr) .. '_' .. lang
end)