单击VS代码Python创建不存在的函数

发布于 2025-02-13 23:20:52 字数 118 浏览 3 评论 0 原文

其他IDE(例如Pycharm,Intellij等)具有一个功能,如果它找到一个未定义的函数,则可以右键单击它,然后单击“创建方法”或类似的内容,类似于自动创建功能定义。它在TDD中有很大帮助。 VS代码中有类似的东西吗?

Other IDE's like PyCharm, IntelliJ, etc. have a feature where if it finds a function being called that is undefined, you can right-click it and click 'create method' or something similar to automatically create the function definition. It helps out a lot in TDD. Is there something similar in VS Code?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

〃安静 2025-02-20 23:20:52

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

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"
    }

”在此处输入图像描述”

You can install the My Code Actions extension, here is a simple example:

Configure in settings.json file:

// 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",
        }
        }
    }
}

The effect when writing code after saving this setting:

enter image description here

enter image description here

enter image description here

Another method that might be useful:

open in sequence File > Preferences > Configure User Snippets. Select python in the command palette. This will create a python.json file in which you can customize the code segment according to the rules.

A simple example:

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

enter image description here

enter image description here

强辩 2025-02-20 23:20:52

目前,没有像Microsoft Python扩展所内置的那样,但是请求它的请求是属于塔的积压:添加对创建功能,类或参数代码操作的支持,具体取决于上下文#5368 。然后,您可以使用快速修复菜单( ctrl/cmd +)。我建议您对该问题票的大拇指表示支持。您也可以订阅它以获取有关讨论和进度的通知。请避免在那里发表嘈杂的评论,例如仅包含“ +1” /“ bump”的评论。

At the moment, there's nothing like this built in to the Microsoft Python extensions, but a request for it is in the backlog for Pylance: Add support for create function, class or parameter code actions depending on context #5368. You'd then be able to use the quick fix menu (ctrl/cmd+.). I suggest that you give that issue ticket a thumbs up to show support for it. You can also subscribe to it to get notified about discussion and progress. Please avoid making noisy comments there like ones that just consist of "+1" / "bump".

流星番茄 2025-02-20 23:20:52

这是 my-code-my-code-actions < /code> jialedu ,

  • 函数参数
  • 生成代码下面的代码 原始函数
  • 考虑 另一个警告“未发现变量...”

"my-code-actions.actions": {
    "[python]": {
        "create method {{diag:$1}}": {
            "diagnostics": ["\"(.*?)\" is not defined", "Undefined variable '(.*?)'"],
            "atCursor": "\\b{{diag:$1}}\\(([^)]*)\\)",
            "insertFind": "{{diag:$1}}\\((.*?)\\)",
            "text": "\ndef {{diag:$1}}({{atCursor:$1}}):\n    pass\n\n",
            "where": "afterLast",           
        },                    
    }


}

Here is an adapted version of the my-code-actions settings from JialeDu,

  • considering function arguments
  • generating the code below the original function call
  • considering another warning "Undfined variable ..."

"my-code-actions.actions": {
    "[python]": {
        "create method {{diag:$1}}": {
            "diagnostics": ["\"(.*?)\" is not defined", "Undefined variable '(.*?)'"],
            "atCursor": "\\b{{diag:$1}}\\(([^)]*)\\)",
            "insertFind": "{{diag:$1}}\\((.*?)\\)",
            "text": "\ndef {{diag:$1}}({{atCursor:$1}}):\n    pass\n\n",
            "where": "afterLast",           
        },                    
    }


}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文