Skip to content

Commit

Permalink
lp:1545649, C-M-a and C-M-e stop at embedded defs.
Browse files Browse the repository at this point in the history
fixed
  • Loading branch information
andreas-roehler committed Feb 16, 2016
1 parent f25692e commit ec7d7ab
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 26 deletions.
49 changes: 35 additions & 14 deletions python-components-move.el
Expand Up @@ -855,12 +855,20 @@ With BOL, return line-beginning-position"
(setq erg (line-beginning-position))))
(or erg (goto-char orig))))

(defun py--backward-def-or-class-matcher (regexp indent)
(while (and (re-search-backward regexp nil 'move 1)
(setq erg (match-beginning 0))
(or
(< indent (current-indentation))
(nth 8 (parse-partial-sexp (point-min) (point)))))
(setq erg nil)))

(defun py--backward-def-or-class-intern (regexp &optional bol)
(let (erg)
(while (and (re-search-backward regexp nil 'move 1)
(setq erg (match-beginning 0))
(nth 8 (parse-partial-sexp (point-min) (point))))
(setq erg nil))
(let ((indent (progn (when (py-in-string-or-comment-p)
(py-backward-statement))
(current-indentation)))
erg)
(py--backward-def-or-class-matcher regexp indent)
(and erg (looking-back "async ")
(goto-char (match-beginning 0))
(setq erg (point)))
Expand All @@ -869,41 +877,54 @@ With BOL, return line-beginning-position"
(and erg py-mark-decorators (setq erg (py--backward-def-or-class-decorator-maybe bol)))
erg))

(defun py-backward-class ()
(defun py-backward-class (&optional nested)
"Go to beginning of class.
If already at beginning, go one class backward.
Returns beginning of class if successful, nil otherwise
With optional NESTED, match next upwards, ignore indentation.
When `py-mark-decorators' is non-nil, decorators are considered too. "
(interactive)
(let ((erg (py--backward-def-or-class-intern py-class-re)))
(interactive "P")
(let ((erg
(if (eq 4 (prefix-numeric-value nested))
(py-up-class)
(py--backward-def-or-class-intern py-class-re))))
(when (and py-verbose-p (called-interactively-p 'any))
(message "%s" erg))
erg))

(defun py-backward-def ()
(defun py-backward-def (&optional nested)
"Go to beginning of def.
If already at beginning, go one def backward.
Returns beginning of def if successful, nil otherwise
With optional NESTED, match next upwards, ignore indentation.
When `py-mark-decorators' is non-nil, decorators are considered too. "
(interactive)
(let ((erg (py--backward-def-or-class-intern py-def-re)))
(interactive "P")
(let ((erg (if (eq 4 (prefix-numeric-value nested))
(py-up-def)
(py--backward-def-or-class-intern py-def-re))))
(when (and py-verbose-p (called-interactively-p 'any))
(message "%s" erg))
erg))

(defun py-backward-def-or-class ()
(defun py-backward-def-or-class (&optional nested)
"Go to beginning of def-or-class.
If already at beginning, go one def-or-class backward.
Returns beginning of def-or-class if successful, nil otherwise
With optional NESTED, match next upwards, ignore indentation.
When `py-mark-decorators' is non-nil, decorators are considered too. "
(interactive)
(let ((erg (py--backward-def-or-class-intern py-def-or-class-re)))
(interactive "P")
(let ((erg (if (eq 4 (prefix-numeric-value nested))
(py-up-def-or-class)
(py--backward-def-or-class-intern py-def-or-class-re))))
(when (and py-verbose-p (called-interactively-p 'any))
(message "%s" erg))
erg))
Expand Down
71 changes: 59 additions & 12 deletions test/py-ert-tests-3.el
Expand Up @@ -217,7 +217,7 @@ More docstring here.
(skip-chars-forward " \t\r\n\f")
(should (eq 4 (current-indentation))))))

(ert-deftest py-backward-indent-test ()
(ert-deftest py-ert-backward-indent-test ()
(py-test-with-temp-buffer
"class A(object):
def a(self):
Expand All @@ -234,7 +234,7 @@ More docstring here.
(py-backward-indent)
(should (eq (char-after) ?s))))

(ert-deftest py-forward-indent-test ()
(ert-deftest py-ert-forward-indent-test ()
(py-test-with-temp-buffer-point-min
"class A(object):
def a(self):
Expand All @@ -253,7 +253,7 @@ More docstring here.
(py-forward-indent)
(should (eq (char-before) ?:))))

(ert-deftest py-beginning-of-indent-p-test ()
(ert-deftest py-ert-beginning-of-indent-p-test ()
(py-test-with-temp-buffer-point-min
"class A(object):
def a(self):
Expand All @@ -264,7 +264,7 @@ More docstring here.
(py-backward-indent)
(should (py--beginning-of-indent-p))))

(ert-deftest py-beginning-of-indent-bol-p-test ()
(ert-deftest py-ert-beginning-of-indent-bol-p-test ()
(py-test-with-temp-buffer-point-min
"class A(object):
def a(self):
Expand All @@ -275,7 +275,7 @@ More docstring here.
(beginning-of-line)
(should (py--beginning-of-indent-bol-p))))

(ert-deftest py-copy-indent-test ()
(ert-deftest py-ert-copy-indent-test ()
(py-test-with-temp-buffer-point-min
"class A(object):
def a(self):
Expand All @@ -288,7 +288,7 @@ More docstring here.
(py-backward-statement)
(should (py--beginning-of-indent-p))))

(ert-deftest py-delete-indent-test ()
(ert-deftest py-ert-delete-indent-test ()
(py-test-with-temp-buffer-point-min
"class A(object):
def a(self):
Expand All @@ -299,7 +299,7 @@ More docstring here.
(should (eobp))
(should (bolp))))

(ert-deftest py-kill-indent-test ()
(ert-deftest py-ert-kill-indent-test ()
(py-test-with-temp-buffer-point-min
"class A(object):
def a(self):
Expand All @@ -311,7 +311,7 @@ More docstring here.
(should (eobp))
(should (bolp))))

(ert-deftest py-mark-indent-test ()
(ert-deftest py-ert-mark-indent-test ()
(py-test-with-temp-buffer-point-min
"class A(object):
def a(self):
Expand All @@ -322,7 +322,7 @@ More docstring here.
;; (message "%s" (buffer-substring-no-properties (region-beginning) (region-end)))
(should (eq 28 (length (buffer-substring-no-properties (region-beginning) (region-end)))))))

(ert-deftest py-backward-comment-test ()
(ert-deftest py-ert-backward-comment-test ()
(py-test-with-temp-buffer-point-min
"class A(object):
def a(self):
Expand All @@ -334,7 +334,7 @@ More docstring here.
(py-backward-comment)
(should (eq (char-after) ?#))))

(ert-deftest py-forward-comment-test ()
(ert-deftest py-ert-forward-comment-test ()
(py-test-with-temp-buffer-point-min
"class A(object):
def a(self):
Expand All @@ -346,7 +346,7 @@ More docstring here.
(py-forward-comment)
(should (eq (char-before) ?\)))))

(ert-deftest py-shift-indent-test ()
(ert-deftest py-ert-shift-indent-test ()
(py-test-with-temp-buffer-point-min
"class A(object):
def a(self):
Expand All @@ -360,7 +360,7 @@ More docstring here.
(py-shift-indent-left)
(should (eq 8 (current-indentation)))))

(ert-deftest py-dont-stop-embedded-test ()
(ert-deftest py-ert-dont-stop-embedded-def-or-class-test ()
(py-test-with-temp-buffer
"# lp:1545649, C-M-a and C-M-e stop at embedded defs.
class Foo:
Expand All @@ -374,5 +374,52 @@ string.
(py-backward-def-or-class)
(should (eq (char-after) ?c))))

(ert-deftest py-ert-dont-stop-embedded-class-test ()
(py-test-with-temp-buffer
"# lp:1545649, C-M-a and C-M-e stop at embedded defs.
class Foo:
def bar(self):
class baz
print(\"\"\"
This is
a nested
string.
\"\"\")
"
(py-backward-class)
(should (eq 0 (current-column)))))

(ert-deftest py-ert-dont-stop-embedded-def-test ()
(py-test-with-temp-buffer
"# lp:1545649, C-M-a and C-M-e stop at embedded defs.
def Foo:
class bar(self):
def baz
print(\"\"\"
This is
a nested
string.
\"\"\")
"
(py-backward-def)
(should (eq 0 (current-column)))))

(ert-deftest py-ert-dont-stop-embedded-def-from-string-test ()
(py-test-with-temp-buffer
"# lp:1545649, C-M-a and C-M-e stop at embedded defs.
def Foo:
class bar(self):
def baz
print(\"\"\"
This is
a nested
string.
\"\"\")
"
(search-backward "string")
(skip-chars-backward " \t\r\n\f")
(py-backward-def)
(should (eq (char-after) ?d))))

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

0 comments on commit ec7d7ab

Please sign in to comment.