diff options
| author | kobarity | 2023-03-25 22:59:05 +0900 |
|---|---|---|
| committer | Dmitry Gutov | 2023-03-26 05:03:07 +0300 |
| commit | 8f42db010d15efa21fb9007e61daedbe1e2dfa53 (patch) | |
| tree | 66a42da235a2becacc7ebd3572c240b592fb0f94 | |
| parent | c4d490490dc24c76fbead7941518ad503672d216 (diff) | |
| download | emacs-8f42db010d15efa21fb9007e61daedbe1e2dfa53.tar.gz emacs-8f42db010d15efa21fb9007e61daedbe1e2dfa53.zip | |
Improve indenting "case" in Python
* lisp/progmodes/python.el (python-info-dedenter-statement-p): Do not
consider the first "case" in the block as dedenter.
* test/lisp/progmodes/python-tests.el
(python-info-dedenter-opening-block-positions-7)
(python-info-dedenter-statement-p-6): New tests. (Bug#62092)
| -rw-r--r-- | lisp/progmodes/python.el | 9 | ||||
| -rw-r--r-- | test/lisp/progmodes/python-tests.el | 38 |
2 files changed, 46 insertions, 1 deletions
diff --git a/lisp/progmodes/python.el b/lisp/progmodes/python.el index 2fe88323c35..bbabce80b4d 100644 --- a/lisp/progmodes/python.el +++ b/lisp/progmodes/python.el | |||
| @@ -5854,7 +5854,14 @@ statement." | |||
| 5854 | (save-excursion | 5854 | (save-excursion |
| 5855 | (python-nav-beginning-of-statement) | 5855 | (python-nav-beginning-of-statement) |
| 5856 | (when (and (not (python-syntax-context-type)) | 5856 | (when (and (not (python-syntax-context-type)) |
| 5857 | (looking-at (python-rx dedenter))) | 5857 | (looking-at (python-rx dedenter)) |
| 5858 | ;; Exclude the first "case" in the block. | ||
| 5859 | (not (and (string= (match-string-no-properties 0) | ||
| 5860 | "case") | ||
| 5861 | (save-excursion | ||
| 5862 | (back-to-indentation) | ||
| 5863 | (python-util-forward-comment -1) | ||
| 5864 | (equal (char-before) ?:))))) | ||
| 5858 | (point)))) | 5865 | (point)))) |
| 5859 | 5866 | ||
| 5860 | (defun python-info-line-ends-backslash-p (&optional line-number) | 5867 | (defun python-info-line-ends-backslash-p (&optional line-number) |
diff --git a/test/lisp/progmodes/python-tests.el b/test/lisp/progmodes/python-tests.el index ed4a08da6ab..50153e66da5 100644 --- a/test/lisp/progmodes/python-tests.el +++ b/test/lisp/progmodes/python-tests.el | |||
| @@ -5940,6 +5940,26 @@ def func(): | |||
| 5940 | (equal (list (python-tests-look-at "if (" -1 t)) | 5940 | (equal (list (python-tests-look-at "if (" -1 t)) |
| 5941 | (python-info-dedenter-opening-block-positions))))) | 5941 | (python-info-dedenter-opening-block-positions))))) |
| 5942 | 5942 | ||
| 5943 | (ert-deftest python-info-dedenter-opening-block-positions-7 () | ||
| 5944 | "Test case blocks." | ||
| 5945 | (python-tests-with-temp-buffer | ||
| 5946 | " | ||
| 5947 | match a: | ||
| 5948 | case 1: | ||
| 5949 | match b: | ||
| 5950 | case 2: | ||
| 5951 | something() | ||
| 5952 | case 3: | ||
| 5953 | " | ||
| 5954 | (python-tests-look-at "case 1:") | ||
| 5955 | (should-not (python-info-dedenter-opening-block-positions)) | ||
| 5956 | (python-tests-look-at "case 2:") | ||
| 5957 | (should-not (python-info-dedenter-opening-block-positions)) | ||
| 5958 | (python-tests-look-at "case 3:") | ||
| 5959 | (equal (list (python-tests-look-at "case 2:" -1) | ||
| 5960 | (python-tests-look-at "case 1:" -1 t)) | ||
| 5961 | (python-info-dedenter-opening-block-positions)))) | ||
| 5962 | |||
| 5943 | (ert-deftest python-info-dedenter-opening-block-message-1 () | 5963 | (ert-deftest python-info-dedenter-opening-block-message-1 () |
| 5944 | "Test dedenters inside strings are ignored." | 5964 | "Test dedenters inside strings are ignored." |
| 5945 | (python-tests-with-temp-buffer | 5965 | (python-tests-with-temp-buffer |
| @@ -6125,6 +6145,24 @@ elif b: | |||
| 6125 | (point)) | 6145 | (point)) |
| 6126 | (python-info-dedenter-statement-p))))) | 6146 | (python-info-dedenter-statement-p))))) |
| 6127 | 6147 | ||
| 6148 | (ert-deftest python-info-dedenter-statement-p-6 () | ||
| 6149 | "Test case keyword." | ||
| 6150 | (python-tests-with-temp-buffer | ||
| 6151 | " | ||
| 6152 | match a: # Comment | ||
| 6153 | case 1: | ||
| 6154 | match b: | ||
| 6155 | case 2: | ||
| 6156 | something() | ||
| 6157 | case 3: | ||
| 6158 | " | ||
| 6159 | (python-tests-look-at "case 1:") | ||
| 6160 | (should-not (python-info-dedenter-statement-p)) | ||
| 6161 | (python-tests-look-at "case 2:") | ||
| 6162 | (should-not (python-info-dedenter-statement-p)) | ||
| 6163 | (python-tests-look-at "case 3:") | ||
| 6164 | (should (= (point) (python-info-dedenter-statement-p))))) | ||
| 6165 | |||
| 6128 | (ert-deftest python-info-line-ends-backslash-p-1 () | 6166 | (ert-deftest python-info-line-ends-backslash-p-1 () |
| 6129 | (python-tests-with-temp-buffer | 6167 | (python-tests-with-temp-buffer |
| 6130 | " | 6168 | " |