vscode fold docstrings python macos

  • Last Update :
  • Techknowledgy :

By default VS Code's cold folding is indentation-based, unaware of the programming language used. Aside from Brett's answer, one hack is to indent the docstring.

def test(a, b, c):
   ""
"[short summary]

[indented long summary parameters returns etc.] ^
^ ^ ^
""
"

return...
  1. Run which code. I got /usr/bin/code, but that was linked to /usr/share/code/bin/code.
  2. Copy the directory /usr/share/code/resources/app/extensions/python to ~/.vscode/extensions.
  3. Add the regex below to ~/.vscode/extensions/python/language-configuration.json.
{
   "folding": {
      "offSide": true,
      "markers": {
         "start": "^\\s*#\\s*region\\b|^\\ *\"{3}(?!.*\"{3}).+",
         "end": "^\\s*#\\s*endregion\\b|^\\ *\"{3}$"
      }
   }
}

Suggestion : 2

>Fold Level 2 and it is causing too much anycodings_visual-studio-code folding.,>Fold Level 3 does not fold the methods' anycodings_visual-studio-code docstrings.,I have tried using the command:,My primary goal is to fold the docstrings anycodings_visual-studio-code and nothing else.

My primary goal is to fold the docstrings anycodings_visual-studio-code and nothing else.

def test(a, b, c):
   ""
"A lot of multiline
docstrings here
that dont get folded
   ""
"
return...

would turn into:

def test(a, b, c):
   return...

By default VS Code's cold folding is anycodings_macos indentation-based, unaware of the anycodings_macos programming language used. Aside from anycodings_macos Brett's answer, one hack is to indent anycodings_macos the docstring.

def test(a, b, c):
   ""
"[short summary]

[indented long summary parameters returns etc.] ^
^ ^ ^
""
"

return...
  1. Run which code. I got /usr/bin/code, but that was linked to /usr/share/code/bin/code.
  2. Copy the directory /usr/share/code/resources/app/extensions/python to ~/.vscode/extensions.
  3. Add the regex below to ~/.vscode/extensions/python/language-configuration.json.
{
   "folding": {
      "offSide": true,
      "markers": {
         "start": "^\\s*#\\s*region\\b|^\\ *\"{3}(?!.*\"{3}).+",
         "end": "^\\s*#\\s*endregion\\b|^\\ *\"{3}$"
      }
   }
}

Suggestion : 3

Visual Studio Code extension to quickly generate docstrings for python functions.,Keyboard shortcut: ctrl+shift+2 or cmd+shift+2 for macCan be changed in Preferences -> Keyboard Shortcuts -> extension.generateDocstring,Can be changed in Preferences -> Keyboard Shortcuts -> extension.generateDocstring,The source code for this extension is hosted on GitHub. Contributions, pull requests, suggestions, and bug reports are greatly appreciated.

Variables

{
   {
      name
   }
} - name of the
function {
   {
      summaryPlaceholder
   }
} - [summary] placeholder {
   {
      extendedSummaryPlaceholder
   }
} - [extended_summary] placeholder

Sections

{
   {
      #args
   }
} - iterate over
function arguments {
   {
      var
   }
} - variable name {
   {
      typePlaceholder
   }
} - [type] or guessed type placeholder {
   {
      descriptionPlaceholder
   }
} - [description] placeholder {
   {
      /args}}

      {
         {
            #kwargs
         }
      } - iterate over
      function kwargs {
         {
            var
         }
      } - variable name {
         {
            typePlaceholder
         }
      } - [type] or guessed type placeholder {
         {
            &
            default
         }
      } -
      default value( & unescapes the variable) {
            {
               descriptionPlaceholder
            }
         } - [description] placeholder {
            {
               /kwargs}}

               {
                  {
                     #exceptions
                  }
               } - iterate over exceptions {
                  {
                     type
                  }
               } - exception type {
                  {
                     descriptionPlaceholder
                  }
               } - [description] placeholder {
                  {
                     /exceptions}}

                     {
                        {
                           #yields
                        }
                     } - iterate over yields {
                        {
                           typePlaceholder
                        }
                     } - [type] placeholder {
                        {
                           descriptionPlaceholder
                        }
                     } - [description] placeholder {
                        {
                           /yields}}

                           {
                              {
                                 #returns
                              }
                           } - iterate over returns {
                              {
                                 typePlaceholder
                              }
                           } - [type] placeholder {
                              {
                                 descriptionPlaceholder
                              }
                           } - [description] placeholder {
                              {
                                 /returns}}

Additional Sections

{
   {
      #argsExist
   }
} - display contents
if args exist {
   {
      /argsExist}}

      {
         {
            #kwargsExist
         }
      } - display contents
      if kwargs exist {
         {
            /kwargsExist}}

            {
               {
                  #parametersExist
               }
            } - display contents
            if args or kwargs exist {
               {
                  /parametersExist}}

                  {
                     {
                        #exceptionsExist
                     }
                  } - display contents
                  if exceptions exist {
                     {
                        /exceptionsExist}}

                        {
                           {
                              #yieldsExist
                           }
                        } - display contents
                        if returns exist {
                           {
                              /yieldsExist}}

                              {
                                 {
                                    #returnsExist
                                 }
                              } - display contents
                              if returns exist {
                                 {
                                    /returnsExist}}

                                    {
                                       {
                                          #placeholder
                                       }
                                    } - makes contents a placeholder {
                                       {
                                          /placeholder}}

1. init

class AnyClass:
   def __init__():
   print("Init called on its own")
obj = AnyClass()

The output of the above code will be given below. Note how we did not call the init method and it got invoked as we created an object for class AnyClass.

Init called on its own

Let’s move to some other example, add gives us the ability to access the built in syntax feature of the character +. Let’s see how,

class AnyClass:
   def __init__(self,
      var):
   self.some_var =
   var
      def __add__(self, other_obj):
      print("Calling the add method")
return self.some_var + other_obj.some_var
obj1 = AnyClass(5)
obj2 = AnyClass(6)
obj1 + obj2

Instead of creating a temporary variable to hold the value of the one while swapping, you can do this instead

>>> FirstName = "kalebu" >>>
   LastName = "Jordan" >>>
   FirstName, LastName = LastName, FirstName >>>
   print(FirstName, LastName)
   ('Jordan', 'kalebu')