Surround selection with method call - eclipse

Often when I'm writing code I forget to surround a section of code with a method. For example, when printing an array, I realize that I forgot to pass the array into Arrays.toString().
String[] foo(){
return new String[3];
}
main() {
System.out.println(foo());
}
Is there a way in Eclipse that I can select foo() and then use auto complete or something to surround it with Arrays.toString()? So I want to end up with this:
main() {
System.out.println(Arrays.toString(foo()));
}
I know I could use templates, but I would have to make a template for each method I want to use. I'm looking for something like Eclipse's auto complete feature, which knows about every class and method in the build path.

Yes, you could use templates for that:
First, experiment with existing templates:
Go to the source editor and select "foo()".
Open the view General > Templates.
Select some template, for example, Java > toArray and see how it works.
Then, add your own template:
Windows > Preferences > Java > Editor > Templates > New.
I think the right context should be "Java".
Another way of accesing templates is through the content assist: In the source code, in a new line, start typing the first letters of your template, then press [CTRL][SPACE]. A selector will appear with the matching templates. You may find it useful to check the checkbox "Automatically inserted" in the template definition window.
And yet another way to access them is to select a line of code and then Context Menu > Surround With.

A quick way:
Double click or use select enclosing element and its cousins to select the expression you wish to wrap. ctrl-x to temporarily cut it. Type a few characters and ctrl-space to insert your method name and parentheses. Finally, ctrl-v to paste what you just cut.
with templates - under Java Statements: ${method}(${word_selection})${cursor}
You can make a template like the one described by #LittleSanti. If you use a fake template variable for the method name (like ${method} or ${name}) instead of a constant like foo, Eclipse will highlight it and let you paste or type or complete over it. Then when you hit return or tab, it will jump the cursor to the end (the position indicated by ${cursor}
Unfortunately I don't think Eclipse provides a "real" template variable for selecting methods in scope. It would be nice if it would let did completion for you on methods.

Related

Custom content assist for default java editor in Eclipse

I'm currently trying to develop an Eclipse Plugin to support code replacement, like what the default content assist in Eclipse do. What I want to implement is something like "insert argument names automatically on method completion with visualized box around the argument" and I can "use the Tab key to navigate between the inserted names" and "while navigating, list of optional variables for current argument can be displayed and be chosen".
In short, it comes to two questions:
How to add the visualized box around the already existed variable or even Java keywords that need replacement? And at the meanwhile I can use Tab key to switch between these boxes.
How to display a list of candidates to select from when I trigger on the box?
By now I only figure out the extension point : org.eclipse.jdt.ui.javaCompletionProposalComputer may be useful, but I have no idea where to start at? Thanks in advance.
Oh, finally I've solved it myself...
For the 'box', it should be the LinkedModeModel, this class should work with LinkedPositionGroup and LinkedPosition to add mutiple boxes. And we should use LinkedModeUI to set it up.
For the content assistant, there's no need to use the extension point. There is a ProposalPosition class which extends LinkedPosition for you to add your proposals for the 'box' in its constructor. And we can simply use the CompletionProposal to construct a ICompletionProposal array as the argument of ProposalPosition's constructor.

Shortcuts key for Eclipse for Return - Eclipse

I was a Netbeans user for almost one year. Now, I´m changing my IDE to Eclipse and I´m learning Shortcuts Keys. In Netbeans I used to type "re", then hit Tab key to complete return keyword. How is this done in Eclipse?
While I don't think there is a predefined option for this, your best bet will be to create a custom Java Editor Template. In your Eclipse Preferences, under Java > Editor > Templates, you can create a new template with the following specifications:
Name: re
Context: Java statements
Automatically Insert: Checked
Pattern: return ${retVal:var('${return_type}')};
What this will do is you can type re and then bring up Content Assist using Ctrl+Space. The very first proposal will be this template and it will be selected already. If you hit Enter and it'll insert a line like the following:
return retVal;
At this point, there will be an outline around retVal and you can hit Ctrl+Space again and it'll give you variables you can return that are in scope and match your method's return type or simply type what you want to return.

How to get content assist for "for loop" anywhere in Eclipse?

In Eclipse, after a line like this:
List list = new ArrayList();
Typing "for" just beneath, and followed by "ctrl-space" (by default), will bring several options that can help completing this "for loop":
But if the variable "list" is declared far from here (e.g. as a class field) which may not be directly inferred from this context, or there are many Lists declared,then the assistance doesn't work well:
## split line ---
In some cases, Eclipse can assist, but just don't work for member variable. E.g. manually type "another" and ENTER after the ":" didn't persuade Eclipse to guess about it....
(P.S. workable case:
Auto guessed
Entered wanted name, and ENTER, works great
)
Does anyone have any tip to make this assistance work under such scenarios?
I followed Ashutosh Jindal's tip and I managed to configure the template that works (tested with Kepler release). Here it is:
for (${iterable_type:elemType(iterable)} ${iterable_element:newName(iterable_type)} : ${iterable:var(java.lang.Iterable)}) {
${cursor}
}
The main point was to change localVar to var in the template definition (the Eclipse docs clearly explain this).
How to use it:
Print the template name (foreach for default template) and hit Enter. The template will be used with default collection selected by Eclipse (the latest collection declared)
Hit TAB twice to jump to collection element. You will get drop down list with all iterable collections that apply.
Use UP/DOWN arrows to select desired collection, hit Enter. Eclipse will adjust element type and name (very cool).
Click for the screenshot
This works almost as good as Intellij templates. The drawbacks are:
template does not include arrays (as opposed to default foreach template). For arrays you have to define another template.
I have not tried this myself, but take a look at the code template definition. For example, the foreach code template is defined in Preferences -> Java -> Editor -> Templates as follows :
The definition is as follows :
for (${iterable_type} ${iterable_element} : ${iterable}) {
${cursor}
}
Notice the variables being used such as iterable_type.
Now take a look at this Eclipse help page.
There is a variable there called ${id:localVar(type[,type]*)} which is described as follows :
Evaluates to a local variable or parameter visible in the current scope that is a subtype of any of the given type. If no type is specified, any non-primitive local variable matches.
${array} is a shortcut for ${array:localVar(java.lang.Object[])}, but also matches arrays of primitive types.
${collection} is a shortcut for ${collection:localVar(java.util.Collection)}.
${iterable} is a shortcut for ${iterable:localVar(java.lang.Iterable)}, but also matches arrays.
A screenshot of the same :
I believe that if you wanted to increase the scope from which the foreach template infers it's variables, you may have to edit the template definition with the appropriate variable.
Let me know if this helps. Unfortunately, I have not delved into editing the code templates before so shall not be able to give a concrete example.
What I usually do to solve the content assist with for loop is the following:
create a local variable by typing collection variable that is declared far above and a semicolon:
list;
press Ctrl+2 L
Eclipse generates a new local variable declaration which looks like this:
List list2 = list;
type my foreach and autocomplete with Ctrl+space getting the following:
List list2 = list;
for (Object object : list2) {
}
place cursor on list2 in the for loop declaration and press Alt+Shift+I which stands for inline variable.
this results in what you want to achieve. The effort is minimal after some practicing:
for (Object object : list) {
}
Years later, if you enter the name of the field/variable you can press Quick Fix (⌘+1) and in the quick fix menu you can choose different types of for-loops for the collection or array.

how to debug JSF/EL

How to debug EL in the JSF page? I'd like to watch variable values, function calls an so on. The best solution would be an eclipse plugin, but any other possibility is better than guessing "Why this expression failed to render correctly?".
Closest what you can get in JSF/Facelets is placing an <ui:debug /> somewhere in the view:
<ui:debug />
Pressing CtrlShiftD should then show a popup window with debug information about the component tree and all available request parameters and request/view/flash/session/application scoped variables. It's basically a representation of the content of all those maps.
The hotkey is by the way configureable by hotkey attribute so that you can choose another whenever it clashes with browser default hotkeys, as it would do in Firefox; CtrlShiftD would by default show the Add bookmarks dialogue. Here's how you could make it to listen on CtrlShiftX instead:
<ui:debug hotkey="x" />
You'd usually also like to hide it in non-development stage, so add a rendered condition like that:
<ui:debug hotkey="x" rendered="#{facesContext.application.projectStage == 'Development'}" />
In the shown debug information, the information provided about scoped variables isn't that great as you would expect. It only shows the Object#toString() outcome of all scoped variables which defaults to com.example.Bean#hashcode. You can't explore their properties and the values of their properties directly like as you could do in debug view of Eclipse's debugger. You'd need to implement toString() on the class accordingly so that as much as possible relevant information is returned (if necessary, you can even let Eclipse autogenerate it by rightclick source code > Source > Generate toString()):
#Override
public String toString() {
return String.format("Bean[prop1=%s,prop2=%s,prop3=%s]", prop1, prop2, prop3);
}
As to method calls, just put a breakpoint on the Java source code the usual way. Eclipse will kick in there as well when EL calls the method. If it's a managed bean, you'll also just see its properties in the Eclipse debugger.
If you are really having problems then if you can get the source for the EL implementation (easy enough for the RI) then you can use Eclipse to set breakpoints in the EL implementation methods. You need to get an understanding of how the EL code works, but it isn't that complicated. Not for the very faint hearted though.
Another possibility would be to create and evaluate the EL programatically. There are examples of how to do this around. You can then use the debugger to fiddle around with what the expression is and what the result is until you've worked out where your problem lies.

Eclipse PDT: show every call of function

Is there an option in Eclipse PDT to show every call of chosen function.
I now if you select some kind of function and hit F3 key you will see a definition of selected function.
Open the Search Menu, Choose 'Search...', and navigate to the PHP tab, then
Enter the method name in the search string text box;
Select Method in the Search for group;
Select References in the Limit to group.
Find method usages http://img217.imageshack.us/img217/83/eclipsepdtfindmethodusa.png
We have a plugin for that... nWire for PHP. It's a comprehensive PHP code analyzer which represents all your code associations, invocations included in one quick and easy to use view. It can also represent the associations graphically.