Live data from Hacker News

Code Substitution Done Right - Switch.vim

vimninjas.com

11–14 of 14 posts

Re: Code Substitution Done Right - Switch.vim

#12
post #5

I don't get it. How often do you need to change between complete opposites? (should/should_not, true/false, &&/||) And what's the point of changing "predicate?" to "true and (predicate?)" or "false or (predicate?)"

I'm the plugin writer. The should/should_not dance is something I keep doing when I write tests. Say I create a user with certain properties and I assert something about them:

    create(:user, :active => true).should be_active
Now, I want to change its properties and assert the opposite. I duplicate the line, change the property, and switch the "should" into a "should_not":

    create(:user, :active => false).should_not be_active
This is a very simple example, but I do things like this on a regular basis while writing tests. I assume other people have different workflows around this, but this works nicely for me. That's why I mentioned in the video that these are my own little timesavers, so you might not find them that useful :).

As for the predicate stuff, it's actually "true or (predicate?)". The idea is that switching to this would result in the first branch always being executed, unconditionally. I use this for view templates when I want to see how the page looks when, for example, there's no logged in user (it'd take more time to log out and then back in :)), or when the user has no trading accounts, or some other simple condition that I want to disable for just a sec to check out if some div got misaligned.

Really, the basic idea is simple -- if there's some code that you often need to change into some other code, you could store that transformation as a "switch" and avoid repeating yourself.

Re: Code Substitution Done Right - Switch.vim

#13
post #6

This looks cool but I'd like filetype-independent definitions. For example, to convert a ruby dictionary pasted into a .js file.

Well, in your case, you could put a definition for javascript. The built-in "ruby hash style" transformation should work for this:

    " in ftplugin/javascript.vim
    let b:switch_definitions =
          \ [
          \   g:switch_builtins.ruby_hash_style
          \ ]
With this, in a javascript file, you can turn ":foo => 'bar'" entries into "foo: 'bar'" ones.

If you do need global (non-filetype) switches, you could define some by placing them in the g:switch_definitions variable. Read up on the docs if you want more information.

Re: Code Substitution Done Right - Switch.vim

#14
post #3

Annnnnd another tutorial that shows me how vastly underwhelming my vim skills are. Though looking back a year ago, I struggled to navigate, let alone copy and paste. Step at a time, right? RIGHT?!

I recently moved to vim as well. Everything I'm doing I'm pretty much aware that I could probably be doing it in some superdooper quick way... I'll get there though. Eventually.

Just check out `:help index` when you get bored, scroll some random amount down, and use a new thing or two that day.
Post reply on HN