sublime text 的新手并尝试设置一些完成,因为我有一堆片段,我宁愿只使用 1 个文件,而不是每个片段的单独文件。
当我保存下面的代码时,我收到无法解析文件的错误,意外的换行符。我假设这是因为我正在使用换行符。是否仅针对短的 1 行代码完成?当我有如下较长的代码时,有什么可以做的,这样我就不必使用单个文件作为代码片段了吗?
{
"completions":
[
{ "trigger": "clearforms", "contents":
"//clear form fields
\$(function() {
\$.fn.clearInput = function() {
return this.focus(function() {
if( this.value == this.defaultValue ) {
this.value = "";
}
}).blur(function() {
if( !this.value.length ) {
this.value = this.defaultValue;
}
});
};
\$('#subscribe').clearInput();
\$('#search').clearInput();
});"
}
]
最佳答案
JSON不允许多行字符串 - 换行符需要编码为 \n
。 $
符号需要像这样双重转义 \\$
,而你的评论正斜杠像这样单一转义 - \/\/
。因此,您的完成文件需要如下所示:
{
"completions":
[
{ "trigger": "clearforms", "contents": "\/\/ clear forms \n\\$(function() { \n\\$.fn.clearInput = function() { \nreturn this.focus(function() { \nif( this.value == this.defaultValue ) { \nthis.value = \"\"; \n} \n}).blur(function() { \nif( !this.value.length ) { \nthis.value = this.defaultValue; \n} \n}); \n}; \n\n\\$('#subscribe').clearInput(); \n\\$('#search').clearInput(); \n});" }
]
}
我会留给你在每个换行符后放入适当的空格。我应该注意,虽然拥有单独的片段文件可能会稍微更“困惑”,但它们确实允许多行条目而无需换行符转义,从而使您可以更轻松地格式化输出。但是,$
和可能的 JS 注释可能需要转义。
关于sublimetext - 完成文件 "contents"是否必须为 1 行(无换行符)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18235058/