AndrewRadev / splitjoin.vim

Switch between single-line and multiline forms of code
http://www.vim.org/scripts/script.php?script_id=3613
MIT License
1.92k stars 89 forks source link

Split inline `if` statements. #67

Open MeanEYE opened 9 years ago

MeanEYE commented 9 years ago

Hi, I was wondering if it would be possible to add support for splitting inline if statements. For example:

if (something) do_something();

To be split into:

if (something)
   do_something();

Right now what happens is this:

if (
   something
) do_something();

At least in PHP does. Thanks in advance!

af commented 9 years ago

Also interested in this for JavaScript, but in this case turning

if (something) doSomething();

into (note the braces):

if (something) {
    doSomething();
}
AndrewRadev commented 9 years ago

This is not going to be difficult, but I'm a bit worried about the curly brackets. I guess that, in both languages, both formats are acceptable:

if (something)
    doSomething();

if (something) {
    doSomething();
}

And then there's the other way around, when you join them:

if (something) doSomething();

if (something) { doSomething(); }

Personally, I prefer always putting the curly brackets, but I can see how some people would prefer, for example, not to put brackets on the single-line case, but to put brackets on the multiline case. I think I'm probably going to have to put some settings to control this feature.

@MeanEYE, @af, what are your preferred styles for multiline and single-line if clauses? I'd like to figure out a way to customize this without going too far in terms of options.

For instance, I could do this:

let g:splitjoin_js_if_clause_curly_brackets = 1
let g:splitjoin_php_if_clause_curly_brackets = 1

And that would always split and join into something that doesn't have curly brackets. So, with let g:splitjoin_php_if_clause_curly_brackets = 0

if (something) doSomething();
// splits into
if (something)
    doSomething();

if (something) { doSomething(); }
// also splits into
if (something)
    doSomething();

Alternatively, I could have 4 options, with _on_split and _on_join, but that seems a bit excessive.

Maybe there are other ways to do this, but for starters, I'd like to hear what both of you have to say about how you prefer the code.

MeanEYE commented 9 years ago

Well, in my code I use something similar to what Linux Kernel coding style suggests. If there's a single line in if then I don't put curly braces, even with else present.

if (something)
   doSomething(); else
   doOtherThing();

However if any of these has multiple lines, I wrap both of them:

if (something) {
   doSomething();

} else {
   doOtherThing();
   andSometingElse();
}

Now granted, people have different coding styles, so having flexible option is a good thing. Perhaps instead of having simple Boolean value you could create option list like Vim does with guioptions or comma separated values. For example having sJ in splitjoin_php_options would mean:

Or something to that nature given that it's not too complicated to make. Notably, joining in this case is pretty much unnecessary unless you are removing braces, otherwise it's the same as Vim's built-in join.

af commented 9 years ago

My preference would definitely be to have an option to control this. The JavaScript world has a wide variety of style guides and formatting preferences, so configurability would be great.

I personally only use the conventions from my original comment, but if there's a documented option I can put in my vimrc to get that, I don't really care what the default is.

AndrewRadev commented 9 years ago

@MeanEYE The idea for the options sounds great! I hadn't thought of that at all. I hope people aren't confused by it, but I think it won't be a big deal.

I personally only use the conventions from my original comment, but if there's a documented option I can put in my vimrc to get that, I don't really care what the default is.

@af: Yeah, I think I should just add an option as well.

I've created a branch called if-clauses-with-curly-braces where this all works as described for PHP and javascript. I'd really appreciate it if both of you could try it out and let me know if it works alright for you and if the documentation for the options is clear enough.

af commented 9 years ago

@AndrewRadev cool, thanks! I set let g:splitjoin_javascript_if_clause_curly_braces = 'Sj' in my vimrc, and joining is working as expected (no braces). But splitting does not create the braces for some reason.

AndrewRadev commented 9 years ago

Oops, my bad. Should be fixed now.

af commented 9 years ago

Works great now, thank you! :+1:

MeanEYE commented 9 years ago

Documentation should be updated with this in mind. :)

MeanEYE commented 9 years ago

Oh, doesn't seem to work for split it always adds curly braces. o_0

af commented 9 years ago

I always use them when splitting so I didn't notice, sorry!

MeanEYE commented 9 years ago

All cool. :) Thanks

AndrewRadev commented 9 years ago

@MeanEYE, are you sure that you're setting the right variable? There are two of them, one for PHP and one for javascript:

let g:splitjoin_php_if_clause_curly_braces = 'sj'
let g:splitjoin_javascript_if_clause_curly_braces = 'sj'

What are you setting the variable(s) to and what is the result? It could be that the combination of settings somehow doesn't work right, or that there's a Vim setting that somehow affects this.

MeanEYE commented 9 years ago

This is from my config:

" configure splitjoin
let g:splitjoin_javascript_if_clause_curly_braces = "sj"

Regardless of my config they behave the same.