Support the remove unused references refactoring #3

citizenmatt opened this Issue Oct 19, 2012 · 6 comments

6 participants

JetBrains member

Is this possble?

  • Don't remove references that are dependencies of other packages
  • When removing a package, using nuget to remove the reference
@citizenmatt citizenmatt was assigned Nov 7, 2012

What's the first place to look for this?

JetBrains member

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 😄


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.


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 👍 from me!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment