片段:检测片段前缀是否用于字符串或常规 javascript

Snippets: Detect if a snippet prefix is used in a string or regular javascript

我想构建一个工具来帮助我在项目中更快地完成工作。为此,我想使用片段功能。 是否有可能知道是否在字符串引号或常规 javascript 代码中使用了片段前缀,例如:

'prefix + tab' (with quotes) -> outputs one thing
 prefix + tab  (no quotes)   -> outputs different thing.

这样:

'bgc + tab'   -> 'background-color: '
 prefix + tab ->  backgroundColor: 

我希望我的问题是有道理的 :D

您需要启用字符串建议。将此添加到您的 settings.json:

"editor.quickSuggestions": {
    "other": true,
    "comments": false,
    "strings": true
  },

然后定义您的代码段:

"Background Color": {
        "prefix": "bgc",
        "body": [
            "background-color: "
        ],
        "description": "Background Color"
    },

您可以通过 selecting User Snippets File > Preferences(代码 > macOS 上的 Preferences) 创建您的代码片段select 语言 (JavaScript)。