Skip to content

Commit 2dcbd4f

Browse files
committed
Set shiftwidth to 0 (defaults to tabstop value)
The current behavior is to set both shiftwidth and tabstop/softtabstop. shiftwidth gets set to the same value as tabstop. If I open a file that editorconfig indents with 8-space hard tabs, then ":set tabstop=4", my shiftwidth will still be 8, meaning that when I indent I will get two tabs. Set shiftwidth to 0, which defaults to the value of tabstop, and disable softtabstop so it doesn't conflict. This should have the same end result with less complication. This removes the g:EditorConfig_softtabstop_space and g:EditorConfig_softtabstop_tab options. Users who want to delete multiple spaces as a single character can enable smarttab.
1 parent 95cb75e commit 2dcbd4f

File tree

3 files changed

+26
-58
lines changed

3 files changed

+26
-58
lines changed

doc/editorconfig.txt

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -149,23 +149,6 @@ max_line_length is set:
149149
<
150150
This option defaults to 0.
151151

152-
*g:EditorConfig_softtabstop_space*
153-
When spaces are used for indent, Vim's 'softtabstop' feature will make the
154-
backspace key delete one indent level. If you turn off that feature (by
155-
setting the option to 0), only a single space will be deleted.
156-
This option defaults to 1, which enables 'softtabstop' and uses the
157-
'shiftwidth' value for it. You can also set this to -1 to automatically follow
158-
the current 'shiftwidth' value (since Vim 7.3.693). Or set this to [] if
159-
EditorConfig should not touch 'softtabstop' at all.
160-
161-
*g:EditorConfig_softtabstop_tab*
162-
When tabs are used for indent, Vim's 'softtabstop' feature only applies to
163-
backspacing over existing runs of spaces.
164-
This option defaults to 1, so backspace will delete one indent level worth of
165-
spaces; -1 does the same but automatically follows the current 'shiftwidth'
166-
value. Set this to 0 to have backspace delete just a single space character.
167-
Or set this to [] if EditorConfig should not touch 'softtabstop' at all.
168-
169152
*g:EditorConfig_verbose*
170153
Set this to 1 if you want debug info printed:
171154
>

plugin/editorconfig.vim

Lines changed: 15 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -64,14 +64,6 @@ if !exists('g:EditorConfig_enable_for_new_buf')
6464
let g:EditorConfig_enable_for_new_buf = 0
6565
endif
6666

67-
if !exists('g:EditorConfig_softtabstop_space')
68-
let g:EditorConfig_softtabstop_space = 1
69-
endif
70-
71-
if !exists('g:EditorConfig_softtabstop_tab')
72-
let g:EditorConfig_softtabstop_tab = 1
73-
endif
74-
7567
" Copy some of the globals into script variables --- changes to these
7668
" globals won't affect the plugin until the plugin is reloaded.
7769
if exists('g:EditorConfig_core_mode') && !empty(g:EditorConfig_core_mode)
@@ -454,38 +446,26 @@ function! s:ApplyConfig(bufnr, config) abort
454446
endif
455447
endif
456448

457-
" Set tabstop. Skip this for terminal buffers, e.g., :FZF (#224).
458-
if s:IsRuleActive('tab_width', a:config) && bufname(a:bufnr) !~# '^!\w*sh$'
459-
let l:tabstop = str2nr(a:config["tab_width"])
460-
call setbufvar(a:bufnr, '&tabstop', l:tabstop)
461-
else
462-
" Grab the current ts so we can use it below
463-
let l:tabstop = getbufvar(a:bufnr, '&tabstop')
464-
endif
465-
466-
if s:IsRuleActive('indent_size', a:config)
467-
" if indent_size is 'tab', set shiftwidth to tabstop;
468-
" if indent_size is a positive integer, set shiftwidth to the integer
469-
" value
470-
if a:config["indent_size"] == "tab"
471-
call setbufvar(a:bufnr, '&shiftwidth', l:tabstop)
472-
if type(g:EditorConfig_softtabstop_tab) != type([])
473-
call setbufvar(a:bufnr, '&softtabstop',
474-
\ g:EditorConfig_softtabstop_tab > 0 ?
475-
\ l:tabstop : g:EditorConfig_softtabstop_tab)
476-
endif
477-
else
449+
" Skip terminal buffers as setting tabstop affects cursor position (#224).
450+
if s:IsRuleActive('indent_size', a:config) && bufname(a:bufnr) !~# '^!\w*sh$'
451+
" When shiftwidth is 0 it uses the value of tabstop
452+
call setbufvar(a:bufnr, '&shiftwidth', 0)
453+
" Disable softtabstop so it doesn't conflict with editorconfig
454+
" indentation settings
455+
call setbufvar(a:bufnr, '&softtabstop', 0)
456+
if a:config["indent_size"] != "tab"
478457
let l:indent_size = str2nr(a:config["indent_size"])
479458
if l:indent_size > 0
480-
call setbufvar(a:bufnr, '&shiftwidth', l:indent_size)
481-
if type(g:EditorConfig_softtabstop_space) != type([])
482-
call setbufvar(a:bufnr, '&softtabstop',
483-
\ g:EditorConfig_softtabstop_space > 0 ?
484-
\ l:indent_size : g:EditorConfig_softtabstop_space)
485-
endif
459+
call setbufvar(a:bufnr, '&tabstop', l:indent_size)
486460
endif
487461
endif
462+
endif
488463

464+
" Set tabstop. Skip this for terminal buffers, e.g., :FZF (#224).
465+
if s:IsRuleActive('tab_width', a:config) && bufname(a:bufnr) !~# '^!\w*sh$'
466+
if a:config["indent_style"] == "tab"
467+
call setbufvar(a:bufnr, '&tabstop', str2nr(a:config["tab_width"]))
468+
endif
489469
endif
490470

491471
if s:IsRuleActive('end_of_line', a:config) &&

tests/plugin/spec/editorconfig_spec.rb

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,22 +35,25 @@ def test_instance(vim)
3535
it '3_space.py' do
3636
test_editorconfig vim, '3_space.txt',
3737
expandtab: '1',
38-
shiftwidth: '3',
38+
shiftwidth: '0',
39+
softtabstop: '0',
3940
tabstop: '3'
4041
end
4142
end
4243

4344
it '4_space.py' do
4445
test_editorconfig vim, '4_space.py',
4546
expandtab: '1',
46-
shiftwidth: '4',
47-
tabstop: '8'
47+
shiftwidth: '0',
48+
softtabstop: '0',
49+
tabstop: '4'
4850
end
4951

5052
it 'space.txt' do
5153
test_editorconfig vim, 'space.txt',
5254
expandtab: '1',
53-
shiftwidth: vim.echo('&l:tabstop')
55+
shiftwidth: '0',
56+
softtabstop: '0'
5457
end
5558

5659
it 'tab.txt' do
@@ -61,14 +64,16 @@ def test_instance(vim)
6164
it '4_tab.txt' do
6265
test_editorconfig vim, '4_tab.txt',
6366
expandtab: '0',
64-
shiftwidth: '4',
67+
shiftwidth: '0',
68+
softtabstop: '0',
6569
tabstop: '4'
6670
end
6771

6872
it '4_tab_width_of_8' do
6973
test_editorconfig vim, '4_tab_width_of_8.txt',
7074
expandtab: '0',
71-
shiftwidth: '4',
75+
shiftwidth: '0',
76+
softtabstop: '0',
7277
tabstop: '8'
7378
end
7479

0 commit comments

Comments
 (0)