How to format comments with one space between // and words? - java

How to automatically format comments with one space between // and words? For example, selecting format in my editors should convert
//login to // login
public interface LoginService {
//login // -> // login
SysAdminDO login(LoginForm form);
}

You are probably looking for the setting
Editor ~> Code Style ~> Java(language) ~> Code Generation
Or else you can alternatively use Comment with line comment shortcut
Just type a line and on that line use the shortcut. e.g.
String var;
I would make this a comment [Press the shortcut]
// I would make this a comment

Related

How to Replace a String to Output Different Text Using Eclipse

My issue is wanting to replace the content of a String with another String. For example, if I have the word Generating Code.., then after waiting two seconds, I want the program to replace Generating Code with Generating Code Successful.
* Note that I am using Eclipse, and the following code works for Notepad *
String code = "Generating code...\r";
String successful = code.replaceAll(code, "Generating the code has been successful.\r");
[...]
try {
System.out.print(code); // prints out Generating Code
Thread.sleep(2000); // Waits two seconds
System.out.print(successful); // Replaces Generating Code with has been successful
}
This code works in Notepad because the \r replaces the text. However, in Eclipse \r does not replace the text, and instead ends the line. Is there a way of getting this functionality to work in Eclipse?
My code in Eclipse is identical to that listed here that works in Notepad++.

Can I automatically pass an existing text into the method parameter?

For example I have the following block of code:
public String getDbSchema() {
return DB_SCHEMA;
}
Is there a shortcut to quickly turn this code into
public String getDbSchema() {
return properties.getProperty(DB_SCHEMA);
}
Currently I have to do properties.getproperty then take out right bracket and re-insert it into the end of the statement
When you select getProperty from the code completion, instead of pressing Enter, press the shortcut of Edit | Complete Current Statement (e.g. Ctrl+Shift+Enter), and DB_SCHEMA will be wrapped into call parentheses.
Sure, you can use a structural find and replace that is a little bit smart.
First, let's presume that this code has the form return XYZ; where XYZ is a constant identifier (CAPS or _)
Then you can go into search and replace in files (ctrl+shift+R), tick Case Sensitive and Regular Expression and enter:
Text to find: return ([A-Z_]*);
Replace with: return properties.getProperty($1);

Remove unnecessary linebreaks on template output?

Using Play 2 I am realising a simple REST API, the output is plain text. My template looks like this:
#(items: Map[String,String])
#for((key, value) <- items) {
#value
#key
}
In the controller:
return ok(views.html.bla.render(itemsMap)).as("text/plain");
This gives the following output:
(empty line)
(empty line)
value
key
(empty line)
value
key
I want to get rid of the first 2 empty lines - is that possible?
Putting the for in the first line removes one of the empty lines at the top, however one still remains and for in the first line makes the template hard to read ): Thanks for any hint!
First off, if you use plain text, you should use txt templates (bla.scala.txt). They also automatically set text/plain; charset=utf-8 content type.
To trim the content, you can return the rendered content directly:
return ok(views.txt.bla.render(itemsMap).body().trim());
In case you want to render HTML content you'd need to change this manually:
return ok(views.html.ble.render().body().trim()).as("text/html; charset=utf-8");
If you are generating plain text output from a map, why do you use views at all? They don't provide any benefit in your case.
You can write the render function in pure Scala. Something like
items.map{ case (k,v) => v + '\n' + k}.mkString('\n')

Eclipse formatter: can it ignore annotations?

Sometimes I want annotations on fields be in a single line and sometimes in a line each. Is there any way to make Eclipse formatter just ignore these annotations and leave the lines breaks just as I did?
Not quite sure what you mean, but you break up lines for field this way:
String text =
"cake" +
"more cake" +
"alot more cake";
This is also an option:
You can go to Properties -> Java code style -> Formatter -> Edit: Then there should be some tags to on/off.
Also include this line in your code:
/* #formatter:on */
I think so. In Preferences go to Java - Code Style - Wrapper. Then edit your active profile, click "Line Wrapping" and then "Annotations - Element-value pairs". Then set the Line wrapping policy to "Do not wrap".

IntelliJ getter/setter format (single-line versus multi-line)

How can you get IntelliJ to generate getter/setters accessor methods on one line like this:
public String getAbc() { return abc; }
… instead of multiple lines like this:
public String getAbc() {
return abc;
}
I'm using IntelliJ IDEA 14.1.0 and you can customise this behaviour.
Just use the "Generate..." option, or use Alt+Insert shortcut, and select "Getter and Setter".
In the "Select Fields" window that gets opened, you have the "Getter Template" option at the top. Use the "..." button next to the dropdown, to edit the template.
Select "IntelliJ Default" and click the "Copy" button to create a new one named "AlwayStartWithGet", which you can edit.
Just remove the following section:
#if ($field.boolean)
#if ($StringUtil.startsWithIgnoreCase($name, 'is'))
#set($name = $StringUtil.decapitalize($name))
#else
is##
#end
#else
get##
#end
And replace it with a simple
get##
You should be left with:
public ##
#if($field.modifierStatic)
static ##
#end
$field.type ##
#set($name = $StringUtil.capitalizeWithJavaBeanConvention($StringUtil.sanitizeJavaIdentifier($helper.getPropertyName($field, $project))))
get##
${name}() {
return $field.name;
}
Now you can use the custom template when generating code, by selecting it in the getter template dropdown.
For Idea 2016.
Getter template
Merge the last 3 lines into a single line:
${name}() { return $field.name; }
Setter template
Add double hash (without a space) at the end of the longest line:
[...] ($field.type, $paramName) {##
Merge the last 2 lines into a single line:
$field.name = $paramName; }
Note: as commented by #mindas, you will probably want instead the visual auto folding that doesn't get versioned.
There are no templates neither for getters nor for equals/hashcode. These are hardcoded in IDEA.
Source
You can see that in this IntelliJ Wishlist
You didn't mention what version of IDEA you are using, so I assume the recent 8 or 9.
Check your Code Style settings, under "Alignment and Braces". You should find a "Simple methods in one line" option there.
I don't know why you want to do this, presumably for saving visual space. If so, just use IntelliJ's feature to fold trivial getters/setters and forget about how lines does it take.
Folding feature can be found in
Settings -> IDE Settings -> Editor -> Code Folding -> Show code folding outline -> Simple property accessors
Alex G and laffuste provided answers that helped me, but even after you follow these instructions, IntelliJ may still automatically format your code. To stop this from happening, go to Settings -> Editor -> Code Style -> Java (or other language). Click on the tab titled "Wrapping and Braces". Under the title "Keep when reformatting" click the checkbox next to "Simple methods in one line".
For further brevity, you can eliminate the blank lines between the getter and setter methods. To do this, click on the tab titled "Blank Lines". In the section titled "Minimum Blank Lines", find the text box next to "Around method". Enter 0 in the text box.
Get
public ##
#if($field.modifierStatic)
static ##
#end
$field.type ##
#set($name = $StringUtil.capitalizeWithJavaBeanConvention($StringUtil.sanitizeJavaIdentifier($helper.getPropertyName($field, $project))))
get##
${name}(){ return $field.name; }
Set
#set($paramName = $helper.getParamName($field, $project))
#if($field.modifierStatic)
static ##
#end
void set$StringUtil.capitalizeWithJavaBeanConvention($StringUtil.sanitizeJavaIdentifier($helper.getPropertyName($field, $project)))($field.type $paramName) { ##
#if ($field.name == $paramName)
#if (!$field.modifierStatic)
this.##
#else
$classname.##
#end
#end
$field.name = $paramName; }

Categories

Resources