Skip to content

Commit 7665325

Browse files
authored
Merge pull request #112 from blueyed/fix-97
Fix indenting of "else" with outer if
2 parents b0d020f + 0cd83eb commit 7665325

File tree

2 files changed

+23
-17
lines changed

2 files changed

+23
-17
lines changed

indent/python.vim

+8-14
Original file line numberDiff line numberDiff line change
@@ -138,27 +138,21 @@ endfunction
138138
" Find possible indent(s) of the block starter that matches the current line.
139139
function! s:find_start_of_block(lnum, types, multiple)
140140
let r = []
141-
let types = copy(a:types)
142141
let re = '\V\^\s\*\('.join(a:types, '\|').'\)\>'
143142
let lnum = a:lnum
144143
let last_indent = indent(lnum) + 1
145144
while lnum > 0 && last_indent > 0
146145
let indent = indent(lnum)
147146
if indent < last_indent
148-
for type in types
149-
let re = '\v^\s*'.type.'>'
150-
if getline(lnum) =~# re
151-
if !a:multiple
152-
return [indent]
153-
endif
154-
if index(r, indent) == -1
155-
let r += [indent]
156-
endif
157-
" Remove any handled type, e.g. 'if'.
158-
call remove(types, index(types, type))
147+
if getline(lnum) =~# re
148+
if !a:multiple
149+
return [indent]
159150
endif
160-
endfor
161-
let last_indent = indent(lnum)
151+
if index(r, indent) == -1
152+
let r += [indent]
153+
endif
154+
let last_indent = indent
155+
endif
162156
endif
163157
let lnum = prevnonblank(lnum - 1)
164158
endwhile

spec/indent/indent_spec.rb

+15-3
Original file line numberDiff line numberDiff line change
@@ -401,10 +401,22 @@
401401
end
402402

403403
describe "when an else is used inside of a nested if" do
404-
before { vim.feedkeys 'iif foo:\<CR>\<TAB>if bar:\<CR>\<TAB>\<TAB>pass\<CR>' }
405-
it "indents an else to the inner if" do
404+
before { vim.feedkeys 'iif foo:\<CR>if bar:\<CR>pass\<CR>' }
405+
it "indents the else to the inner if" do
406406
vim.feedkeys 'else:'
407-
indent.should == shiftwidth * 2
407+
indent.should == shiftwidth
408+
end
409+
end
410+
411+
describe "when an else is used outside of a nested if" do
412+
before { vim.feedkeys 'iif True:\<CR>if True:\<CR>pass\<CR>\<Esc>0' }
413+
it "indents the else to the outer if" do
414+
indent.should == 0
415+
proposed_indent.should == shiftwidth
416+
417+
vim.feedkeys 'ielse:'
418+
indent.should == 0
419+
proposed_indent.should == 0
408420
end
409421
end
410422

0 commit comments

Comments
 (0)