Docstring Comment like PyCharm in Emacs I wanted an automatic generation function, so I prepared a function.
Demo
Just paste the following code into init.el and load it. It is generated when the cursor is placed on the line of the function name definition and C-cd is executed.
;; docstring comment
(defun python-docstring-comment()
(interactive)
(let* ((begin-point (point-at-bol))
(end-point (point-at-eol))
(function-line (buffer-substring begin-point end-point))
(space (format " %s" (replace-regexp-in-string "def.*" "" function-line))))
(goto-char end-point)
(insert "\n")
(insert (format "%s\"\"\"\n" space))
(when (string-match ".*(\\(.+\\)):" function-line)
(dolist (arg (split-string (match-string 1 function-line) ","))
(if (not (equal arg "self"))
(insert (format "%s:param TYPE %s:\n" space (replace-regexp-in-string "^\\s-+\\|\\s-+$" "" arg))))))
(insert (format "%s:rtype: TYPE\n" space))
(insert (format "%s\"\"\"" space))))
(define-key python-mode-map (kbd "C-c d") 'python-docstring-comment)
Recommended Posts