From 2fbeb799f46daa22e58caa34854812fb20f75cbe Mon Sep 17 00:00:00 2001 From: umlx5h Date: Tue, 28 Nov 2023 16:44:51 +0900 Subject: [PATCH] Add options to customize vim indentation options --- README.md | 9 +++++++++ doc/guess_indent.txt | 6 ++++++ lua/guess-indent/config.lua | 11 +++++++++++ lua/guess-indent/init.lua | 14 +++++++++----- 4 files changed, 35 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 7777412..0caa1a0 100644 --- a/README.md +++ b/README.md @@ -62,6 +62,15 @@ require('guess-indent').setup { "terminal", "prompt", }, + on_tab_options = { -- A table of vim options when tabs are detected + ["expandtab"] = false, + }, + on_space_options = { -- A table of vim options when spaces are detected + ["expandtab"] = true, + ["tabstop"] = "detected", -- If the option value is 'detected', The value is set to the automatically detected indent size. + ["softtabstop"] = "detected", + ["shiftwidth"] = "detected", + }, } ``` diff --git a/doc/guess_indent.txt b/doc/guess_indent.txt index 39b6249..d109294 100644 --- a/doc/guess_indent.txt +++ b/doc/guess_indent.txt @@ -65,6 +65,12 @@ instead. `buftype_exclude` Same as `filetype_exclude` but for 'buftype' instead. +`on_tab_options` A table of vim options when tabs are detected. + +`on_space_options` A table of vim options when spaces are detected. + If the option value is `'detected'`, The value is set + to the automatically detected indent size. + ============================================================================== COMMANDS *GuessIndent-commands* diff --git a/lua/guess-indent/config.lua b/lua/guess-indent/config.lua index e3b023e..0c9e8ad 100644 --- a/lua/guess-indent/config.lua +++ b/lua/guess-indent/config.lua @@ -5,6 +5,8 @@ local M = {} ---@field override_editorconfig boolean? Whether or not to override indentation set by Editorconfig ---@field filetype_exclude string[]? Filetypes to ignore indentation detection in ---@field buftype_exclude string[]? Buffer types to ignore indentation detection in +---@field on_tab_options table? A table of vim options when tabs are detected +---@field on_space_options table? A table of vim options when spaces are detected ---@class GuessIndentConfigModule: GuessIndentConfig ---@field set_config fun(GuessIndentConfig) @@ -23,6 +25,15 @@ local default_config = { "terminal", "prompt", }, + on_tab_options = { + ["expandtab"] = false, + }, + on_space_options = { + ["expandtab"] = true, + ["tabstop"] = "detected", + ["softtabstop"] = "detected", + ["shiftwidth"] = "detected", + }, } -- The current active config diff --git a/lua/guess-indent/init.lua b/lua/guess-indent/init.lua index b6aa370..5243c84 100644 --- a/lua/guess-indent/init.lua +++ b/lua/guess-indent/init.lua @@ -110,13 +110,17 @@ local function set_indentation(indentation, bufnr, silent) local notification = "Failed to detect indentation style." if indentation == "tabs" then - set_buffer_opt(bufnr, "expandtab", false) + for opt, value in pairs(config.on_tab_options) do + set_buffer_opt(bufnr, opt, value) + end notification = "Did set indentation to tabs." elseif type(indentation) == "number" and indentation > 0 then - set_buffer_opt(bufnr, "expandtab", true) - set_buffer_opt(bufnr, "tabstop", indentation) - set_buffer_opt(bufnr, "softtabstop", indentation) - set_buffer_opt(bufnr, "shiftwidth", indentation) + for opt, value in pairs(config.on_space_options) do + if value == "detected" then + value = indentation + end + set_buffer_opt(bufnr, opt, value) + end notification = ("Did set indentation to %s space(s)."):format(indentation) end if not silent then