GithubHelp home page GithubHelp logo

Comments (6)

AndrewRadev avatar AndrewRadev commented on June 18, 2024 1

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.

from switch.vim.

AndrewRadev avatar AndrewRadev commented on June 18, 2024 1

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:

from switch.vim.

gerazov avatar gerazov commented on June 18, 2024 1

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

from switch.vim.

gerazov avatar gerazov commented on June 18, 2024

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

from switch.vim.

AndrewRadev avatar AndrewRadev commented on June 18, 2024

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

from switch.vim.

gerazov avatar gerazov commented on June 18, 2024

I just added a page 😉

from switch.vim.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.