Skip to content

Commit 222fbda

Browse files
aldanorblueyed
authored andcommitted
Indent empty lines inside blocks (#81)
1 parent d2e98db commit 222fbda

File tree

2 files changed

+25
-2
lines changed

2 files changed

+25
-2
lines changed

indent/python.vim

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -303,17 +303,19 @@ function! s:indent_like_previous_line(lnum)
303303
return base + s:sw()
304304
endif
305305

306+
let empty = getline(a:lnum) =~# '^\s*$'
307+
306308
" If the previous statement was a stop-execution statement or a pass
307309
if getline(start) =~# s:stop_statement
308310
" Remove one level of indentation if the user hasn't already dedented
309-
if indent(a:lnum) > base - s:sw()
311+
if empty || current > base - s:sw()
310312
return base - s:sw()
311313
endif
312314
" Otherwise, trust the user
313315
return -1
314316
endif
315317

316-
if s:is_dedented_already(current, base)
318+
if !empty && s:is_dedented_already(current, base)
317319
return -1
318320
endif
319321

spec/indent/indent_spec.rb

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,27 @@
161161
end
162162
end
163163

164+
describe "when line is empty inside a block" do
165+
it "is indented like the previous line" do
166+
vim.feedkeys 'idef a():\<CR>1\<CR>\<CR>2\<ESC>kcc'
167+
indent.should == shiftwidth
168+
end
169+
end
170+
171+
describe "when line is empty inside a block following multi-line statement" do
172+
it "is indented like the previous line" do
173+
vim.feedkeys 'idef a():\<CR>x = (1 +\<CR>2)\<CR>\<CR>y\<ESC>kcc'
174+
indent.should == shiftwidth
175+
end
176+
end
177+
178+
describe "when line is empty inside a block following stop statement" do
179+
it "is indented like the previous line minus shiftwidth" do
180+
vim.feedkeys 'iif x:\<CR>if y:\<CR>pass\<CR>\<CR>z\<ESC>kcc'
181+
indent.should == shiftwidth
182+
end
183+
end
184+
164185
describe "when using simple control structures" do
165186
it "indents shiftwidth spaces" do
166187
vim.feedkeys 'iwhile True:\<CR>pass'

0 commit comments

Comments
 (0)