Question

I have a massive amount of repeating code that I wish to replace in the legacy code base and found Resharper's Structural Search and Replace feature to be a good match for the task. However I have problems making it match my patterns.

The pattern I wish to have quick-fixes for is:

public string PropertyName
{
    get { return base.GetSubNode(_parameterNode, "ElementName").InnerText; }
    set { base.GetSubNode(_parameterNode, "ElementName").InnerText = value; }
}

It's to be replaced with:

public string PropertyName
{
    get { return GetProperty("ElementName"); }
    set { SetProperty("ElementName", value); }
}

I created a pattern:

public string $propName$
{
    get { return base.GetSubNode(_parameterNode, $elementName$).InnerText; }
    set { base.GetSubNode(_parameterNode, $elementName$).InnerText = value; }
}

The pattern matches if i do a search, but does not result in a quick fix being available. If I instead create a separate pattern for the getter and setter I get quick fixes, but the getter pattern matches both the getter and the setter.

The getter pattern:

base.GetSubNode(_parameterNode, $elementName$).InnerText

How can I get quick-fixes for this getter/setter pair?

Was it helpful?

Solution

The following search/replace pattern works like a charm in ReSharper 6.1:

    <wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
    <s:Boolean x:Key="/Default/PatternsAndTemplates/StructuralSearch/Pattern/=B4B745BE8847784E9BF34E217AEB9C3B/@KeyIndexDefined">True</s:Boolean>
    <s:String x:Key="/Default/PatternsAndTemplates/StructuralSearch/Pattern/=B4B745BE8847784E9BF34E217AEB9C3B/Comment/@EntryValue">GetSubNode calls in property accessors</s:String>
    <s:Boolean x:Key="/Default/PatternsAndTemplates/StructuralSearch/Pattern/=B4B745BE8847784E9BF34E217AEB9C3B/FormatAfterReplace/@EntryValue">False</s:Boolean>
    <s:Boolean x:Key="/Default/PatternsAndTemplates/StructuralSearch/Pattern/=B4B745BE8847784E9BF34E217AEB9C3B/IsReplacePattern/@EntryValue">True</s:Boolean>
    <s:String x:Key="/Default/PatternsAndTemplates/StructuralSearch/Pattern/=B4B745BE8847784E9BF34E217AEB9C3B/LanguageName/@EntryValue">CSHARP</s:String>
    <s:String x:Key="/Default/PatternsAndTemplates/StructuralSearch/Pattern/=B4B745BE8847784E9BF34E217AEB9C3B/ReplaceComment/@EntryValue">Replace GetSubNode with GetProperty or SetProperty call</s:String>
    <s:String x:Key="/Default/PatternsAndTemplates/StructuralSearch/Pattern/=B4B745BE8847784E9BF34E217AEB9C3B/ReplacePattern/@EntryValue">public string $propName$&#xD;
{&#xD;
    get { return GetProperty($elementName$); }&#xD;
    set { SetProperty($elementName$, value); }&#xD;
}&#xD;
</s:String>
    <s:String x:Key="/Default/PatternsAndTemplates/StructuralSearch/Pattern/=B4B745BE8847784E9BF34E217AEB9C3B/SearchPattern/@EntryValue">public string $propName$&#xD;
{&#xD;
    get { return base.GetSubNode(_parameterNode, $elementName$).InnerText; }&#xD;
    set { base.GetSubNode(_parameterNode, $elementName$).InnerText = value; }&#xD;
}&#xD;
</s:String>
    <s:String x:Key="/Default/PatternsAndTemplates/StructuralSearch/Pattern/=B4B745BE8847784E9BF34E217AEB9C3B/Severity/@EntryValue">SUGGESTION</s:String>
    <s:Boolean x:Key="/Default/PatternsAndTemplates/StructuralSearch/Pattern/=B4B745BE8847784E9BF34E217AEB9C3B/ShortenReferences/@EntryValue">False</s:Boolean>
    <s:Boolean x:Key="/Default/PatternsAndTemplates/StructuralSearch/Pattern/=B4B745BE8847784E9BF34E217AEB9C3B/CustomPatternPlaceholder/=elementName/@KeyIndexDefined">True</s:Boolean>
    <s:String x:Key="/Default/PatternsAndTemplates/StructuralSearch/Pattern/=B4B745BE8847784E9BF34E217AEB9C3B/CustomPatternPlaceholder/=elementName/Properties/=Maximal/@EntryIndexedValue">-1</s:String>
    <s:String x:Key="/Default/PatternsAndTemplates/StructuralSearch/Pattern/=B4B745BE8847784E9BF34E217AEB9C3B/CustomPatternPlaceholder/=elementName/Properties/=Minimal/@EntryIndexedValue">-1</s:String>
    <s:String x:Key="/Default/PatternsAndTemplates/StructuralSearch/Pattern/=B4B745BE8847784E9BF34E217AEB9C3B/CustomPatternPlaceholder/=elementName/Type/@EntryValue">ArgumentPlaceholder</s:String>
    <s:Boolean x:Key="/Default/PatternsAndTemplates/StructuralSearch/Pattern/=B4B745BE8847784E9BF34E217AEB9C3B/CustomPatternPlaceholder/=propName/@KeyIndexDefined">True</s:Boolean>
    <s:String x:Key="/Default/PatternsAndTemplates/StructuralSearch/Pattern/=B4B745BE8847784E9BF34E217AEB9C3B/CustomPatternPlaceholder/=propName/Properties/=CaseSensitive/@EntryIndexedValue">True</s:String>
    <s:String x:Key="/Default/PatternsAndTemplates/StructuralSearch/Pattern/=B4B745BE8847784E9BF34E217AEB9C3B/CustomPatternPlaceholder/=propName/Properties/=ExactType/@EntryIndexedValue">False</s:String>
    <s:String x:Key="/Default/PatternsAndTemplates/StructuralSearch/Pattern/=B4B745BE8847784E9BF34E217AEB9C3B/CustomPatternPlaceholder/=propName/Properties/=RegEx/@EntryIndexedValue"></s:String>
    <s:String x:Key="/Default/PatternsAndTemplates/StructuralSearch/Pattern/=B4B745BE8847784E9BF34E217AEB9C3B/CustomPatternPlaceholder/=propName/Type/@EntryValue">IdentifierPlaceholder</s:String></wpf:ResourceDictionary>

Save it to a .dotsettings file, and import to your ReSharper installation via ReSharper > Manage Options > [layer name] > Import/Export Settings > Import from file

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top