Kornél Regius
Kornél Regius

Reputation: 3049

Resharper - how to import all missing namespaces?

I replaced the usage of an extension method with Find-Replace, because Resharper unfortunately could't replace it properly. Now, I have 4000 errors in the solution, and all of them are because the namespace for the new method is missing, so I'm in a desperate need of a "fix/import all missing namespaces in the solution" functionality.

Does Resharper or some other tool have such a thing or I'm going to spend the next hours going through the files, pressing ALT+ENTER?

Upvotes: 7

Views: 5579

Answers (2)

Joel Wiklund
Joel Wiklund

Reputation: 1875

To build on trailmax answer:

Add line of text to beginning of multiple files and cleanup namespaces

  1. Open up Notepad++.
  2. Press Ctrl+f and choose Find in Files.
  3. Tick "Regular expression" AND ". matches newline"
  4. In "Find what", write: (.*)
  5. In "Replace with", write: YOURTEXT\r\n\1
  6. In "Filters", write: *.cs (or your file ending of choosing)
  7. Choose a directory (I removed obj and bin folders before this).
  8. Click on "Replace in Files" (Use Sourcetree to see that everything looks fine)

(.*) find everything (if the dot matches newline has been ticked)

\r\n is the normal eol for windows

\1 represents what has been matched by (.*)

  1. Open up Visual studio.
  2. Rebuild
  3. Fix any errors
  4. Then use Resharper on all the files to cleanup namespaces: select project in Solution Explorer, hit Ctrl+Shift+R and select "Adjust namespaces".

Upvotes: 0

trailmax
trailmax

Reputation: 35106

Been there, done that.

This might be not the most elegant way, but it worked for me and took like 10 minutes on a massive project. I've used global replace tool and added using MyApp.Domain.Required.Namespace to the top of all files. Then used R# on all the files to cleanup namespaces: select project in Solution Explorer, hit Ctrl+Shift+R and select "Adjust namespaces".

But do a checkin/shelf/stash whatever with your version control so you can roll back. It took me a few attempts before I got R# to cleanup namespaces correctly.

Upvotes: 10

Related Questions