]>
Commit | Line | Data |
---|---|---|
53e6db90 DC |
1 | (defvar elpy-snippet-split-arg-arg-regex |
2 | "\\([[:alnum:]*]+\\)\\(:[[:blank:]]*[[:alpha:]]*\\)?\\([[:blank:]]*=[[:blank:]]*[[:alnum:]]*\\)?" | |
3 | "Regular expression matching an argument of a python function. | |
4 | First group should give the argument name.") | |
5 | ||
6 | (defvar elpy-snippet-split-arg-separator | |
7 | "[[:blank:]]*,[[:blank:]]*" | |
8 | "Regular expression matching the separator in a list of argument.") | |
9 | ||
10 | (defun elpy-snippet-split-args (arg-string) | |
11 | "Split the python argument string ARG-STRING into a tuple of argument names." | |
12 | (mapcar (lambda (x) | |
13 | (when (string-match elpy-snippet-split-arg-arg-regex x) | |
14 | (match-string-no-properties 1 x))) | |
15 | (split-string arg-string elpy-snippet-split-arg-separator t))) | |
16 | ||
17 | (defun elpy-snippet-current-method-and-args () | |
18 | "Return information on the current definition." | |
19 | (let ((current-defun (python-info-current-defun)) | |
20 | (current-arglist | |
21 | (save-excursion | |
22 | (python-nav-beginning-of-defun) | |
23 | (when (re-search-forward "(" nil t) | |
24 | (let* ((start (point)) | |
25 | (end (progn | |
26 | (forward-char -1) | |
27 | (forward-sexp) | |
28 | (- (point) 1)))) | |
29 | (elpy-snippet-split-args | |
30 | (buffer-substring-no-properties start end)))))) | |
31 | class method args) | |
32 | (unless current-arglist | |
33 | (setq current-arglist '("self"))) | |
34 | (if (and current-defun | |
35 | (string-match "^\\(.*\\)\\.\\(.*\\)$" current-defun)) | |
36 | (setq class (match-string 1 current-defun) | |
37 | method (match-string 2 current-defun)) | |
38 | (setq class "Class" | |
39 | method "method")) | |
40 | (list class method current-arglist))) | |
41 | ||
42 | (defun elpy-snippet-init-assignments (arg-string) | |
43 | "Return the typical __init__ assignments for arguments in ARG-STRING." | |
44 | (let ((indentation (make-string (save-excursion | |
45 | (goto-char start-point) | |
46 | (current-indentation)) | |
47 | ?\s))) | |
48 | (mapconcat (lambda (arg) | |
49 | (if (string-match "^\\*" arg) | |
50 | "" | |
51 | (format "self.%s = %s\n%s" arg arg indentation))) | |
52 | (elpy-snippet-split-args arg-string) | |
53 | ""))) | |
54 | ||
55 | (defun elpy-snippet-super-form () | |
56 | "Return (Class, first-arg).method if Py2. | |
57 | Else return ().method for Py3." | |
58 | (let* ((defun-info (elpy-snippet-current-method-and-args)) | |
59 | (class (nth 0 defun-info)) | |
60 | (method (nth 1 defun-info)) | |
61 | (args (nth 2 defun-info)) | |
62 | (first-arg (nth 0 args)) | |
63 | (py-version-command " -c 'import sys ; print(sys.version_info.major)'") | |
64 | ;; Get the python version. Either 2 or 3 | |
65 | (py-version-num (substring (shell-command-to-string (concat elpy-rpc-python-command py-version-command))0 1))) | |
66 | (if (string-match py-version-num "2") | |
67 | (format "(%s, %s).%s" class first-arg method) | |
68 | (format "().%s" method)))) | |
69 | ||
70 | (defun elpy-snippet-super-arguments () | |
71 | "Return the argument list for the current method." | |
72 | (mapconcat (lambda (x) x) | |
73 | (cdr (nth 2 (elpy-snippet-current-method-and-args))) | |
74 | ", ")) |