>From ac047bcebe08a8f850e3a3400156316088fd9520 Mon Sep 17 00:00:00 2001 From: Dmitry Gutov Date: Sat, 28 Apr 2012 22:11:00 +0400 Subject: [PATCH 2/2] * lisp/progmodes/ruby-mode.el: Go back to method whitelisting for regexps. ruby-syntax-methods-before-regexp: New variable. (ruby-syntax-propertize-function): Use it to recognize regexps. Don't look at the text after regexp, just use the old approach. * test/indent/ruby.rb: Update examples, add a new one. --- lisp/progmodes/ruby-mode.el | 38 ++++++++++++++++++++------------------ test/indent/ruby.rb | 25 +++++++++++++------------ 2 files changed, 33 insertions(+), 30 deletions(-) diff --git a/lisp/progmodes/ruby-mode.el b/lisp/progmodes/ruby-mode.el index 432680f..14ce846 100644 --- a/lisp/progmodes/ruby-mode.el +++ b/lisp/progmodes/ruby-mode.el @@ -226,6 +226,13 @@ Also ignores spaces after parenthesis when 'space." "Use `ruby-encoding-map' to set encoding magic comment if this is non-nil." :type 'boolean :group 'ruby) +(defcustom 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." + :group 'ruby) + ;; Safe file variables (put 'ruby-indent-tabs-mode 'safe-local-variable 'booleanp) (put 'ruby-indent-level 'safe-local-variable 'integerp) @@ -1137,28 +1144,23 @@ See `add-log-current-defun-function'." ;; Not within a string. (nth 3 (syntax-ppss (match-beginning 0)))) (string-to-syntax "\\")))) - ;; Regexps: regexps are distinguished from division either because - ;; of the keyword/symbol before them, or because of the code - ;; following them. + ;; Regexps: regexps are distinguished from division because + ;; of the keyword, symbol, or method name before them. ((concat ;; Special tokens that can't be followed by a division operator. - "\\(?:\\(^\\|[[=(,~?:;<>]\\|\\(?:^\\|\\s \\)" + "\\(^\\|[[=(,~?:;<>]" + ;; Control flow keywords and operators following bol or whitespace. + "\\|\\(?:^\\|\\s \\)" (regexp-opt '("if" "elsif" "unless" "while" "until" "when" "and" - "or" "&&" "||" - "gsub" "gsub!" "sub" "sub!" "scan" "split" "split!")) - "\\)\\s *\\)?" + "or" "not" "&&" "||")) + ;; Method name from the list. + "\\|\\_<" + (regexp-opt ruby-syntax-methods-before-regexp) + "\\)\\s *" ;; The regular expression itself. - "\\(/\\)[^/\n\\\\]*\\(?:\\\\.[^/\n\\\\]*\\)*\\(/\\)" - ;; Special code that cannot follow a division operator. - ;; FIXME: Just because the second slash of "/foo/ do bar" can't - ;; be a division, doesn't mean it can't *start* a regexp, as in - ;; "x = toto/foo; if /do bar/". - "\\([imxo]*\\s *\\(?:,\\|\\_\\)\\)?") - (2 (when (or (match-beginning 1) (match-beginning 4)) - (string-to-syntax "\"/"))) - (3 (if (or (match-beginning 1) (match-beginning 4)) - (string-to-syntax "\"/") - (goto-char (match-end 2))))) + "\\(/\\)[^/\n\\\\]*\\(?:\\\\.[^/\n\\\\]*\\)*\\(/\\)") + (2 (string-to-syntax "\"/")) + (3 (string-to-syntax "\"/"))) ("^=en\\(d\\)\\_>" (1 "!")) ("^\\(=\\)begin\\_>" (1 "!")) ;; Handle here documents. diff --git a/test/indent/ruby.rb b/test/indent/ruby.rb index 7a9d123..4f2e9e6 100644 --- a/test/indent/ruby.rb +++ b/test/indent/ruby.rb @@ -1,24 +1,25 @@ -# Don't mis-match "sub" at the end of words. -a = asub / aslb + bsub / bslb; - +# Percent literals. b = %Q{This is a "string"} -c = %w(foo +c = %w!foo bar - baz) -d = %!hello! + baz! +d = %(hello (nested) world) # Don't propertize percent literals inside strings. "(%s, %s)" % [123, 456] -# Nor inside comments. +# Or inside comments. x = # "tot %q/to"; = y = 2 / 3 -# A "do" after a slash means that slash is not a division, but it doesn't imply -# it's a regexp-ender, since it can be a regexp-starter instead! -x = toto / foo; if /do bar/ then - toto = 1 - end +# Regexp after whitelisted method. +"abc".sub /b/, 'd' + +# Don't mis-match "sub" at the end of words. +a = asub / aslb + bsub / bslb; + +# Highlight the regexp after "if". +x = toto / foo if /do bar/ =~ "dobar" # Some Cucumber code: Given /toto/ do -- 1.7.10.msysgit.1