Skip to content

Commit

Permalink
Detect new PEP 492 async coroutines
Browse files Browse the repository at this point in the history
  • Loading branch information
andreas-roehler committed Sep 27, 2015
1 parent 5c000f6 commit 05e65ce
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 7 deletions.
16 changes: 10 additions & 6 deletions python-components-mode.el
Expand Up @@ -2386,10 +2386,10 @@ See py-no-outdent-re-raw for better readable content ")
(defconst py-assignment-re "\\_<\\w+\\_>[ \t]*\\(=\\|+=\\|*=\\|%=\\|&=\\|^=\\|<<=\\|-=\\|/=\\|**=\\||=\\|>>=\\|//=\\)"
"If looking at the beginning of an assignment. ")

(defconst py-block-re "[ \t]*\\_<\\(class\\|def\\|for\\|if\\|try\\|while\\|with\\)\\_>[:( \n\t]*"
(defconst py-block-re "[ \t]*\\_<\\(class\\|def\\|async def\\|async for\\|for\\|if\\|try\\|while\\|with\\|async with\\)\\_>[:( \n\t]*"
"Matches the beginning of a compound statement. ")

(defconst py-minor-block-re "[ \t]*\\_<\\(for\\|if\\|try\\|with\\|except\\)\\_>[:( \n\t]*"
(defconst py-minor-block-re "[ \t]*\\_<\\(for\\|async for\\|if\\|try\\|with\\|async with\\|except\\)\\_>[:( \n\t]*"
"Matches the beginning of an `for', `if', `try', `except' or `with' block. ")

(defconst py-try-block-re "[ \t]*\\_<try\\_>[: \n\t]"
Expand All @@ -2398,7 +2398,7 @@ See py-no-outdent-re-raw for better readable content ")
(defconst py-except-block-re "[ \t]*\\_<except\\_> *a?s? *[[:print:]]*[: \n\t]"
"Matches the beginning of a `except' block. ")

(defconst py-for-block-re "[ \t]*\\_<for\\_> +[[:alpha:]_][[:alnum:]_]* +in +[[:alpha:]_][[:alnum:]_()]* *[: \n\t]"
(defconst py-for-block-re "[ \t]*\\_<\\(for\\|async for\\)\\_> +[[:alpha:]_][[:alnum:]_]* +in +[[:alpha:]_][[:alnum:]_()]* *[: \n\t]"
"Matches the beginning of a `try' block. ")

(defconst py-if-block-re "[ \t]*\\_<if\\_> +[[:alpha:]_][[:alnum:]_]* *[: \n\t]"
Expand All @@ -2410,14 +2410,17 @@ See py-no-outdent-re-raw for better readable content ")
(defconst py-class-re "[ \t]*\\_<\\(class\\)\\_>[ \n\t]"
"Matches the beginning of a class definition. ")

(defconst py-def-or-class-re "[ \t]*\\_<\\(def\\|class\\)\\_>[ \n\t]"
(defconst py-def-or-class-re "[ \t]*\\_<\\(def\\|class\\|async def\\)\\_>[ \n\t]"
"Matches the beginning of a class- or functions definition. ")

(defconst py-def-re "[ \t]*\\_<\\(def\\)\\_>[ \n\t]"
;; (defconst py-def-re "[ \t]*\\_<\\(async def\\|def\\)\\_>[ \n\t]"
(defconst py-def-re "[ \t]*\\_<\\(def\\|async def\\)\\_>[ \n\t]"
"Matches the beginning of a functions definition. ")

(defconst py-block-or-clause-re-raw
(list
"async for"
"async with"
"elif"
"else"
"except"
Expand All @@ -2426,7 +2429,8 @@ See py-no-outdent-re-raw for better readable content ")
"if"
"try"
"while"
"with")
"with"
)
"Matches the beginning of a compound statement or it's clause. ")

(defvar py-block-or-clause-re
Expand Down
4 changes: 3 additions & 1 deletion python-components-move.el
Expand Up @@ -722,10 +722,12 @@ With BOL, return line-beginning-position"
(defun py--backward-def-or-class-intern (regexp &optional indent bol)
(while (and (re-search-backward regexp nil 'move 1)
(nth 8 (parse-partial-sexp (point-min) (point)))))
(when (looking-back "async ")
(goto-char (match-beginning 0)))
(let ((erg (when (looking-at regexp)
(if bol (line-beginning-position) (point)))))
;; bol-forms at not at bol yet
(and erg (goto-char erg))
(and bol erg (goto-char erg))
(and erg py-mark-decorators (setq erg (py--backward-def-or-class-decorator-maybe bol)))
erg))

Expand Down
22 changes: 22 additions & 0 deletions test/py-ert-tests-3.el
Expand Up @@ -110,5 +110,27 @@ def foo():
;; (sit-for 0.1)
;; (should (string= "def foo" (py-find-definition)))))

(ert-deftest py-ert-async-backward-block-test ()
(py-test-with-temp-buffer
"async def coro(name, lock):
print('coro {}: waiting for lock'.format(name))
async with lock:
print('coro {}: holding the lock'.format(name))
await asyncio.sleep(1)
print('coro {}: releasing the lock'.format(name))"
(py-backward-block)
(should (looking-at "async with"))))

(ert-deftest py-ert-async-backward-def-test ()
(py-test-with-temp-buffer
"async def coro(name, lock):
print('coro {}: waiting for lock'.format(name))
async with lock:
print('coro {}: holding the lock'.format(name))
await asyncio.sleep(1)
print('coro {}: releasing the lock'.format(name))"
(py-backward-def)
(should (looking-at "async def"))))

(provide 'py-ert-tests-3)
;;; py-ert-tests-3.el ends here

0 comments on commit 05e65ce

Please sign in to comment.