11; ;; -*- 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+
219(require 'yasnippet )
320(defvar yas-text )
421
522(defvar python-split-arg-arg-regex
6- " \\ ([[:alnum:]*]+\\ )\\ (:[[:blank:]]*[ [:alpha:]]*\\ )?\\ ([[:blank:]]*=[[:blank:]]*[[:alnum:]]*\\ )?"
23+ " \\ ([[:alnum:]*]+\\ )\\ (:[[:blank:]]*\\ ([] [:alpha:][ ]*\\ )\\ ) ?\\ ([[:blank:]]*=[[:blank:]]*\\ ( [[:alnum:]]*\\ ) \\ )?"
724" 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" )
929
1030(defvar python-split-arg-separator
1131" [[:space:]]*,[[:space:]]*"
1232" Regular expression matching the separator in a list of argument." )
1333
1434(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
1740 (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+ )))
1946 (split-string arg-string python-split-arg-separator t )))
2047
2148(defun python-args-to-docstring ()
@@ -26,7 +53,9 @@ First group should give the argument name.")
2653 (formatted-args (mapconcat
2754 (lambda (x )
2855 (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+ ))
3059 args
3160 indent)))
3261 (unless (string= formatted-args " " )
@@ -36,11 +65,37 @@ First group should give the argument name.")
3665 " return docstring format for the python arguments in yas-text"
3766 (let* ((args (python-split-args yas-text))
3867 (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 " )))
4073 (formatted-params (mapconcat format-arg args " \n " ))
4174 (formatted-ret (mapconcat format-arg (list (list " out" )) " \n " )))
4275 (unless (string= formatted-params " " )
4376 (mapconcat 'identity
4477 (list " \n Parameters\n ----------" formatted-params
4578 " \n Returns\n -------" formatted-ret)
4679 " \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