"\\(%\\)[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.
"\\|\\(?:^\\|\\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'."
(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
# 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.
(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