IntelliJ IDEA Java code style: Space between Annotation and Enum - java

How to add space between ) and Enum name?
I want to see this:
enum SampleEnum {
#Annotation("1") ONE,
#Annotation("2") TWO,
#Annotation THREE
}
This is how auto-formatting works:
enum SampleEnum {
#Annotation("1")ONE,
#Annotation("2")TWO,
#Annotation THREE
}
Don't want to use #formatter:off / on
UPDATE:
I added an issue https://youtrack.jetbrains.com/issue/IDEA-170457

The settings for auto formatting code are grabbed from the settings for "Code Style." These settings can be found at:
File - Settings - Editor - Code Style - Java
The setting your looking for is probably under the "Spaces" tab
Hopefully this fixes your problem.
Alternatively you can look through the IDEA add on repository for code style add-ons:
https://plugins.jetbrains.com/search/idea?correctionAllowed=true&search=code+style

Related

How can I use the googleJavaFormat to make reflowLongStrings set to a max of 120 chars per line

I am using the Spotless plugin to format my Java code. However, when I am enabling the reflowLongStrings rule, it breaks lines to be no longer than 80 chars.
Is it possible to change it to 120?
Google java format is not configurable by design (have a look at this) so there is no way for configuring a custom max line width. What you can do is to configure spotless plugin to work with a custom eclipse jdt formatter. Here you can see how this is done in the spotless documentation.
For example, for gradle you would have:
spotless {
java {
eclipse().configFile('custom-config.xml')
}
}
Instead of:
spotless {
java {
googleJavaFormat()
}
}

Using SonarLint with VSCode. How can I change the regex rule S116 for Jave uses?

I am using Sonarlint with VSCode on a Java project on Windows 10. My project has a variable naming convention such that we use underscores as long as it isn't the first character. These variables are triggering Java Rule S116 "Field names should comply with a naming convention". The doc on this rule says that the default regex it uses is '^[a-z][a-zA-Z0-9]*$' It also says:
Parameters
Following parameter values can be set in the
SonarLint:Rules user settings. In connected mode, server side
configuration overrides local settings.
format Regular expression used to check the field names against.
(Default value: ^[a-z][a-zA-Z0-9]*$)
This strongly implies that the value of the regex can be changed by the user and that it can be done with local configuration. But I can't figure out from this information what exactly do I do to change the value. Any ideas?
In the settings GUI find the setting for this extension and change it to
^[a-z][a-zA-Z0-9_]*$
There wasn't a place in the setting GUI for this, but it can be set in the user (not workspace) settings.json file, by adding this entry:
"sonarlint.rules": {
"java:S116": {
"parameters": {
"format": "^[a-z][a-zA-Z0-9_]*$"
}
}|
}

Groovy script without default imports

Problem
With Groovy's default imports any script has access to java.{lang,util,io,net}.*, groovy.{lang,util}.*, java.math.BigInteger, and java.math.BigDecimal without importing anything.
What if I want none of that?
Is it possible to create a GroovyShell that would not have any default import?
Example demonstrating the issue
def importCustomizer = new ImportCustomizer()
// importCustomizer.clearImports() <-- I wish that would exist
def configuration = new CompilerConfiguration()
configuration.addCompilationCustomizers(importCustomizer)
def binding = new Binding("Date", "today")
def shell = new GroovyShell(binding, configuration)
println shell.evaluate("Date")
It prints class java.util.Date.
I'd like to get today instead, using Date from the binding.
(It's just an example, inspired by this recent question).
What I have tried so far
With CompilerConfiguration and ImportCustomizer, I can only add more imports, I don't know how to start from a clean slate.
I've also seen this answer (for which I just fixed the dead link), which pointed me to where those default imports are defined, but I don't see a way to remove imports, again it's only about adding more.
It looks like I have to hook "Semantic Analysis" phase:
This includes resolving classes, static imports and scope of variables.
Using groovyConsole confirms this: if I type x=Date in there, then use "Script" menu → "Inspect AST", the tool shows:
after "Conversion" I still have x=Date
after "Semantic Analysis" I see x = java.util.Date
And at that phase the ResolveVisitor does its thing (using default imports) and I have not found a way to get around that.

IntelliJ: foreach Live Template code formatting

In IntelliJ idea when I insert the foreach live template it will put newline after ':' so it will look like this:
for ( :
) {
}
I want to have the for statement on one line like this:
for ( : ) {
}
I tried to change my code formatting preferences, but could not figure out what setting influences this particular case.
So my question is how to set code style options to achieve the desired behavior?
Use the iter live template rather than the foreach. foreach is under the Android block, and the default style for that is what adds the newline.
Update:
As of at least 2018.1.1 (not sure when it was added), you can now type the <name of your collection>.for then tab and it will expand out into a foreach loop.
It's also brought in the same surrounding/expansion for stuff like <array>.stream then tab and probably a few others I'm not aware of.
Go to File -> Settings -> Editor -> Code Style -> Live Template.
At the right side open Android list and stay on foreach .
In the Options area uncheck Reformat according to style.
You can see how to do it in the IntelliJ IDEA settings foreach style
You can change the template for the enhanced for loop in IntelliJ by changing the setting in Live Templates.
Go to File -> Settings -> Editor -> Live Templates. In the right side, choose iterations -> "iter (Iterate Iterable | Array in J2SDK 5.0 syntax)". At the bottom you can see the template text and you can change it by introducing the newline where you want it. Change
for ($ELEMENT_TYPE$ $VAR$ : $ITERABLE_TYPE$) {
$END$
}
to
for ($ELEMENT_TYPE$ $VAR$ :
$ITERABLE_TYPE$) {
$END$
}
and apply your changes.
In the source code editor, choose Code -> Insert Live Template... -> iter, then IntelliJ will insert the code template as you've specified, with boxes around the variable names for changing them.
for (String arg :
args)
{
}

How to do code format switching in eclipse?

A)
public class SomeClass
{
private SomeClass()
{
}
public String someMethod()
{
return "";
}
}
B)
public class SomeOtherClass{
private SomeOtherClass(){
}
public String someOtherMethod(){
return "";
}
}
I have joined a new team and will be working on a project which follows the A) convention. However, I have always been the B) java style person and am way more comfortable with B).
1)On the checked out code, is there a way I could convert the java code style in my eclipse to B)
2)And also ensure the project->Team->Synch with Repo ignores this style change when checking for updates ?
3)Before comitting, I want to switch the code back to the commonly followed style and check it in. I synch for changes every morning and commit changes throughout the day.
Is creating a new profile in the preferences->code style->Formatter the only way ? I also looked at http://astyle.sourceforge.net/ but I am somehow confident there is a simpler eclipse solution to this. How could I achieve this in the simplest possible way ?
I am using eclipse kepler
Work flow:
In Windows > Preferences > Java > Editors Save Actions deselect formatting on save.
Check out code.
Clean up your code(Right click on project go to Source > Clean up. Note this works on project level but not on working set, so you have to do it on each and every project) with your Formatter(B) profile enabled.
In Windows > Preferences > Java > Editors Save Actions select formatting on save and start working.
Same as step 1.
Same as 3 but with formatter profile A.
Commit the code.
These steps can be automated with Ant/Maven script(?) or by developing your own eclipse plug-in.
On sync comparator will NOT ignore style change. IMHO there is no escape. Clean up before sync is only the go.
In Git SCM there are some commit and checkout hooks but I haven't explored on this.

Categories