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

Enable switching booleans anywhere on the line and returning to original cursor position #79

Closed
gerazov opened this issue Apr 15, 2021 · 6 comments

Comments

@gerazov
Copy link

gerazov commented Apr 15, 2021

Great plugin! 👍

It would be awesome to be able to switch (bools) anywhere on the line and then have the cursor return to the original position. Possibly related to #49 and #14.

I implemented a function to do this for Python. I dunno if it can be used to upgrade the plugin, but it could be useful for someone else, so maybe include it as an example in the documentation?

function! MySwitch()                                                            
    let save_pos = getpos(".")                                                   
    call search('true\|false', '', line('.'))                                           
    execute 'Switch'                                                             
    call setpos(".", save_pos)                                                   
endfunction                                                                     
nmap <Leader>s :call MySwitch()<CR> 

I've used this function as a bool only toggle plug-in vim-toggle-bool

@AndrewRadev
Copy link
Owner

That's a good way to handle one-off patterns. The problem is the more complicated the pattern gets, it gets harder to build something compatible on top of them :D.

One thing I'll note is that I think your search only works if ignorecase is on. If you'd like to make sure other people's configs work with this function, maybe search('\ctrue\|false', ... would be better. The \c modifier makes the entire pattern after the \c ignore case. You might also want to check if the result of search is > 0, so the function doesn't activate the Switch command if there's no true or false on the line.

I implemented a function to do this for Python. I dunno if it can be used to upgrade the plugin, but it could be useful for someone else, so maybe include it as an example in the documentation?

I agree it might be useful -- could you add it to the wiki? https://github.com/AndrewRadev/switch.vim/wiki

I've linked to the wiki in the docs under "Customization", although I don't know how visible it is.

@gerazov
Copy link
Author

gerazov commented Apr 22, 2021

Thanks for the tip!

I implemented your recommendation and tried to include also 0 and 1 as they do the bool job in some languages. Switch doesn't switch them so I'm doing a search and replace which works in Vim but not inside my function. Can you take a look?

function! ToggleBool()
   let save_pos = getpos(".")
   normal 0
   if call search('\c\<true\>\|\<false\>', '', line('.')) > 0
       execute 'Switch'
   else
       execute 's/\<1\>/0/'
       execute 's/\<0\>/1/'
   endif
   call setpos(".", save_pos)
endfunction

@AndrewRadev
Copy link
Owner

AndrewRadev commented Apr 24, 2021

The problem seems to be if call search. The code search(...) is an expression, while call search(...) is a command. In the command-line, you need to execute commands, which is why you need :call. But the if-clause expects an expression for its condition.

You can try if search('\c\<true\>\|\<false\>', '', line('.')) > 0 and that should work. Bear in mind though, that the execute calls won't work, because they'll throw errors if there's no match on the current line. And if they do work, the first substitution replaces 1 with 0 and the second replacement replaces it back to 1 :). Try this:

function! ToggleBool()
  let save_pos = getpos(".")
  normal! 0

  if search('\c\<true\>\|\<false\>', '', line('.')) > 0
    Switch
  elseif search('\<1\>', '', line('.')) > 0
    " decrement 1 -> 0
    execute "normal! \<c-x>"
  elseif search('\<0\>', '', line('.')) > 0
    " increment 0 -> 1
    execute "normal! \<c-a>"
  endif

  call setpos(".", save_pos)
endfunction

The mappings <c-x> and <c-a> decrement or increment numbers, so they're an easy way to work locally on 0 or 1. You need execute "normal! .." with double quotes in particular -- take a look at these two articles for an explanation why:

@gerazov
Copy link
Author

gerazov commented Apr 26, 2021

Awesome! Merci man - it works perfectly 😎 gerazov/vim-toggle-bool@285ff87

@AndrewRadev
Copy link
Owner

Awesome :). I'll close this issue for now, but as I said, feel free to add a link to your plugin in the wiki 👍

@gerazov
Copy link
Author

gerazov commented Apr 26, 2021

I just added a page 😉

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

2 participants