pythonpython-3.xvisual-studio-codecode-completioncode-assist

VS Code assist for Python create new method if they not exists?


I am new to VS code and Python :). I am trying :)

Java is my main coding language and I use(love) IntelliJ IDEA for coding. I chose VS code for python(for many reasons I cannot control)

In InteliJ when coding java , InteliJ code assist prompt for creation of new methods if they not exists, I found this very useful( check attached image) enter image description here

I couldn't find equivalence in VS code for python, Is there is any extension I can use to get this behaviour and many other InteliJ assists( eg: suggestions) ?


Solution

  • Two suggestions that might help:

    1. My Code Actions extension

    After installation, add configuration in settings.json according to the format.

    E.g:

      "my-code-actions.actions": {
        "[python]": {
          "import {{diag:$1}}": {
            "diagnostics": ["\"(.*?)\" is not defined"],
            "text": "import {{diag:$1}}\n",
            "where": "afterLast",
            "insertFind": "^(import |from \\w+ import )"
          }
        }
      }
    

    Effect:

    enter image description here

    enter image description here

    2. Custom code snippets

    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.

    E.g:

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

    Effect:

    enter image description here

    enter image description here