Skip to content

Commit

Permalink
Add options to customize vim indentation options (#17)
Browse files Browse the repository at this point in the history
Fixes #12
  • Loading branch information
umlx5h authored Jul 6, 2024
1 parent 6c75506 commit 6cd61f7
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 5 deletions.
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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",
},
}
```

Expand Down
6 changes: 6 additions & 0 deletions doc/guess_indent.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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*

Expand Down
11 changes: 11 additions & 0 deletions lua/guess-indent/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, any>? A table of vim options when tabs are detected
---@field on_space_options table<string, any>? A table of vim options when spaces are detected

---@class GuessIndentConfigModule: GuessIndentConfig
---@field set_config fun(GuessIndentConfig)
Expand All @@ -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
Expand Down
14 changes: 9 additions & 5 deletions lua/guess-indent/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 6cd61f7

Please sign in to comment.