forked from marubu/yasnippet-numpy-style
-
Notifications
You must be signed in to change notification settings - Fork 0
/
parameters
85 lines (84 loc) · 3.35 KB
/
parameters
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
# -*- mode: snippet -*-
#name : parameters
#key : parameters
#type : command
#contributor : MATSUBARA Naoya
# --
(flet ((parse-args
(count tmp-buf)
(while (and
(< count 5)
(= 1
(call-process-shell-command
(format
(concat "python << ___EOF___\n"
"%s\n"
" pass\n"
"print(%s.__code__.co_varnames)\n"
"___EOF___")
(replace-regexp-in-string
"^ *def" "def"
(buffer-substring-no-properties
(line-beginning-position)
(line-end-position (incf count))))
(replace-regexp-in-string
"^ *def *\\(.*?\\)(.*$" "\\1"
(buffer-substring-no-properties
(line-beginning-position) (line-end-position)))
)
nil
(list tmp-buf nil))))))
(form-template
(arg-list template)
(let ((count 0))
(dolist (narg arg-list template)
(setq template
(format (concat "%s${%d:%s: ${%d:type}\n"
(make-string python-indent ? )
"${%d:description}\n}")
template
(incf count) narg (incf count) (incf count)))))
(replace-regexp-in-string "}\\n}$" "}}" template))
(get-word-from-nline
(n)
(replace-regexp-in-string
"[ \t-]*" ""
(buffer-substring-no-properties
(line-beginning-position n) (line-end-position n)))))
(if (and (equal "" (get-word-from-nline 0))
(equal "parameters" (downcase (get-word-from-nline -1))))
(delete-region (line-beginning-position -1)
(line-beginning-position 1)))
(let ((indent (python-indent-calculate-indentation))
(match)
(template "Parameters\n----------\n")
(tmp-buf "*tmp sni-param*"))
(save-excursion
(while (and (re-search-backward "^[ \t]*\\(def\\|class\\)" nil t)
(setq match (match-string-no-properties 1))
(re-search-forward "[ \t]*[dc]" nil t)
(not (= (- (point) (line-beginning-position) 1)
(- indent python-indent)))))
(cond
((equal match "def")
(parse-args 0 tmp-buf)
(setq template
(form-template (split-string
(with-current-buffer tmp-buf (buffer-string))
"[()', \n]+" t)
template))
(kill-buffer tmp-buf))
((equal match "class")
(while (and (re-search-forward "^[ \t]+def +__init__" nil t)
(not (= (python-indent-calculate-levels)
indent))))
(parse-args 0 tmp-buf)
(setq template
(form-template (cdr
(split-string
(with-current-buffer tmp-buf (buffer-string))
"[()', \n]+" t))
template))
(kill-buffer tmp-buf))))
(yas-expand-snippet template nil nil '((yas-indent-line 'fixed)))
))