From 0fa3cc1dbf47935dbab9f1251a8960a1b516d8d6 Mon Sep 17 00:00:00 2001 From: Josh Rahm Date: Fri, 4 Apr 2025 18:10:18 +0000 Subject: Change how substitute works again. This time, it's using TextChanged and CursorMoved hooks to determine what the word was before and after a text change occured. This makes it much cleaner and doesn't rely on remapping anything. --- plugin/substitute.vim | 59 +++++++++++++++------------------------------------ 1 file changed, 17 insertions(+), 42 deletions(-) diff --git a/plugin/substitute.vim b/plugin/substitute.vim index f06afb9..82afab9 100644 --- a/plugin/substitute.vim +++ b/plugin/substitute.vim @@ -1,14 +1,10 @@ - -" gs will create a substitution command to replace the last yanked string with -" the last inserted string over a motion. -" -" This is useful when renaming a variable for example, one can place the cursor -" over the varibale to rename and type: +" gs will propegate a change made to a over a text object. This is done +" by the plugin taking note of the word under the cursor before a text change is +" made and what the word was changed to after the text was changed, and gs will +" create a substitute command based on that and apply it to a text object. " -" ciwnew_variable_namegsiB -" -" and this will replace that variable name with 'new_variable_name' inside the -" Body. +" This is extremely useful and ergonomic for renaming variables, types or +" otherwise in a text body. noremap gs :set operatorfunc=do_subst_enterg@ vnoremap gs :call v_do_subst_enter() @@ -23,49 +19,28 @@ vnoremap gS :call v_do_subst_no_enter() " Hotkey for replacing the most recently searched matches. noremap :%s// -let s:entered_with_change = v:false -let s:entered_with_append = v:false - -let s:old_word='' -let s:new_word='' +let s:current_word_1 = '' +let s:current_word = '' -let s:override_word = '' -function! OverrideChange() - let s:entered_with_change = v:true - let s:override_word = expand('') - return '' -endfunction - -function! s:save_register_on_change() -endfunction +let s:old_word = '' +let s:new_word = '' -function! s:save_old_word() abort - if s:entered_with_change - let s:old_word = s:override_word - else - let s:old_word = expand("") - endif +function! s:save_cur_word() abort + let s:current_word_1 = s:current_word + let s:current_word = expand("") endfunction function! s:save_new_word() abort - if s:entered_with_change - let s:entered_with_change = v:false - let s:new_word = expand("") - else - let s:new_word = expand("") - endif + let s:old_word = s:current_word_1 + let s:new_word = expand("") endfunction augroup FieldMarshalSubstitute au! - autocmd InsertEnter * call s:save_old_word() - autocmd InsertLeave * call s:save_new_word() + autocmd CursorMoved * call s:save_cur_word() + autocmd TextChanged * call s:save_new_word() augroup END -noremap c OverrideChange() . 'c' -noremap s OverrideChange() . 's' -noremap a OverrideChange() . 'a' - function! s:do_subst_enter(...) abort call s:do_subst_priv("'[", "']", v:true) endfunction -- cgit