From fa2520242e19064d110dc2150081f8970f198aa9 Mon Sep 17 00:00:00 2001 From: Mildred Ki'Lya Date: Fri, 29 Jul 2022 23:52:12 +0200 Subject: [PATCH] Improve setting option values, and add user events Add user events to allow the user to customize the indentation after the detection, to fix some options that might be inherited from the initial configuration. Allows to customize via the configuration which options are set. When tabs are detected, set shiftwidth=0 to ensure that the shifting value is the same as the tabstop. Same with softtabstop=0 to disable it. When spaces are detected, make optional setting of the tabstop and softtabstop option. --- lua/guess-indent/config.lua | 2 ++ lua/guess-indent/init.lua | 15 +++++++++++++-- 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/lua/guess-indent/config.lua b/lua/guess-indent/config.lua index 017c0a1..483e1cd 100644 --- a/lua/guess-indent/config.lua +++ b/lua/guess-indent/config.lua @@ -2,6 +2,8 @@ local M = {} local default_config = { auto_cmd = true, + set_tabstop = true, + set_softtabstop = true, filetype_exclude = { "netrw", "tutor", diff --git a/lua/guess-indent/init.lua b/lua/guess-indent/init.lua index 31ef811..87f9c4b 100644 --- a/lua/guess-indent/init.lua +++ b/lua/guess-indent/init.lua @@ -66,15 +66,26 @@ local function set_indentation(indentation) if indentation == "tabs" then set_buffer_opt(0, "expandtab", false) + set_buffer_opt(0, "shiftwidth", 0) + if config.set_softtabstop then + set_buffer_opt(0, "softtabstop", 0) + end print("Did set indentation to tabs.") + vim.api.nvim_exec_autocmds("User", { pattern = "GuessIndentTabs" }) elseif type(indentation) == "number" and indentation > 0 then set_buffer_opt(0, "expandtab", true) - set_buffer_opt(0, "tabstop", indentation) - set_buffer_opt(0, "softtabstop", indentation) set_buffer_opt(0, "shiftwidth", indentation) + if config.set_softtabstop then + set_buffer_opt(0, "softtabstop", indentation) + end + if config.set_tabstop then + set_buffer_opt(0, "tabstop", indentation) + end print("Did set indentation to", indentation, "spaces.") + vim.api.nvim_exec_autocmds("User", { pattern = "GuessIndentSpaces" }) else print("Failed to detect indentation style.") + vim.api.nvim_exec_autocmds("User", { pattern = "GuessIndentNone" }) end end