Klimi's new dotfiles with stow.
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.

68 lines
2.8 KiB

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