What is the Eclipse equivalent of IntelliJ "Live templates"? - java

I mean stuff like typing "iter" and getting a "for" loop with a choice of what variable to iterate on , typing "soutv" to generate a "System.out.println" with the "variable=" already in ...
Thanks !

It is called Templates and it's found under,
Window → Preferences → Java → Editor → Templates
The "soutv" template does not exist ("sysout" does, and it's similar), but it's easy to add. I used this pattern:
System.out.println("variable=" + ${cursor}${});

For 'soutv' particularly, I found the following pattern worked well in Eclipse:
System.out.println("${var} = ${cursor}" + ${var});
As others have mentioned, you can add this template by navigating to Window > Preferences > Java > Editor > Templates and clicking New.
The equivalent of 'iter' seems to be 'for' in Eclipse.

Check under
Window -> Preferences -> Java -> Editor -> Templates
Reference:
Template Variables

They are called Templates.
Go to Preferences > Java > Editor > Templates to see a list of pre-defined templates.
For example, sysout is:
System.out.println(${word_selection}${});${cursor}
You can also create your own.

Related

IntelliJ: foreach Live Template code formatting

In IntelliJ idea when I insert the foreach live template it will put newline after ':' so it will look like this:
for ( :
) {
}
I want to have the for statement on one line like this:
for ( : ) {
}
I tried to change my code formatting preferences, but could not figure out what setting influences this particular case.
So my question is how to set code style options to achieve the desired behavior?
Use the iter live template rather than the foreach. foreach is under the Android block, and the default style for that is what adds the newline.
Update:
As of at least 2018.1.1 (not sure when it was added), you can now type the <name of your collection>.for then tab and it will expand out into a foreach loop.
It's also brought in the same surrounding/expansion for stuff like <array>.stream then tab and probably a few others I'm not aware of.
Go to File -> Settings -> Editor -> Code Style -> Live Template.
At the right side open Android list and stay on foreach .
In the Options area uncheck Reformat according to style.
You can see how to do it in the IntelliJ IDEA settings foreach style
You can change the template for the enhanced for loop in IntelliJ by changing the setting in Live Templates.
Go to File -> Settings -> Editor -> Live Templates. In the right side, choose iterations -> "iter (Iterate Iterable | Array in J2SDK 5.0 syntax)". At the bottom you can see the template text and you can change it by introducing the newline where you want it. Change
for ($ELEMENT_TYPE$ $VAR$ : $ITERABLE_TYPE$) {
$END$
}
to
for ($ELEMENT_TYPE$ $VAR$ :
$ITERABLE_TYPE$) {
$END$
}
and apply your changes.
In the source code editor, choose Code -> Insert Live Template... -> iter, then IntelliJ will insert the code template as you've specified, with boxes around the variable names for changing them.
for (String arg :
args)
{
}

How do I modify exception variable default name at “try-catch” template in Settings of IDEA/Android Studio?

I want to have on generating try-catch block "ex" name of exception, not "e".
IntelliJ IDEA 2019
Go to File > Settings... > Editor > File and Code Templates > Code tab > Catch Statement Declaration and replace ${EXCEPTION} by ex.
you have to modify code template, Check the intellij documentation here.

How to add extra code into main java method?

I want to add a sentence to all new classes that I create in java. By default Java entry point comes like this:
public static void main(String[] args) {
}
and I want to achive something like this:
public static void main(String[] args) {
Scanner key = new Scanner(System.in);
}
Is this possible? Thank you!
Eclipse Java EE IDE for Web Developers.
Version: Mars Release (4.5.0)
Build id: 20150621-1200
You can create/edit/ any code template you want
For Eclipse go to
Window
Preferences
Java
Editor
Templates
More here:
http://help.eclipse.org/mars/index.jsp?topic=%2Forg.eclipse.jdt.doc.user%2Freference%2Fpreferences%2Fjava%2Feditor%2Fref-preferences-templates.htm
another here :
http://help.eclipse.org/mars/index.jsp?topic=%2Forg.eclipse.jdt.doc.user%2FgettingStarted%2Fqs-EditorTemplates.htm
In Eclipse it is possible to edit built-in code templates (the one used by Eclipse e.g. for new class files) and create your own named snippets (for fast access to often-reused templates).
Code Templates
Go to Window > Preferences > Java > Code Style > Code Templates, then to Code > New Java files. Adapt this or some other template to suit your needs.
Templates
Usually it's better though to add a smaller template via Window > Preferences > Java > Editor > Templates and use it wherever you need. Smaller bricks are more reusable then hardcoded class templates. Those are applied with <type-beginning-of-template-name + ctrl + space.

Eclipse import shortcut does not work as expected [duplicate]

Is there anyway to get Eclipse to automatically look for static imports? For example, now that I've finally upgraded to Junit 4, I'd like to be able to write:
assertEquals(expectedValue, actualValue);
hit Ctrl + Shift + O and have Eclipse add:
import static org.junit.Assert.assertEquals;
Maybe I'm asking too much.
I'm using Eclipse Europa, which also has the Favorite preference section:
Window > Preferences > Java > Editor > Content Assist > Favorites
In mine, I have the following entries (when adding, use "New Type" and omit the .*):
org.hamcrest.Matchers.*
org.hamcrest.CoreMatchers.*
org.junit.*
org.junit.Assert.*
org.junit.Assume.*
org.junit.matchers.JUnitMatchers.*
All but the third of those are static imports. By having those as favorites, if I type "assertT" and hit Ctrl+Space, Eclipse offers up assertThat as a suggestion, and if I pick it, it will add the proper static import to the file.
If you highlight the method Assert.assertEquals(val1, val2) and hit Ctrl + Shift + M (Add Import), it will add it as a static import, at least in Eclipse 3.4.
Eclipse 3.4 has a Favourites section under Window->Preferences->Java->Editor->Content Assist
If you use org.junit.Assert a lot, you might find some value to adding it there.
Not exactly what I wanted, but I found a workaround. In Eclipse 3.4 (Ganymede), go to
Window->Preferences->Java->Editor->Content Assist
and check the checkbox for Use static imports (only 1.5 or higher).
This will not bring in the import on an Optimize Imports, but if you do a Quick Fix (CTRL + 1) on the line it will give you the option to add the static import which is good enough.
From Content assist for static imports
To get content assist proposals for static members configure your list of favorite static members on the Opens the Favorites preference page Java > Editor > Content Assist > Favorites preference page.
For example, if you have added java.util.Arrays.* or org.junit.Assert.* to this list, then all static methods of this type matching the completion prefix will be added to the proposals list.
Open Window » Preferences » Java » Editor » Content Assist » Favorites
For SpringFramework Tests, I would recommend to add the below as well
org.springframework.test.web.servlet.request.MockMvcRequestBuilders
org.springframework.test.web.servlet.request.MockMvcResponseBuilders
org.springframework.test.web.servlet.result.MockMvcResultHandlers
org.springframework.test.web.servlet.result.MockMvcResultMatchers
org.springframework.test.web.servlet.setup.MockMvcBuilders
org.mockito.Mockito
When you add above as new Type it automatically add .* to the package.
Shortcut for static import:
CTRL + SHIFT + M
Select the constant, type
Ctrl + 1 (quick fix)
Select "Convert to static import." from the drop down.
"Quick fix" has options even though it is not an error.
In Eclipse 4.9, you can static import existing invocations using a quick fix.
A new quick fix has been implemented that allows the user to convert static field accesses and static methods to use a static import. It's also possible to replace all occurrences at the same time.
More details here

Eclipse plugin development: Disabling Java Structure Compare when comparing files?

I used the following code to compare two files:
CompareConfiguration config = new CompareConfiguration();
config.setRightEditable(false);
config.setLeftEditable(false);
config.setLeftLabel("Klasse 1");
config.setRightLabel("Klasse 2");
config.setProperty(CompareConfiguration.USE_OUTLINE_VIEW, Boolean.FALSE);
CompareUI.openCompareEditorOnPage(new EditorInput(config, file1, file2), page);
This works and a compare editor is opened. But unfortunately, the Java Source Compare and the Java Structure Compare is openend. I want to permanently disable Java Structure Compare. How can I do that?
Windows -> Preferences -> General -> Compare/Patch
un-check 'Open structure compare automatically'.
Apply and Close.
Try using a CompareEditorInput (instead of EditorInput) and then setting the Configuration property of that to a CompareConfiguration which you can set the set a preference store using one of it's constructors. The ComparePreferences has a preference of doing the structure compare (or not presumably).

Categories