From: Johann Klähn Date: Fri, 8 Nov 2013 19:53:33 +0000 (+0100) Subject: correctly indent after parens in control structures X-Git-Url: https://git.madduck.net/etc/vim.git/commitdiff_plain/94bfc42df230d422ce004667f91fec535a6258e7?ds=inline;hp=--cc correctly indent after parens in control structures fixes #7 --- 94bfc42df230d422ce004667f91fec535a6258e7 diff --git a/indent/python.vim b/indent/python.vim index 7b6580a..3b64cf4 100644 --- a/indent/python.vim +++ b/indent/python.vim @@ -132,13 +132,22 @@ function! s:indent_like_opening_paren(lnum) if nothing_after_opening_paren if starts_with_closing_paren - return base + let res = base else - return base + s:sw() + let res = base + s:sw() endif else " Indent to match position of opening paren. - return paren_col + let res = paren_col + endif + + " If this line is the continuation of a control statement + " indent further to distinguish the continuation line + " from the next logical line. + if text =~# s:control_statement && res == base + s:sw() + return base + s:sw() * 2 + else + return res endif endfunction diff --git a/spec/indent/indent_spec.rb b/spec/indent/indent_spec.rb index a622cf8..df402e8 100644 --- a/spec/indent/indent_spec.rb +++ b/spec/indent/indent_spec.rb @@ -88,6 +88,47 @@ shared_examples_for "vim" do end end + describe "when using parens and control statements" do + it "avoids ambiguity by using extra indentation" do + vim.feedkeys 'iif (111 and\' + if shiftwidth == 4 + indent.should == shiftwidth * 2 + else + indent.should == 4 + end + vim.feedkeys '222):\' + indent.should == shiftwidth + vim.feedkeys 'pass\' + indent.should == 0 + end + + it "still aligns parens properly if not ambiguous" do + vim.feedkeys 'iwhile (111 and\' + indent.should == 7 + vim.feedkeys '222):\' + indent.should == shiftwidth + vim.feedkeys 'pass\' + indent.should == 0 + end + + it "still handles multiple parens correctly" do + vim.feedkeys 'iif (111 and (222 and 333\' + indent.should == 13 + vim.feedkeys 'and 444\' + indent.should == 13 + vim.feedkeys ')\' + if shiftwidth == 4 + indent.should == shiftwidth * 2 + else + indent.should == 4 + end + vim.feedkeys 'and 555):\' + indent.should == shiftwidth + vim.feedkeys 'pass\' + indent.should == 0 + end + end + describe "when a line breaks with a manual '\\'" do it "indents shiftwidth spaces on normal line" do vim.feedkeys 'ivalue = test + \\\\\'