From 503e56ed103b58b6bd0880a3e61225c8b1c40033 Mon Sep 17 00:00:00 2001 From: Andrew Radev Date: Mon, 8 Jan 2024 11:53:32 +0200 Subject: [PATCH] Fix ruby if-clause splitting with noisy strings --- autoload/sj/ruby.vim | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/autoload/sj/ruby.vim b/autoload/sj/ruby.vim index b35da6ad..4afc004a 100644 --- a/autoload/sj/ruby.vim +++ b/autoload/sj/ruby.vim @@ -6,15 +6,23 @@ let s:invalid_function_names = [ \ ] function! sj#ruby#SplitIfClause() - let line = getline('.') - let pattern = '\v(.*\S.*) (if|unless|while|until) (.*)' + let pattern = '\v(.*\S.*) \zs(if|unless|while|until) (.*)' + let skip = sj#SkipSyntax(['rubyString', 'rubyComment']) - if line =~ pattern - call sj#ReplaceMotion('V', substitute(line, pattern, '\2 \3\n\1\nend', '')) - return 1 - else + normal! 0 + + if sj#SearchSkip(pattern, skip, 'W', line('.')) <= 0 return 0 endif + + let line = getline('.') + let body = trim(strpart(line, 0, col('.') - 1)) + let if_clause = trim(strpart(line, col('.') - 1)) + + let replacement = if_clause . "\n" . body . "\nend" + call sj#ReplaceMotion('V', replacement) + + return 1 endfunction function! sj#ruby#JoinIfClause()