From 746507dc3b9f555ff6e8e6282ff03ac211752325 Mon Sep 17 00:00:00 2001 From: Dmitry Gutov Date: Sat, 16 Dec 2023 04:57:44 +0200 Subject: [PATCH] ruby-syntax-methods-before-regexp: Drop this whitelist * 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. --- lisp/progmodes/ruby-mode.el | 32 +++++++------------ .../progmodes/ruby-mode-resources/ruby.rb | 6 ++-- test/lisp/progmodes/ruby-mode-tests.el | 2 +- 3 files changed, 15 insertions(+), 25 deletions(-) diff --git a/lisp/progmodes/ruby-mode.el b/lisp/progmodes/ruby-mode.el index 5fab2bf02c4..bca86a57c7d 100644 --- a/lisp/progmodes/ruby-mode.el +++ b/lisp/progmodes/ruby-mode.el @@ -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 t) "\\)\\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'." @@ -2183,20 +2175,18 @@ It will be properly highlighted even when the call omits parens.") (when (save-excursion (forward-char -1) (cl-evenp (skip-chars-backward "\\\\"))) - (let ((state (save-excursion (syntax-ppss (match-beginning 1)))) - division-like) + (let ((state (save-excursion (syntax-ppss (match-beginning 1))))) (when (or ;; Beginning of a regexp. (and (null (nth 8 state)) - (save-excursion - (setq division-like - (or (eql (char-after) ?\s) - (not (eql (char-before (1- (point))) ?\s)))) - (forward-char -1) - (looking-back ruby-syntax-before-regexp-re - (line-beginning-position))) - (not (and division-like - (match-beginning 2)))) + (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 diff --git a/test/lisp/progmodes/ruby-mode-resources/ruby.rb b/test/lisp/progmodes/ruby-mode-resources/ruby.rb index 81d0dfd75c9..a411b39a8fc 100644 --- a/test/lisp/progmodes/ruby-mode-resources/ruby.rb +++ b/test/lisp/progmodes/ruby-mode-resources/ruby.rb @@ -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. diff --git a/test/lisp/progmodes/ruby-mode-tests.el b/test/lisp/progmodes/ruby-mode-tests.el index a931541ba35..fea5f58b92e 100644 --- a/test/lisp/progmodes/ruby-mode-tests.el +++ b/test/lisp/progmodes/ruby-mode-tests.el @@ -164,7 +164,7 @@ VALUES-PLIST is a list with alternating index and value elements." (ruby-assert-state "x = index/3" 3 nil)) (ert-deftest ruby-regexp-not-division-when-only-space-before () - (ruby-assert-state "x = index /3" 3 ?/)) + (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)) -- 2.39.2