Help with creating a Custom Pattern

I can't seem to figure this out..  We use a custom log where the methods have a built in string.Format.  So I could log something like this 'Log.Info("var1 is: {0} and var2 is {1}.", var1, var2);'  Or I could just log a plain string such as 'Log.Info("No formatting here");'.  So what I'm trying to do is make it act just like string.Format acts with resharper (match the braces to the variables they refer to and tell me if I have a variable with no brace or brace with no variable).  But I can't wrap my head around or find the right documentation to help me figure out how Custom Patterns work.  If someone could tell me how this is supposed to work or point me towards the proper documentation that'd be super helpful.

8 comments
Comment actions Permalink

Not everyone at my company uses ReSharper so I'm not going to start adding ReSharper tags to our code base.  Is there not a way to do this with Custom Patterns?

0
Comment actions Permalink

Here it is a few months later and I see someone has commented on the actual Defining Custom String Formatting Methods page.  With a link to this article (ReSharper NullReferenceException Analysis and Its Contracts) referencing the External Annotations section.  I'm pretty sure that's what I need.  Here is the excerpt

<assembly name=”System.Xml, Version=4.0.0.0″> <!—The attribute name contains the assembly name. If you don’t specify the version, this file’s attributes will be applied to all versions of the assemblies of that name –>
<member name=”M:System.Xml.XmlReader.Create(System.IO.Stream)”> <!—This shows the name of the member whose attributes are complemented; the notation is the same as XmlDoc –>
<attribute ctor=”M:JetBrains.Annotations.NotNullAttribute.#ctor” /> <!—attribute constructor names are also specified using XmlDoc notation –>
<parameter name=”input”>
  <attribute ctor=”M:JetBrains.Annotations.NotNullAttribute.#ctor” />
</parameter>
</member>
</assembly>

So the assembly name is pretty straight forward (unless I'm off, it should just be the .dll the project is reference)

so let's say I'm using lib.dll
and there's a method in the Log class that is public void Error(string message, object[] args) {...}
so what I've got is this:

<assembly name=”lib.dll″>
<member name=”M:lib.Log.Error(System.String, ? ? ?)”> <!—What goes here? –>
<attribute ctor=”M:SystemFormatMethodAttribute.#ctor” />
<parameter name=”message”>
  <attribute ctor=”M:JetBrains.Annotations.SystemFormatMethodATtribute.#ctor” />
</parameter>
<parameter name=”args”>
  <attribute ctor=”M:JetBrains.Annotations.SystemFormatMethodATtribute.#ctor” /> <!—put both parameters by name here with the exact same attribute tag? –>
</parameter>
</member>
</assembly>

Google wasn't any help.  I can't find any examples of this anywhere.  So if someone could point me to an example or just type it out below it would be greatly appreciated.

0
Comment actions Permalink

Based on the existing external annotations, I'd guess that you need something like this:

<member name="M:lib.Log.Error(System.String, System.Object[])">
    <attribute ctor="M:JetBrains.Annotations.StringFormatMethodAttribute.#ctor(System.String)">
        <argument>message</argument>
    </attribute>
</member>

0
Comment actions Permalink

So your best guess is no annotation for the args parameter?

And actually, that layout is different from the only example I could find (the one above with a different annotation)..

0
Comment actions Permalink

Correct - you're trying to use the XML annotations to replicate:

[StringFormatMethod("message")]
public void Error(string message, object[] args)
{
   ...
}


The only annotation is on the method; there's no annotation on either argument.

0
Comment actions Permalink

Then what was the above example doing with the named parameter tag and the attribute tag inside it?

0
Comment actions Permalink

That example is equivalent to:

[NotNull]
public static XmlReader Create([NotNull] Stream input)
{
   ...
}


In this case, there is an attribute on the method parameter, which is why there is a named parameter tag with an attribute tag inside.

0

Please sign in to leave a comment.