Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Resharper string.format shortcut

Tags:

c#

resharper

I often find myself writing

var message = "Hello {0}";

and then going back and typing in

var message = string.Format("Hello {0}", world);

It would be nice if as I were typing the string, when I put the " at the end, resharper figured out there was a parameter in the string and immediately surrounded the string with the string.Format method and put the cursor at the first argument.

Is there a straightforward way to do this? I'm using Resharper 6.1

like image 612
Superman Avatar asked Dec 29 '11 23:12

Superman


5 Answers

Just type it in dumber:

 "Hello " + world

Alt+EnterEnter, done1:

 string.Format("Hello {0}", world);

Obviously, this also works when the whole thing is much more complex. I know it will strip useless calls to .ToString(), and I suspect it will automatically lift any format expressions, like

 int i = 42;
 "i = " + i.ToString("X2"); 

Alt+EnterEnter

 string.Format("i = {0:X2}", i);

1 If you're unlucky/the surrounding code contains many things that trigger Resharper suggestions(?) you might have to position the cursor over one of the + operators

like image 93
sehe Avatar answered Oct 17 '22 21:10

sehe


Shameless plug

I've also tried to formulate an approach to make string format creation easier, and what I came up with is string splicing a-la PHP:

enter image description here

This is part of a ReSharper plug-in that you can find here.

like image 20
Dmitri Nesteruk Avatar answered Oct 17 '22 20:10

Dmitri Nesteruk


I ended up writing an extension method for strings named FormatWith(arg0, ar1...) because of this. Then I found that the Humanizer library did the same exact thing. Add the Humanizer NuGet package and now you'll be able to write "Heres my formatted string on the {0}st try!".FormatWith(1)" with hopefully less bouncing around. If you have ReSharper and like that it highlights the matching placemarkers with the parameter, install the Humanizer Annotations R# Extension and you'll get them back.

like image 2
Sam Rueby Avatar answered Oct 17 '22 20:10

Sam Rueby


You can almost do this with a Visual Studio snippet (i.e. without ReSharper).

Save the following as a file with a .snippet extension.

<?xml version="1.0" encoding="utf-8"?>
<CodeSnippet Format="1.0.0" xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
    <Header>
        <Title>string format</Title>
        <Author>Matthew Strawbridge</Author>
        <Description>Wraps the selected text with string.Format</Description>
        <SnippetTypes>
            <SnippetType>SurroundsWith</SnippetType>
        </SnippetTypes>
    </Header>
    <Snippet>
        <Declarations>
            <Literal>
                <ID>variable</ID>
                <Default>value</Default>
            </Literal>
        </Declarations>
        <Code Language="CSharp">
            <![CDATA[string.Format($selected$, $variable$);]]>
        </Code>
    </Snippet>
</CodeSnippet>

Then you can load it via Tools | Code Snippets Manager | Import.

Once the snippet is available, you can type

var message = "Hello {0}"

but you'd have to select the string and then press CtrlK CtrlS and select the snippet name to apply it. This would generate

var message = string.Format("Hello {0}", value);

with the value part selected for editing.

Edit: There's also a Snippet Designer extension that makes working with snippets easier.

like image 1
Matthew Strawbridge Avatar answered Oct 17 '22 21:10

Matthew Strawbridge


Here is an alternate to Matthew's visual studio code snippet. This snippet asks for the variable name but defaults to message which is optional an the only thing required is the variable name. HTH

Looks like:

var message = string.Format( "abc {0}", variable ); 

as the default (the abc {0} was the highlighted text)

<?xml version="1.0" encoding="utf-8"?>
<CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
  <CodeSnippet Format="1.0.0">
    <Header>
      <Title>String Format</Title>
      <Author>OmegaMan</Author>
      <Description>Surrounded text gets format</Description>
      <HelpUrl></HelpUrl>
      <SnippetTypes />
      <Keywords />
      <Shortcut>#SF</Shortcut>
    </Header>
    <Snippet>
      <References />
      <Imports />
      <Declarations>
        <Literal Editable="true">
          <ID>name</ID>
          <Type></Type>
          <ToolTip>What the variable name should be.</ToolTip>
          <Default>message</Default>
          <Function></Function>
        </Literal>
        <Literal Editable="true">
          <ID>Vars</ID>
          <Type></Type>
          <ToolTip>The target variable for format.</ToolTip>
          <Default>variable</Default>
          <Function></Function>
        </Literal>
      </Declarations>
      <Code Language="csharp" Kind="" Delimiter="$"><![CDATA[var $name$ = string.Format($selected$, $Vars$);$end$ ]]></Code>
    </Snippet>
  </CodeSnippet>
</CodeSnippets>
like image 1
ΩmegaMan Avatar answered Oct 17 '22 22:10

ΩmegaMan