Formatting code in Netbeans to printable format - netbeans

I am writing Java class file using NetBeans and I use ALT+SHIFT+F for auto formatting.However, This is not wrapping the code to be printable in single page.
How to change customize Formatting settings so that when i do auto formatting the code gets formatted as such it can be printable in single page?

There are a couple of different ways to handle this:
If you go to Tools -> Options , Select Editor at the top and the Formatting tab, select Java in the Language drop-down list and Wrapping in the Category drop-down list. There are many options that make the auto-formatting break more lines by switching (Never to either Always or If Long ) which would keep line lengths shorter but still not guarantee that a really long line doesn't extend off the page. It would probably have to break the syntax of Java in order to always be able to break those lines.
Another option would be to leave auto-formatting alone and just adjust the Print options. Select the text file. Go To File -> Print .... On the first dialog click Print Options. On that dialog click the Wrap Lines checkbox and/or change the zoom to print in fewer pages.

Related

Disable line breaks after loops without body

Consider a Java while-loop without a body, for example this:
while (map.values().remove(value));
When formatting it (Shift + Alt + F), Eclipse breaks the line before the semicolon and indents it:
while (map.values().remove(value))
;
How can I configure the formatter to not add these line breaks?
Be aware that by setting this semicolon to a new line, you may avoid unintended endless loops and a long search to find them.
However, in Eclipse all code formatting properties are 1) in the project properties and 2) in the Eclipse properties. The project properties inherit from the Eclipse properties.
So if you open the project properties, type "format" in the search field. It will show you a "Formatter" menu item, that may depend on the current language (at least in Java it is called "Formatter"). Then, in this example, allow the project-specific settings, and then click edit. You will see a flood of possible details you can decide.
See here:
Then, in the tab "New Lines", uncheck the checkbox "Put empty statement on new line", in the section "Empty statements".
Often you have to try and search a bit because there are really many options. The part at right shows you a preview of the code formatting.

ignore differences in syntax in beyondcompare

In a branch of code I have changed all of the code from obj.varname to obj("varname") and when I compare the code I would like to ignore these differences since varname is the same.
I have a regular expression that I think I need but unfortunately can't get the comparison to be ignored using Beyond Compare from Scooter
^obj\("\w*"\)|obj\.\w*$
I am following this tutorial http://www.scootersoftware.com/support.php?zz=kb_unimportantv3
So my question: is this even possible with beyond compare? If yes, please share a solution including either instructions or post your screenshots.
Beyond Compare 3's Professional edition supports this through its Text Replacements feature. If you've already purchased a Standard edition license you need to revert to trial mode to test it: http://www.scootersoftware.com/suppo...?zz=kb_evalpro
Load your two files in the Text Compare.
Open the Session Settings dialog from the the Session menu, and on the Replacements tab click New to create a new replacement.
In the Text to find edit, use (\w+)\.(\w+)
In the Replace with edit, use $1("$2")
Check the Regular expression checkbox.
The alternative would be to mark any instance of obj.varname and obj("varname") as unimportant. The basic steps would be this:
Load your two files in the Text Compare.
Open the Session Settings dialog from the Session menu, and on the Importance tab click the Edit Grammar... button.
In the next dialog click the New... button below the top listbox.
Change the Element name field to something useful (say, "PropertyAccess").
Change the Category* to List.
In the Text in list* edit, add these two lines:
obj.varname
obj("varname")
Click OK to close the Grammar Item dialog and then click OK again to close the Text Format* grammar item.
Uncheck "PropertyAccess" (or whatever you named it) in the Grammar elements listbox in the Session Settings dialog, then click OK to close it.
This approach isn't as flexible or clean. In the steps above you're matching specific, hardcoded object and variable names, so obj.varname is unimportant but obj.othervar isn't, even if it's aligned against obj("othervar"). If text on both sides is unimportant the difference will be unimportant; if one side is important it will be an important difference. So, with the above steps, obj.varname and obj("varname") will be unimportant everywhere, but it will work correctly since they'll either be matched to other cases that also match those definitions (and thus unimportant) or will be matched to something else that doesn't match that definition, which will be important and will make the difference important.
You can use regular expressions to match more general text categories, but you probably don't want to. For example, if you wanted to match all text that followed that pattern you could use these two lines instead:
\w+\.\w+
\w+\("\w+"\)
And then check the Regular expressions checkbox in the Grammar Item dialog so they're matched that way.
The upside/downside to that is that any text that matches those patterns is then unimportant. abc.newvar vs. def.varname would be considered an unimportant difference because both sides match the unimportant definition. That's good for things like comments or whitespace changes, but probably isn't what you want to do here.

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-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

What tool can do a visual comparison of two sections within the same file?

Good file comparison tools were already discussed to the pain, but my problem is more exotic. Is there any visual text comparison tool (like WinMerge) that would allow me easily do visual comparison on two sections within the same file?
I have multiple configurations within vcproj file and need to maintain them. It is a pain to do this manually -- splitting windows, scrolling character-by character. On top of that xml is very verbose and takes lots of screen real-estate. I cannot believe there is no tool to do automatic file section comparison, since this sounds like a very common problem.
Please, do not offer me to use property pages, I do not want more complexity, I want less. Splitting manually into files and then comparing them is also too medieval (I am doing this now anyways).
I use Beyond Compare (not free, but I think a shareware version is available). You can select the same file for left and right sides, then right-click the beginning of your section on each side and select "Align Manually". This would allow you to compare two sections of the same file relatively easily.
Overall, I highly recommend the product. I haven't tried version 3, which is what they currently have on their Web site, but version 2 is a fabulous tool. A+
Emacs Ediff.
I use UltraEdit for most of my text editing and they have a product called UltraCompare that does a visual compare.
Update by Mofi
UltraCompare Professional supports also a comparison of text snippets in addition to entire files.
After starting UltraCompare, select Text Compare in menu Mode if not already selected. Select in text editor the first text block which should be compared, press Ctrl+C, switch back to UC and paste with Ctrl+V the block into left text area pane. Switch again to text editor, select the other block in same file, press Ctrl+C, switch back to UC, click into right pane and paste the block with Ctrl+V. The two blocks are immediately compared and the differences are displayed.
Such a text snippet comparison for two blocks in same file can be started also directly from within UltraEdit. Select the first block in file, press Ctrl+C, Ctrl+N, Ctrl+V and Ctrl+A to copy, paste and reselect this block in a new file. Select the second block in file. Execute command Compare from menu File in UltraEdit with option Compare selected text automatically being enabled and click on button Compare. UC Professional is started with just the 2 selected blocks for comparison.
You can use Meld to do this
Open up meld without specifying file names
Meld with prompt which type of comparison you want. Choose file comparison
Meld will present the the icon to select the file names. Below that it will prompt for a Blank comparison. Choose that.
In the file comparison window, paste the sections of the file you want to compare.