resharper-nuget icon indicating copy to clipboard operation
resharper-nuget copied to clipboard

Support the remove unused references refactoring

Open citizenmatt opened this issue 13 years ago • 6 comments

Is this possble?

  • Don't remove references that are dependencies of other packages
  • When removing a package, using nuget to remove the reference

citizenmatt avatar Oct 19 '12 10:10 citizenmatt

What's the first place to look for this?

mwjackson avatar Feb 12 '13 13:02 mwjackson

There are several entry points. Fire up dotPeek, and look for:

  • AnalyzeReferencesAction (this is called when you right click the References folder in solution explorer and select "Optimize References"
  • AnalyzeReferencesRemoveSelectedAction is what happens when you right click an unused references in the window displayed after selecting "Optimize References"
  • RemoveUnusedReferencesAction is right click References -> "Remove Unused References", and starts the refactoring workflow, with dialog boxes and things

Then it will be a matter of legwork - follow the code to see where it goes, and what it's looking for, and how we can hook into it. If I remember rightly, ReSharper is trying to get all references to types that exist in the known references. If it finds them, they're in use, and can be removed. If it doesn't, then it flags the reference as something that can be removed.

I haven't really looked into how we can get this working yet, but there are lots of things we can do - if ReSharper is looking for references, can we add a reference to nuget assemblies? What would that give us? Or can we hook assembly removal, so at the least, can we call package uninstall when an assembly is removed? Or perhaps we can add a group into the analyse references window that shows what assemblies are nuget packages, so even if ReSharper can't tell us if a package should be removed or not, at least we're showing the user that these are packages. Or something else - ReSharper has LOTS of extensibility points.

Basically, I'm saying I don't know how to do this, but there's always options :smile:

citizenmatt avatar Feb 13 '13 10:02 citizenmatt

+1

jrnail23 avatar Aug 30 '13 20:08 jrnail23

You can use ReferencedCodeSearcher solution component. This is the core engine for 'optimize references', 'find dependent code' and some other features. In a couple of words, this engine asynchronously, in multiple threads, searches for references inside the given scope to the given targets.

kskrygan avatar Nov 27 '13 15:11 kskrygan

+1

mitchell-etter avatar Feb 09 '15 14:02 mitchell-etter

Not having this makes the "Remove Unused References" pretty useless in any project these days, as almost all projects have at least one NuGet reference and most have a ton. So a big :+1: from me!

asbjornu avatar Feb 11 '15 07:02 asbjornu