question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Feature request: Convert to interpolated string code refactoring

See original GitHub issue

Currently, there is a code refactoring to convert to verbatim string.

image

Another possible code refactoring (should only be offered when the string contains { and } is to convert to interpolated string.

Example

I wanted something like this while working with unit tests in roslyn and roslyn-analyzer codebases. So, here is an example. Suppose there is an existing unit test like the following

[Fact]
public void TestSomething()
{
    var code = @"
namespace Mynamespace
{
    public class Program
    {
        public static void Main() {Console.WriteLine();}
    }
}";
}

For some reason, I want to update the unit test to:

[Theory]
[InlineData("")]
[InlineData("string[] args")]
public void TestSomething(string mainArgs)
{
    var code = $@"
namespace Mynamespace
{{
    public class Program
    {{
        public static void Main({mainArgs}) {{Console.WriteLine();}}
    }}
}}";
}

It would be much easier to make this update if the IDE can offer a refactoring that converts the original code to the following:

var code = $@"
namespace Mynamespace
{{
    public class Program
    {{
        public static void Main() {{}}
    }}
}}";

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:8 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
louis-zcommented, Oct 11, 2020

I came up with a new implementation, based on AbstractConvertConcatenationToInterpolatedStringRefactoringProvider and its derived classes (thanks @Youssef1313 for pointing me in the right direction 😉). I will submit a PR shortly.

0reactions
alenroscommented, Dec 18, 2020

If it makes any difference, I came here exactly because I expected “Convert to interpolated string” to do just that, precisely because I wanted it since I kept forgetting to add a $.

Read more comments on GitHub >

github_iconTop Results From Across the Web

c# - "Convert to interpolated string" refactoring suggestion ...
I'm using Visual Studio 2017 Version 15.4.1. Mysteriously and suddenly I am not presented with this option anymore. Where is the "Convert to...
Read more >
Convert to String Interpolation | CodeRush
Press the Ctrl + . or Ctrl + ~ shortcut to invoke the Code Actions menu. Select Convert to String Interpolation from the...
Read more >
Refactorings - "Convert to string interpolation" ...
Refactorings - "Convert to string interpolation" refactoring produces incorrectly formatted code when applied to a method with wrapped arguments.
Read more >
VS2019.4: "Convert to interpolated string" refactoring is not ...
Place the cursor anywhere between the first space and before the } in $" = {ListingId}" or $" = {DealerId}" and bring up...
Read more >
C# Quick Actions and Refactorings
Press Ctrl+.to trigger the Quick Actions and Refactorings menu. Select Convert to interpolated string. Convert placeholder to interpolated ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found