This Bugzilla instance is a read-only archive of historic NetBeans bug reports. To report a bug in NetBeans please follow the project's instructions for reporting issues.

Bug 47399 - Automatically add import statement when selecting a class via code completion
Summary: Automatically add import statement when selecting a class via code completion
Status: RESOLVED FIXED
Alias: None
Product: editor
Classification: Unclassified
Component: Completion & Templates (show other bugs)
Version: 4.x
Hardware: All All
: P3 blocker with 5 votes (vote)
Assignee: Martin Roskanin
URL:
Keywords:
: 2196 30286 (view as bug list)
Depends on:
Blocks:
 
Reported: 2004-08-18 15:47 UTC by swpalmer
Modified: 2007-11-05 13:38 UTC (History)
2 users (show)

See Also:
Issue Type: ENHANCEMENT
Exception Reporter:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description swpalmer 2004-08-18 15:47:44 UTC
Rather than leaving the fully qualified class name in place there 
should be an option to automatically add the relevant import 
statement so long as a class with the same name is not already 
in scope or imported.  Then the code completion can use the 
shorter name.

Also, if the class is already imported it should simply use the 
shorter name.
Comment 1 _ gtzabari 2004-09-10 18:36:45 UTC
*** Issue 30286 has been marked as a duplicate of this issue. ***
Comment 2 _ gtzabari 2004-09-10 18:38:25 UTC
*** Issue 2196 has been marked as a duplicate of this issue. ***
Comment 3 rsk 2004-09-15 19:26:39 UTC
I swear this was filed or something exactly like it was filed since
2002.. anyway, +1 for me.
Comment 4 _ potingwu 2005-03-14 18:36:25 UTC
More comments and suggestions:

    *   Quite a few customers mentioned that they want us to do more to help
with imports - currently if they use a class that needs an import statement, we
give them a compile error and they have to use hotkeys to trigger fast import.
Instead, they all cited Eclipse as have a far superior user model where it
automatically handles the imports for them. We should study how they do it, and
how they handle it if there are more than one possible choice to import from
(for when there are 2 classes of the same name "Foo" but are in different
packages... which one do you mean?).

    * So we took a look at Eclipse - here are some things to note - Eclipse
handles import as option with code completion - as you type class name, code
completion pops up and let's you choose which class from all possible classes in
the classpath, not just those visible to that class... not just what's “visible”
but also what's “accessible”... everything available in the class path. It would
be really great if our code completion could "extend" it's view of the world to
the classpath with some import management as well.

    * Some use cases we looked at:
         1. Typing "Fi" should show all the Fi* classnames found in the class
path – and if you choose a class that is not in the import list already, it will
add it in.
         2. Typing "Foo", if there is only one class found in the class path
named "Foo", if you have chosed auto-import it will automatically insert the
appropriate import stmt (without prompting you). If you have not chosen
autoimport, code completion will prompt you with the one class/package which
when you choose it will get imported (this is preferred because it mitigates
typos triggering incorrect import guesses)
         3. Note that Eclipse doesn't remove the import if the class reference
is later removed... this is ok due to there's a compiler setting that controls
warning if imports are included but not used so can clean that up later (this
would also be nice to have, if we don't have it already?)
         4. One other use case is what to do when pasting large group of code
from Eclipse to Eclipse - there's an option for Eclipse to remember the imports
from the copied source and bring them in as well. If Eclipse can't find original
import (say it came from emacs or something outside the tool) – then Eclipse
shows compile error (red underline similar to our squiggly line) and user can
use code completion to add the imports that way

    * Benefits:
          o User doesn't need to remember entire classname nor package name -
since code completion is triggered off the classpath, it can help the user try
to figure out what it was called and where it lives
          o User doesn't have to know about separate import capability
          o Nice that the user doesn't need separate key to do import, if they
just remember the code completion hot key that give them everything they need

    * Eclipse import settings (equivalent to our "options")
         1. if only one import option, should I insert automatically or present
in popup (folks seem to like prompt option in case of typos)
         2. number of imports needed before using a import * (defaults to 99)
         3. manage imports – what groupings and order you should put them into
your source
         4. pasting code also has setting which will allow to automatically
include the imports needed “update imports on paste” - extra fancy, upon paste
action can remember import that was referenced in original source class
         5. there is some compiler setting which allows them to flag unnecessary
imports so they can get cleaned out

    * Also thought this looked interesting: See "Import Assistant" on the
following page:
          o http://www.jetbrains.com/idea/features/code_analysis.html

Comment 5 Roman Strobl 2005-04-20 16:26:33 UTC
This feature was implemented during NetBeans 4.1 development cycle. If you
choose a class by code completion, it add missing import automatically. It can
also handle colisions (e.g. for java.util.List vs java.awt.List). Please try the
functionality in latest daily builds. If you will miss any functionality feel
free to reopen but please specify what you miss.
Comment 6 Martin Matula 2005-05-12 13:27:40 UTC
Target milestone->4.1