Personal emacs config
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

74 lines
3.1 KiB

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