EmacsでもPyCharmとかにあるようなDocstringCommentの
自動生成機能がほしかったので関数を用意してみた。
Demo
導入方法
以下のコードをinit.elに貼り付け読み込むだけ。
関数名定義の行にカーソルを置きC-c dを実行すると生成される。
;; 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)