3

I would like to re-factor various blocks of code throughout a project using ReSharper 7.1 Search / Replace with pattern.

The code blocks are similar to the following simplified example:

someControl.StatusProgressBar.IsIndeterminate = false;
someControl.StatusProgressBar.Visibility = Visibility.Visible;
someControl.StatusProgressBar.Minimum = 0;
someControl.StatusProgressBar.Maximum = 100;
someControl.StatusProgressBar.Value = percentage;

And I would like to change them to:

someControl.StatusProgressBar.Use(p =>
{
  p.IsIndeterminate = false;
  p.Visibility = Visibility.Visible;
  p.Minimum = 0;
  p.Maximum = 100;
  p.Value = percentage;
});

'Use' is an extension method

This is easy enough if all the blocks of code are setting the same number of properties. The following search and replace patterns will do the job:

SEARCH

$someControl$.$SomeProperty$.$SubProperty1$ = $val1$;
$someControl$.$SomeProperty$.$SubProperty2$ = $val2$;
$someControl$.$SomeProperty$.$SubProperty3$ = $val3$;
$someControl$.$SomeProperty$.$SubProperty4$ = $val4$;
$someControl$.$SomeProperty$.$SubProperty5$ = $val5$;

REPLACE

$someControl$.$SomeProperty$.Use(p=>
{
    p.$SubProperty1$ = $val1$;
    p.$SubProperty2$ = $val2$;
    p.$SubProperty3$ = $val3$;
    p.$SubProperty4$ = $val4$;
    p.$SubProperty5$ = $val5$;
});


However, if I also have a code block such as:

someControl.StatusProgressBar.IsIndeterminate = false;
someControl.StatusProgressBar.Visibility = Visibility.Visible;
someControl.StatusProgressBar.Minimum = 0;
someControl.StatusProgressBar.Maximum = 100;
someControl.StatusProgressBar.Value = percentage;
someControl.StatusProgressBar.Orientation = Vertical;

Is it possible, with ReSharper, to capture and replace both code blocks with one pattern? The later having one extra property setting but could easily be more than one extra or less.

I am thinking this is not possible. It would require the ability to create some kind of variable pattern and I can't see a way to do that, be it with regular expressions or otherwise.

Any ideas?

Bas Leijdekkers
  • 23,709
  • 4
  • 70
  • 68
skeandu
  • 155
  • 1
  • 6
  • I do not see any chance to do it by one replace pattern (with my version R# 5.1.3). I guess you need one pattern for every possible count of rows. – thersch Nov 15 '12 at 15:15
  • I think you are right. I can just start with a pattern that catches all and reduce the lines it captures one by one. – skeandu Nov 16 '12 at 09:18
  • I suspect the only way to achieve this is via a custom add-in. The new R# API looks quite straightforward compared to previous versions. – Drew Noakes Sep 19 '13 at 23:23

0 Answers0