Postprocessing of file after cleanup is complete..

Hi.. First thanks for ReSharper, it's a great product I use every day!

At my workplace we are supposed to use the "Cleanup code" feature before every commit.
However this is quite easy to forget.

What I want to accomplish is to create a svn pre-commit hook that can check whether a code cleanup has been done or not,
and then ask the user if they really want to commit the code without first cleaning up.

To do this I need to somehow mark the file as being cleaned up. My approach is that I want to insert a hash inside the file of the file itself,
between a pair of special markers. This way the hash will be wrong when file is altered after cleanup..


// This is a source code file. ##CLEANUPHASH:abcdef##


using blah;

This has to be done right after the cleanup is complete, and I have to do it on the "raw data" of the file.
So preferably I don't want to traverse the node tree to do this, the way I've seen examples ICodeCleanupModule do.

Is this even possible, and how?

1 comment
Comment actions Permalink

Hi there! Your assumption is correct – you can indeed create a comment in the file header that adds the hash of the file. Here are the steps to do this:

  • Create a code cleanup module. You can take a look at the SamplePlugin in the SDK, specifically the

    UseIntConstantsInsteadOfLiterals.cs
    file.
  • In the code cleanup’s

    Process()
    method, it’s easy to get the
    IFile
    from the
    IPsiSourceFile
    that gets provided as a parameter. To get the actual text in the file, you can just call
    GetText()
    .
  • Once you calculate the new hash (I imagine you can just use

    string.GetHashCode()
    for this), you’ll need to create a comment. Check out the Comments section of the Plugin Development Guide.
  • Lastly, you need to add or replace the comment in your chosen location. For example, if you choose your comment to be the first in the file, it’s as easy as checking the file’s

    FirstChild
    and then using
    ModificationUtil
    to either update or insert the comment.

Don’t forget that adding the comment changes the effective hash, so you might want to actually exclude it (i.e., do a

string.Replace()
on the
##CLEANUPHASH:abcdef##
) when calculating the hash in the first place.
0

Please sign in to leave a comment.