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

Split inline if statements. #67

Open
MeanEYE opened this issue Mar 5, 2015 · 14 comments
Open

Split inline if statements. #67

MeanEYE opened this issue Mar 5, 2015 · 14 comments

Comments

@MeanEYE
Copy link

MeanEYE commented Mar 5, 2015

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
Copy link
Contributor

af commented Mar 20, 2015

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

if (something) doSomething();

into (note the braces):

if (something) {
    doSomething();
}

@AndrewRadev
Copy link
Owner

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
Copy link
Author

MeanEYE commented Mar 30, 2015

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:

  • s: lowercase for regular split, upper case for added braces;
  • j: lowercase for regular join and removing braces, uppercase for keeping them.

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
Copy link
Contributor

af commented Mar 31, 2015

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
Copy link
Owner

@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
Copy link
Contributor

af commented Apr 10, 2015

@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
Copy link
Owner

Oops, my bad. Should be fixed now.

@af
Copy link
Contributor

af commented Apr 12, 2015

Works great now, thank you! 👍

@MeanEYE
Copy link
Author

MeanEYE commented Apr 20, 2015

Documentation should be updated with this in mind. :)

@MeanEYE
Copy link
Author

MeanEYE commented Apr 20, 2015

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

@af
Copy link
Contributor

af commented Apr 20, 2015

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

@MeanEYE
Copy link
Author

MeanEYE commented Apr 21, 2015

All cool. :) Thanks

@AndrewRadev
Copy link
Owner

@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
Copy link
Author

MeanEYE commented Apr 21, 2015

This is from my config:

" configure splitjoin
let g:splitjoin_javascript_if_clause_curly_braces = "sj"

Regardless of my config they behave the same.

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

3 participants