Skip to content

Merge elpy snippets #278

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 17 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
94 changes: 83 additions & 11 deletions snippets/python-mode/.yas-setup.el
Original file line number Diff line number Diff line change
@@ -1,23 +1,33 @@
(require 'yasnippet)
(defvar yas-text)

(defvar python-split-arg-arg-regex
"\\([[:alnum:]*]+\\)\\(:[[:blank:]]*[[:alpha:]]*\\)?\\([[:blank:]]*=[[:blank:]]*[[:alnum:]]*\\)?"
"Regular expression matching an argument of a python function.
First group should give the argument name.")

(defvar python-split-arg-separator
"[[:space:]]*,[[:space:]]*"
"Regular expression matching the separator in a list of argument.")

(defun python-split-args (arg-string)
"Split a python argument string into ((name, default)..) tuples"
"Split a python argument string ARG-STRING into a tuple of argument names."
(mapcar (lambda (x)
(split-string x "[[:blank:]]*=[[:blank:]]*" t))
(split-string arg-string "[[:blank:]]*,[[:blank:]]*" t)))
(when (string-match python-split-arg-arg-regex x)
(match-string-no-properties 1 x)))
(split-string arg-string python-split-arg-separator t)))

(defun python-args-to-docstring ()
"return docstring format for the python arguments in yas-text"
"Return docstring format for the python arguments in yas-text."
(let* ((indent (concat "\n" (make-string (current-column) 32)))
(args (python-split-args yas-text))
(max-len (if args (apply 'max (mapcar (lambda (x) (length (nth 0 x))) args)) 0))
(formatted-args (mapconcat
(lambda (x)
(concat (nth 0 x) (make-string (- max-len (length (nth 0 x))) ? ) " -- "
(if (nth 1 x) (concat "\(default " (nth 1 x) "\)"))))
args
indent)))
(lambda (x)
(concat (nth 0 x) (make-string (- max-len (length (nth 0 x))) ? ) " -- "
(if (nth 1 x) (concat "\(default " (nth 1 x) "\)"))))
args
indent)))
(unless (string= formatted-args "")
(mapconcat 'identity (list "Keyword Arguments:" formatted-args) indent))))

Expand All @@ -34,6 +44,68 @@
"\nReturns\n-------" formatted-ret)
"\n"))))

(defun yas-snips-snippet-current-method-and-args ()
"Return information on the current definition."
(let ((current-defun (python-info-current-defun))
(current-arglist
(save-excursion
(python-nav-beginning-of-defun)
(when (re-search-forward "(" nil t)
(let* ((start (point))
(end (progn
(forward-char -1)
(forward-sexp)
(- (point) 1))))
(yas-snips-snippet-split-args
(buffer-substring-no-properties start end))))))
class method args)
(when (not current-arglist)
(setq current-arglist '("self")))
(if (and current-defun
(string-match "^\\(.*\\)\\.\\(.*\\)$" current-defun))
(setq class (match-string 1 current-defun)
method (match-string 2 current-defun))
(setq class "Class"
method "method"))
(list class method current-arglist)))

(defun yas-snips-snippet-init-assignments (arg-string)
"Return the typical __init__ assignments for arguments in ARG-STRING."
(let ((indentation (make-string (current-indentation)
?\s)))
(mapconcat (lambda (arg)
(if (string-match "^\\*" arg)
""
(format "self.%s = %s\n%s" arg arg indentation)))
(yas-snips-snippet-split-args arg-string)
"")))

(defun yas-snips-snippet-super-form ()
"Return (Class, first-arg).method if Py2.
Else return ().method for Py3."
(let* ((defun-info (yas-snips-snippet-current-method-and-args))
(class (nth 0 defun-info))
(method (nth 1 defun-info))
(args (nth 2 defun-info))
(first-arg (nth 0 args))
(py-version-command " -c 'import sys ; print(sys.version_info.major)'")
;; Check for distribution-defined or user-defined python-interpreter.
;; If python-interpreter is not set, then py-version-command
;; will always return "2" for "python" on PEP 394-compliant
;; systems.
(if (fboundp 'python-interpreter)
(py-version-num (substring (shell-command-to-string (concat python-interpreter py-version-command)) 0 1))
;; Otherwise get the version from "python" in $PATH
;; This preserves support for virtualenvs that may have
;; either version 2 or 3 installed as "python"--and also
;; for distributions that install Python 3 to $PATH/python
(py-version-num (substring (shell-command-to-string (concat "python" py-version-command)) 0 1))))
(if (string-match py-version-num "2")
(format "(%s, %s).%s" class first-arg method)
(format "().%s" method))))

(add-hook 'python-mode-hook
'(lambda () (set (make-local-variable 'yas-indent-line) 'fixed)))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't see a mention of this removal in the commit messages. Is it an accident?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The silent removal is accidental. I seem to remember seeing this add-hook while ediffing, and thinking something like "that's bad style and should be done differently elsewhere", and so I think I must have pressed b and let it get overwritten...which is arguably a mistake of laziness... IIRC it is possible to inherit python-mode's indentation style, which should already be 'fixed, without using a hook.

(defun yas-snips-snippet-super-arguments ()
"Return the argument list for the current method."
(mapconcat (lambda (x) x)
(cdr (nth 2 (yas-snips-snippet-current-method-and-args)))
", "))
7 changes: 7 additions & 0 deletions snippets/python-mode/__abs__
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# -*- mode: snippet -*-
# name: __abs__
# key: _abs
# group: Special methods
# --
def __abs__(self):
return $0
7 changes: 7 additions & 0 deletions snippets/python-mode/__add__
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# -*- mode: snippet -*-
# name: __add__
# key: _add
# group: Special methods
# --
def __add__(self, other):
return $0
7 changes: 7 additions & 0 deletions snippets/python-mode/__and__
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# -*- mode: snippet -*-
# name: __and__
# key: _and
# group: Special methods
# --
def __and__(self, other):
return $0
7 changes: 7 additions & 0 deletions snippets/python-mode/__bool__
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# -*- mode: snippet -*-
# name: __bool__
# key: _bool
# group: Special methods
# --
def __bool__(self):
return $0
7 changes: 7 additions & 0 deletions snippets/python-mode/__call__
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# -*- mode: snippet -*-
# name: __call__
# key: _call
# group: Special methods
# --
def __call__(self, ${1:*args}):
return $0
7 changes: 7 additions & 0 deletions snippets/python-mode/__cmp__
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# -*- mode: snippet -*-
# name: __cmp__
# key: _cmp
# group: Special methods
# --
def __cmp__(self, other):
return $0
7 changes: 7 additions & 0 deletions snippets/python-mode/__coerce__
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# -*- mode: snippet -*-
# name: __coerce__
# key: _coerce
# group: Special methods
# --
def __coerce__(self, other):
return $0
7 changes: 7 additions & 0 deletions snippets/python-mode/__complex__
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# -*- mode: snippet -*-
# name: __complex__
# key: _complex
# group: Special methods
# --
def __complex__(self):
return $0
8 changes: 4 additions & 4 deletions snippets/python-mode/__contains__
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# -*- mode: snippet -*-
# name: __contains__
# key: cont
# group: dunder methods
# key: _contains
# group: Special methods
# --
def __contains__(self, el):
$0
def __contains__(self, item):
return $0
7 changes: 7 additions & 0 deletions snippets/python-mode/__del__
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# -*- mode: snippet -*-
# name: __del__
# key: _del
# group: Special methods
# --
def __del__(self):
$0
7 changes: 7 additions & 0 deletions snippets/python-mode/__delattr__
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# -*- mode: snippet -*-
# name: __delattr__
# key: _delattr
# group: Special methods
# --
def __delattr__(self, name):
$0
7 changes: 7 additions & 0 deletions snippets/python-mode/__delete__
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# -*- mode: snippet -*-
# name: __delete__
# key: _delete
# group: Special methods
# --
def __delete__(self, instance):
$0
7 changes: 7 additions & 0 deletions snippets/python-mode/__delitem__
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# -*- mode: snippet -*-
# name: __delitem__
# key: _delitem
# group: Special methods
# --
def __delitem__(self, key):
$0
7 changes: 7 additions & 0 deletions snippets/python-mode/__div__
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# -*- mode: snippet -*-
# name: __div__
# key: _div
# group: Special methods
# --
def __div__(self, other):
return $0
7 changes: 7 additions & 0 deletions snippets/python-mode/__divmod__
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# -*- mode: snippet -*-
# name: __divmod__
# key: _divmod
# group: Special methods
# --
def __divmod__(self, other):
return $0
6 changes: 3 additions & 3 deletions snippets/python-mode/__enter__
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
# -*- mode: snippet -*-
# name: __enter__
# key: ent
# group: dunder methods
# key: _enter
# group: Special methods
# --
def __enter__(self):
$0

return self
return self
7 changes: 7 additions & 0 deletions snippets/python-mode/__eq__
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# -*- mode: snippet -*-
# name: __eq__
# key: _eq
# group: Special methods
# --
def __eq__(self, other):
return $0
8 changes: 4 additions & 4 deletions snippets/python-mode/__exit__
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# -*- mode: snippet -*-
# name: __exit__
# key: ex
# group: dunder methods
# key: _exit
# group: Special methods
# --
def __exit__(self, type, value, traceback):
$0
def __exit__(self, exc_type, exc_value, traceback):
$0
7 changes: 7 additions & 0 deletions snippets/python-mode/__float__
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# -*- mode: snippet -*-
# name: __float__
# key: _float
# group: Special methods
# --
def __float__(self):
return $0
7 changes: 7 additions & 0 deletions snippets/python-mode/__floordiv__
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# -*- mode: snippet -*-
# name: __floordiv__
# key: _floordiv
# group: Special methods
# --
def __floordiv__(self, other):
return $0
7 changes: 7 additions & 0 deletions snippets/python-mode/__ge__
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# -*- mode: snippet -*-
# name: __ge__
# key: _ge
# group: Special methods
# --
def __ge__(self, other):
return $0
7 changes: 7 additions & 0 deletions snippets/python-mode/__get__
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# -*- mode: snippet -*-
# name: __get__
# key: _get
# group: Special methods
# --
def __get__(self, instance, owner):
return $0
7 changes: 7 additions & 0 deletions snippets/python-mode/__getattr__
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# -*- mode: snippet -*-
# name: __getattr__
# key: _getattr
# group: Special methods
# --
def __getattr__(self, name):
return $0
7 changes: 7 additions & 0 deletions snippets/python-mode/__getattribute__
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# -*- mode: snippet -*-
# name: __getattribute__
# key: _getattr
# group: Special methods
# --
def __getattribute__(self, name):
return $0
8 changes: 4 additions & 4 deletions snippets/python-mode/__getitem__
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# -*- mode: snippet -*-
# name: __getitem__
# key: getit
# group: dunder methods
# key: _getitem
# group: Special methods
# --
def __getitem__(self, ${1:key}):
$0
def __getitem__(self, key):
return $0
7 changes: 7 additions & 0 deletions snippets/python-mode/__gt__
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# -*- mode: snippet -*-
# name: __gt__
# key: _gt
# group: Special methods
# --
def __gt__(self, other):
return $0
7 changes: 7 additions & 0 deletions snippets/python-mode/__hash__
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# -*- mode: snippet -*-
# name: __hash__
# key: _hash
# group: Special methods
# --
def __hash__(self):
return $0
7 changes: 7 additions & 0 deletions snippets/python-mode/__hex__
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# -*- mode: snippet -*-
# name: __hex__
# key: _hex
# group: Special methods
# --
def __hex__(self):
return $0
7 changes: 7 additions & 0 deletions snippets/python-mode/__iadd__
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# -*- mode: snippet -*-
# name: __iadd__
# key: _iadd
# group: Special methods
# --
def __iadd__(self, other):
return $0
7 changes: 7 additions & 0 deletions snippets/python-mode/__iand__
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# -*- mode: snippet -*-
# name: __iand__
# key: _iand
# group: Special methods
# --
def __iand__(self, other):
return $0
7 changes: 7 additions & 0 deletions snippets/python-mode/__idiv__
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# -*- mode: snippet -*-
# name: __idiv__
# key: _idiv
# group: Special methods
# --
def __idiv__(self, other):
return $0
7 changes: 7 additions & 0 deletions snippets/python-mode/__ifloordiv__
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# -*- mode: snippet -*-
# name: __ifloordiv__
# key: _ifloordiv
# group: Special methods
# --
def __ifloordiv__(self, other):
return $0
7 changes: 7 additions & 0 deletions snippets/python-mode/__ilshift__
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# -*- mode: snippet -*-
# name: __ilshift__
# key: _ilshift
# group: Special methods
# --
def __ilshift__(self, other):
return $0
Loading