Created
November 30, 2016 07:41
-
-
Save gergelypolonkai/7b062a00d3b8a2555024521273cecfee to your computer and use it in GitHub Desktop.
Add Python docstring to anywhere
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(defun gpolonkai/prog-in-string-p () | |
"Return `t' if point is inside a string." | |
(nth 3 (syntax-ppss))) | |
(defun gpolonkai/prog-in-comment-p () | |
"Return `t' if point is inside a comment." | |
(nth 4 (syntax-ppss))) | |
(defun gpolonkai/python-add-docstring () | |
"Add a Python docstring to the current thing. If point is | |
inside a function, add docstring to that. If point is in a | |
class, add docstring to that. If neither, add docstring to the | |
beginning of the file." | |
(interactive) | |
(save-restriction | |
(widen) | |
(beginning-of-defun) | |
(if (not (looking-at-p "\\(def\\|class\\) ")) | |
(progn | |
(goto-char (point-min)) | |
(back-to-indentation) | |
(forward-char) | |
(while (gpolonkai/prog-in-comment-p) | |
(forward-line) | |
(back-to-indentation) | |
(forward-char))) | |
(search-forward ":") | |
(while (or (gpolonkai/prog-in-string-p) | |
(gpolonkai/prog-in-comment-p)) | |
(search-forward ":"))) | |
(if (eq 1 (count-lines 1 (point))) | |
(open-line-above) | |
(open-line-below)) | |
(insert "\"\"\"") | |
(open-line-below) | |
(insert "\"\"\"") | |
(open-line-above))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment