-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpython-docstring.el
More file actions
237 lines (200 loc) · 7.39 KB
/
python-docstring.el
File metadata and controls
237 lines (200 loc) · 7.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
;;; python-docstring.el --- Insert a python docstring -*- lexical-binding: t; -*-
;;
;; Copyright (C) 2020 Alex Day
;;
;; Author: Alex Day <http://github/AlexanderDavid>
;; Maintainer: Alex Day <alexday135@gmail.com>
;; Created: May 11, 2020
;; Modified: May 11, 2020
;; Version: 0.0.1
;; Keywords: Python, Docstring, Google-Style
;; Homepage: https://github.com/alexexanderdavid/python-docstring
;; Package-Requires: ((emacs 26.3) (cl-lib "0.5"))
;;
;; This file is not part of GNU Emacs.
;;
;;; Commentary:
;;
;; To use this file, put something like the following in your ~/.emacs:
;;
;; (add-to-list 'load-path /path/to/python-docstring)
;; (require 'python-docstring)
;;
;; And then bind the (python-docstring/generate-docstring) to a certain
;; key like so:
;;
;; (global-set-key (kbd "<f9>") 'python-docstring/generate-docstring)
;;
;; This function can be called when the cursor is over a python function
;; header. It will produce a google-ish style docstring.
;;
;;; To Do:
;; - Find way to prompt user to enter data with tab stuff
;;
;;; Code:
;; Struct to store information for one argument
(cl-defstruct python-docstring/--arg name type def)
(defun python-docstring/get-function-header ()
"Find the function header at the current cursor position."
;; Search backwards to the def and then forwards to the :
(let ((start (search-backward "def"))
(end (re-search-forward "\).*:")))
;; Replace newlines and whitespace with blanks and return the
;; substring
(replace-regexp-in-string "\n" ""
(replace-regexp-in-string "\s*" ""
(buffer-substring start end))))
)
(defun python-docstring/get-function-indentation ()
"Get the indentation of the current function."
(let ((start (search-backward "def")))
(beginning-of-line)
(- start (point))
)
)
(defun python-docstring/--get-function-name (header)
"Given a function HEADER return the function's name."
;; Match the regex that contains def[ANYTHING](. The ANYTHING
;; should contain the function name
(string-match "def.*\("
header)
;; Return the substring that contains the function name
(substring (match-string 0 header) 4 -1)
)
(defun python-docstring/--get-function-return-type (header)
"Get the return type from the HEADER if one exists."
;; When the string contains -> return the type
(when (string-match "->" header)
(substring header (match-end 0) -1)
)
)
(defun python-docstring/--argument-builder (arg-text)
"Build an argument struct from an ARG-TEXT string."
(if (string-match ":.*=" arg-text)
(let ((args (split-string (replace-regexp-in-string "=" ":"
(replace-regexp-in-string " " "" arg-text)) ":")))
(make-python-docstring/--arg
:name (car args)
:type (car (cdr args))
:def (car (cdr (cdr args)))))
(if (string-match ":" arg-text)
(let ((args (split-string arg-text ":")))
(make-python-docstring/--arg
:name (car args)
:type (car (cdr args))))
(if (string-match "=" arg-text)
(let ((args (split-string arg-text "=")))
(make-python-docstring/--arg
:name (car args)
:def (car (cdr args))))
(make-python-docstring/--arg
:name arg-text))))
)
(defun python-docstring/--get-function-args (header)
"Given a function HEADER (from --get-function-header) return all args, types, and default values."
;; Match inbetween the parenthesis to get the args
(string-match "\(.*\)"
header)
;; Define the arguments as the arguments from the header split by commas with optional
;; spaces
(let* ((arguments-text (substring (match-string 0 header) 1 -1))
(arguments (split-string arguments-text ",\s?")))
;; Only proceed if arguments is not an empty string
(if (not (= (length arguments-text) 0))
;; Apply the argument-builder to each argument string
(mapcar 'python-docstring/--argument-builder arguments)))
)
(defun python-docstring/--print-arg (arg indentation)
"Print the given ARG for a docstring with a given INDENTATION.
This will print the comment in the Google docstring style:
arg_name (arg_type, optional: arg_default_value)"
;; Only print the string if it is not the "self" argument
(when (not (string= (python-docstring/--arg-name arg) "self"))
;; Indent
(insert indentation)
(insert indentation)
(insert indentation)
;; Insert the argument name
(insert (python-docstring/--arg-name arg))
;; Check for either a type or a default value
(when (or
(python-docstring/--arg-def arg)
(python-docstring/--arg-type arg))
;; Insert the paren for the type/default value
(insert " (")
;; If there is a type then return the type
(if (python-docstring/--arg-type arg)
(insert (python-docstring/--arg-type arg)))
;; Print the comma if there is both a default argument and
;; a type
(if (and
(python-docstring/--arg-def arg)
(python-docstring/--arg-type arg))
(insert ", "))
;; If there is a default argument then print that
(when (python-docstring/--arg-def arg)
(insert "default: ")
(insert (python-docstring/--arg-def arg)))
;; Close the paren
(insert ")"))
;; Insert the : and go to the next line
(insert ":\n"))
)
(defun python-docstring/generate-docstring ()
"Generate documentation for function under cursor."
(interactive)
;; Get information about the function
(let* ((header (python-docstring/get-function-header))
(args (python-docstring/--get-function-args header))
(return-type (python-docstring/--get-function-return-type header))
(indentation (make-string (python-docstring/get-function-indentation) ?\s)))
;; Go to the last char of the function
(re-search-forward "\).*:")
;; Go down one line
(forward-line)
;; Insert the start of the docstring and a FIXME for the comment
(insert indentation)
(insert indentation)
(insert "\"\"\" FIXME: Insert function comment\n")
;; If there are arguments then print them
(unless (eq args nil)
;; Insert the start of the argument list
(insert indentation)
(insert indentation)
(insert "Args:\n")
;; Map the print function to each argument with the specified indentation
(mapc (lambda (arg)
(python-docstring/--print-arg arg indentation)) args))
;; Print the return type if one exists
(when return-type
;; Print the returns header
(insert "\n")
(insert indentation)
(insert indentation)
(insert "Returns:\n")
;; Indent the return type
(insert indentation)
(insert indentation)
(insert indentation)
;; Print the return type
(insert return-type)
(insert ":\n")
)
;; Print the ending comment
(insert indentation)
(insert indentation)
(insert "\"\"\"\n")
;; Search back to the beginning and put the mark over the F in FIXME
(forward-line -1)
(search-backward "\"\"\"")
(forward-char 4)
)
)
(define-minor-mode python-docstring-minor-mode
"Minor mode for inserting python docstring in google style."
:lighter " docstring"
:keymap (let ((map (make-sparse-keymap)))
(define-key map (kbd "<f9>") 'python-docstring/generate-docstring)
map))
(provide 'python-docstring)
;;; python-docstring.el ends here