In a code block like this ( '|' representing the caret position):
public void myMethod(){
|
}
The caret position is at the start. I want the line to automatically insert the tabs/indents to the line so it looks like this:
public void myMethod(){
|
}
I know it's just me being lazy but Eclipse had this feature and I'd like to know how to get IntelliJ to do this too.
Thanks.
You can ctrl + alt + l and refactor & reformat your code. You can do this before or after selecting the specific code block to be refactored.
Related
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);
I started out coding using the Allman style, with aligned braces:
void foobar()
{
if(foo)
{
bar();
}
}
After decades I've decided I want that extra screen space; and besides, my client uses non-matched braces, so it's hard to switch back and forth from work to my personal code. So I want to convert all my existing code to K&R:
void foobar() {
if(foo) {
bar();
}
}
Eclipse 4.4 has a sufficient code formatter, and if I select my source tree I can even format files in bulk. The problem is that if I have a line comment where a K&R brace would be, Eclipse will refuse to move the brace up a line, leaving me with a hodgepodge of coding styles:
void foobar() {
if(foo) //if foo
{
bar();
}
}
How can I tell Eclipse to move the brace up, even if there is a comment on the line where the brace goes?
void foobar() {
if(foo) { //if foo
bar();
}
}
A regular expression replacement over files; replace
\)\s*(//.*)(\r?\n)(\s*)\{
1 2 3
with:
) {\2\3 \1\2
Update (Garret Wilson): After much experimentation, the ideal regular expression replacement that addressed all the variations and met my needs was:
^([^/]*(?:\)|\>|do|else|Exception|static|try))\s*(//.*)(\r?\n)(\s*)\{
replaced with:
\1 { \2
I am not that much aware of Eclipse Shortcuts.
I copied code from some link and I pasted in Eclipse Indigo but it is coming like
"public String doLogin() throws ApplicationException{ long executionStartTime = System.cu... }"
I want to format it in java style like
public String doLogin() throws ApplicationException{
long executionStartTime = System.cu...
}
I google it and found few shortcuts like,
"Shift + Tab" , "Ctrl + I", "Ctrl + Shift + F". but is not giving me the behavior I want.
is there I need to add custom formatter or I am expecting more.
You can format text using the Ctrl+Shift+F shortcuts.
You can use Ctrl+A to select all text or you can format only several lines (which you have selected).
There is a caveat however: If your code does not compile the formatting does not work. I don't know whether this is intentional or a bug.
You may want to enable auto-formatting in Eclipse it is a handy feature I usually use:
You can find the formatter settings here:
I also recommend the Eclipse Color Theme plugin!
With default key mapping Ctrl+Shift+F should format your code (current class or selection if any). Of course syntax has to be valid.
You should be able to access that feature using the menu Source > Format where shortcut is displayed if existing.
Type Ctrl+Shift+L to get list of shortcuts in Eclipse.... and to format Ctrl+Shift+F
You can create your own formatter:
Window -> Preferences -> Java -> Code Style -> Formatter.
"Ctrl + Shift + F" should work but make sure you remove " from start and end of the code you copied.
select all text using ctrl + a
and press
Ctrl + Shift + F
to format text
I have to check some textField with the following rule:
if (the value A != the value B) {
this.A.forceInvalid("");
}
This work really fine, but I want to remove the "info bubble" of the error,on the textfield.
I have tried to simply put a css with red border around it, but it seems that my superior dont want that.
How can I erase that bubble?
First of all, this validation looks wrong. It seems you are using gxt Field which already has built-in validators. Here is an example:
yourField.addValidator(new Validator<String>() {
#Override
public List<EditorError> validate(Editor<String> editor, String value) {
final List<EditorError> errors = new ArrayList<EditorError>();
if (!value.equals("test")) {
errors.add(new DefaultEditorError(yourField, "Value is not \"test\"", value));
}
return errors;
}
});
By default it looks like this:
You can change it by using non-default ErrorHandler on the field. It is not clear what exactly do you want, as far as I can understand you want to get rid of (!) sign and its popup. I think that TitleErrorHandler will suit your needs.
yourField.setErrorSupport(new TitleErrorHandler(yourField));
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; }