aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDmitry Gutov2012-08-14 08:38:11 -0400
committerStefan Monnier2012-08-14 08:38:11 -0400
commitf063063a8a8bfc1ba343e7e9eb5d17f866f6fccd (patch)
tree2c68a80c0f9f14d93250ad8f5b4619eae3870f31
parente636fafe20f0238d0aaabc4b822642efe68cad9b (diff)
downloademacs-f063063a8a8bfc1ba343e7e9eb5d17f866f6fccd.tar.gz
emacs-f063063a8a8bfc1ba343e7e9eb5d17f866f6fccd.zip
* lisp/progmodes/ruby-mode.el (ruby-syntax-methods-before-regexp): New const.
(ruby-syntax-propertize-function): Use it to recognize regexps. Don't look at the text after regexp, just use the whitelist. * test/indent/ruby.rb: Rearrange examples, add new ones. Fixes: debbugs:6286
-rw-r--r--lisp/ChangeLog3
-rw-r--r--lisp/progmodes/ruby-mode.el39
-rw-r--r--test/ChangeLog4
-rw-r--r--test/indent/ruby.rb30
4 files changed, 46 insertions, 30 deletions
diff --git a/lisp/ChangeLog b/lisp/ChangeLog
index db4cc8e7fab..875ab5d3188 100644
--- a/lisp/ChangeLog
+++ b/lisp/ChangeLog
@@ -10,6 +10,9 @@
10 (ruby-syntax-propertize-percent-literal): Only propertize when not 10 (ruby-syntax-propertize-percent-literal): Only propertize when not
11 inside a simple string or comment. When the literal is unclosed, 11 inside a simple string or comment. When the literal is unclosed,
12 leave the text after it unpropertized. 12 leave the text after it unpropertized.
13 (ruby-syntax-methods-before-regexp): New constant.
14 (ruby-syntax-propertize-function): Use it to recognize regexps.
15 Don't look at the text after regexp, just use the whitelist.
13 16
142012-08-14 Andreas Schwab <schwab@linux-m68k.org> 172012-08-14 Andreas Schwab <schwab@linux-m68k.org>
15 18
diff --git a/lisp/progmodes/ruby-mode.el b/lisp/progmodes/ruby-mode.el
index 42e1ac72b33..457c7fee36c 100644
--- a/lisp/progmodes/ruby-mode.el
+++ b/lisp/progmodes/ruby-mode.el
@@ -1178,7 +1178,13 @@ See `add-log-current-defun-function'."
1178 (eval-and-compile 1178 (eval-and-compile
1179 (defconst ruby-percent-literal-beg-re 1179 (defconst ruby-percent-literal-beg-re
1180 "\\(%\\)[qQrswWx]?\\([[:punct:]]\\)" 1180 "\\(%\\)[qQrswWx]?\\([[:punct:]]\\)"
1181 "Regexp to match the beginning of percent literal.")) 1181 "Regexp to match the beginning of percent literal.")
1182
1183 (defconst ruby-syntax-methods-before-regexp
1184 '("gsub" "gsub!" "sub" "sub!" "scan" "split" "split!" "index" "match"
1185 "assert_match" "Given" "Then" "When")
1186 "Methods that can take regexp as the first argument.
1187It will be properly highlighted even when the call omits parens."))
1182 1188
1183 (defun ruby-syntax-propertize-function (start end) 1189 (defun ruby-syntax-propertize-function (start end)
1184 "Syntactic keywords for Ruby mode. See `syntax-propertize-function'." 1190 "Syntactic keywords for Ruby mode. See `syntax-propertize-function'."
@@ -1196,28 +1202,23 @@ See `add-log-current-defun-function'."
1196 ;; Not within a string. 1202 ;; Not within a string.
1197 (nth 3 (syntax-ppss (match-beginning 0)))) 1203 (nth 3 (syntax-ppss (match-beginning 0))))
1198 (string-to-syntax "\\")))) 1204 (string-to-syntax "\\"))))
1199 ;; Regexps: regexps are distinguished from division either because 1205 ;; Regexps: regexps are distinguished from division because
1200 ;; of the keyword/symbol before them, or because of the code 1206 ;; of the keyword, symbol, or method name before them.
1201 ;; following them.
1202 ((concat 1207 ((concat
1203 ;; Special tokens that can't be followed by a division operator. 1208 ;; Special tokens that can't be followed by a division operator.
1204 "\\(?:\\(^\\|[[=(,~?:;<>]\\|\\(?:^\\|\\s \\)" 1209 "\\(^\\|[[=(,~?:;<>]"
1210 ;; Control flow keywords and operators following bol or whitespace.
1211 "\\|\\(?:^\\|\\s \\)"
1205 (regexp-opt '("if" "elsif" "unless" "while" "until" "when" "and" 1212 (regexp-opt '("if" "elsif" "unless" "while" "until" "when" "and"
1206 "or" "&&" "||" 1213 "or" "not" "&&" "||"))
1207 "gsub" "gsub!" "sub" "sub!" "scan" "split" "split!")) 1214 ;; Method name from the list.
1208 "\\)\\s *\\)?" 1215 "\\|\\_<"
1216 (regexp-opt ruby-syntax-methods-before-regexp)
1217 "\\)\\s *"
1209 ;; The regular expression itself. 1218 ;; The regular expression itself.
1210 "\\(/\\)[^/\n\\\\]*\\(?:\\\\.[^/\n\\\\]*\\)*\\(/\\)" 1219 "\\(/\\)[^/\n\\\\]*\\(?:\\\\.[^/\n\\\\]*\\)*\\(/\\)")
1211 ;; Special code that cannot follow a division operator. 1220 (2 (string-to-syntax "\"/"))
1212 ;; FIXME: Just because the second slash of "/foo/ do bar" can't 1221 (3 (string-to-syntax "\"/")))
1213 ;; be a division, doesn't mean it can't *start* a regexp, as in
1214 ;; "x = toto/foo; if /do bar/".
1215 "\\([imxo]*\\s *\\(?:,\\|\\_<do\\_>\\)\\)?")
1216 (2 (when (or (match-beginning 1) (match-beginning 4))
1217 (string-to-syntax "\"/")))
1218 (3 (if (or (match-beginning 1) (match-beginning 4))
1219 (string-to-syntax "\"/")
1220 (goto-char (match-end 2)))))
1221 ("^=en\\(d\\)\\_>" (1 "!")) 1222 ("^=en\\(d\\)\\_>" (1 "!"))
1222 ("^\\(=\\)begin\\_>" (1 "!")) 1223 ("^\\(=\\)begin\\_>" (1 "!"))
1223 ;; Handle here documents. 1224 ;; Handle here documents.
diff --git a/test/ChangeLog b/test/ChangeLog
index a0163b56e6d..f1bf458f812 100644
--- a/test/ChangeLog
+++ b/test/ChangeLog
@@ -1,3 +1,7 @@
12012-08-14 Dmitry Gutov <dgutov@yandex.ru>
2
3 * indent/ruby.rb: Rearrange examples, add new ones.
4
12012-08-12 Dmitry Gutov <dgutov@yandex.ru> 52012-08-12 Dmitry Gutov <dgutov@yandex.ru>
2 6
3 * automated/ruby-mode-tests.el (ruby-move-to-block-stops-at-opening) 7 * automated/ruby-mode-tests.el (ruby-move-to-block-stops-at-opening)
diff --git a/test/indent/ruby.rb b/test/indent/ruby.rb
index c4a747a1c78..4f2e9e63377 100644
--- a/test/indent/ruby.rb
+++ b/test/indent/ruby.rb
@@ -1,17 +1,25 @@
1# Don't mis-match "sub" at the end of words. 1# Percent literals.
2a = asub / aslb + bsub / bslb;
3
4b = %Q{This is a "string"} 2b = %Q{This is a "string"}
5c = %w(foo 3c = %w!foo
6 bar 4 bar
7 baz) 5 baz!
8d = %!hello! 6d = %(hello (nested) world)
7
8# Don't propertize percent literals inside strings.
9"(%s, %s)" % [123, 456]
10
11# Or inside comments.
12x = # "tot %q/to"; =
13y = 2 / 3
14
15# Regexp after whitelisted method.
16"abc".sub /b/, 'd'
17
18# Don't mis-match "sub" at the end of words.
19a = asub / aslb + bsub / bslb;
9 20
10# A "do" after a slash means that slash is not a division, but it doesn't imply 21# Highlight the regexp after "if".
11# it's a regexp-ender, since it can be a regexp-starter instead! 22x = toto / foo if /do bar/ =~ "dobar"
12x = toto / foo; if /do bar/ then
13 toto = 1
14 end
15 23
16# Some Cucumber code: 24# Some Cucumber code:
17Given /toto/ do 25Given /toto/ do