On Thu, Dec 8, 2011 at 6:17 AM, Bartosz <gan...@poczta.onet.pl> wrote:
> Hi.
>
> I would like to implement the Autoremove of the unused imports feature.
> I found that the autoremove of unused imports was already implemented, but it 
> is not enabled by default.
> It seems that there some problems with it.
> The source code is located at:
>   
> aptana-Pydev/plugins/com.python.pydev.analysis/src/com/python/pydev/analysis/OrganizeImportsFixesUnused.java
>
> What was wrong with that feature?
> How can I enable it (I would like to test it, and maybe fix the problems with 
> it)?

That class is incomplete.

It's a very rough implementation that just iterates over the
annotations that say that some import is unused, but it's missing on
how to map that back to the code. Also, it tries to handle something
in wild imports, but that's also not well done (one would have to
gather all the tokens from the wild import and choose just the ones
that are actually used).

I think the best approach would be doing it as a part of
com.python.pydev.analysis.organizeimports.OrganizeImports (which
already gathers all the imports) and just see if a given import is
unused (through an annotation) and if it is, remove it (use the
OrganizeImportsFixesUnused just as a reference on how to gather the
annotations) -- and just leave wild imports as is (without changing
it).

Note that in this case, the OrganizeImports will depend on having
annotations up to date (as it'll rely on having unused imports
annotations), so before starting to remove the imports, one has to do
something as:

PyParser parser = edit.getParser();
parser.addParseListener(listener);
parser.forceReparse(new Tuple<String,
Boolean>(AnalysisParserObserver.ANALYSIS_PARSER_OBSERVER_FORCE,
true));

And the listener will later remove itself from hearing the other
parses and will actually do the job of organizing the imports (and
removing unused ones). This is needed to be sure that annotations are
up to date (but shouldn't be needed in case the user just does an
organize imports without removing unneeded imports -- an option should
be added to org.python.pydev.ui.importsconf.ImportsPreferencesPage to
choose whether to use that feature or not).

I haven't done it because in Python, it may be easy to break things
removing unused imports (i.e.: as you may have a module that imports
something just to leave that import in the public interface of that
module without actually using it), but I don't think it's a bad thing
if it's optional (so long as people know what they're using). Also,
right now if used properly (i.e.: unused imports are properly marked
with a comment saying it's meant to remain unused -- which the code
analyzer should do properly), it may be a nice feature to have.

Please let me know if you need more information to make it work. I
assume you already got the basic code setup following instructions
from: http://pydev.org/developers.html -- if something is missing,
please let me know so that I can update that page -- note that you can
just send a pull request in github -- that page still asks for a patch
:).

Cheers,

Fabio

------------------------------------------------------------------------------
Cloud Services Checklist: Pricing and Packaging Optimization
This white paper is intended to serve as a reference, checklist and point of 
discussion for anyone considering optimizing the pricing and packaging model 
of a cloud services business. Read Now!
http://www.accelacomm.com/jaw/sfnl/114/51491232/
_______________________________________________
pydev-code mailing list
pydev-code@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/pydev-code

Reply via email to