Skip to content

Commit cab1e63

Browse files
committed
Fix edge cases with python string splitting
1 parent dfd979d commit cab1e63

3 files changed

Lines changed: 133 additions & 3 deletions

File tree

autoload/sj/python.vim

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -341,11 +341,47 @@ function! sj#python#SplitString()
341341
let body = string[1:-2]
342342
let indent = indent(lineno)
343343

344-
if body =~ '^\(''\|""\)'
345-
" then we're trying to split a string that's already multiline, ignore:
344+
if body =~ '^[''"]$'
345+
" our body is a single quote, we're trying to split a triple-quoted string
346346
return 0
347347
endif
348348

349+
if body =~ '^\(''''\s*''''\|""\s*""\)$'
350+
if search('\(''''\zs\s*''''\|""\zs\s*""\)', 'W', line('.')) <= 0
351+
return 0
352+
endif
353+
354+
if delimiter == '"'
355+
call sj#ReplaceMotion('va"', "\"\n\"")
356+
elseif delimiter == "'"
357+
call sj#ReplaceMotion("va'", "'\n'")
358+
else
359+
return 0
360+
endif
361+
362+
return 1
363+
endif
364+
365+
if body =~ '^\(''''\|""\)\S'
366+
" then the string is already triple-quoted, just replace the insides
367+
if search('\(''''\|""\)\zs\S', 'W', line('.')) <= 0
368+
return 0
369+
endif
370+
371+
if delimiter == '"'
372+
let inner_body = sj#GetMotion('vi"')
373+
call sj#ReplaceMotion('vi"', "\n"..inner_body.."\n")
374+
elseif delimiter == "'"
375+
let inner_body = sj#GetMotion("vi'")
376+
call sj#ReplaceMotion("vi'", "\n"..inner_body.."\n")
377+
else
378+
return 0
379+
endif
380+
381+
call sj#SetIndent(lineno + 1, lineno + 1, indent + shiftwidth())
382+
return 1
383+
endif
384+
349385
if delimiter == '"'
350386
if len(body) == 0
351387
call sj#ReplaceMotion('vi"', '"""'.."\n"..'"""')

ftplugin/htmldjango/splitjoin.vim

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,14 @@
22

33
if !exists('b:splitjoin_split_callbacks')
44
let b:splitjoin_split_callbacks = [
5-
\ 'sj#html#SplitTags'
5+
\ 'sj#html#SplitTags',
6+
\ 'sj#html#SplitAttributes'
67
\ ]
78
endif
89

910
if !exists('b:splitjoin_join_callbacks')
1011
let b:splitjoin_join_callbacks = [
12+
\ 'sj#html#JoinAttributes',
1113
\ 'sj#html#JoinTags'
1214
\ ]
1315
endif

spec/plugin/python_spec.rb

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,98 @@ def example():
303303
EOF
304304
end
305305

306+
it "splits single-line \"\"\" strings" do
307+
set_file_contents <<~EOF
308+
string = """something, 'anything'"""
309+
EOF
310+
311+
vim.search '"""'
312+
split
313+
314+
assert_file_contents <<~EOF
315+
string = """
316+
something, 'anything'
317+
"""
318+
EOF
319+
end
320+
321+
it "splits single-line ''' strings" do
322+
set_file_contents <<~EOF
323+
string = '''something, "anything"'''
324+
EOF
325+
326+
vim.search "'''"
327+
split
328+
329+
assert_file_contents <<~EOF
330+
string = '''
331+
something, "anything"
332+
'''
333+
EOF
334+
end
335+
336+
it "splits empty single-line ''' strings" do
337+
set_file_contents <<~EOF
338+
string = ''' '''
339+
EOF
340+
341+
vim.search "'''"
342+
split
343+
344+
assert_file_contents <<~EOF
345+
string = '''
346+
'''
347+
EOF
348+
end
349+
350+
it "splits empty single-line \"\"\" strings" do
351+
set_file_contents <<~EOF
352+
string = """ """
353+
EOF
354+
355+
vim.search '"""'
356+
split
357+
358+
assert_file_contents <<~EOF
359+
string = """
360+
"""
361+
EOF
362+
end
363+
364+
it "doesn't split already-multiline \"\"\"-strings" do
365+
set_file_contents <<~EOF
366+
string = """
367+
something, 'anything'
368+
"""
369+
EOF
370+
371+
vim.search '"""'
372+
split
373+
374+
assert_file_contents <<~EOF
375+
string = """
376+
something, 'anything'
377+
"""
378+
EOF
379+
end
380+
381+
it "doesn't split already-multiline '''-strings" do
382+
set_file_contents <<~EOF
383+
string = '''
384+
something, 'anything'
385+
'''
386+
EOF
387+
388+
vim.search "'''"
389+
split
390+
391+
assert_file_contents <<~EOF
392+
string = '''
393+
something, 'anything'
394+
'''
395+
EOF
396+
end
397+
306398
it "splits normal strings into multiline strings" do
307399
set_file_contents 'string = "\"anything\""'
308400

0 commit comments

Comments
 (0)