Eclipse Neon 2.0 Append automatically comments - eclipse

Smalle question.
Is there a way to append automatically generate comments/trigger notification/popup window at the end of new code.
Example:
int i = 0; //my comment
Whenever I start writing "int i = 0;", "//my comment" is appended automatically.
Or if u have any suggestion regarding something like this.
Kind regards,
Gregory

There are code templates
Goto Window -> Preferences -> Java -> Code Style -> Code Templates
Under Code there are different options, maybe yours is Method Body
Click on Edit, remove the comment and Save

Related

Eclipse shortcut - generate some a method from some code

is there a shortcut to generate a method, when selecting a piece of code in my program? Sth like encapsule code or sth like that. Does that exist in eclipse? I cannot find anything in the help section.
I really appreciate your answer!
In order to move a code part to a method you have to do the following:
select the code which should be extracted
Right click and select: Refactor -> Extract Method
in the option window: enter a method name and click OK

Eclipse auto-formatter, disable auto line-wrapping of comment lines

I love eclipse auto formatting, but there's one feature that's driving me raging mad:
Since I use wrap-lines in my auto-formmatter, code like this:
private static Location _location = null; // this is a comment
turns into horrible, horrible code like this:
private static Location _location = null; // this
// is
// a
// comment
this is not only painful to watch, but not at all convenient to change back...
Is there any way to remove line-wrapping for comments, or at least fix so it doesn't look like an absolute mess?
Thanks
I think that the thing you are specifically asking about you can achieve by editing your formatter:
Window -> Preferences -> Java -> Code Style -> Formatter. There edit you click on Edit...
If you are using the default Eclipse formatter you will need to edit the profile name (you cna not edit built in profile).
Go to comments
Deselect line comment formatting.
That way no formatting of comments of the type // will be done.
This annoys me also. To fix the wrapped comments you can use the keyboard shortcut CTL+ALT+J to join the comments onto one line. The comment-indicator characters (// or *) will still be there but at least it will save you some steps. Then you can just run a find and replace for the comment-indicators.

Looking for a Combo(Viewer) in SWT/JFace which supports autocomplete

I'm looking for a Combo(Viewer) in SWT/JFace which supports autocomplete / type-ahead, i.e. the user can enter a couple of characters and the drop down list should show all matching elements.
You can also check out the org.eclipse.jface.fieldassist.AutoCompleteField class. It's not a combo, just a text field, but it adds auto complete functionality as if it were a combo very easily. You can do something as simple as this:
Text textField = new Text(parentComposite, SWT.BORDER);
new AutoCompleteField(textField, new TextContentAdapter(), new String[]
{"autocomplete option 1", "autocomplete option 2"});
I don't think there is anything like this built into either Combo or ComboViewer.
As thehiatus suggests org.eclipse.jface.fieldassist.AutoCompleteField is probably the best place to look for this, however, there is support for Combos:
new AutoCompleteField(combo, new ComboContentAdapter(), new String[]
{"item0", "item1"});
You may be interested in Eclipse's "Content Assist" feature. You can see it in action when using the Eclipse IDE's Java editor. As you edit source code, you will sometimes see a drop-down menu with phrases that complete what you were typing. (Note that you can press Ctrl+Space to force the drop-down menu to be displayed.)
You can implement this in your own SWT/JFace application as well. The "Java Developer's Guide to Eclipse" has an sample application that implements Content Assist. The sample application is a SQL editor, and it is described in Chapter 26, "Building a Custom Text Editor with JFace Text." There's actually an online overview of the chapter here. The sample SQL editor project, com.ibm.jdg2e.editor.jfacetext.sql, can be found here.
On the other hand, if you want to create your own Combo widget and auto-populate it based on input that is being entered, then this might not be very applicable. I'm thinking the org.eclipse.jface.viewers.ComboViewer might be helpful (though I'm not positive).
Check out: http://sourceforge.net/projects/swtaddons/
I use it in my project (with a little tweak).
It's really dead easy to set this up.
As thanks to paz117's comment, thought I'd share the code to make this work:
String[] proposals = new String[controller.model().size()];
for (int i = 0; i < controller.model().size(); i++)
proposals[i] = controller.model().get(i).getAppropriateName();
comboViewer = new ComboViewer(parent, SWT.NONE);
comboViewer.setContentProvider(new ArrayContentProvider());
comboViewer.setLabelProvider(new AppropriateLabelProvider());
comboViewer.setInput(_controller.model());
// additionally, configure the comboViewer arbitrary
new AutoCompleteField(comboViewer.getCombo(), new ComboContentAdapter(), proposals);
The only minor nuisance is that you have to separately populate the model of ComboViewer and AutoCompleteField separately, but that can be at least automated via a static utility method or something similar.
As reference for future visitors, the AutocompleteComboInput (SWT Add-on), can also be a way to achieve this.
Code snippet for screenshot (refer to documentation link above for the code template):
import net.sf.swtaddons.autocomplete.combo.AutocompleteComboInput;
...
subjectCodeCombo = new Combo(tab3Composite, SWT.DROP_DOWN);
// other code modifying Combo appearance here...
// returns a String[] of items retrieved from database
String[] subjectCodeArray = dbQuery.subjectsToArray();
subjectCodeCombo.setItems(subjectCodeArray);
subjectCodeCombo.setText("- SELECT -");
new AutocompleteComboInput(subjectCodeCombo);
The add-on requires all JARs below to be added to the Library: (more info)
eclipse-equinox-common-3.5.0.jar
net.sf.swtaddons_0.1.1_bin_src.jar (sourceforge)
org.eclipse.core.commands.jar
org.eclipse.jface-3.6.0.jar
Click here for JAR pack.

Does the Eclipse editor have an equivalent of Emacs's "align-regex"?

I've been using Eclipse pretty regularly for several years now, but I admit to not having explored all the esoterica it has to offer, particularly in the areas of what formatting features the editors offer.
The main thing I miss from (X)emacs is the "align-regex" command, which let me take several lines into a region and then format them so that some common pattern in all lines was aligned. The simplest example of this is a series of variable assignments:
var str = new String('aString');
var index = 0;
var longCamelCaseObjectName = new LongNameObject();
After doing align-regex on "=", that would become:
var str = new String('aString');
var index = 0;
var longCamelCaseObjectName = new LongNameObject();
Now, you may have your own thoughts on stylistic (ab)use of white space and alignment, etc., but that's just an example (I'm actually trying to align a different kind of mess entirely).
Can anyone tell me off-hand if there's an easy key-combo-shortcut for this in Eclipse? Or even a moderately-tricky one?
You can set the formatter to do this:
Preferences -> Java -> Code Style -> Formatter. Click 'Edit' on the profile (you may need to make a new one since you can't edit the default).
In the indentation section select 'Align fields with columns'.
Then, in your code CTRL+SHIFT+F will run that formatter.
That will of course run all the other rules, so you may want to peruse the various options here.
Version 2.8.7 of the Emacs+ plugin now supplies a fairly complete align-regexp implementation in Eclipse
If you wish to do something more complex than just aligning fields (or anything else the Eclipse code formatter offers you) you are pretty much left with having to write your own plugin.
columns4eclipse is a nice option. It is an Eclipse plugin that allow you to do the alignments you mention in your question. I use it with Eclipse 4.3 and 4.5, it works well. I had made a gif video showing its use but my answer got deleted by a mod, so I let you try and see by yourself.
This plug-in does exactly what you want: OCDFormat
It works in all text files, not only Java.

How to minimize code area in Eclipse?

There is a nice feature in Visual Studio: you can create special code areas which can be minimized just as class methods in Eclipse are minimized. Like:
#region
//some code
#endregion
Is there a way do make such pleasant feature in Eclipse?
It's called "collapse all", click on the editor view that you want to collapse all your methods in go to
help>
key assist...>
double click "collapse all">, everything is collapsed
Click on ' - (minus)' symbol on the side of the editor, Right click on minus symbol , Go to folding / Collapse All
Keyboard shortcut : Ctrl+Shift+NumbPad_Divide
First check Folding is enable or disable if enabled then you can minimize or collapse code and expand code. You can check Folding through
Folding is configured under Window -> Preferences -> Java -> Editor ->Folding
Please check Enable Folding if its unchecked
Now you can minimize your code
Collapse All (all functions on page) : Ctrl+Shift+NumbPad_Divide
Expand All (all function on page) : Ctrl+Shift+NumbPad_Mulitply
Collapse One function : Ctrl+NumbPad_Minus
Expand One Function : Ctrl+NumbPad_Plus
If you mean by minimizing folding, then I don't think Eclipse has the folding option you want. When I look at the eclipse folding options it tells me that it can fold
Comments
Header Comments
Inner Types
Members
Imports
If you don't mean folding, then I'm sorry for the confusion.
There are some plugins for this, like "Coffee Bytes Java Folding" for Eclipse.
Other IDE like Netbeans as an native equivalent.
It's really IDE dependent in Java, not implemented in the language like in C# for example.
Java Equivalent to #region in c#
Check the second answer
//region MY REGION
code here
//endregion
Heres how I do it:
Create an empty class called Blank
Use this outline for your code:
Blank b = new Blank()
{
public void myCode()
{
/Insert code here/
}
};
b.myCode();