Resharper find pattern and replace - how to insert strings
Asked Answered
K

3

10

We have a sanity check method

void IsNotNull<T>(T obj){...}

invocation

IsNotNull(obj); 

I want to replace this to invoke the other overload that takes a second param of type string (message)

void IsNotNull<T>(T obj, string message){...}

So I want to change the invocation as

IsNotNull(obj, "obj is null");

I'm trying to achieve this using resharper's find pattern and replace.

So my find pattern is : IsNotNull($args$) - This works fine and it finds the method calls

Replace pattern: IsNotNull($args$, "$args$ is null") - This doesn't do anything

I also tried this IsNotNull($args$, """" + $args$ + """")

--Edited-- The suggestion box showing the correct wording(for both argument and identifier), but once applied it's different. I'm using Resharper 6

enter image description here

After applying the suggestion I get this enter image description here

When I click Edit Pattern enter image description here

Kresic answered 31/12, 2012 at 13:39 Comment(1)
What R# are you using? I have tried version proposed by @hmemcpy and it all worked fine. R# 7.1Unreasoning
S
7

What is your $args$ parameter defined as in the Search and Replace? If you make it to be Identifier, then you replace should work:

Find: IsNotNull($args$) - where $args$ is an Identifier
Replace: IsNotNull($args$, "$args$ is null")

You should have the result you want, i.e. IsNotNull(obj, "obj is null").

Shandashandee answered 31/12, 2012 at 14:55 Comment(2)
thanks, unfortunately it didn't solve the problem it still prints IsNotNull(obj, obj). I want my second param enclosed within double quotesKresic
"where $args$ is an Identifier" is the important part.Neoplasticism
U
1

You can consider trying to use this pattern:

IsNotNull($args$, string.Format("{0} is null", $args$))

It works perfectly for me with ReSharper 7.1.

It seems, that R# doesn't want to evaluate argument expression inside of string literals normally. Given your pattern

IsNotNull($args$, "$args$ is null")

It replaced IsNotNull(5); by IsNotNull(5, 5); which is odd

Unreasoning answered 31/12, 2012 at 14:10 Comment(2)
In this instance I need the name of the args, not the value of args as args.ToString(). I basically need to enclose my args with double quotesKresic
I see, sorry for misunderstanding, because I've used to trace parameter values, not the name of the arguments. I will try to see if I can modify a solution to fit your needUnreasoning
N
1

The easiest method would be to rewrite the original method like so:

void IsNotNull<T>(T obj){
    IsNotNull(obj, "obj is null");
}

Then click on the method signature, and choose Refactor->Inline Method (Ctrl+R, Ctrl+I). If you need to keep the original method signature around, you can, or you can check the box for "Remove inlined method declaration."

EDIT: Looks like hmemcpy's solution works in 7.1, so I'd suggest an upgrade. If that's not possible, however, try the following regular expression find-and-replace in Visual Studio.

Find: IsNotNull\(([^\),]+)\);
Replace: IsNotNull($1, "$1 is null");

Make sure "Use Regular Expressions" is checked, and "Look in:" should be "Entire Solution".

Neoplasticism answered 2/1, 2013 at 16:40 Comment(8)
Thanks for the reply, but your suggestion not solving my problem in the context of Resharper-Replacement-Pattern or in general. I want specific message to be diplayed. Not just 'obj is null', in my question it was just an exampleKresic
@NasmiSabeer: No problem. Actually, just figured it out, and am updating my solution.Neoplasticism
@NasmiSabeer: Actually, it looks like hmemcpy's solution is correct. The key is that $args$ has to be defined as an Identifier in the Search With Pattern dialog, and not an Argument. I'm not sure why they have the quote limitation for Argument placeholders, but Identifier placeholders appear to work just fine.Neoplasticism
Can you give an example that it didn't work on, and what version of Resharper you're using? It appears to work correctly on my system.Neoplasticism
Thanks. Can you show what comes up if you click the "Edit pattern" link?Neoplasticism
Please my image attachment in the questionKresic
Odd. I've used the exact same settings, and it works perfectly using Identifier. The only difference I can see is that you're using ReSharper 6, and I'm on 7.1.1. Maybe they revamped something in the interim. Can you try the 7.1 30-day demo and see if it works with that?Neoplasticism
Sorry for the delay Chris, it took me a while to find a spare PC with Resharper 7. Thanks it worked indeed.Kresic

© 2022 - 2024 — McMap. All rights reserved.