Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Proposal: to make Flashy usable with user-defined operators #16

Open
mwgkgk opened this issue Feb 6, 2020 · 0 comments
Open

Proposal: to make Flashy usable with user-defined operators #16

mwgkgk opened this issue Feb 6, 2020 · 0 comments

Comments

@mwgkgk
Copy link

mwgkgk commented Feb 6, 2020

To use Flashy with my custom operator, I only had to change 2 functions to be autoload instead of script-local. For the sake of example, here's my Stage-and-commit-hunk-under-cursor-with-message-specified-by-motion, making use of vim-gitgutter, vim-operator-user, and vim-operator-flashy (plus some of my helper functions under git# namespace):

function! _vim_gitgutter#operators#commit_current_hunk_with_message(motion_wiseness)
    if &modified
        echo 'Buffer has unwritten changes'
        return
    endif

    if git#diff#has_staged_changes()
        echo 'There are staged changes'
        return
    endif

    if empty(_vim_gitgutter#current_hunk())
        echo 'No hunk under cursor'
        return
    endif

    if s:is_empty_region()
      return
    endif

    " Save cursor position for linewise motion like 'yj'.
    if a:motion_wiseness is# 'line'
      let w = winsaveview()
    endif

    let visual_command = operator#user#visual_command_from_wise_name(a:motion_wiseness)
    let start = [line("'["), virtcol("'[")]
    let end = [line("']"), virtcol("']")]
    let pattern = vital#_operator_flashy#Coaster#Search#pattern_by_range(visual_command, start, end)

    normal! `[yv`]

    GitGutterStageHunk

    if git#commit#with_message(git#commit#flatten_message(getreg('"')))
        echo 'Commit ' . git#log#last_commit()

        call operator#flashy#flash(pattern, g:operator#flashy#flash_time)
    endif
endfunction

function! s:is_empty_region() abort
  return line("'[") is# line("']") + 1
endfunction

Things that had to be changed for this to work:

  • in vim-operator-flashy/autoload/operator/flashy.vim, make a public copy of s:flash named operator#flashy#flash;
  • in vim-operator-flashy/autoload/vital/_operator_flashy/Coaster/Search.vim, make a public copy of s:pattern_by_range named vital#_operator_flashy#Coaster#Search#pattern_by_range.

Of course the second one is very awkward with regards to how the import is originally used :)

What do you think about making calls to operator#flashy#flash officially supported? I wanted to do this to my custom operators for a while, however looking at operator-flashy internals the implementation is too robust to try to imitate in a simplified manner and too intricate to only rip out the things I need.

Thank you. Big fan of your works :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant