diff options
author | Josh Rahm <rahm@google.com> | 2025-04-04 18:10:18 +0000 |
---|---|---|
committer | Josh Rahm <rahm@google.com> | 2025-04-04 18:10:18 +0000 |
commit | 0fa3cc1dbf47935dbab9f1251a8960a1b516d8d6 (patch) | |
tree | d8543f7fffe8c9fb4e9be2302d38f4da193e14b3 | |
parent | 9e99546a2dc05803ded7a664cb319244d91a010b (diff) | |
download | fieldmarshal.vim-0fa3cc1dbf47935dbab9f1251a8960a1b516d8d6.tar.gz fieldmarshal.vim-0fa3cc1dbf47935dbab9f1251a8960a1b516d8d6.tar.bz2 fieldmarshal.vim-0fa3cc1dbf47935dbab9f1251a8960a1b516d8d6.zip |
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.
-rw-r--r-- | plugin/substitute.vim | 59 |
1 files 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 <cword> 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_name<esc>gsiB -" -" 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 <silent> gs :set operatorfunc=<SID>do_subst_enter<cr>g@ vnoremap <silent> gs :call <SID>v_do_subst_enter()<cr> @@ -23,49 +19,28 @@ vnoremap <silent> gS :call <SID>v_do_subst_no_enter()<cr> " Hotkey for replacing the most recently searched matches. noremap <C-s> <esc>:<C-u>%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('<cword>') - 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("<cword>") - endif +function! s:save_cur_word() abort + let s:current_word_1 = s:current_word + let s:current_word = expand("<cword>") endfunction function! s:save_new_word() abort - if s:entered_with_change - let s:entered_with_change = v:false - let s:new_word = expand("<cword>") - else - let s:new_word = expand("<cword>") - endif + let s:old_word = s:current_word_1 + let s:new_word = expand("<cword>") 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 <expr> c OverrideChange() . 'c' -noremap <expr> s OverrideChange() . 's' -noremap <expr> a OverrideChange() . 'a' - function! s:do_subst_enter(...) abort call s:do_subst_priv("'[", "']", v:true) endfunction |