Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add interface to all classes with a certain naming pattern

I am looking to refactor a large number of classes.

All the classes follow the naming convention

class SomethingModel

I want to find them all and replace them with

class SomethingModel : IModel

But I also want to exclude some template derives I have that look like

class SomethingController : GenController<SomethingModel>

I am trying to do this in Visual Studio.

The obvious choice is regex Find and Replace but I also have Resharper and Ideally I would do it using the pattern search replace

Through some searching on regex questions I have got the first part working

(?<=class)(.*Model)

but I cant seem to exclude the template. I am starting to play with

(class .*Model)(?!"(?<="Generic"))

I very rarely use regex and if there is a none-regex solution I would much prefer it. In particular I would quite like to use Resharper Search with Pattern

Which I tried the following with: enter image description here Which worked really well....apart from all my classes became empty... the statements tag does not seem to work (set to inifinite)

UPDATE

Positive cases:

  • public class DoorModel
  • public class HandleModel
  • public class BellModel

Negative cases

  • public class DoorController : GenericController
  • public class WindowModel : IModel
like image 865
chrispepper1989 Avatar asked Aug 03 '26 13:08

chrispepper1989


1 Answers

There is a hacky solution if you do wish to leverage Resharper. But its a hack.

According to Jetbrains the pattern search and replace is designed for functions, which is why it doesn't handle my class example above very well (see my conversation here )

What I did was open all the files I wanted to edit and did a find replace to replace to get resharper to ignore my class contents.

I wanted my resharper pattern replace so that I could replace this pattern:

class $ClassName$ : IModel
{

}

with this one

class $ClassName$ : AbstractModel<$ClassName$s>{}

So I simply did a find replace that replaced "IModel" with "IModel{}//$$"

Then ran the resharper pattern replace, which then worked because it obviously didnt "see" the contents of the class anymore.

Then I just replaced ">{}//$$" with ">" (hence the extra crazy symbols).

So yer its very hacky, but it does work. Thankfully its very rare you want to do crazy stuff like this (and should generally avoid it all costs). I actually discovered a few other "Resharper hacks" while messing around with this code which I might have to post about later.

like image 127
chrispepper1989 Avatar answered Aug 05 '26 11:08

chrispepper1989



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!