Skip to content

Commit c4c52c3

Browse files
author
Daniel
committed
FIX: More relaxed regexes.
1 parent 8367da8 commit c4c52c3

File tree

1 file changed

+29
-6
lines changed

1 file changed

+29
-6
lines changed

snippets/python-mode/.yas-setup.el

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,25 @@
1919
(require 'yasnippet)
2020
(defvar yas-text)
2121

22-
(defvar python-split-arg-arg-regex
23-
"\\([[:alnum:]*]+\\)\\(:[[:blank:]]*\\([][:alpha:][]*\\)\\)?\\([[:blank:]]*=[[:blank:]]*\\([[:alnum:]]*\\)\\)?"
22+
(defvar yas-python-regex-identifier "[[:alnum:]_]+" "Simplified Python identifier.")
23+
(defvar yas-python-regex-quoted-or-identifier (concat
24+
"\\("
25+
yas-python-regex-identifier
26+
"\\)"
27+
"\\|" "\".*\""
28+
"\\|" "'.*'"
29+
)
30+
"Simplified Python identifier or quoted string.
31+
Does not work well with multiple or escaped quotes")
32+
33+
(defvar python-split-arg-regex
34+
(concat
35+
"\\(" yas-python-regex-identifier "\\)" ; name
36+
"\\(:[[:blank:]]*\\([][:alpha:]_[]*\\)\\)?" ; type
37+
"\\([[:blank:]]*=[[:blank:]]*\\("
38+
yas-python-regex-quoted-or-identifier ; default
39+
"\\)\\)?"
40+
)
2441
"Regular expression matching an argument of a python function.
2542
Groups:
2643
- 1: the argument name
@@ -37,7 +54,7 @@ Groups:
3754
The result is a list ((name, type, default), ...) of argument names, types and
3855
default values."
3956
(mapcar (lambda (x) ; organize output
40-
(when (string-match python-split-arg-arg-regex x)
57+
(when (string-match python-split-arg-regex x)
4158
(list
4259
(match-string-no-properties 1 x) ; name
4360
(match-string-no-properties 3 x) ; type
@@ -84,10 +101,10 @@ default values."
84101
(ert-deftest test-split ()
85102
"For starters, only test a single string for expected output."
86103
(should (equal
87-
(python-split-args "foo=3, bar: int = 2, baz: Optional[MyType], foobar")
88-
(list '("foo" nil "3")
104+
(python-split-args "_foo='this', bar: int = 2, baz: Optional[My_Type], foobar")
105+
(list '("_foo" nil "'this'")
89106
'("bar" "int" "2")
90-
'("baz" "Optional[MyType]" nil)
107+
'("baz" "Optional[My_Type]" nil)
91108
'("foobar" nil nil)))
92109
))
93110

@@ -96,6 +113,12 @@ default values."
96113
;; (setq yas-text "foo=3, bar: int = 2, baz: Optional[MyType], foobar")
97114
;; (split-string yas-text python-split-arg-separator t)
98115
;;
116+
;; (save-match-data
117+
;; (setq my-string "_foo: my_bar = 'this'")
118+
;; (string-match python-split-arg-regex my-string)
119+
;; (match-string 5 my-string)
120+
;; )
121+
;;
99122
;; (python-split-args yas-text)
100123
;; (python-args-to-docstring)
101124
;; (python-args-to-docstring-numpy)

0 commit comments

Comments
 (0)