diff options
| author | Dmitry Gutov | 2023-12-09 19:04:55 +0200 |
|---|---|---|
| committer | Dmitry Gutov | 2023-12-09 19:04:55 +0200 |
| commit | 91f2ade57bb72e9bb4a44da44e5dc69adb3c7584 (patch) | |
| tree | fed4bf06cedd388dc2df6de198b9f2018d9be5d6 | |
| parent | 0f361cc985d35202556233f04235df1c885f532e (diff) | |
| download | emacs-91f2ade57bb72e9bb4a44da44e5dc69adb3c7584.tar.gz emacs-91f2ade57bb72e9bb4a44da44e5dc69adb3c7584.zip | |
ruby-mode: Better detect regexp vs division (bug#67569)
* lisp/progmodes/ruby-mode.el (ruby-syntax-before-regexp-re):
Add grouping around methods from the whitelist.
(ruby-syntax-propertize): Also look for spaces around the slash.
| -rw-r--r-- | lisp/progmodes/ruby-mode.el | 12 | ||||
| -rw-r--r-- | test/lisp/progmodes/ruby-mode-tests.el | 12 |
2 files changed, 21 insertions, 3 deletions
diff --git a/lisp/progmodes/ruby-mode.el b/lisp/progmodes/ruby-mode.el index b252826680c..0ecb3579278 100644 --- a/lisp/progmodes/ruby-mode.el +++ b/lisp/progmodes/ruby-mode.el | |||
| @@ -2124,7 +2124,7 @@ It will be properly highlighted even when the call omits parens.") | |||
| 2124 | "or" "not" "&&" "||")) | 2124 | "or" "not" "&&" "||")) |
| 2125 | ;; Method name from the list. | 2125 | ;; Method name from the list. |
| 2126 | "\\|\\_<" | 2126 | "\\|\\_<" |
| 2127 | (regexp-opt ruby-syntax-methods-before-regexp) | 2127 | (regexp-opt ruby-syntax-methods-before-regexp t) |
| 2128 | "\\)\\s *") | 2128 | "\\)\\s *") |
| 2129 | "Regexp to match text that can be followed by a regular expression.")) | 2129 | "Regexp to match text that can be followed by a regular expression.")) |
| 2130 | 2130 | ||
| @@ -2182,14 +2182,20 @@ It will be properly highlighted even when the call omits parens.") | |||
| 2182 | (when (save-excursion | 2182 | (when (save-excursion |
| 2183 | (forward-char -1) | 2183 | (forward-char -1) |
| 2184 | (cl-evenp (skip-chars-backward "\\\\"))) | 2184 | (cl-evenp (skip-chars-backward "\\\\"))) |
| 2185 | (let ((state (save-excursion (syntax-ppss (match-beginning 1))))) | 2185 | (let ((state (save-excursion (syntax-ppss (match-beginning 1)))) |
| 2186 | division-like) | ||
| 2186 | (when (or | 2187 | (when (or |
| 2187 | ;; Beginning of a regexp. | 2188 | ;; Beginning of a regexp. |
| 2188 | (and (null (nth 8 state)) | 2189 | (and (null (nth 8 state)) |
| 2189 | (save-excursion | 2190 | (save-excursion |
| 2191 | (setq division-like | ||
| 2192 | (or (eql (char-after) ?\s) | ||
| 2193 | (not (eql (char-before (1- (point))) ?\s)))) | ||
| 2190 | (forward-char -1) | 2194 | (forward-char -1) |
| 2191 | (looking-back ruby-syntax-before-regexp-re | 2195 | (looking-back ruby-syntax-before-regexp-re |
| 2192 | (line-beginning-position)))) | 2196 | (line-beginning-position))) |
| 2197 | (not (and division-like | ||
| 2198 | (match-beginning 2)))) | ||
| 2193 | ;; End of regexp. We don't match the whole | 2199 | ;; End of regexp. We don't match the whole |
| 2194 | ;; regexp at once because it can have | 2200 | ;; regexp at once because it can have |
| 2195 | ;; string interpolation inside, or span | 2201 | ;; string interpolation inside, or span |
diff --git a/test/lisp/progmodes/ruby-mode-tests.el b/test/lisp/progmodes/ruby-mode-tests.el index 117385ea3e8..a931541ba35 100644 --- a/test/lisp/progmodes/ruby-mode-tests.el +++ b/test/lisp/progmodes/ruby-mode-tests.el | |||
| @@ -157,6 +157,18 @@ VALUES-PLIST is a list with alternating index and value elements." | |||
| 157 | (ert-deftest ruby-regexp-is-not-mistaken-for-slash-symbol () | 157 | (ert-deftest ruby-regexp-is-not-mistaken-for-slash-symbol () |
| 158 | (ruby-assert-state "x = /foo:/" 3 nil)) | 158 | (ruby-assert-state "x = /foo:/" 3 nil)) |
| 159 | 159 | ||
| 160 | (ert-deftest ruby-slash-not-regexp-when-surrounded-by-spaces () | ||
| 161 | (ruby-assert-state "x = index / 3" 3 nil)) | ||
| 162 | |||
| 163 | (ert-deftest ruby-slash-not-regexp-when-no-spaces () | ||
| 164 | (ruby-assert-state "x = index/3" 3 nil)) | ||
| 165 | |||
| 166 | (ert-deftest ruby-regexp-not-division-when-only-space-before () | ||
| 167 | (ruby-assert-state "x = index /3" 3 ?/)) | ||
| 168 | |||
| 169 | (ert-deftest ruby-slash-not-regexp-when-only-space-after () | ||
| 170 | (ruby-assert-state "x = index/ 3" 3 nil)) | ||
| 171 | |||
| 160 | (ert-deftest ruby-indent-simple () | 172 | (ert-deftest ruby-indent-simple () |
| 161 | (ruby-should-indent-buffer | 173 | (ruby-should-indent-buffer |
| 162 | "if foo | 174 | "if foo |