Is it possible autocomplete in Eclipse Java project. Something like I type:
public static void clr()
then press something and get:
public static void clr()
{
}
Alt+/ not working in this case.
In your situation, I don't think you will get any shorter than just typing { and Enter.
Alternatively, you could use the template for public_static_method, i.e. start typing pub..., hit Ctrl+Space, select the template (just press down once and then Enter), type in the method's return type and name and then Enter again to get straight to the body.
You can also go to Preferences -> Java -> Editor -> Templates to adapt that template to your own needs (give it a shorter name, or move the { to a separate line) or define your own ones.
Related
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.
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.
For example, if I type sysout and then ctrl + space, it'll automaticlly fill System.out.println();.
How can I add a custom shortcut? Thanks in advance!
access the menu, window>preference, type the name of your language, at threeview select editor>template click at new put your template and OK
Window -> Preferences -> New -> Java -> Editor -> Templates
Name: MainProgram (which is like sysout)
Pattern:
public static void main(String args[]){
}
And then ok.
Now If you type MainProgram and Ctrl+space which gives you the full program..
window-preferences-General-key
then set you custom key on "content assistant" item
I like to use auto-complete in Eclipse and it works fine most of the time.
However, in template functions:
template <class T> int existeEmPilha (const stack<**T**> &stack1, const T &v1){
stack1.(...) //it does not complete
}
But it works fine in the case:
template <class T> int existeEmPilha (const stack<**int**> &stack1, const T &v1){
stack1.(...) //auto-complete appears with functions like size(), pop(), etc...
}
I need to do template functions and I would like to use auto-complete when doing so. Is it possible?
I'm using Eclipse Juno SR1.
In order to make a new function template go on
Eclipse > Preferences... > Java > Editor > Templates
You will see there the set of templates that has been already defined. You can add a new template by pressing the New... button.
In order to write the pattern of your method check how existing method patterns are described. For example your method might be described as :
public void stack1() {
${cursor}
}
IntelliJ has some such facilities and options. For example, I want it to format like:
#Override public
void foo()
{ ... }
Better yet would be the ability to indent the annotations and visibility like so
#Override public
void foo()
{ ... }
but I don't want to be greedy.
Do such formatting options exist in Eclipse?
Java -> Code Style -> Formatter -> Edit --> New Lines --> Annotation:
Unclick Insert new line after annotations on methods
Don't think you can do your second option.
In case you still have Annotation formatting problems in Eclipse 2018 (Oxygen.3a, 4.7.3a) try this:
In Preferences go to: Java -> Code Style -> Formatter -> Edit --> New Lines --> Annotations:
You have to select Annotations and then set "line wrapping policy" to a "Wrap ..." setting. Without this setting any complex set of Annotations was formatted to very long lines in Eclipse
Go to settings-> Editor-> Code Style -> Java
Choose your code formatter and choose tab Wrapping and Braces (Try to find tab Wrapping if using different Intellij Version).
Look for option Class Annotation and Method Annotation and select option "Wrap Always"
Open the preferences dialog. Go to "Java" -> "Code Style" -> "Formatter".
Check the many options :-)