You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何配置Cucumber autocomplete,将{string}补全为""而非正则格式?

解决Cucumber自动补全{string}生成正则而非空双引号的问题

我使用@badeball/cypress-cucumber-preprocessor包与VS Code的Cucumber (Gherkin) Full Support扩展构建Cypress框架时,遇到一个问题:自动补全包含{string}的步骤时,会生成("|')[^\1]*\1或("|')\1格式的内容,我希望将{string}直接替换为""。

当前配置文件

.cypress-cucumber-preprocessorrc.json

{
  "json": {
    "enabled": true
  },
  "stepDefinitions": "**/*.steps.js"
}

VS Code settings.json

{
    "explorer.sortOrder": "filesFirst",
    "cucumberautocomplete.steps": "**/*.steps.js",
    "cucumberautocomplete.syncfeatures": "**/features/*.feature",
    "cucumberautocomplete.strictGherkinCompletion": true,
    "cucumberautocomplete.strictGherkinValidation": true,
    "cucumberautocomplete.smartSnippets": true,
    "cucumberautocomplete.stepsInvariants": true,
    "cucumberautocomplete.skipDocStringsFormat": true,
    "cucumberautocomplete.onTypeFormat": true,
    "editor.quickSuggestions": {
        "comments": false,
        "strings": true,
        "other": true
    },
    "cucumberautocomplete.gherkinDefinitionPart": "(Given|When|Then)\\"
}

示例步骤定义

When("I click the {string}", (element) => {
    // some code
});

当前补全效果

  • 当smartSnippets设为true时,补全结果:When I click the ("|')\1
  • 当smartSnippets设为false时,补全结果:When I click the ("|')[^\1]*\1

期望补全效果

When I click the ""

解决方法

修改VS Code的settings.json,添加自定义参数类型配置,并关闭smartSnippets:

  1. 在settings.json中添加parameterTypes配置,指定{string}的补全模板为""
  2. 将cucumberautocomplete.smartSnippets设为false,避免扩展生成默认正则式补全

修改后的关键配置片段:

{
    // 保留原有其他配置...
    "cucumberautocomplete.smartSnippets": false,
    "cucumberautocomplete.parameterTypes": [
        {
            "name": "string",
            "type": "string",
            "regexp": "\"\"",
            "suggestion": "\"\""
        }
    ]
}

通过自定义parameterTypes,可以强制扩展在补全{string}参数时直接插入"",满足需求。

内容的提问来源于stack exchange,提问作者RobbieDixonBr-dge

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.17 11:27:35