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.

39 lines
1.7 KiB

5 years ago
  1. (require 'yasnippet)
  2. (defvar yas-text)
  3. (defun python-split-args (arg-string)
  4. "Split a python argument string into ((name, default)..) tuples"
  5. (mapcar (lambda (x)
  6. (split-string x "[[:blank:]]*=[[:blank:]]*" t))
  7. (split-string arg-string "[[:blank:]]*,[[:blank:]]*" t)))
  8. (defun python-args-to-docstring ()
  9. "return docstring format for the python arguments in yas-text"
  10. (let* ((indent (concat "\n" (make-string (current-column) 32)))
  11. (args (python-split-args yas-text))
  12. (max-len (if args (apply 'max (mapcar (lambda (x) (length (nth 0 x))) args)) 0))
  13. (formatted-args (mapconcat
  14. (lambda (x)
  15. (concat (nth 0 x) (make-string (- max-len (length (nth 0 x))) ? ) " -- "
  16. (if (nth 1 x) (concat "\(default " (nth 1 x) "\)"))))
  17. args
  18. indent)))
  19. (unless (string= formatted-args "")
  20. (mapconcat 'identity (list "Keyword Arguments:" formatted-args) indent))))
  21. (defun python-args-to-docstring-numpy ()
  22. "return docstring format for the python arguments in yas-text"
  23. (let* ((args (python-split-args yas-text))
  24. (format-arg (lambda(arg)
  25. (concat (nth 0 arg) " : " (if (nth 1 arg) ", optional") "\n")))
  26. (formatted-params (mapconcat format-arg args "\n"))
  27. (formatted-ret (mapconcat format-arg (list (list "out")) "\n")))
  28. (unless (string= formatted-params "")
  29. (mapconcat 'identity
  30. (list "\nParameters\n----------" formatted-params
  31. "\nReturns\n-------" formatted-ret)
  32. "\n"))))
  33. (add-hook 'python-mode-hook
  34. '(lambda () (set (make-local-variable 'yas-indent-line) 'fixed)))