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.
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 ?.
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:
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.
© 2022 - 2024 — McMap. All rights reserved.
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