Word-wrapping comments in Netbeans - netbeans

/*
*
* This is a long comment. I broke it into lines, but that made it impossible to edit without screwing up the formatting. Is there a way to make Netbeans add line breaks automatically?
*
*/
Reads: This is a long comment. I broke it into lines, but that made it impossible to edit without screwing up the formatting. Is there a way to make Netbeans add line breaks automatically?

As for today, the current NetBeans (7.3) word-wraps comments with some nice available extra options when formatting. NB: Unfortunately, this is currently only available for the Java language.
You can launch the formatter with the context menu→Format, or if you did not change the standard shortcuts, Alt+Shift+F should do.
To access the corresponding configuration: Tools → Options → Editor → Formatting; Language: Java; Category: Comments

Your IDE won't format the comment for you. You will have to do it by hand. Yes, that means you have to re-format it once you add new words to it.

If you press Enter to insert a new line, NetBeans will simply extend the docblock formatting - it won't break anything. There's also a useful option in the Preferences to display a line in the right margin, so you know when to break a line. It's in:
Tools > Options > Editor > Formatting > Right Margin
Is this what you're looking for?

Go to tools->options->editor->formatting
find line wrap and choose one the wrapping style you desire,there are two options : afterword and anywhere.
and then click OK! :D
nb : I suggest you to choose afterword

Related

Eclipse: Automatic line wrapping to specified width

I'd like for my lines, especially within comments, to be automatically managed so they don't get too long.
I remember once I had a configuration for vim which automatically moved the word I was typing to the next line once I reached 72 characters. It wasn't smart enough to rearrange the paragraph if I edit it, but it was a start.
Is there something that can manage these for me? I have a tendency to write really long comments in my code, and it helps to make them look neat by having consistent width, but it's always a pain to do this because oftentimes editing a sentence requires editing the entire rest of the paragraph.
I have just recently discovered the Ctrl+Shift+F feature. It is amazing and superior to Ctrl+I which is what I was using up till now, but I noticed that it does not do anything to clean up my comments.
Update: The answers are correct when working with Java in Eclipse. It seems like I have to wait for the CDT to incorporate this feature.
In "Windows -> Preferences", go to "Java -> Code style -> Formatter" to customize the formatter (called when you click Ctrl+Shift+F). In the tab "comment", you can set the maximum line width for comments (it can be different then the line width for code).
Tip: in the preferences, "Java -> Editor -> Save actions", you can make Eclipse to automatically format your file when you save it, so your code is always correctly indented !
The automatic formatting of Eclipse great no question.
If your comments are reformatted depends on what comment type and how you already have inserted line breaks.
Writing for example one very long line comment starting with // will be broken down by the formatter into multiple lines.
However you later edit the formatted lines - e.g. delete parts of it the formatter will leave them as they are. Only over-long lines will be changed.
Just in difference to block comments like this: /* comment */
Those comments will always be re-formatted in case the line is too short or too long.
If you want to format your header comment, you have to check Enable header comment formatting - that was the trick for me.
Obviously, to use this, you must create new formatter profile.

Eclipse automated code formation and Hyperlinks

I'm a big fan of the automated code formation (STRG + SHIFT + F) from eclipse. It makes your code so much more readable. However, now that I'm comenting my code, I'm getting the problem with Hyperlinks. Code formation adds line breaks anywhere in your code, so if you have e.g. a very long hyperlink it breaks into multiple lines and makes it unresolvable :(
Is there a way that eclipse doesn't format specific comment parts like Hyperlinks?
Regards,
Stefan
Code formating doesn't put line breaks in links inside a tags:
<a href=
"http://www.example.com/very-long-url">Example</a>
There is a line break just after the href=, so it may look ugly in a paragraph. Enclosing urls in <tt> tags prevents line breaks, but the url won't be a clickable link. Combine both and you get hyperlinks without line breaks. If they exceed the maximum line length, they will start on a new line though.
<tt>Example</tt>
There is also //#formatter:off to disable formatting for the following lines and //#formatter:on to enable it again.
I don't think there is any way from setting that up in the menu. The workaround is to disable block or line comment formatting in the formatter profile:
Window -> Preferences -> Java -> Code Style -> Formatter -> Edit -> tab Comments
As pointed out by Kheldar, you can always customise/extend the Java Codeformatter using the extension point, but that's probably not a one hour job for someone unfamiliar with the JDT.
Please indicate, if you need any assistance coding an extension for the formatter.

Eclipse-like comment formatting in IntelliJ IDEA?

In Eclipse, I can format comments by selecting them and pressing Shift + Ctrl + F. For example, when I select a method comment like this:
/**
* This method
* does some stuff.
*/
and press Shift + Ctrl + F, Eclipse automatically wraps it:
/**
* This method does some stuff.
*/
Is there anything comparable to this in IDEA?
EDIT: To clarify, I'm looking for comment formatting that also breaks lines that are too long into multiple lines.
The closest thing that you can get is Edit | Join Lines (Ctrl+Shift+J). You have to select the lines you want to join first.
To wrap long comments enable Settings | Code Style | JavaDoc | Wrap at right margin.
For Javadoc comments, you want to make sure the "Wrap at right margin" setting is checked. See Code Style > JavaDoc, under "Other". However, this setting only seems to take effect when you reformat the whole file, since a reformat of just the Javadoc (i.e., select the Javadoc, then do a Code (menu) > Reformat Code... or CtrlAltL) that exceeds the right margin doesn't force it to wrap. If I reformat the entire file, then it wraps at the margin as expected.
This seems like a bug (though one that doesn't seem to have been reported), since if you have to set the "Ensure right margin is not exceeded" checked, then selecting the Javadoc text and doing a reformat code does indeed wrap the lines. This setting is in Settings > Code Style > Wrapping and Braces. You can also do a search in the Settings dialog for "ensure right margin".
You'll still have to manually join the lines using CtrlShiftJ
This might be worthy of an improvement request to JetBrains.
Existing comment will be reformatted when you do "Reformat Code" (⌥⌘L in Mac).
#kghastie uncovered the key.
Steps:
Set the Code Style > Java > JavaDoc > Wrap at right margin setting.
Select the full lines of the entire JavaDoc comment.
Reformat Code (Ctrl-Alt-L or ⌥⌘L).
Lesser alternative:
Set the Code Style > Java > JavaDoc > Wrap at right margin setting and the Code Style > Java > Wrapping and Braces > Ensure right margin is not exceeded setting.
Select some text within a JavaDoc comment.
Join Lines (Ctrl-Shift-J) followed by reformat Code (Ctrl-Alt-L or ⌥⌘L).
Beware: This will leave all the selected lines joined even where you had paragraph breaks (<p/> or \n\n).
The JetBrains plugin Wrap to Column is made for this:
From the overview:
Wraps text to the specified column width. Similar to the Emacs command 'Fill Paragraph' and Vim's gq (format lines) command. This is a replacement for the native Intellij Fill Paragraph command, which doesn't work quite how I need it to.
This plugin provies two IDE actions:
Wrap Line to Column: Wraps selected text or the current line if no text is selected. This is useful for IdeaVim users who wish to pair the command with motions like vip (select current paragraph).
Wrap Paragraph to Column: Wraps the paragraph (multiple lines) in which the cursor appears. No selection is needed, and will be ignored.
I'm using IntelliJ 14 on a Mac, which has a Fill Paragraph command. Access it via the awesome universal Command-Shift-A action search feature. Works like a charm!
This is a hack, not a really good solution, but if you have a block of code that you want formatted like this and it's in serious need of auto format, because it's going over the 80 line max, or it's just unreadable...
You can just put if ("foo" == "bar") { on top of whatever you want formatted, and then and the} at the bottom of the if statement, to close it, and voila, your code should auto-indent, auto format, etc... Then take it out, highlight all of what you just formatted and press SHIFT+TAB to move it back 4 spaces and remove the dummy if statement

Automatic EOL conversion in Eclipse

Need to keep EOL format consistent in all resources under Eclipse workspace.
I know about Eclipse preference that sets new line style for newly created files, but I would like to have automatic conversion for already existing files. Is there some settings/plugins?
I want just setup once and be sure that all line endings are in the same format.
In addition to the Window > Preferences > General > Workspace setting for new files that you already know about, there is a File > Convert Line Delimiters To option. I don't know of any existing plugin/tool that will do this automatically when you save, but you could certainly write one or make converting the line ending part of your process.
To make it easier on yourself, you can bind keyboard shortcuts to the conversion commands by going to Window > Preferences > General > Keys and filtering using "delimiter":
In Eclipse, to convert the line endings for existing files:
Go to the file browser view, and click on the project/folder/file that you wish to convert.
From the menu bar, select File > Convert Line Delimiters To > Windows / Unix / MacOS 9.
You can Search your resources with the Search-Dialog and go to the tab File Search. There you can enter a Regular expression. Enter \r\n or whatever line ending you want to change.
Then hit the Replace .. Button instead of Search.
I want just setup once and be sure that all line endings are in the same format.
... ok, my answer does not consider this.
You might get usefull results with Eclipse save actions: If the eclipse formatter also converts the EOL style, you could use it to modify EOL style only for the files you are modifying.
Unfortunately I don't have eclipse here, so I can't test if this actually works. Worth a try, however.

Automatic indentation on Eclipse PDT when copy-pasting

This is a simple question :
Is there any automatic indentation alignment on Eclipse PDT when you do a copy-paste ?
Because it doesn't work on my installation (raw copy-paste), but maybe this is because I use tabs instead of spaces, so I want to know if it will work if I switch to spaces.
Thanks
Actually, such a function exists now for eclipse CDT. In preferences : C++ -> Editor -> Typing, "when pasting", "adjust indentation".
Perhaps it is the same for PDT ?
I don't know specifically about PDT, but normally you can indent code with Ctrl + i, and format code with Ctrl + Shift + f (under Preferences -> Java -> Code Style -> Formatter you can specify how the formatter should work).
You can even select multiple files in the navigator and select Source -> Format to format several files then.
Tony Vermeiren had a suggestion that worked for me
(from https://bugs.eclipse.org/bugs/show_bug.cgi?id=351771#c17)
open workspace/.metadata/.plugins/org.eclipse.core.runtime/.settings/org.eclipse.php.ui.prefs
add "smartPaste=false" at the end
save & restart eclipse
A similar idea to Davids is the use of Save actions: in the Preferences selecting Java/Editor/Save actions you can define an action to format the java code files during saves. It would solve the paste format problems during the save.
If you manage codes with different coding standards, where this formatting might have problems, define multiple formats, on a per-project basis (using the Project properties window).
Ok then I guess the answer is : no, this functionnality doesn't exists yet. I've moved to PhpStorm and I'm more than happy.
TLDR: uncheck "C/C++->Editor->Typing->Adjust indentation"
Explanation:
When i copy paste the following:
And "C/C++->Editor->Typing->Adjust indentation" checked
The result is that the second line has unwanted indentation:
To fix this just uncheck "C/C++->Editor->Typing->Adjust indentation"