5

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.

Kit
  • 20,354
  • 4
  • 60
  • 103
Yoeri
  • 1,876
  • 15
  • 24
  • Do you really need to search for the whole property? Maybe you can just search for `OnPropertyChanged($String$);` and replace it by `OnPropertyChanged(() => $String$);`. – brgerner Feb 23 '12 at 09:16
  • 1
    When doing this, the OnpropertyChanges("test") gets replaced with OnPropertyChanged(() => "test"). The test placeholder is defined as an expression of type string. – Yoeri Feb 23 '12 at 10:11
  • You are right. It was my mistake. – brgerner Feb 23 '12 at 11:20

2 Answers2

2

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:

enter image description here

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.

brgerner
  • 4,287
  • 4
  • 23
  • 38
  • +1 Thanks, i'll accept it when the results from the jetbrains community forums is negative :-) – Yoeri Feb 23 '12 at 13:50
  • I filed an issue after receiving the answer @ the jetbrains community forums: Hello Yoeri I'm afraid attributes cannot be preserved in this case. You're welcome to put a request through our bug tracker: http://youtrack.jetbrains.net/issues/RSRP. Thank you! Andrey Serebryansky Senior Support Engineer JetBrains, Inc – Yoeri Feb 24 '12 at 14:29
0

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.

Archeg
  • 8,364
  • 7
  • 43
  • 90
  • 1
    We don't intend to do a replace all. We are using shared Resharper settings and request from the developers to fix warnings anbd errors before checking in. We gradually add new items to Resharper to improve consistency in code. Replace all is good as one-time action, the resharper checks also highlight when adding new code. – Yoeri Feb 23 '12 at 10:13
  • Didn't know that resharper patterns are producing warnings. Any way it seems that resharper cannot use regex for such things - maybe you need to find or write a plugin for it. Or maybe you should consider using StyleCop+. It could be easily extend with handwritten plugins, that can warn such things – Archeg Feb 23 '12 at 10:25
  • Yep, with code analysis enabled in resharper, you can trigget either a warning, error, suggestion or hint. They result in code being underlined and drawing attention from the developer. We always intent te checkin files with no resharper warnings nor errors. – Yoeri Feb 23 '12 at 10:27