简体   繁体   English

使逐字字符串文字自动缩进以保持与附近代码保持一致

[英]Make verbatim string literals auto-indent to stay aligned with nearby code

In C#, I often use verbatim string literals (eg, @"Arrr!" ) to break long strings across multiple lines while preserving the layout. 在C#中,我经常使用逐字字符串文字(例如, @"Arrr!" )来跨越多行来打破长字符串,同时保留布局。 For example, I use it to break up inline SQL like this: 例如,我使用它来分解内联SQL,如下所示:

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”声明后:

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? 我如何使Visual Studio自动缩进我的逐字字符串文字的行,就像它与其他代码行一样?

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? 如果在Visual Studio中全局设置此行为会通过缩进不应该的其他逐字字符串文字而导致意外后果,是否有一种方法可以在代码中标记单个逐字字符串文字,以便它们自动缩进而其他所有字符串文字不?

I was able to get VS2017 to behave this way by making a change to the "Indenting" settings. 通过更改“缩进”设置,我能够让VS2017以这种方式运行。 The one I changed was at Tools -> Options -> Text Editor -> C# -> Tabs . 我更改的是工具 - > 选项 - > 文本编辑器 - > C# - > 选项卡 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). 将其从“智能”更改为“阻止”导致的行为类似于VS2010的开箱即用(至少对我而言)。

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. 我只是做了这个改变所以我不确定其他类型的缩进的全部含义,但对于逐字字符串它似乎工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM