Skip to content

Commit

Permalink
Try to handle whitespace in Vimscript better
Browse files Browse the repository at this point in the history
  • Loading branch information
AndrewRadev committed Nov 12, 2023
1 parent 89e6e2e commit 8b00772
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 14 deletions.
36 changes: 28 additions & 8 deletions autoload/sj/vim.vim
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,21 @@ function! sj#vim#Split()
if sj#BlankString(new_line)
return 0
else
if sj#settings#Read('vim_split_whitespace_after_backslash')
let add_whitespace = 1

if col('.') > 1
let pair = strpart(getline('.'), col('.') - 2, 2)

if pair =~ '\s'
" we're breaking on whitespace, so make sure to add some:
let add_whitespace = 1
elseif pair =~ '^\S\S$'
" we're breaking a word, so make sure not to have any:
let add_whitespace = 0
endif
endif

if add_whitespace
let new_line = "\n\\ ".sj#Trim(new_line)
else
let new_line = "\n\\".sj#Trim(new_line)
Expand All @@ -29,16 +43,22 @@ function! sj#vim#Join()

if next_lineno > line('$') || next_line !~ continuation_pattern
return 0
else
exe next_lineno.'s/'.continuation_pattern.'//'
exe current_lineno.','.next_lineno.'join'
endif

if sj#settings#Read('normalize_whitespace')
call sj#CompressWhitespaceOnLine()
endif
if next_line =~ continuation_pattern.'\s'
" Then there's some whitespace after the \, rely on :join
keeppatterns exe next_lineno.'s/'.continuation_pattern.'//'
exe current_lineno.','.next_lineno.'join'
else
" No whitespace, let's join them directly
keeppatterns exe current_lineno.'s/\n'.continuation_pattern.'//'
endif

return 1
if sj#settings#Read('normalize_whitespace')
call sj#CompressWhitespaceOnLine()
endif

return 1
endfunction

function! sj#vim#SplitIfClause()
Expand Down
10 changes: 4 additions & 6 deletions spec/plugin/vim_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
assert_file_contents contents
end

specify "backslashes" do
specify "backslashes with a preceding space" do
set_file_contents <<~EOF
let foo = 2 + 2
EOF
Expand All @@ -49,24 +49,22 @@
end

specify "backslashes without a space" do
vim.command('let g:splitjoin_vim_split_whitespace_after_backslash = 0')

set_file_contents <<~EOF
let foo = 2 + 2
let foo = 2+2
EOF

vim.search('+')
split

assert_file_contents <<~EOF
let foo = 2
\\+ 2
\\+2
EOF

join

assert_file_contents <<~EOF
let foo = 2 + 2
let foo = 2+2
EOF
end
end

0 comments on commit 8b00772

Please sign in to comment.