endfunction
function! GetPythonPEPIndent(lnum)
-
" First line has indent 0
if a:lnum == 1
return 0
let thisline = getline(a:lnum)
let thisindent = indent(a:lnum)
- " If the line starts with 'elif' or 'else', line up with 'if' or 'elif'
- if thisline =~ '^\s*\(elif\|else\)\>'
+ " If the line starts with 'elif', line up with 'if' or 'elif'
+ if thisline =~ '^\s*elif\>'
let bslnum = s:BlockStarter(a:lnum, '^\s*\(if\|elif\)\>')
if bslnum > 0
return indent(bslnum)
endif
endif
- " If the line starts with 'except' or 'finally', line up with 'try'
- " or 'except'
+ " If the line starts with 'except', or 'finally', line up with 'try'
+ " or 'except'.
if thisline =~ '^\s*\(except\|finally\)\>'
let bslnum = s:BlockStarter(a:lnum, '^\s*\(try\|except\)\>')
if bslnum > 0
endif
endif
+ " If the line starts with 'else', line it up with 'try', 'except', 'for',
+ " 'if', or 'elif'.
+ if thisline =~ '^\s*else\>'
+ :echom thisline
+ let bslnum = s:BlockStarter(a:lnum, '^\s*\(try\|except\|if\|elif\|for\)\>')
+ if bslnum > 0
+ return indent(bslnum)
+ else
+ return -1
+ endif
+ endif
+
" Examine previous line
let plnum = a:lnum - 1
let pline = getline(plnum)
end
end
+ describe "when an 'if' is followed by" do
+ before { vim.feedkeys 'i\<TAB>\<TAB>if x:\<CR>' }
+ it "an elif, it lines up with the 'if'" do
+ vim.feedkeys 'elif y:'
+ indent.should == shiftwidth * 2
+ end
+
+ it "an 'else', it lines up with the 'if'" do
+ vim.feedkeys 'else:'
+ indent.should == shiftwidth * 2
+ end
+ end
+
+ describe "when a 'for' is followed by" do
+ before { vim.feedkeys 'i\<TAB>\<TAB>for x in y:\<CR>' }
+ it "an 'else', it lines up with the 'for'" do
+ vim.feedkeys 'else:'
+ indent.should == shiftwidth * 2
+ end
+ end
+
+ describe "when a 'try' is followed by" do
+ before { vim.feedkeys 'i\<TAB>\<TAB>try:\<CR>' }
+ it "an 'except', it lines up with the 'try'" do
+ vim.feedkeys 'except:'
+ indent.should == shiftwidth * 2
+ end
+
+ it "an 'else', it lines up with the 'try'" do
+ vim.feedkeys 'else:'
+ indent.should == shiftwidth * 2
+ end
+
+ it "a 'finally', it lines up with the 'try'" do
+ vim.feedkeys 'finally:'
+ indent.should == shiftwidth * 2
+ end
+ end
+
+ describe "when an 'except' is followed by" do
+ before { vim.feedkeys 'i\<TAB>\<TAB>except:\<CR>' }
+ it "an 'else', it lines up with the 'except'" do
+ vim.feedkeys 'else:'
+ indent.should == shiftwidth * 2
+ end
+
+ it "another 'except', it lines up with the previous 'except'" do
+ vim.feedkeys 'except:'
+ indent.should == shiftwidth * 2
+ end
+
+ it "a 'finally', it lines up with the 'except'" do
+ vim.feedkeys 'finally:'
+ indent.should == shiftwidth * 2
+ end
+ end
+
def shiftwidth
@shiftwidth ||= vim.echo("exists('*shiftwidth') ? shiftwidth() : &sw").to_i
end