1

我希望在遗留代码库中替换大量重复代码,并发现 Resharper 的结构搜索和替换功能非常适合该任务。但是,我在使它与我的模式匹配时遇到了问题。

我希望快速修复的模式是:

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

它将被替换为:

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

我创建了一个模式:

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

如果我进行搜索,则该模式匹配,但不会导致快速修复可用。如果我改为为 getter 和 setter 创建单独的模式,我会得到快速修复,但 getter 模式同时匹配 getter 和 setter。

吸气剂模式:

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

我怎样才能得到这个 getter/setter 对的快速修复?

4

1 回答 1

2

以下搜索/替换模式就像 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>

将其保存到.dotsettings文件,然后通过ReSharper > Manage Options > [layer name] > Import/Export Settings > Import from file导入您的 ReSharper 安装

于 2012-01-13T23:02:01.427 回答