Skip to content

Commit 19c53ee

Browse files
jakarblueyed
authored andcommitted
Add option for hanging closing brackets (#94)
The main idea is discussed at length in PyCQA/pycodestyle#103. * Add tests for `python_pep8_indent_hang_closing` This might be overkill. It reruns the current "vim" set of tests once for each value of the new setting. Of course, this makes the test suite take longer to run. I couldn't think of a good way to factor out the relevant test cases without creating a mess.
1 parent 5f4184f commit 19c53ee

File tree

4 files changed

+72
-14
lines changed

4 files changed

+72
-14
lines changed

README.rst

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,32 @@ With content already, it will be aligned to the opening parenthesis::
7474

7575
Existing indentation (including ``0``) in multiline strings will be kept, so this setting only applies to the indentation of new/empty lines.
7676

77+
g:python_pep8_indent_hang_closing
78+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
79+
80+
Control closing bracket indentation with ``python_pep8_indent_hang_closing``, set globally or per buffer.
81+
82+
By default (set to ``0``), closing brackets line up with the opening line::
83+
84+
my_list = [
85+
1, 2, 3,
86+
4, 5, 6,
87+
]
88+
result = some_function_that_takes_arguments(
89+
'a', 'b', 'c',
90+
'd', 'e', 'f',
91+
)
92+
93+
With ``python_pep8_indent_hang_closing = 1``, closing brackets line up with the items::
94+
95+
my_list = [
96+
1, 2, 3,
97+
4, 5, 6,
98+
]
99+
result = some_function_that_takes_arguments(
100+
'a', 'b', 'c',
101+
'd', 'e', 'f',
102+
)
77103

78104
Notes
79105
-----

indent/python.vim

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@ if !exists('g:python_pep8_indent_multiline_string')
3434
let g:python_pep8_indent_multiline_string = 0
3535
endif
3636

37+
if !exists('g:python_pep8_indent_hang_closing')
38+
let g:python_pep8_indent_hang_closing = 0
39+
endif
40+
3741
let s:block_rules = {
3842
\ '^\s*elif\>': ['if', 'elif'],
3943
\ '^\s*except\>': ['try', 'except'],
@@ -203,8 +207,11 @@ function! s:indent_like_opening_paren(lnum)
203207
\ s:skip_after_opening_paren, paren_lnum, paren_col+1)
204208
let starts_with_closing_paren = getline(a:lnum) =~# '^\s*[])}]'
205209

210+
let hang_closing = get(b:, 'python_pep8_indent_hang_closing',
211+
\ get(g:, 'python_pep8_indent_hang_closing', 0))
212+
206213
if nothing_after_opening_paren
207-
if starts_with_closing_paren
214+
if starts_with_closing_paren && !hang_closing
208215
let res = base
209216
else
210217
let res = base + s:sw()

spec/indent/indent_spec.rb

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@
5252

5353
it "puts the closing parenthesis at the same level" do
5454
vim.feedkeys ')'
55-
indent.should == 0
55+
indent.should == (hang_closing ? shiftwidth : 0)
5656
end
5757
end
5858

@@ -87,7 +87,7 @@
8787

8888
it "lines up the closing parenthesis" do
8989
vim.feedkeys '}'
90-
indent.should == 0
90+
indent.should == (hang_closing ? shiftwidth : 0)
9191
end
9292
end
9393

@@ -514,18 +514,31 @@
514514
end
515515
end
516516

517-
describe "vim when using width of 4" do
518-
before {
519-
vim.command("set sw=4 ts=4 sts=4 et")
520-
}
521-
it_behaves_like "vim"
522-
end
517+
SUITE_SHIFTWIDTHS = [4, 3]
518+
SUITE_HANG_CLOSINGS = [nil, false, true]
523519

524-
describe "vim when using width of 3" do
525-
before {
526-
vim.command("set sw=3 ts=3 sts=3 et")
527-
}
528-
it_behaves_like "vim"
520+
SUITE_SHIFTWIDTHS.each do |sw|
521+
describe "vim when using width of #{sw}" do
522+
before {
523+
vim.command("set sw=#{sw} ts=#{sw} sts=#{sw} et")
524+
}
525+
it "sets shiftwidth to #{sw}" do
526+
shiftwidth.should == sw
527+
end
528+
529+
SUITE_HANG_CLOSINGS.each do |hc|
530+
describe "vim when hang_closing is set to #{hc}" do
531+
before {
532+
set_hang_closing hc
533+
}
534+
it "sets hang_closing to #{hc}" do
535+
hang_closing.should == !!hc
536+
end
537+
538+
it_behaves_like "vim"
539+
end
540+
end
541+
end
529542
end
530543

531544
describe "vim when not using python_pep8_indent_multiline_string" do

spec/spec_helper.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,18 @@ def multiline_indent(prev, default)
4747
i = vim.echo("get(g:, 'python_pep8_indent_multiline_string', 0)").to_i
4848
return (i == -2 ? default : i), i < 0 ? (i == -1 ? prev : default) : i
4949
end
50+
def hang_closing
51+
i = vim.echo("get(g:, 'python_pep8_indent_hang_closing', 0)").to_i
52+
return (i != 0)
53+
end
54+
def set_hang_closing(value)
55+
if value.nil?
56+
vim.command("unlet! g:python_pep8_indent_hang_closing")
57+
else
58+
i = value ? 1 : 0
59+
vim.command("let g:python_pep8_indent_hang_closing=#{i}")
60+
end
61+
end
5062

5163
vim
5264
end

0 commit comments

Comments
 (0)