]> git.eshelyaron.com Git - emacs.git/commitdiff
ruby-syntax-methods-before-regexp: Drop this whitelist
authorDmitry Gutov <dmitry@gutov.dev>
Sat, 16 Dec 2023 02:57:44 +0000 (04:57 +0200)
committerEshel Yaron <me@eshelyaron.com>
Sat, 16 Dec 2023 19:15:05 +0000 (20:15 +0100)
* lisp/progmodes/ruby-mode.el (ruby-syntax-before-regexp-re):
Match only based on keywords and operators.
(ruby-syntax-methods-before-regexp): Delete.
(ruby-syntax-propertize): Use the new heuristic based on spaces
instead of checking for method names before (bug#67569).

* test/lisp/progmodes/ruby-mode-tests.el
(ruby-regexp-not-division-when-only-space-before):
Use non-whitelisted method name.

* test/lisp/progmodes/ruby-mode-resources/ruby.rb:
Adjust two examples.

(cherry picked from commit 746507dc3b9f555ff6e8e6282ff03ac211752325)

lisp/progmodes/ruby-mode.el
test/lisp/progmodes/ruby-mode-resources/ruby.rb
test/lisp/progmodes/ruby-mode-tests.el

index 5c34ddc562bcdbe0057e0b8394233a691d74ef4d..bca86a57c7d13d0c7b31ede160699e2e0c87b2ec 100644 (file)
@@ -2106,12 +2106,6 @@ or `gem' statement around point."
     "\\(%\\)[qQrswWxIi]?\\([[:punct:]]\\)"
     "Regexp to match the beginning of percent literal.")
 
-  (defconst ruby-syntax-methods-before-regexp
-    '("gsub" "gsub!" "sub" "sub!" "scan" "split" "split!" "index" "match"
-      "assert_match" "Given" "Then" "When")
-    "Methods that can take regexp as the first argument.
-It will be properly highlighted even when the call omits parens.")
-
   (defvar ruby-syntax-before-regexp-re
     (concat
      ;; Special tokens that can't be followed by a division operator.
@@ -2123,11 +2117,9 @@ It will be properly highlighted even when the call omits parens.")
      "\\|\\(?:^\\|\\s \\)"
      (regexp-opt '("if" "elsif" "unless" "while" "until" "when" "and"
                    "or" "not" "&&" "||"))
-     ;; Method name from the list.
-     "\\|\\_<"
-     (regexp-opt ruby-syntax-methods-before-regexp)
      "\\)\\s *")
-    "Regexp to match text that can be followed by a regular expression."))
+    "Regexp to match text that disambiguates a regular expression.
+A slash character after any of these should begin a regexp."))
 
 (defun ruby-syntax-propertize (start end)
   "Syntactic keywords for Ruby mode.  See `syntax-propertize-function'."
@@ -2187,10 +2179,14 @@ It will be properly highlighted even when the call omits parens.")
             (when (or
                    ;; Beginning of a regexp.
                    (and (null (nth 8 state))
-                        (save-excursion
-                          (forward-char -1)
-                          (looking-back ruby-syntax-before-regexp-re
-                                        (line-beginning-position))))
+                        (or (not
+                             ;; Looks like division.
+                             (or (eql (char-after) ?\s)
+                                 (not (eql (char-before (1- (point))) ?\s))))
+                            (save-excursion
+                              (forward-char -1)
+                              (looking-back ruby-syntax-before-regexp-re
+                                            (line-beginning-position)))))
                    ;; End of regexp.  We don't match the whole
                    ;; regexp at once because it can have
                    ;; string interpolation inside, or span
index 81d0dfd75c99745ba0a64b497f0e544897c754f4..a411b39a8fc02961b6fee01bede72e5a493b27b0 100644 (file)
@@ -34,11 +34,11 @@ x = # "tot %q/to"; =
 # Regexp after whitelisted method.
 "abc".sub /b/, 'd'
 
-# Don't mismatch "sub" at the end of words.
-a = asub / aslb + bsub / bslb;
+# Don't mistake division for regexp.
+a = sub / aslb + bsub / bslb;
 
 # Highlight the regexp after "if".
-x = toto / foo if /do bar/ =~ "dobar"
+x = toto / foo if / do bar/ =~ "dobar"
 
 # Regexp options are highlighted.
 
index 117385ea3e828a8dad75b5aee6e76942b0610be9..fea5f58b92e4e33de265de1dfd78f6c3a9dac523 100644 (file)
@@ -157,6 +157,18 @@ VALUES-PLIST is a list with alternating index and value elements."
 (ert-deftest ruby-regexp-is-not-mistaken-for-slash-symbol ()
   (ruby-assert-state "x = /foo:/" 3 nil))
 
+(ert-deftest ruby-slash-not-regexp-when-surrounded-by-spaces ()
+  (ruby-assert-state "x = index / 3" 3 nil))
+
+(ert-deftest ruby-slash-not-regexp-when-no-spaces ()
+  (ruby-assert-state "x = index/3" 3 nil))
+
+(ert-deftest ruby-regexp-not-division-when-only-space-before ()
+  (ruby-assert-state "x = foo_index /3" 3 ?/))
+
+(ert-deftest ruby-slash-not-regexp-when-only-space-after ()
+  (ruby-assert-state "x = index/ 3" 3 nil))
+
 (ert-deftest ruby-indent-simple ()
   (ruby-should-indent-buffer
    "if foo