代码之家  ›  专栏  ›  技术社区  ›  Mikalai Trafimovich

有没有办法在VSCode中映射每个文件的特定启动配置?

  •  0
  • Mikalai Trafimovich  · 技术社区  · 2 年前

    如果我想跑步怎么办 streamlit 对于 app.py python 的其余部分 *.py 文件。如何跳过选择步骤并仅使用的快速组合 Ctrl+F5 (或 F5 )让VSCode根据文件名来决定?

    我在中添加了多个启动配置 .vscode/launch.json :

    {
        // Use IntelliSense to learn about possible attributes.
        // Hover to view descriptions of existing attributes.
        // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
        "version": "0.2.0",
        "configurations": [
            {
                "name": "Python: app.py",
                "type": "python",
                "request": "launch",
                "module": "streamlit",
                "console": "integratedTerminal",
                "justMyCode": true,
                "args": [
                    "run",
                    "app.py"
                ],
                "serverReadyAction": {
                    "pattern": "You can now view your Streamlit app in your browser",
                    "uriFormat": "http://localhost:8501",
                    "action": "openExternally"
                  }
            },
            {
                "name": "Python: Current File",
                "type": "python",
                "request": "launch",
                "program": "${file}",
                "console": "integratedTerminal",
                "justMyCode": true
            }
        ]
    }
    

    所以现在我可以选择其中的任何一个来运行当前在VSCode中打开的文件,但我想节省一些时间。

    0 回复  |  直到 2 年前
        1
  •  0
  •   rioV8    2 年前

    我已经写了扩展 Command Variable v1.57.0版本。

    与扩展一起 Launch Configs 通过ArturoDent,您可以定义 F5 替换基于当前编辑器文件启动配置的键绑定:

      {
        "key": "f5",
        "command": "extension.commandvariable.transform",
        "when": "debuggersAvailable && debugState == 'inactive'",
        "args": {
          "text": "${command:launchCommand}",
          "command": {
            "launchCommand": {
              "command": "${transform:launchCommand}",
              "transform": {
                "launchCommand": {
                  "text": "${command:launchCommand}",
                  "command": {
                    "launchCommand": {
                      "command": "extension.commandvariable.file.fileAsKey",
                      "args": {
                        "app.py": "launches.Streamlit",
                        "@default": "launches.OtherPython"
                      }
                    }
                  }
                }
              }
            }
          }
        }
      }
    

    你需要编辑 settings.json 对于启动配置:

      "launches": {
        "Streamlit": "Python: app.py",
        "OtherPython": "Python: Current File"
      }
    

    也许您需要添加工作区/文件夹名称,请参阅启动配置文档。