Find constructors with more than 3 parameters with Resharpers pattern catalogue
Asked Answered
K

2

8

Is it possible to create a search pattern in the pattern catalog of Resharper to find all constructors with more than 3 parameters?
If so, how?
My problem is that I don't know how to tell Resharper that only constructor definitions should match.

Kristykristyn answered 27/7, 2011 at 16:53 Comment(4)
Haven't tested it, but since all constructors have a specified pattern you could try something like public $id$() where id is a placeholder of type identifier which matches the regex of word followed by paranthesis followed by a regex for four or more comma separated set of characters. (.*?,.*?\,.*?,.*?\). I'm assuming you can't create an identifier with the whole regex, so you might have to break it into smaller pieces of identifiers + arguments.Fanlight
Thanks for the proposal. However, I couldn't get it to work. See this comment and the answer from Jura - it looks like it is not possible to use that feature to find patterns on class level.Kristykristyn
If/when this turns out to not be possible, a post-compile approach would be possible with reflection, but this of course would not be as nice.Dayfly
@AakashM: Thanks for the idea, but as you said: It wouldn't be so nice...Kristykristyn
A
4

As far as I know the patterns in Resharper can only be matched within a method. So you couldn't match the constructor declaration.

I just tried the following pattern though:

new $type$($args$)

Where type is a placeholder for a type (who would have guessed?) and args for at least 3 arguments. This indeed finds all uses of at least 3 arguments constructors, but it wouldn't find constructors that are not used, and most importantly, it would find this:

public class MyClass : MyAbstractClass
{
    public MyClass(int foo1, int foo2) : base(foo1, foo2, 0)
    {
        // ...
    }
}

So maybe if you think you're going to have these cases, instead of using Resharper patterns you should try to use regex Find. It can be hard because come to think of it C# syntax is quite complex, but you could get to something...

Edit: I adapted a visual studio regex search for a constructor declaration, recognizes new lines and at least arguments (which can have optional values):

^(:b|\n)*((public|internal|private|protected|static|sealed)(:b|\n)+)+:i(:b|\n)*\((:b|\n)*:i(:b|\n)+:i(:b|\n)*(|\=(:b|\n)*:a*(:b|\n)*)(,(:b|\n)*:i(:b|\n)+:i(:b|\n)*(|\=(:b|\n)*:a*(:b|\n)*))^2(,(:b|\n)*:i(:b|\n)+:i(:b|\n)*(|\=(:b|\n)*:a*(:b|\n)*))*\)

it's ugly mainly because the VS custom regex doesn't have any translation for the standard \w, {2,} and ?.

Absorbed answered 17/8, 2011 at 12:22 Comment(1)
+1 for the effort, but Regex isn't what I want. I want Resharper to highlight this as code smell, so it is immediatelly visible.Kristykristyn
F
3

I know this question is specifically targetted at R#6.0, but I wanted to provide an answer for R# 2016.3, as I had the same question recently.

Resharper 2016(.3.1)


Search with Pattern allows this type of searching. Here's what you need to do:

Select Resharper > Find > Search With Pattern

Add Three Placeholders:

  • args: Argument Placeholder - set the min/max to how many params you want to search for
  • code: Statement Placeholder - set no limit on number of statements
  • type: Identifier Placeholder - enter no regex

Ensure that you set the Search Pattern to C#

Set the pattern to the following:

public $type$($args$) 
{
$code$
}

Set Look In as you need to (e.g. Solution). And hit Find.

You should end up with a search box that looks like this:

Search With Pattern

This will find all (public) constructors which contain the number of params you are interested in. I was able to use this within several solutions successfully. As an added bonus, it seems to be quite happy to find constructors with param lists over multiple lines, not just a single line.

Fag answered 26/1, 2017 at 12:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.