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

Conversation

umlx5h
Copy link
Contributor

@umlx5h umlx5h commented Nov 28, 2023

Hello.

I have added an option to set any vim option when indentation is detected.
Right now the options to set are hard coded, but I do not think this is appropriate from a customizability standpoint.
Because vim's indentation-related settings are so complex, I thought it best to allow users to set them freely.

There are many vim users who have the tab width set to 8 or have not changed it.
I do not think it is good from the user's point of view to change the 'tabstop' when spaces are detected.

For example, projects such as gcc and vim use an indentation style that mixes tabs and spaces, but only assumes that the tab width is 8.
The old-fashioned GNU style seems to use this indent style.
Therefore, making the tab width the same as the indent width will mess up the layout.
You can see this by opening the following code in vim. (Please disable modeline in vim project)
https://github.com/gcc-mirror/gcc/blob/8f8db5553935edcb4731db32279418ca37e1f094/libgomp/config/posix/sem.c#L73-L76
https://github.com/vim/vim/blob/1bf1bf569b96d2f9b28e0cce0968ffbf2fb80aac/src/alloc.c#L97-L102

Another advantage of separating 'tabstop' from 'shiftwidth' is that if a tab is mistakenly entered, it is easier to tell that it is a tab without using :set list. The same size makes it impossible to distinguish between tabs and spaces.

For example, vim-sleuth does not change it.
The vim help below explains why.

:h 'tabstop'
:h usr_25.txt
:h usr_30.txt

I am going to define the default values as follows and change the following options.

default: 2 spaces, 8-width tab

opt.tabstop = 8          -- default
opt.softtabstop = -1  -- same as 'shiftwidth'
opt.shiftwidth = 2      -- indent is 2
opt.expandtab = true -- tab is not used
      on_tab_options = {
        ["expandtab"] = false,
        ["softtabstop"] = 0, -- make disabled
        ["shiftwidth"] = 0,   -- same as 'tabstop', Currently this is not set, so 'shiftwidth' is 2. I want to change this option when detecting tab.
      },
      on_space_options = {
        ["expandtab"] = true, -- same as default
        ["shiftwidth"] = "detected", -- only this option will be changed
        ["softtabstop"] = -1, -- same as default
      },

This is advantageous from a performance standpoint because it reduces the number of options to be set as much as possible.
Currently the same value is set for 'tabstop', 'softtabstop', and 'shiftwidth' when spaces are detected, but If 'softtabstop' is set to -1, there is no need to set it. or if 'tabstop' and 'softabstop' are equal, 'softtabstop' is almost no point, so it can be set to 0 or 1 instead.

BTW, The editorconfig that comes with neovim has almost the same configuration values.
(The fact that 'tabstop' is also changed is a result of following the editorconfig specification.)
https://github.com/neovim/neovim/blob/master/runtime/lua/editorconfig.lua#L50-L71

Also, the specific options to be set will be written in the README, which will make it easier to understand.

Default values are to be taken over from the current settings for compatibility reason.

This PR also fixes
PR: #9
Issue: #12

also related
PR: #14

@umlx5h umlx5h force-pushed the custom-tab-option branch from ab8d40a to 2fbeb79 Compare July 4, 2024 14:28
@umlx5h
Copy link
Contributor Author

umlx5h commented Jul 4, 2024

@NMAC427

I force pushed because other PR was merged and conflicted recently, could you please consider this PR as well?
This PR is related to some other Issues and PRs and may be able to close them.

If you don't like this approach, I would be happy to consider other approaches.

At least when tabs are detected, changing only the tabstop is not enough, usually shiftwidth must be set to 0 at the same time.
If shiftwidth is not set to a value that matches tabstop or to 0, then it end up with strange indentation with the > and < operators.

I have written a lengthy explanation in the PR description, but the current hard-coded indentation settings are not appropriate for this reason alone.
It is better to give plugin users freedom of setting.

@NMAC427
Copy link
Owner

NMAC427 commented Jul 6, 2024

Sorry for not getting to this earlier. These changes look very reasonable. I also really like that it also adresses #12.
Thanks for the contribution!

@NMAC427 NMAC427 merged commit 6cd61f7 into NMAC427:main Jul 6, 2024
3 checks passed
@umlx5h
Copy link
Contributor Author

umlx5h commented Jul 6, 2024

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants