Skip to content

Commit

Permalink
Merge elpy snippets (again) (#465)
Browse files Browse the repository at this point in the history
* elpy-initialize-variables: Add the Elpy snippet directory.

Elpy now ships with a few YASnippet snippets for Python.

Fixes #43.

* Small rework of the snippets.

The enter and exit snippets now conform to the yasnippet-snippets
repository (mostly), while super now makes use of the .yas-setup.el
mechanism and is seriously improved. There's a new snippet for
__init__ methods, too, which automatically assigns arguments to
instance variables.

* Rework and expand the snippet collection.

* Add __bool__ snippet.

* updated yasnippet variable 'text' to 'yas-text'

* Return correct form of "super" depending on Python version

Previously when using the yasnippet "super", it will always return
"(class, arg).method" which was the Py2 way of doing it. In Py3, you
could simply do "().method" and Python will understand.

* Move yasnippet-snippets onto their own branch

* PR #259 Change keys for __foo__ from "foo" to "_foo"

Thanks to galaunay for the idea to use this to "avoid unnecessary
keyspace pollution."

* Fix bug due to passage from elpy to yasnippet

* Use snippet expansion for foo -> __foo__

* Fix bug when specifying arguments on several lines

* Allow detection of Python 3 on PEP 394-compliant systems.

Please note that python-interpreter must be set to "python3" for this
to function correctly.

As a side-effect it also allows the use of Python 2 on systems that
install version 3 to /usr/bin/python and version 2 to
/usr/bin/python2.

* Change elpy prefixes to yas-snips

Thanks to Andrea Crotti for the review and suggestion.

* Add newline at end of file for all files that were missing one.

* Make python snippets able to handle arguments with type annotations.

* Use start-point from yas--apply-transform in snippet-init-assignment

yas-snips-snippet-init-assignments should use values provided by
yas--apply-transform instead of grabbing them itself.

Thanks to npostavs for finding this issue.  See review discussion at
PR #278 for more info (search for "yas--apply-transform").

* Removing Python 2 snippets, it's EOL since 2020-01-01.

* Some consistenty in group names.

* Removing a duplicate.

* Owner is documented as optional.

cf. https://docs.python.org/3/reference/datamodel.html#object.__get__

* Resolve a conflict between _getattr and _getattribute

* Deduplicate: it already exists in master (named pdb).

* Dedup: already exists as __repr__

* Dedup: already exists as __str__

* fix: try is already used by ./try

* fix misleading key.

* Consistency with other dunders.

* Consistency with other naming.

* Removing super, we don't have all needed elisp yet.

* Deduplicate two keys.

* s not needed.

* The hook does not works for me.

* Add few missing newlines at end of file.

---------

Co-authored-by: Jorgen Schaefer <[email protected]>
Co-authored-by: Jorgen Schaefer <[email protected]>
Co-authored-by: Daniel Wu <[email protected]>
Co-authored-by: Daniel Gopar <[email protected]>
Co-authored-by: Nicholas D Steeves <[email protected]>
Co-authored-by: galaunay <[email protected]>
  • Loading branch information
7 people authored Jan 5, 2024
1 parent 12103bb commit 9f67370
Show file tree
Hide file tree
Showing 152 changed files with 654 additions and 148 deletions.
31 changes: 19 additions & 12 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 @@ -33,6 +43,3 @@
(list "\nParameters\n----------" formatted-params
"\nReturns\n-------" formatted-ret)
"\n"))))


(add-hook 'python-mode-hook #'yasnippet-snippets--fixed-indent)
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/__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
6 changes: 3 additions & 3 deletions snippets/python-mode/eq → snippets/python-mode/__eq__
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# -*- mode: snippet -*-
# name: __eq__
# key: eq
# group: dunder methods
# key: _eq
# group: Special methods
# --
def __eq__(self, other):
return self.$1 == other.$1
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=None):
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: _getattribute
# 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/__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
7 changes: 7 additions & 0 deletions snippets/python-mode/__imod__
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# -*- mode: snippet -*-
# name: __imod__
# key: _imod
# group: Special methods
# --
def __imod__(self, other):
return $0
7 changes: 7 additions & 0 deletions snippets/python-mode/__imul__
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# -*- mode: snippet -*-
# name: __imul__
# key: _imul
# group: Special methods
# --
def __imul__(self, other):
return $0
7 changes: 7 additions & 0 deletions snippets/python-mode/__index__
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# -*- mode: snippet -*-
# name: __index__
# key: _index
# group: Special methods
# --
def __index__(self):
return $0
6 changes: 3 additions & 3 deletions snippets/python-mode/init → snippets/python-mode/__init__
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# -*- mode: snippet -*-
# name: init
# key: init
# name: __init__
# key: _init
# group : definitions
# --
def __init__(self${1:, args}):
${2:"${3:docstring}"
}$0
}$0
7 changes: 7 additions & 0 deletions snippets/python-mode/__instancecheck__
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# -*- mode: snippet -*-
# name: __instancecheck__
# key: _instancecheck
# group: Special methods
# --
def __instancecheck__(self, instance):
return $0
7 changes: 7 additions & 0 deletions snippets/python-mode/__int__
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# -*- mode: snippet -*-
# name: __int__
# key: _int
# group: Special methods
# --
def __int__(self):
$0
7 changes: 7 additions & 0 deletions snippets/python-mode/__invert__
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# -*- mode: snippet -*-
# name: __invert__
# key: _invert
# group: Special methods
# --
def __invert__(self):
return $0
7 changes: 7 additions & 0 deletions snippets/python-mode/__ior__
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# -*- mode: snippet -*-
# name: __ior__
# key: _ior
# group: Special methods
# --
def __ior__(self, other):
return $0
7 changes: 7 additions & 0 deletions snippets/python-mode/__ipow__
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# -*- mode: snippet -*-
# name: __ipow__
# key: _ipow
# group: Special methods
# --
def __ipow__(self, other):
return $0
Loading

0 comments on commit 9f67370

Please sign in to comment.