在 VS Code Python 中单击即可创建不存在的函数

问题描述 投票:0回答:2

其他 IDE,如 PyCharm、IntelliJ 等,有一个功能,如果它发现正在调用的函数未定义,您可以右键单击它,然后单击“创建方法”或类似的内容来自动创建函数定义。它对 TDD 有很大帮助。 VS Code 里有类似的东西吗?

python visual-studio-code tdd
2个回答
3
投票

您可以安装我的代码操作扩展,这是一个简单的示例:

settings.json
文件中配置:

// settings.json file
{
    "my-code-actions.actions": {
        "[python]": {
        "create new methond {{diag:$1}}": {
            "diagnostics": ["\"(.*?)\" is not defined"],
            "text": "def {{diag:$1}}():\n    pass\n",
            "where": "afterLast",
        }
        }
    }
}

保存此设置后编写代码时的效果:

另一种可能有用的方法:

依次打开文件 > 首选项 > 配置用户片段。在命令面板中选择python。这将创建一个

python.json
文件,您可以在其中根据规则自定义代码段。

一个简单的例子:

    "Print to console":{
        "prefix": "defHello",
        "body": [
            "def Hello():",
            "${1:    }print('Hello worle')",
            "$2",
        ],
        "description": "print hello world"
    }


0
投票

目前,Microsoft Python 扩展中还没有内置类似的功能,但 Pylance 的待办事项中有这样的请求:根据上下文添加对创建函数、类或参数代码操作的支持#5368。然后,您就可以使用快速修复菜单 (ctrl/cmd+.)。我建议您对该问题票竖起大拇指以表示支持。您还可以订阅它以获取有关讨论和进展的通知。请避免在那里发表嘈杂的评论,比如只包含“+1”/“bump”的评论。

© www.soinside.com 2019 - 2024. All rights reserved.