How do I force opening braces to share the previous code line? - uncrustify

Using the Uncrustify tool I would like to apply the 1TBS style, which places braces like this:
if (x < 0) {
printf("Negative");
}
How do I do this? I can't seem to find the right options.

I think you need to set nl_if_brace to ignore/add/remove/force.
Also, check the sp_else_brace and sp_brace_else options. Set it to 1, otherwise your code will end up with code such as:
if (condition){
instead of
if (condition) {

There's a tool called UniversalIndentGui that lets you play with Uncrustify's various tersely-worded settings and see immediate impact on a block of sample code.
Screenshots of UniversalIndentGUI

Related

regex limitations within live template / can use live-template functions as replacement?

I'm removing builder pattern on multiple places. Following example would help me with the task, but mainly I'd like to learn how to use live templates more.
Preexisting code:
Something s = s.builder
.a(...)
.b(bbb)
.build();
and I'd like to remove it to:
Something s = new Something();
s.setA(...);
s.setB(bbb);
part of it can be trivially done using intellij regex, pattern: \.(.*)$ and replacement .set\u$1. Well it could be improved, but lets keep it simple.
I can create surround live template using variable:
regularExpression(SELECTION, "\\.(.*)", "\\u$1")
but \\u will be evaluated as u.
question 1: is it possible to get into here somehow \u functionality?
but I might get around is differently, so why not try to use live temlate variable:
regularExpression(SELECTION, "\\.(.)(.*)", concat(capitalize($1), "$2"))
but this does not seem to work either. .abc is replaced to bc
question 2: why? How would correct template look like? And probably, if it worked, this would behave incorrectly for multiline input. How to make it working and also for multiline inputs?
sorry for questions, I didn't find any harder examples of live templates than trivial replacements.
No, there is no \u functionality in the regularExpression() Live Template macro. It is just a way to call String.replaceAll(), which doesn't support \u.
You can create a Live Template like this:
set$VAR$
And set the following expression for the $VAR$ variable:
capitalize(regularExpression(SELECTION, "\\.(.*)", "$1"))

How can I configure different spacing rules inside for loops in IntelliJ Idea's code style settings?

I am currently working on a Java project, and I am finding IntelliJ Idea's code style system to be extreamly frustrating as it refuses to accept the settings that I want to give it. Specifically, I want spaces around various operators unless they're inside a for loop's header. For example, the following code should be output:
for(int x=0;x<10;++x){ // no spaces around operators when they're in a for loop's header
System.out.println(x);
}
int a = 10; //spaces around = when not in a for loop's header
int b = 50;
a = b;
if(a < b){ //an if statement is not a for loop's header
doSomething();
}
In essence, I want IntelliJ to make an exception to normal space rules when in the header of a for loop, removing these spaces. It looks absolutely disgusting to me to have what should be a dense construction filled with superfluous spaces, to the point that it is making me not want to use IntelliJ Idea at all, despite its numerous great features. THis, for example, severely diminishes the utility of its automatic refactoring ability because I need to manually go through and fix its formatting errors after every refactor.
When I look in the code style settings, the "around operators" checkboxes seem to have no mechanism for a different setting inside for loops:
How can I get IntelliJ Idea to format my code correctly?

How to get output of Kotlin IntelliJ scratch file?

A script as simple as
println("a")
doesn't produce any output in the Scratch output window. I expect an a to appear in the output window.
I'm using IntelliJ 2019.1.2 CE.
EDIT: Information regarding the expected functionality
According to a question I had asked: IntelliJ Ultimate Kotlin Script REPL skips first printed lines - Scratch Output cut off
It seems to be the case that the REPL wants to output per line, and will only overflow into the bottom area after a certain line length is reached.
In the question I state that I generally add some initial padding so that it always flows into the lower area with something similar to:
repeat(10) { println("BLANK ") }
END EDIT
You have to make sure that Interactive Mode is turned off at the top of the scratch window
This will cause prints to be put into the Scratch Output window.
Be warned, atleast in the version I have of IntelliJ ultimate 2019.1.3, the first 5-9 lines generally dont print so I do something like:
repeat(9) { println("blank") }
If you've defined everything inside of a function, the script won't execute the function automatically - you'll need to explicitly call the function.
You can create your own print, like this:
fun <T> myPrint(x: T) : T = x
then calling e.g.:
myPrint(5)
should show 5 in the result window.

How can I automatically format C# line indentations like VB.NET

When I write something in VB.NET, the IDE automatically formats my line indents perfectly, I don't have to use the tab key at anytime.
In C#, when I needed to edit a line of code, and I perhaps wrecked up the indentation like this...
void MyVoid()
{
if (1==1)
{
int iThis = 5; //line with ugly indentation
}
}
..., and I have to correct the indentation manually or use Ctrl AKF to fix it.
In VB.NET, the IDE would fix it automatically when I skip to any other line.
How can I make this as easy as it is in VB.NET?
You can use my (commercial) Continuous Formatting extension that formats the code as you type.
Power Commands has an option to automatically format the document when you save. There's also a lighter-weight extension titled Format document on Save that should do the same.

How to set IDEA better align parameters passed to some method?

I cannot make IDEA perfectly align parameters passed to some method when I break them into new line by Enter key.
Here is visually what I need. I have a method like this.
When I break parameters into new row each, it looks like this.
Even trying to align them with Tab key does not help.
You see how ugly this is, not to say it's not easy to reach such code. And all I want is too look like this:
(Note: this one I set via Space key).
I must have been doing something wrong as I can bet this can be set in IDEA. But where?
Bring up Settings panel :
Code Style > Java > Wrapping and Braces (tab)
Enable Align when multiline option in Method declaration parameters and Method call arguments
Apart from the way suggested by #Rangi Lin, also make sure that your
Code Style > Java > Keep When Reformating > Line Breaks
in your settings is also checked. Otherwise reformatting your code could realign your method params into a single line.