Eclipse/FDT closes imports when typing in a new import - eclipse

For example I type the following:
import shared.
And on the dot, it closes the imports forcing me to reopen the imports and continue typing. Is there a way to disable this? What is the point of this?

This cannot be disabled without completely disabling autocomplete.
With autocomplete, when a selection is made, FDT will use that and then return you to the editor so you can enter another keyword.
You shouldn't ever need to type an import with FDT. You can:
1) Use autocomplete on a class / type and have FDT auto import the class
2) Use Organize Imports
3) Use a quick fix to import the class.
See more here:
http://fdt.powerflasher.com/docs/Main_Page

Related

Dart library import shortcuts

In cases where I have to import the main file from a library in dart I have to do:
import 'package:<package_name>/<package_name>.dart';
Is there a shorthand for doing this in Dart?
Something like
import 'package:<package_name>';
Cause mentioning the package name twice seems redundant.
Seems redundant but there is no way around.
The new "add import" quick fix makes this mostly a non-issue IMHO.
DartEditor ctrl+1
WebStorm/IntelliJ ctrl+Enter
You have to wait until analyzing is done and a hint/warning (wiggled underline) is shown.

Auto Import With Eclipse Snippets

I started using snippets recently. The most common one I use inserts this at the cursor:
private Logger log = LoggerFactory.getLogger(getClass());
However, I still have to do a CTRL-SHIFT-O to import Logger and LoggerFactory.
Out of pure laziness I ask this: Is there a way to do an automatic import when I insert this snippet?
I don't think a Snippet can automatically insert an import statement (or do anything other than insert text at the current cursor location). But you can set up an automatic Save Action to do the organize imports whenever you save. Open the project properties and navigate to Java Editor > Save Actions.
I usually set up a few different things in the Additional Actions including Organize Imports, removed trailing whitespace, insert missing annotations, etc.

Eclipse optimize imports to include static members and methods

Long user of eclipse and Java. One issue that i've found with Eclipse, is it seems that there is no easy way to import static members and methods.
Namely, the jUnit fail() method from org.junit.Assert
I create several classes a day, and manually add
import static org.junit.Assert.fail;
to the import statements. This is quite annoying. I absolute LOVE using Ctrl+Shift+O to organize my imports, but it still doesn't find static members and methods.
Also, the import does not show up in eclipse.
Funny thing is, is that i've seen it work previously, but i cant recall the variables.
So to my question:
Does anybody know what I need to do to ensure that this static import is always recognized and can be found using Ctrl+Shift+O?
Thanks #qqilihq.
Note:
The answer that was accepted does not work with the Organize Imports keyboard shortcut that I preferred in eclipse, but does work for the "hover over" suggestion.
You can use Ctrl + Shift + M, for example you want to import verify method from Mockito class then
Mockito.verify() // select verify and press Ctrl + Shift + M
This will import verify static method from Mockito class.
Did you have a look at Preferences > Java > Editor > Content Assist > Favorites? You can define candidates for static imports there. Result:
For less used classes you can lower the value of Preferences > Java > Code Style > Organize Imports > Number of static imports needed for .* but beware that you may get .* for classes that contain generically named methods such as getInstance. This in turn may lead to confusion and/or naming conflicts.
You can add the classes that you statically import from Preferences > Java > Editor > Content Assist > Favorites page in Eclipse. Then, Ctrl+Space shortcut lists all the static members of your favourite classes in the content assist menu.

Eclipse wraps NOPMD Tag after imports

I have a little problem with Eclipse. When i set a //NOPMD tag behind an import it wraps the tag to the end of the imports. How can I avoid that eclipse wraps any //NOPMD tags during beautifying the java program?
Example before beautifying:
import java.io.IOException;//NOPMD
Example after beautifying:
import java.io.IOException;
//NOPMD
How about using #SupressWarnings("PMD.NameOfRule"). This will suppress all instances of that rule in the class which may or may not be what you want. But it is immune to code reformatting and organize imports.
As a reference, here are the options for suppressing.
Found a better answer once I stopped looking at this as a PMD problem! The accepted answer to this question shows how to turn off formatting on sections of the code by writing:
// #formatter:off
...
// #formatter:on
You'd need to do this on each class around your imports. Or each class that has //NOPMD that is. And you'd need to turn on the setting in your workspace. And have your teammates to this same if you are on a team.

Is there a way to tell Eclipse how to resolve ambiguous java names?

My eclipse is configured so that when I save the java source file, it automatically inserts the missing import declarations. Except when the reference is ambiguous. Fpr instance, the most annoying ambiguity is with List<T> - both java.util and java.awt declare it. Here eclipse demands manual resolution.
I was wondering if it was possible to configure somewhere that whenever List<T> is used then java.util should be imported. Or alternatively, since I am not using java.awt, I could just remove it from the list of possible suggestions.
Any ideas?
Thanks.
This sounds like a possible duplicate of Exclude packages from Eclipse's organize imports.
Basically, you want to change your Type Filters preference to exclude java.awt.* packages. Keep in mind that doing so will make things harder/confusing if you ever try to write AWT/Swing code.
One thing that pops into my mind is altering the class template in the preferences to include the line:
import java.util.List
Unless you are going to use the AWT version of a List, or care about unused import statements, that should solve this annoying issue ..
Manually invoke Organize Imports and it will ask you when an ambiguity is found.