1
1
; ;; -*- lexical-binding : t -*-
2
+
3
+ ; Copyright (C) miscellaneous contributors, see git history
4
+ ; Copyright (C) 2024 Daniel Hornung <[email protected] >
5
+ ;
6
+ ; This program is free software: you can redistribute it and/or modify
7
+ ; it under the terms of the GNU General Public License as
8
+ ; published by the Free Software Foundation, either version 3 of the
9
+ ; License, or (at your option) any later version.
10
+ ;
11
+ ; This program is distributed in the hope that it will be useful,
12
+ ; but WITHOUT ANY WARRANTY; without even the implied warranty of
13
+ ; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
+ ; GNU General Public License for more details.
15
+ ;
16
+ ; You should have received a copy of the GNU General Public License
17
+ ; along with this program. If not, see <https://www.gnu.org/licenses/>.
18
+
2
19
(require 'yasnippet )
3
20
(defvar yas-text )
4
21
5
22
(defvar python-split-arg-arg-regex
6
- " \\ ([[:alnum:]*]+\\ )\\ (:[[:blank:]]*[ [:alpha:]]*\\ )?\\ ([[:blank:]]*=[[:blank:]]*[[:alnum:]]*\\ )?"
23
+ " \\ ([[:alnum:]*]+\\ )\\ (:[[:blank:]]*\\ ([] [:alpha:][ ]*\\ )\\ ) ?\\ ([[:blank:]]*=[[:blank:]]*\\ ( [[:alnum:]]*\\ ) \\ )?"
7
24
" Regular expression matching an argument of a python function.
8
- First group should give the argument name." )
25
+ Groups:
26
+ - 1: the argument name
27
+ - 3: the type
28
+ - 5: the default value" )
9
29
10
30
(defvar python-split-arg-separator
11
31
" [[:space:]]*,[[:space:]]*"
12
32
" Regular expression matching the separator in a list of argument." )
13
33
14
34
(defun python-split-args (arg-string )
15
- " Split a python argument string ARG-STRING into a tuple of argument names."
16
- (mapcar (lambda (x )
35
+ " Split python argument string ARG-STRING.
36
+
37
+ The result is a list ((name, type, default), ...) of argument names, types and
38
+ default values."
39
+ (mapcar (lambda (x ) ; organize output
17
40
(when (string-match python-split-arg-arg-regex x)
18
- (match-string-no-properties 1 x)))
41
+ (list
42
+ (match-string-no-properties 1 x) ; name
43
+ (match-string-no-properties 3 x) ; type
44
+ (match-string-no-properties 5 x) ; default
45
+ )))
19
46
(split-string arg-string python-split-arg-separator t )))
20
47
21
48
(defun python-args-to-docstring ()
@@ -26,7 +53,9 @@ First group should give the argument name.")
26
53
(formatted-args (mapconcat
27
54
(lambda (x )
28
55
(concat (nth 0 x) (make-string (- max-len (length (nth 0 x))) ? ) " -- "
29
- (if (nth 1 x) (concat " \( default " (nth 1 x) " \) " ))))
56
+ (if (nth 1 x) (concat (nth 1 x) " : " ))
57
+ (if (nth 2 x) (concat " \( default " (nth 2 x) " \) " ))
58
+ ))
30
59
args
31
60
indent)))
32
61
(unless (string= formatted-args " " )
@@ -36,11 +65,37 @@ First group should give the argument name.")
36
65
" return docstring format for the python arguments in yas-text"
37
66
(let* ((args (python-split-args yas-text))
38
67
(format-arg (lambda (arg )
39
- (concat (nth 0 arg) " : " (if (nth 1 arg) " , optional" ) " \n " )))
68
+ (concat (nth 0 arg) " : " ; name
69
+ (if (nth 1 arg) (nth 1 arg)) ; type TODO handle Optional[Foo] correctly
70
+ (if (nth 2 arg) (concat (when (nth 1 arg) " , " )
71
+ " default=" (nth 2 arg))) ; default
72
+ " \n " )))
40
73
(formatted-params (mapconcat format-arg args " \n " ))
41
74
(formatted-ret (mapconcat format-arg (list (list " out" )) " \n " )))
42
75
(unless (string= formatted-params " " )
43
76
(mapconcat 'identity
44
77
(list " \n Parameters\n ----------" formatted-params
45
78
" \n Returns\n -------" formatted-ret)
46
79
" \n " ))))
80
+
81
+
82
+ ; ; Tests
83
+
84
+ (ert-deftest test-split ()
85
+ " For starters, only test a single string for expected output."
86
+ (should (equal
87
+ (python-split-args " foo=3, bar: int = 2, baz: Optional[MyType], foobar" )
88
+ (list '(" foo" nil " 3" )
89
+ '(" bar" " int" " 2" )
90
+ '(" baz" " Optional[MyType]" nil )
91
+ '(" foobar" nil nil )))
92
+ ))
93
+
94
+ ; ; For manual testing and development:
95
+
96
+ ; ; (setq yas-text "foo=3, bar: int = 2, baz: Optional[MyType], foobar")
97
+ ; ; (split-string yas-text python-split-arg-separator t)
98
+ ; ;
99
+ ; ; (python-split-args yas-text)
100
+ ; ; (python-args-to-docstring)
101
+ ; ; (python-args-to-docstring-numpy)
0 commit comments