Skip to content

Commit 784f6cc

Browse files
authored
Fix indent of "elif" after "else" (#128)
Fixes #125.
1 parent 8c347a6 commit 784f6cc

File tree

2 files changed

+37
-12
lines changed

2 files changed

+37
-12
lines changed

indent/python.vim

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -48,13 +48,13 @@ if !exists('g:python_pep8_indent_searchpair_timeout')
4848
endif
4949

5050
let s:block_rules = {
51-
\ '^\s*elif\>': ['if', 'elif'],
52-
\ '^\s*except\>': ['try', 'except'],
53-
\ '^\s*finally\>': ['try', 'except', 'else']
54-
\ }
51+
\ '^\s*elif\>': [['if', 'elif'], ['else']],
52+
\ '^\s*except\>': [['try', 'except'], []],
53+
\ '^\s*finally\>': [['try', 'except', 'else'], []]
54+
\ }
5555
let s:block_rules_multiple = {
56-
\ '^\s*else\>': ['if', 'elif', 'for', 'try', 'except'],
57-
\ }
56+
\ '^\s*else\>': [['if', 'elif', 'for', 'try', 'except'], []]
57+
\ }
5858
" Pairs to look for when searching for opening parenthesis.
5959
" The value is the maximum offset in lines.
6060
let s:paren_pairs = {'()': 50, '[]': 100, '{}': 1000}
@@ -150,15 +150,23 @@ function! s:find_start_of_multiline_statement(lnum)
150150
endfunction
151151

152152
" Find possible indent(s) of the block starter that matches the current line.
153-
function! s:find_start_of_block(lnum, types, multiple)
153+
function! s:find_start_of_block(lnum, types, skip, multiple) abort
154154
let r = []
155155
let re = '\V\^\s\*\('.join(a:types, '\|').'\)\>'
156+
if !empty(a:skip)
157+
let re_skip = '\V\^\s\*\('.join(a:skip, '\|').'\)\>'
158+
else
159+
let re_skip = ''
160+
endif
156161
let lnum = a:lnum
157162
let last_indent = indent(lnum) + 1
158163
while lnum > 0 && last_indent > 0
159164
let indent = indent(lnum)
160165
if indent < last_indent
161-
if getline(lnum) =~# re
166+
let line = getline(lnum)
167+
if !empty(re_skip) && line =~# re_skip
168+
let last_indent = indent
169+
elseif line =~# re
162170
if !a:multiple
163171
return [indent]
164172
endif
@@ -239,14 +247,16 @@ function! s:indent_like_block(lnum)
239247
let text = getline(a:lnum)
240248
for [multiple, block_rules] in [
241249
\ [0, s:block_rules],
242-
\ [1, s:block_rules_multiple]]
243-
for [line_re, blocks] in items(block_rules)
250+
\ [1, s:block_rules_multiple],
251+
\ ]
252+
for [line_re, blocks_ignore] in items(block_rules)
244253
if text !~# line_re
245254
continue
246255
endif
247256

248-
let indents = s:find_start_of_block(a:lnum - 1, blocks, multiple)
249-
if !len(indents)
257+
let [blocks, skip] = blocks_ignore
258+
let indents = s:find_start_of_block(a:lnum - 1, blocks, skip, multiple)
259+
if empty(indents)
250260
return -1
251261
endif
252262
if len(indents) == 1

spec/indent/indent_spec.rb

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -744,3 +744,18 @@
744744
indent.should == shiftwidth
745745
end
746746
end
747+
748+
describe "elif after else" do
749+
before {
750+
vim.feedkeys '\<ESC>ggdG'
751+
}
752+
753+
it "is indented to the outer if" do
754+
vim.feedkeys 'iif 1:\<CR>if 2:\<CR>pass\<CR>else:\<CR>pass\<CR>elif 3:\<Esc>'
755+
indent.should == 0
756+
757+
vim.feedkeys '\<ESC>ggdG'
758+
vim.feedkeys 'i if 1:\<CR>if 2:\<CR>pass\<CR>else:\<CR>pass\<CR>elif 3:\<Esc>'
759+
indent.should == 4
760+
end
761+
end

0 commit comments

Comments
 (0)