visual-studio 转义代码片段中的 $ 字符

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3215705/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-22 12:12:09  来源:igfitidea点击:

Escaping the $ character in snippets

visual-studiocode-snippets

提问by cory-fowler

I find myself doing a ton of jQuery these days, so I started to abstract out some of the common things I do into snippets. I look forward to sharing these with the community, but I'm running into an issue right now.

这些天我发现自己做了大量的 jQuery,所以我开始将我做的一些常见事情抽象成片段。我期待与社区分享这些,但我现在遇到了一个问题。

The literals in snippets are defined by adding dollar signs ($) around the name of the literal to delimit where the value you would like to provide will go. This is difficult because jQuery uses the dollar sign notation in order to use a lot of its functionality.

片段中的文字是通过在文字名称周围添加美元符号 ($) 来定义的,以分隔您要提供的值的位置。这很困难,因为 jQuery 使用美元符号表示法来使用它的许多功能。

What is the escape sequence for snippets, so I am able to use the dollar sign, and have my snippets still function?

代码段的转义序列是什么,以便我能够使用美元符号,并且我的代码段仍然有效?

回答by Ahmad Mageed

To have a literal $try doubling it: $$

有一个字面$尝试加倍它:$$

回答by Jesus David Sanchez Suarez

This is the right way for Visual Studio Code: \\$.

这是正确的方式的Visual Studio代码\\$

This makes the $a literal part of the snippet rather than the start of a $-prefixed construct.

这使得$片段的文字部分而不是-prefixed 结构的开始。$

回答by Dirk Seefeld

There is an "Delimiter" attribute defined for a Code element. This defaults to $ but you can set it to a different character like ~ or so.

为 Code 元素定义了一个“Delimiter”属性。这默认为 $,但您可以将其设置为不同的字符,例如 ~ 左右。

...

...

<Snippet>
<Code Language="JavaScript" Delimiter="~"><![CDATA[(function ($) {
    $(document).ready(function () {

    });
})(jQuery);]]></Code>
</Snippet>

...

...

回答by cory-fowler

Although the jQuery response is valid, it's a nicer syntax to use the $ notation.

尽管 jQuery 响应是有效的,但使用 $ 表示法是一种更好的语法。

I've found an answer: Making the $ character a literal with a default value of $.

我找到了一个答案:使 $ 字符成为默认值为 $ 的文字。

<Literal Editable="true">

<ID>dollar</ID> <ToolTip>replace the dollar sign character</ToolTip> <Default>$</Default> <Function> </Function> </Literal>

回答by Joseph Morgan

I used this for a formattable string in C#. I used the example above from cory-fowlerverbatim:

我将它用于 C# 中的可格式化字符串。我使用了上面来自cory-fowler的例子:

<Literal Editable="true">
    <ID>dollar</ID>
    <ToolTip>Replace the dollar sign character</ToolTip>
    <Default>$</Default>
    <Function></Function>
</Literal>

Usage (line breaks are added for clarity on Stack Overflow, not in the original.):

用法(为了清楚起见,在 Stack Overflow 中添加了换行符,而不是在原始版本中。)

    string errMessage = $dollar$"Error occurred in
       {MethodBase.GetCurrentMethod().Module}, in procedure
       {MethodBase.GetCurrentMethod().Name}: {ex.Message}".ToString();

Thanks, cory-fowler!

谢谢,科里福勒!