Skip to content

Commit

Permalink
Add options to customize vim indentation options
Browse files Browse the repository at this point in the history
  • Loading branch information
umlx5h committed Nov 28, 2023
1 parent b8ae749 commit ab8d40a
Show file tree
Hide file tree
Showing 4 changed files with 33 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
9 changes: 9 additions & 0 deletions lua/guess-indent/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,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 @@ -65,13 +65,17 @@ local function set_indentation(indentation)
end

if indentation == "tabs" then
set_buffer_opt(0, "expandtab", false)
for opt, value in pairs(config.on_tab_options) do
set_buffer_opt(0, opt, value)
end
print("Did set indentation to tabs.")
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)
for opt, value in pairs(config.on_space_options) do
if value == "detected" then
value = indentation
end
set_buffer_opt(0, opt, value)
end
print("Did set indentation to", indentation, "spaces.")
else
print("Failed to detect indentation style.")
Expand Down

0 comments on commit ab8d40a

Please sign in to comment.