Reputation: 1906
I am writing some Resarper Custom Patterns to warn us about some code constructs that need attention. One of these is replacing OnpropertyChanged("String") with a lambda variant OnPropertyChanged(() => propertyname)
The search Pattern I defined is:
public $type$ $property$
{
get { return $backingfield$; }
set
{
if($backingfield$ != value) {
$backingfield$ = value;
OnPropertyChanged($String$);
}
}
}
This pattern is being replaced with:
public $type$ $property$
{
get { return $backingfield$; }
set
{
if($backingfield$ != value) {
$backingfield$ = value;
OnPropertyChanged(() => $property$);
}
}
}
Problem: When applying this, Resharper throws away the attributes defined on the property. This snippet:
[MyAttribute]
public int Test
{
get { return _Test; }
set
{
if (_Test != value)
{
_Test = value;
OnPropertyChanged("Test");
}
}
}
gets replaced with
public int Test
{
get { return _Test; }
set
{
if (_Test != value)
{
_Test = value;
OnPropertyChanged(() => Test);
}
}
}
How can I preserve the attributes??
UPDATE: Adding a type placeholder derived from System.Attribute to both search and replace pattern fixes it partially.
[$Attributes$]
...
Remaining problem is that the Attribute placeholder only matches one attribute, it fails on multiple attributes.
Upvotes: 5
Views: 1561
Reputation: 4371
If you cannot get another solution there is a workaround.
You use your Search pattern (without using replace pattern) to show the warnings. I think that works already.
Then you create a Surround Template that replaces a string to ()=>PropName. See the picture for an example:
Then you have the warnings by Search pattern and the replacing by a Surround Template.
The usage is: If you see the warning select the string, press Ctrl+E, Ctrl+U and select template String to func returning property.
Of course the string selection is bothering. But that is the best that I have found out up to now.
Upvotes: 2
Reputation: 8462
For such tasks I've used regular expressions. VS supports replacing by regular expressions, but sometimes they hang or just working very slow. However in most cases they work.
Will that help you?
Upd. You don't need to have all property in the replace group, as brgerner suggests, you just need to have only one string converted. For example the search regular expression will be OnPropertyChanged\("{:w*}"\);
, and the replace string will be OnPropertyChanged(() => \1);
Not sure whether you can do that in Resharper replace pattern though.
Upvotes: 0