Skip to content

Commit

Permalink
Merge pull request #1343 from galaunay/Improved-argument-separator-fo…
Browse files Browse the repository at this point in the history
…r-snippets

Make snippets able to handle arguments with type annotations.
  • Loading branch information
galaunay committed Nov 15, 2020
2 parents 7531378 + d9123ce commit 165c72d
Show file tree
Hide file tree
Showing 90 changed files with 30 additions and 636 deletions.
30 changes: 18 additions & 12 deletions snippets/python-mode/.yas-setup.el
Original file line number Diff line number Diff line change
@@ -1,8 +1,18 @@
(defvar elpy-snippet-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 elpy-snippet-split-arg-separator
"[[:blank:]]*,[[:blank:]]*"
"Regular expression matching the separator in a list of argument.")

(defun elpy-snippet-split-args (arg-string)
"Split a python argument string into ((name, default)..) tuples"
"Split the 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 elpy-snippet-split-arg-arg-regex x)
(match-string-no-properties 1 x)))
(split-string arg-string elpy-snippet-split-arg-separator t)))

(defun elpy-snippet-current-method-and-args ()
"Return information on the current definition."
Expand All @@ -20,29 +30,25 @@
(buffer-substring-no-properties start end))))))
class method args)
(unless current-arglist
(setq current-arglist '(("self"))))
(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"))
(setq args (mapcar #'car current-arglist))
(list class method args)))
(list class method current-arglist)))

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

Expand Down
7 changes: 0 additions & 7 deletions snippets/python-mode/__abs__

This file was deleted.

7 changes: 0 additions & 7 deletions snippets/python-mode/__add__

This file was deleted.

7 changes: 0 additions & 7 deletions snippets/python-mode/__and__

This file was deleted.

7 changes: 0 additions & 7 deletions snippets/python-mode/__bool__

This file was deleted.

7 changes: 0 additions & 7 deletions snippets/python-mode/__call__

This file was deleted.

7 changes: 0 additions & 7 deletions snippets/python-mode/__cmp__

This file was deleted.

7 changes: 0 additions & 7 deletions snippets/python-mode/__coerce__

This file was deleted.

7 changes: 0 additions & 7 deletions snippets/python-mode/__complex__

This file was deleted.

7 changes: 0 additions & 7 deletions snippets/python-mode/__contains__

This file was deleted.

7 changes: 0 additions & 7 deletions snippets/python-mode/__del__

This file was deleted.

7 changes: 0 additions & 7 deletions snippets/python-mode/__delattr__

This file was deleted.

7 changes: 0 additions & 7 deletions snippets/python-mode/__delete__

This file was deleted.

7 changes: 0 additions & 7 deletions snippets/python-mode/__delitem__

This file was deleted.

7 changes: 0 additions & 7 deletions snippets/python-mode/__div__

This file was deleted.

7 changes: 0 additions & 7 deletions snippets/python-mode/__divmod__

This file was deleted.

9 changes: 0 additions & 9 deletions snippets/python-mode/__enter__

This file was deleted.

7 changes: 0 additions & 7 deletions snippets/python-mode/__eq__

This file was deleted.

7 changes: 0 additions & 7 deletions snippets/python-mode/__exit__

This file was deleted.

7 changes: 0 additions & 7 deletions snippets/python-mode/__float__

This file was deleted.

7 changes: 0 additions & 7 deletions snippets/python-mode/__floordiv__

This file was deleted.

7 changes: 0 additions & 7 deletions snippets/python-mode/__ge__

This file was deleted.

7 changes: 0 additions & 7 deletions snippets/python-mode/__get__

This file was deleted.

7 changes: 0 additions & 7 deletions snippets/python-mode/__getattr__

This file was deleted.

7 changes: 0 additions & 7 deletions snippets/python-mode/__getattribute__

This file was deleted.

7 changes: 0 additions & 7 deletions snippets/python-mode/__getitem__

This file was deleted.

7 changes: 0 additions & 7 deletions snippets/python-mode/__gt__

This file was deleted.

7 changes: 0 additions & 7 deletions snippets/python-mode/__hash__

This file was deleted.

7 changes: 0 additions & 7 deletions snippets/python-mode/__hex__

This file was deleted.

7 changes: 0 additions & 7 deletions snippets/python-mode/__iadd__

This file was deleted.

7 changes: 0 additions & 7 deletions snippets/python-mode/__iand__

This file was deleted.

7 changes: 0 additions & 7 deletions snippets/python-mode/__idiv__

This file was deleted.

7 changes: 0 additions & 7 deletions snippets/python-mode/__ifloordiv__

This file was deleted.

7 changes: 0 additions & 7 deletions snippets/python-mode/__ilshift__

This file was deleted.

7 changes: 0 additions & 7 deletions snippets/python-mode/__imod__

This file was deleted.

7 changes: 0 additions & 7 deletions snippets/python-mode/__imul__

This file was deleted.

7 changes: 0 additions & 7 deletions snippets/python-mode/__index__

This file was deleted.

10 changes: 0 additions & 10 deletions snippets/python-mode/__init__

This file was deleted.

7 changes: 0 additions & 7 deletions snippets/python-mode/__instancecheck__

This file was deleted.

7 changes: 0 additions & 7 deletions snippets/python-mode/__int__

This file was deleted.

7 changes: 0 additions & 7 deletions snippets/python-mode/__invert__

This file was deleted.

7 changes: 0 additions & 7 deletions snippets/python-mode/__ior__

This file was deleted.

7 changes: 0 additions & 7 deletions snippets/python-mode/__ipow__

This file was deleted.

0 comments on commit 165c72d

Please sign in to comment.