13

In C#, I often use verbatim string literals (e.g., @"Arrr!") to break long strings across multiple lines while preserving the layout. For example, I use it to break up inline SQL like this:

var sqlString = @" 
    SELECT 
        Column1
        , ...
    FROM
        Table1 INNER JOIN ...
    WHERE 
        Column2 = @Value2
        , ...
    ";

...or to break down a regex pattern like this:

var regexPattern = @"
    (?<state>\w+)[.]?                       (?#*** Matches state {optionally having a period at the end} ***)
    (\s+|,|,\s+|\s*\u2022\s*)               (?#*** Matches space between state and zip {can include a comma or bullet point} ***)
    (?<zip>(\d{5}-\d{4}\b|\d{5}(?![\w-])))  (?#*** Matches zip code ***)
    ";

 

If this code gets auto-indented later on, however, the verbatim string literal does not auto-indent. It gets left behind while the lines of code above and below it move to the right. The result is that it falls out of alignment with everything else. For example:

BEFORE:

verbatim = @"               ___
    I likes my                 |
    strings purty              | indented the way I like!
    ";                      ___|
fooBar = "nomnom";

AFTER SURROUNDING WITH 'IF' STATEMENTS:

if (true)
{
    if (maybe)
    {
        verbatim = @"       ___
    I likes my                 |
    strings purty              | no longer indented the way I like =(
    ";                      ___|
        fooBar = "nomnom";
    }
}

 

How can I make Visual Studio auto-indent the lines of my verbatim string literal the same way it does with the other lines of code around it?

 

NOTES

  • I don't want to use normal strings on each line and concatenate them all together like this (this is how I used to do it but I find the free-form verbatim string literals so much easier to read and modify):

    notverbatim = 
        "Alas! " 
        + "This doth " 
        + "make me sad =( " 
        ; 
    
  • Because indenting a multi-line verbatim string literal "grows the string" by adding new whitespace to the left of each line, I realize there are some verbatim string literals where this would not be desirable. If setting this behavior globally in Visual Studio would lead to unintended consequences by indenting other verbatim string literals that shouldn't be, is there a way to mark individual verbatim string literals in the code such that they get auto-indented while all others do not?

Community
  • 1
  • 1
RSW
  • 1,517
  • 1
  • 16
  • 36
  • 1
    I pretty sure there's nothing built into VS that would allow this. You'd pretty much have to write some type of custom plugin. However you could define your string literals into constant variables somewhere that will not be effected by the formatting of where it is actually used. – juharr Mar 16 '16 at 16:28
  • You might be able to write your own VS extension to do this. I doubt VS has this built in. Personally, I don't find it annoying to just highlight and tab manually. – ryanyuyu Mar 16 '16 at 16:29
  • I don't really have a good answer, but you might find the concatenation approach more readable if you move the `+` to the end of the lines. – recursive Mar 16 '16 at 16:47
  • 3
    Remember any indenting whitespace used will become, like the newlines, part of the string: hence nothing that would modify the string in such a manner is built in. – Richard Jul 01 '16 at 12:07
  • @Richard True, as I mention in the 2nd Note at the end of my post. However, it would be great if there were some way you could mark a verbatim string literal as "the kind that indent" and leave others as "the kind that don't". Maybe nothing exists like that right now, but it would be nice if it did! (and posts like this sometimes lead to new features being added if they are popular enough...not sure this one is though) – RSW Jul 01 '16 at 13:38
  • Please see (and upvote) my suggestion for Visual Studio: [Indent multi-line verbatim strings](https://developercommunity.visualstudio.com/idea/602807/indent-multi-line-verbatim-strings.html). – Olivier Jacot-Descombes Aug 11 '19 at 17:48

1 Answers1

1

I was able to get VS2017 to behave this way by making a change to the "Indenting" settings. The one I changed was at Tools -> Options -> Text Editor -> C# -> Tabs. At the top of that screen is a section titled Indenting. Changing it from "Smart" to "Block" resulted in behavior that was similar to how VS2010 worked out of the box (at least for me).

I just made this change so I'm not sure of the full implications to other kinds of indenting, but for the verbatim strings it seems to work.

troutdog8
  • 11
  • 2
  • It's not working for me in VS2015. When I change the indent style to "Block" and then place some curly braces around an existing verbatim string, the verbatim string doesn't automatically indent to the right with the other code when I re-auto-format. Even if it did work, I wouldn't be able to live with the inconvenience of the "Block" indenting style for everything else as I've gotten so dependent on the "Smart" indenting style. – RSW Jul 24 '18 at 19:38
  • That's exactly what I was looking for. Tested in Visual Studio 2019 Professional. Unfortunately, VS won't automatically properly indent inside for example code blocks with that settings. It simply indents every new line with the same indentation level as the previous line, so if you create a new code block, you have to manually add an indentation level. So I'll stick with the *Smart* indenting for now. You can check the [documentation for details on those configuration settings](https://docs.microsoft.com/en-us/visualstudio/ide/reference/options-text-editor-all-languages-tabs#indenting). – David Ferenczy Rogožan Dec 14 '20 at 13:20
  • The *Smart* indenting is compliant with the configured code style, so you don't have to learn and manually maintain all indentation code style rules. The *Block* indenting just mechanically uses the same indentation level as the previous line, which is great inside the verbatim strings, but you lose more important features, in my opinion. – David Ferenczy Rogožan Dec 14 '20 at 13:26