branch: elpa/smartparens commit ec15aaa748b21c9b8453dd95604ccc95fc1138cf Author: Matus Goljer <matus.gol...@gmail.com> Commit: Matus Goljer <matus.gol...@gmail.com>
feat(python): add auto-colon insert on async def and class See #1075 --- smartparens-python.el | 25 +++++++++++++++++++++---- test/smartparens-python-test.el | 12 ++++++++++++ 2 files changed, 33 insertions(+), 4 deletions(-) diff --git a/smartparens-python.el b/smartparens-python.el index 4ad3cf2bd7..93698ce572 100644 --- a/smartparens-python.el +++ b/smartparens-python.el @@ -52,8 +52,13 @@ :group 'smartparens) (defcustom sp-python-insert-colon-in-function-definitions t - "Whether to auto-insert a colon when parens insertion is -triggered while typing a function definition" + "If non-nil, auto-insert a colon after parens insertion in definition. + +Supported definitions are: + +- def +- async def +- class" :group 'smartparens-python :type 'boolean) @@ -64,14 +69,26 @@ triggered while typing a function definition" (add-to-list 'sp-sexp-suffix (list it 'regexp ""))) (defun sp-python-maybe-add-colon-python (_id action _context) - "Adds a colon after parentheses in a python function definition" + "Adds a colon after parentheses in a python definition. + +Works for (async) def forms and class forms. + +See also the option `sp-python-insert-colon-in-function-definitions'." ;; here, caret supposed to be in between parens, i.e. (|) (when (and sp-python-insert-colon-in-function-definitions (eq action 'insert) (looking-at ")\\s-*$") (save-excursion (goto-char (line-beginning-position)) - (re-search-forward "^\\s-*def\\b" (line-end-position)))) + (re-search-forward + (rx bol + (* (syntax whitespace)) + (? "async") + (* (syntax whitespace)) + (or "def" "class") + word-boundary) + (line-end-position) + t))) (save-excursion (forward-char) ;; skip closing paren (insert ":")))) diff --git a/test/smartparens-python-test.el b/test/smartparens-python-test.el index 42cf176d7b..a08ef63a4d 100644 --- a/test/smartparens-python-test.el +++ b/test/smartparens-python-test.el @@ -122,3 +122,15 @@ Make sure to skip even in inactive sexps." (sp-test--python-mode) (sp-rewrap-sexp '("'" . "'")) (sp-buffer-equals "'''f|oo'''"))) + +(ert-deftest sp-test-python-autoinsert-colon-on-def () + (sp-test-with-temp-buffer "def foo|" + (sp-test--python-mode) + (execute-kbd-macro (kbd "(")) + (sp-buffer-equals "def foo(|):"))) + +(ert-deftest sp-test-python-autoinsert-colon-on-class () + (sp-test-with-temp-buffer "class Foo|" + (sp-test--python-mode) + (execute-kbd-macro (kbd "(")) + (sp-buffer-equals "class Foo(|):")))