Typing comment block in eclipse - java

I have just started to learn Java and I am a beginner to Eclipse.
When I start writing a source file, I start with a comment, for example:
/*
* This is the format of the comment I intend to get.
*/
To do so, I type /**/ and the take in cursor to the middle of it and press Enter.
To Eclipse's credit, it automatically starts the next comment lines with *, which is what I also want, however, here is something annoying happening which I haven't been able to figure out how to get rid of:
When I go to the middle of /**/ and press Enter, it gives me
/*
* */
*/
instead of intended
/*
*/
Which means that every time I have to go to the end and delete the extra /*. How can I avoid getting that extra */?

Just type:
/*
and then 'enter'.
It will start a comment block for you with just the single ending
*/
and your cursor in the middle. At least, that's what I think you are asking?

All you need to do is typ /* and then enter and it will create what you want. You can also change the template for the way eclipse does comments (or the way elcipse does anything automatically). Follow the link if you want to learn more about how to customize the settings in eclipse.
http://help.eclipse.org/luna/index.jsp?topic=%2Forg.eclipse.jdt.doc.user%2Freference%2Fpreferences%2Fjava%2Fcodestyle%2Fref-preferences-code-templates.htm
Hope this helps. Cheers.

You can also comment out a line of code by placing the cursor anywhere on that line and pressing CNTL + /.
You can also highlight several consecutive lines and comment them all out by pressing CNTL + / (multiple single-line) or CNTL + SHIFT + / (Block commented out)

Try using shortcuts, makes your life a lot easier.
On Windows,
Ctrl + / for single line comment
Ctrl + Shift + / for multiple line comment
On Mac/OS X replace Ctrl with ⌘
More shorcuts
http://www.shortcutworld.com/en/win/Eclipse.html#link_7

Related

How can I change the post fix template in Eclipse? [duplicate]

I am aware of the Eclipse snippet "sysout" which is neatly replaced with System.out.println(); when tab-completed.
This is very useful but sometimes, I need to wrap some existing code in a System.out.println();
In Eclipse internals, the template is defined as being able to wrap a "selected word". But how can I use the snippet with selected text since typing "sysout" obviously removes the selected text.
Any idea?
The sysout template acts upon entire Java statements.
Highlight a statement in the editor.
Hit CTRL-SPACE (or whatever you have set up for content assist.)
Type sysout then hit enter. Note that when you're typing sysout it will temporarily overwrite your statement (but it will come back wrapped by System.out.println when you hit enter.)
Eclipse has "Surround Width" Option which can do this for you.
SHIFT + ALT + Z should get you that to see how that templates meta-data layout.
I don't think you can do it in one go, but what about cutting the selected text and then: tab+space, ctrl+v
it's just one key combination more.
if you use content assist (ctrl-space on Windows), at the end of the list will be the sysout option. you might want to augment the template with quotes around the word selection so you dont need to type them in.
Preference>General>Keys.
then search "content assist" or "content" in "type filter text".
don't press Enter.
choice "content assist".
type Binding and set your own shortcuts.
such as "control+space".
The main part is into:
Java->Editor->Templates
This can accessed by:
Preferences window: into Java->Editor->Templates.
Surrounding a text and press:
Windows: SHIFT + ALT + Z (as sadhasivam said)
Mac: CMD + OPT + Z
to display the Preferences Window

Format specific part of Java code in Eclipse Mars

I am able to format my Java code by configuring it in Save Actions.
What I get after Saving the file is that the whole code gets formatted according to my settings. What I need is, that only a part of code gets formatted according to the settings.
Say, there are 10 methods in my code and I add one more to it. What I want is that only my 11th method gets formatted and the previous 10 gets untouched.
Is that possible? I am using Eclipse Mars
NOTE:
The format includes, removing unnecessary casts, parenthesis, etc.
Source code formatting on save can be limited to edited lines only:
The other save actions however are applied to the whole file.
Select the text you want to format, and press Ctrl + Shift + F to format the selection.
Alternatively you can do Ctrl + I on the selected text to just correct the indentation.
I always use CTRL + SHIFT + F to format my code. In eclipse you can just drag your mouse and select the code you want, then press CTRL + SHIFT + F. That way it will just format the selected code.
Normal the Ctrl+Shift+F will format the current class. Or when a selection is made, only that selected part will be formated.
To make a quick selection, from the current place, use Ctrl+Shift+Arrow-Up to make it bigger. Or Ctrl+Shift+Arrow-Down to make it smaller again.
Telling Eclipse "manually" which parts of your code to format and which not can be a daunting task. Especially if others work on the same project and don't know which parts of the code have to be left out.
Because of this Eclipse allows you to set "markers", simply surround your code with these tags:
// #formatter:off
Here goes your code
// #formatter:on
In order to make this work you have to adjust the Java Code Formatter (it can be reached via Window / Preferences) settings like this:
As far as I know this has been in Eclipse since version 3.6.

Is it possible to modify Eclipse's comment behavior to add the slashes at the current level of indentation, rather than at the beginning of the row?

By default, pressing the Comment out shortcut keys Ctrl + /, has behavior like this:
Notice that the // get added at the left-most position of the row.
Is there a way to set it so that the // appear at the same level of indention as the code that is being commented out?
e.g.
Perhaps a Python habit, but I find it much easier to parse when everything is at the same indention level.
Is there a way to change where the slashes show up?
Yes, it's possible.
go to this link for detailed info
First go to Preferences, then
On the left side of the box, expand the Java option, then expand Code
Style and finally click Formatter
You'll land on the Java-Profile page:
The other, temporary way is via CTRL+SHIFT+F shortcut ( Eclipse: Change the block comment style of ctrl+shift+/ ) . So you'd highlight those comments, and press the combo.
I would then also use a shortcut for CTRL+SHIFT+F , like ALT+F5 (press CTRL+3 , 'keys' to get the preferences page.

How to toggle commenting and uncommeting code in IDEA?

Is there a way to comment/uncomment a block of selected text in IDEA?
Yes, highlight the block and type Ctrl + Shift + /. To uncomment, just repeat.
with Shift this makes for block style comments /**/, without it for line style comments Ctrl + / -> \\ (tested with IDEA 10U)
Jetbrains provides a printable reference card with the default set of Windows and Mac key bindings.
For the benefit of others who might stumble across this question & answers... In addition to the windows key binding provided in other answers, there is also a keystroke for toggling a comment bock in IntelliJ IDEA on the Mac.
Line Comment
⌘+/
Block Comment
⌘+SHIFT+/
The Cmd+Shift+/ doesn't work under Mac with Spanish keyboard layout, because this shortcut it's bind to the main character on the key, which in this case is 7, not /.
For someone else having the same issue, pressing the Cmd+- (Ctrl+- in Windows) is the shorcut for single-line comment while Cmd+Shift+- works for multi-line comments.
use this Simbol
/*
write any thing
*/
For windows
Ctrl + Shift + /
For mac
Cmd+Shift+-
If the Cmd+Shift+/ doesn't work for you (often the case with spanish / german and other keyboards) and you don't want to set it to US/EN keyboard, then use:
Cmd+Alt+s to open the IntelliJ-settings -> select Keymap and search for "Comment with Line Comment" or "Comment with Block Comment". Then rightclick on it and select "add Keyboard Shortcut" and set your own shortcut, for example: Cmd+Shift+NumPad /

How to use the "sysout" snippet in Eclipse with selected text?

I am aware of the Eclipse snippet "sysout" which is neatly replaced with System.out.println(); when tab-completed.
This is very useful but sometimes, I need to wrap some existing code in a System.out.println();
In Eclipse internals, the template is defined as being able to wrap a "selected word". But how can I use the snippet with selected text since typing "sysout" obviously removes the selected text.
Any idea?
The sysout template acts upon entire Java statements.
Highlight a statement in the editor.
Hit CTRL-SPACE (or whatever you have set up for content assist.)
Type sysout then hit enter. Note that when you're typing sysout it will temporarily overwrite your statement (but it will come back wrapped by System.out.println when you hit enter.)
Eclipse has "Surround Width" Option which can do this for you.
SHIFT + ALT + Z should get you that to see how that templates meta-data layout.
I don't think you can do it in one go, but what about cutting the selected text and then: tab+space, ctrl+v
it's just one key combination more.
if you use content assist (ctrl-space on Windows), at the end of the list will be the sysout option. you might want to augment the template with quotes around the word selection so you dont need to type them in.
Preference>General>Keys.
then search "content assist" or "content" in "type filter text".
don't press Enter.
choice "content assist".
type Binding and set your own shortcuts.
such as "control+space".
The main part is into:
Java->Editor->Templates
This can accessed by:
Preferences window: into Java->Editor->Templates.
Surrounding a text and press:
Windows: SHIFT + ALT + Z (as sadhasivam said)
Mac: CMD + OPT + Z
to display the Preferences Window

Categories

Resources