From 8b00772a03c416d23954baeff8a32154bb626293 Mon Sep 17 00:00:00 2001 From: Andrew Radev Date: Sun, 12 Nov 2023 22:48:38 +0100 Subject: [PATCH] Try to handle whitespace in Vimscript better --- autoload/sj/vim.vim | 36 ++++++++++++++++++++++++++++-------- spec/plugin/vim_spec.rb | 10 ++++------ 2 files changed, 32 insertions(+), 14 deletions(-) diff --git a/autoload/sj/vim.vim b/autoload/sj/vim.vim index 1a58c015..2aa0837a 100644 --- a/autoload/sj/vim.vim +++ b/autoload/sj/vim.vim @@ -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) @@ -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() diff --git a/spec/plugin/vim_spec.rb b/spec/plugin/vim_spec.rb index b6d4840c..df4c1bf9 100644 --- a/spec/plugin/vim_spec.rb +++ b/spec/plugin/vim_spec.rb @@ -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 @@ -49,10 +49,8 @@ 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('+') @@ -60,13 +58,13 @@ 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