New Snippet New Snippet Recent Snippets Recent Snippets My Snippets My Snippets Web Code Search Snippets Search
Sign inor Register
Language: C#

ReSharper plugin for converting spaces to underscores

174 Views
Copy Code Show/Hide Line Numbers
[ContextAction(Group = "C#", Name = "ConvertSpacesToUnderscore", Description = "Adds context action to convert spaces in method names to underscore")]
public class ConvertSpacesToUnderscore : BulbItemImpl, IContextAction
{
    readonly ICSharpContextActionDataProvider _provider;
 
    public ConvertSpacesToUnderscore(ICSharpContextActionDataProvider provider)
    {
        _provider = provider;
    }
 
    protected override Action<ITextControl> ExecuteTransaction(ISolution solution, IProgressIndicator progress)
    {
        var selection = _provider.Selection;
        var document = _provider.Document;
 
        var selectedText = document.GetText(selection);
        var adjustedText = selectedText.Replace(" ", "_");
        document.ReplaceText(selection, adjustedText);
 
        return null;
        //Causes an exception being thrown:
        //"All documents should be committed, but these are dirty: <PathToEditedFile>"
    }
 
    public override string Text
    {
        get { return "Convert spaces to underscore"; }
    }
 
    public bool IsAvailable(IUserDataHolder cache)
    {
        var selection = _provider.Selection;
        var document = _provider.Document;
 
        if (selection.Length > 1)
        {
            //Should find a way to enable the plugin only if there is a problem in the current line/selection
            var selectedText = document.GetText(selection);
            return selectedText.Contains(" ");
        }
        return false;
    }
}
by Geir-Tore Lindsve
  February 03, 2010 @ 1:05am
Tags:
Comment:
I'm trying to create a plugin for quickly fixing method names when I write test code, so that I can quickly write a method name with spaces, select the range and replace all spaces with underscores.

Might be a better way to enable the plugin (only if the current line/selection has error and selection contains spaces)

It currently throws an exception when executed => "All documents should be committed, but these are dirty: <PathToEditedFile>"

Add a comment


Report Abuse
brought to you by:
West Wind Techologies


If you find this site useful and use it frequently please consider making a donation to support this free service.
Donate