1

I am trying to create a surround-with template with resharper that formats a selection like this

string foo = "A text with spaces";

into this:

string foo = Translate("ATextWithSpaces");

I want to select the "A text with spaces" myself, press the surround-with shortcut and just watch it happen!

I have a template that produces

string foo = Translate("A text with spaces")

...but that's not good enough for me. Any suggestions?

Per Åkerberg
  • 354
  • 1
  • 11
  • 1
    could you create a method that formatted your string as you want it (without spaces and camal-case), so your call would be `string foo = Translate(RemoveSpaces("A text with spaces"))` or include it in your `Translate` method? – Piers Myers Apr 04 '12 at 11:52
  • Including it in my translate method would actually work. Creating such a method in code would not be that hard actually. Good thinking! However, I parse the .cs files for usages of Translate() for other reasons, and there it would be best to have the correct value directly. I guess I could use that same functionality there though... – Per Åkerberg Apr 04 '12 at 11:55
  • Thinking about it for another few seconds, your approach has some other advantages. Seeing the value in code, as it was originally written is beneficial! What a difference a pair of new eyes have! – Per Åkerberg Apr 04 '12 at 11:58

2 Answers2

2

If you're prepared to venture into ReSharper plug-in dev territory, this yields a very, very simple plug-in that would take minutes to implement. Basically, what you can do is make a context action that, when the caret is on a string literal, would take said literal, remove spaces (with string.Replace), then create a new expression using e.g., CSharpElementFactory.CreateExpressionAsIs("Translate($1)", x) where x is the modified literal.

If you're interested in doing this and need more info, feel free to contact me (skype: dmitri.nesteruk, email: dn at jetbrains dot com) with any questions you may have.

Dmitri Nesteruk
  • 23,067
  • 22
  • 97
  • 166
1

Expanding on my comment:

You could create a new method that formats your string as you want it, something like:

public string RemoveSpaces(string input)
{
    return new System.Globalization.CultureInfo("en-GB", false).TextInfo.ToTitleCase(input).Replace(" ", "");
}
Piers Myers
  • 10,611
  • 6
  • 46
  • 61