How to get content assist for "for loop" anywhere in Eclipse? - 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.

Related

deleting an element in debug mode in eclipse

I'm running my code in debug mode in eclipse and in the middle of it, I want to change the size of a List,say from 9 to 6, by deleting 3 elements.
But I'm not seeing any option to do that, in fact what I'm seeing is the option to change the values present in the elements.
So how can I delete the elements itself from the List ?
Make sure you have the "Variables" tabs on eclipse "Debug" view focused on your current evaluated code.
In "Debug" view, right-click on "Value" cell inside the "Variables" table, and select "Change value".
You will have an option to write a Java expression so you can add something like:
yourList.add("newItem"); or: yourList.remove(0);
Make sure to reload the variable ("F5") once you are done and you will see the updated state.
Note that not every List implementation supports add() or remove() methods.
See this for more details if you encounter an exception.
See also:
Eclipse docs - Variables view
Eclipse docs - Change variable value
I was looking to do something similar. I had an ArrayList containing 1 element, and I wanted to remove it.
I tried #Leet-Falcon's answer, e.g. yourList.remove(0), and Eclipse replied "Unsupported operation exception".
What ended up working was : return new java.util.ArrayList<>();
Looks like the "Debug Shell" view allows this, or additionally if it's a simple enough list, the following simple Change Variable can also work:
new ArrayList<String>(java.util.Arrays.asList("1","2")) // or any other simple list
Modifying Java Collection (List) Variable in Eclipse Debugger

Surround selection with method call

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.

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.

how to better use of eclipse code templates (PHP)?

One particular problem I was having was using ${word_selection} in an Eclipse PDT template.
I was recently trying to use some code templates with Eclipse PDT 2.1 to speed up some common tasks. We use a lot of getters/setters, so I wrote the following template.
function get${word_selection}() {
return $$this->getData('${word_selection}');
}
function set${word_selection}($$${word_selection}) {
$$this->setData('${word_selection}', $$${word_selection});
}
I named the template "getset" and the only way I know to use the Code Assist is to type: "getset" then hit my code assist keys (I have it set to Esc, but I think the default was Ctrl+Space). The problem is, this doesn't actually let me select a word to be used by the ${word_selection}.
how do I type in my template name, hit the key combo, and have a word selected all at the same time?
I also want to know what kinds of templates people have set up and any other tips for using templates to speed of programming.
Look at this link : http://2tbsp.com/node/104
It describes two things : pdt code templates and code snippets.
how do I type in my template name, hit the key combo, and have a word selected all at the same time?
I think this cannot be achieved with code templates, but with code snippets. Personnally I do not use them at all, but I might start :-)

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.