Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add options to customize vim indentation options #17

Merged
merged 1 commit into from
Jul 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading