I use the snippet function_docstring_numpy (shortcut: fdn) in python to define nicely documented functions. They look like this:

def flip(batch):
    """Flip a batch of images 
    Works for a single image as well as many images, using the Ellipsis object.

images : a numpy array with dimensions (num_images, height, width, num_channels)

out : an array of identical dimensions to input array, with images flipped

    return images[...,::-1,:]

I really love this snippet, although that (lack of) indentation of the Parameters and Returns sections bothers me.

The snippet (show below) call a Lisp function:

def ${1:name}($2):

I got to the function using C-x h function_docstring_numpy and opening the linked function: python-args-to-docstring-numpy, which looks like this:

(defun python-args-to-docstring-numpy ()
  "return docstring format for the python arguments in yas-text"
  (let* ((args (python-split-args yas-text))
         (format-arg (lambda(arg)
                       (concat (nth 0 arg) " : " (if (nth 1 arg) ", optional") "\n")))
         (formatted-params (mapconcat format-arg args "\n"))
         (formatted-ret (mapconcat format-arg (list (list "out")) "\n")))
    (unless (string= formatted-params "")
      (mapconcat 'identity
                 (list "\nParameters\n----------" formatted-params
                       "Returns\n-------" formatted-ret)

I managed to shift the Returns sections up a line be removing a newline symbol, but my question is:

How can I ensure the entire docstring including Parameters and Returns are indented?


I found that there is a simple setting from YASnippet itself. In the menu-bar, under YASnippet, there is an indentation sub-menu. There are three possibilities: (1) auto (2) fixed (3) none.

This can be set with the menu-bar itself or using a line like this in your init file:

(setq yas-indent-line 'auto)    ;; the other option is the value: 'fixed

Doing it in the menu bar also persists between sessions.

  • That should be 'auto and 'fixed. Symbols, not strings. – npostavs Nov 8 '17 at 12:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.