Skip to content

Commit b0d020f

Browse files
authored
Fix multiline handling with blank lines (#111)
1 parent 6d5ce1e commit b0d020f

File tree

2 files changed

+31
-13
lines changed

2 files changed

+31
-13
lines changed

indent/python.vim

+7-7
Original file line numberDiff line numberDiff line change
@@ -337,11 +337,11 @@ endfunction
337337
" Is the syntax at lnum (and optionally cnum) a python string?
338338
function! s:is_python_string(lnum, ...)
339339
let line = getline(a:lnum)
340-
let linelen = len(line)
341-
if linelen < 1
342-
let linelen = 1
340+
if a:0
341+
let cols = type(a:1) != type([]) ? [a:1] : a:1
342+
else
343+
let cols = range(1, max([1, len(line)]))
343344
endif
344-
let cols = a:0 ? type(a:1) != type([]) ? [a:1] : a:1 : range(1, linelen)
345345
for cnum in cols
346346
if match(map(synstack(a:lnum, cnum),
347347
\ "synIDattr(v:val, 'name')"), 'python\S*String') == -1
@@ -361,7 +361,7 @@ function! GetPythonPEPIndent(lnum)
361361
let prevline = getline(a:lnum-1)
362362

363363
" Multilinestrings: continous, docstring or starting.
364-
if s:is_python_string(a:lnum-1, len(prevline))
364+
if s:is_python_string(a:lnum-1, max([1, len(prevline)]))
365365
\ && (s:is_python_string(a:lnum, 1)
366366
\ || match(line, '^\%("""\|''''''\)') != -1)
367367

@@ -379,8 +379,8 @@ function! GetPythonPEPIndent(lnum)
379379
endif
380380

381381
if s:is_python_string(a:lnum-1)
382-
" Previous line is (completely) a string.
383-
return indent(a:lnum-1)
382+
" Previous line is (completely) a string: keep current indent.
383+
return -1
384384
endif
385385

386386
if match(prevline, '^\s*\%("""\|''''''\)') != -1

spec/indent/indent_spec.rb

+24-6
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@
130130
vim.echo('synIDattr(synID(line("."), col("."), 0), "name")'
131131
).downcase.should include 'string'
132132
vim.feedkeys 'a\<CR>'
133-
proposed_indent.should == 0
133+
proposed_indent.should == -1
134134
indent.should == 0
135135
end
136136

@@ -464,7 +464,7 @@
464464
end
465465
end
466466

467-
describe "when after assigning an unfinished string" do
467+
describe "when after assigning an indented unfinished string" do
468468
before { vim.feedkeys 'i test = """' }
469469

470470
it "it indents the next line" do
@@ -475,7 +475,7 @@
475475
end
476476
end
477477

478-
describe "when after assigning a finished string" do
478+
describe "when after assigning an indented finished string" do
479479
before { vim.feedkeys 'i test = ""' }
480480

481481
it "it does indent the next line" do
@@ -508,12 +508,30 @@
508508
end
509509

510510
describe "when breaking a string after opening parenthesis" do
511-
before { vim.feedkeys 'i foo("""bar<Left><Left><Left>' }
511+
before { vim.feedkeys 'i foo("""bar\<Left>\<Left>\<Left>' }
512512
it "it does indent the next line as after an opening multistring" do
513513
vim.feedkeys '\<CR>'
514-
expected_proposed, expected_indent = multiline_indent(4, 4 + shiftwidth)
514+
_, expected_indent = multiline_indent(4, 4 + shiftwidth)
515515
indent.should == expected_indent
516-
proposed_indent.should == expected_proposed
516+
proposed_indent.should == -1
517+
518+
# it keeps the indent after an empty line
519+
vim.feedkeys '\<CR>'
520+
proposed_indent, expected_indent = multiline_indent(4, 4 + shiftwidth)
521+
indent.should == expected_indent
522+
proposed_indent.should == proposed_indent
523+
524+
# it keeps the indent of nonblank above
525+
vim.feedkeys '\<End>\<CR>'
526+
proposed_indent, expected_indent = multiline_indent(4, 4 + shiftwidth)
527+
indent.should == expected_indent
528+
proposed_indent.should == proposed_indent
529+
530+
# it keeps the indent of nonblank above before an empty line
531+
vim.feedkeys '\<CR>'
532+
proposed_indent, expected_indent = multiline_indent(4, 4 + shiftwidth)
533+
indent.should == expected_indent
534+
proposed_indent.should == proposed_indent
517535
end
518536
end
519537
end

0 commit comments

Comments
 (0)