Subject: [PATCH] Merge changes from upstream ruby-mode

From: Dmitry Gutov <[email-masked]>
Date: Fri, 10 Aug 2012 04:36:01 +0400

This is part 1, 1 or 2 more to come.

The change to `ruby-syntax-propertize-function' is kind of messy, but I 
don't see how to do it better without seriously rewriting 
`ruby-syntax-propertize-heredoc'.

--Dmitry
diff --git a/lisp/ChangeLog b/lisp/ChangeLog
index 635456a..38379cd 100644
--- a/lisp/ChangeLog
+++ b/lisp/ChangeLog
@@ -1,3 +1,17 @@
+2012-08-09  Dmitry Gutov  <[email-masked]>
+
+	Merge stuff from upsteam ruby-mode, part 1.
+
+	* progmodes/ruby-mode.el (ruby-mode-map): Remove deprecated
+	binding (use `M-;' instead).
+	(ruby-expr-beg, ruby-parse-partial): ?, _, and : are symbol
+	constituents, ! is not (but kinda should be).
+	(ruby-singleton-class-p): New function.
+	(ruby-expr-beg, ruby-in-here-doc-p)
+	(ruby-syntax-propertize-heredoc): Use it.
+	(ruby-syntax-propertize-function): Adjust for changes in
+	`ruby-syntax-propertize-heredoc'.
+
 2012-08-07  Andreas Schwab  <[email-masked]>
 
 	* calc/calc-prog.el (math-do-defmath): Use backquote forms.  Fix
diff --git a/lisp/progmodes/ruby-mode.el b/lisp/progmodes/ruby-mode.el
index 091a7b7..68abaff 100644
--- a/lisp/progmodes/ruby-mode.el
+++ b/lisp/progmodes/ruby-mode.el
@@ -151,7 +151,6 @@ This should only be called after matching against `ruby-here-doc-beg-re'."
     (define-key map (kbd "C-M-h") 'backward-kill-word)
     (define-key map (kbd "C-j")   'reindent-then-newline-and-indent)
     (define-key map (kbd "C-m")   'newline)
-    (define-key map (kbd "C-c C-c") 'comment-region)
     map)
   "Keymap used in Ruby mode.")
 
@@ -380,11 +379,19 @@ and `\\' when preceded by `?'."
           ((and (eq c ?:) (or (not b) (eq (char-syntax b) ? ))))
           ((eq c ?\\) (eq b ??)))))
 
+(defun ruby-singleton-class-p (&optional pos)
+  (save-excursion
+    (when pos (goto-char pos))
+    (forward-word -1)
+    (and (or (bolp) (not (eq (char-before (point)) ?_)))
+         (looking-at "class\\s *<<"))))
+
 (defun ruby-expr-beg (&optional option)
   "TODO: document."
   (save-excursion
     (store-match-data nil)
-    (let ((space (skip-chars-backward " \t")))
+    (let ((space (skip-chars-backward " \t"))
+          (start (point)))
       (cond
        ((bolp) t)
        ((progn
@@ -393,7 +400,8 @@ and `\\' when preceded by `?'."
                (or (eq (char-syntax (char-before (point))) ?w)
                    (ruby-special-char-p))))
         nil)
-       ((and (eq option 'heredoc) (< space 0)) t)
+       ((and (eq option 'heredoc) (< space 0))
+        (not (progn (goto-char start) (ruby-singleton-class-p))))
        ((or (looking-at ruby-operator-re)
             (looking-at "[\\[({,;]")
             (and (looking-at "[!?]")
@@ -409,7 +417,7 @@ and `\\' when preceded by `?'."
                                         ruby-block-mid-keywords)
                                 'words))
                    (goto-char (match-end 0))
-                   (not (looking-at "\\s_")))
+                   (not (looking-at "\\s_\\|!")))
                   ((eq option 'expr-qstr)
                    (looking-at "[a-zA-Z][a-zA-z0-9_]* +%[^ \t]"))
                   ((eq option 'expr-re)
@@ -581,9 +589,7 @@ and `\\' when preceded by `?'."
                         (eq ?. w)))))
          (goto-char pnt)
          (setq w (char-after (point)))
-         (not (eq ?_ w))
          (not (eq ?! w))
-         (not (eq ?? w))
          (skip-chars-forward " \t")
          (goto-char (match-beginning 0))
          (or (not (looking-at ruby-modifier-re))
@@ -1159,7 +1165,10 @@ See `add-log-current-defun-function'."
           ("^\\(=\\)begin\\_>" (1 "!"))
           ;; Handle here documents.
           ((concat ruby-here-doc-beg-re ".*\\(\n\\)")
-           (7 (prog1 "\"" (ruby-syntax-propertize-heredoc end))))
+           (7 (unless (ruby-singleton-class-p (match-beginning 0))
+                (put-text-property (match-beginning 7) (match-end 7)
+                                   'syntax-table (string-to-syntax "\""))
+                (ruby-syntax-propertize-heredoc end))))
           ;; Handle percent literals: %w(), %q{}, etc.
           ("\\(?:^\\|[[ \t\n<+(,=]\\)\\(%\\)[qQrswWx]?\\([[:punct:]]\\)"
            (1 (prog1 "|" (ruby-syntax-propertize-general-delimiters end)))))
@@ -1174,7 +1183,8 @@ See `add-log-current-defun-function'."
               (beginning-of-line)
               (while (re-search-forward ruby-here-doc-beg-re
                                         (line-end-position) t)
-                (push (concat (ruby-here-doc-end-match) "\n") res)))
+                (unless (ruby-singleton-class-p (match-beginning 0))
+                  (push (concat (ruby-here-doc-end-match) "\n") res))))
             (let ((start (point)))
               ;; With multiple openers on the same line, we don't know in which
               ;; part `start' is, so we have to go back to the beginning.
@@ -1310,7 +1320,8 @@ isn't in a string or another comment."
       (let ((old-point (point)) (case-fold-search nil))
         (beginning-of-line)
         (catch 'found-beg
-          (while (re-search-backward ruby-here-doc-beg-re nil t)
+          (while (and (re-search-backward ruby-here-doc-beg-re nil t)
+                      (not (ruby-singleton-class-p)))
             (if (not (or (ruby-in-ppss-context-p 'anything)
                          (ruby-here-doc-find-end old-point)))
                 (throw 'found-beg t)))))))
diff --git a/test/ChangeLog b/test/ChangeLog
index 03d43d7..86f3019 100644
--- a/test/ChangeLog
+++ b/test/ChangeLog
@@ -1,3 +1,9 @@
+2012-08-09  Dmitry Gutov  <[email-masked]>
+
+	* automated/ruby-mode-tests.el (ruby-should-indent)
+	(ruby-assert-state): New functions.
+	Add new tests.
+
 2012-07-29  David Engster  <[email-masked]>
 
 	* automated/xml-parse-tests.el (xml-parse-tests--qnames): New
diff --git a/test/automated/ruby-mode-tests.el b/test/automated/ruby-mode-tests.el
index 1a91f51..fbe1b8d 100644
--- a/test/automated/ruby-mode-tests.el
+++ b/test/automated/ruby-mode-tests.el
@@ -23,16 +23,39 @@
 
 (require 'ruby-mode)
 
-(ert-deftest indent-line-after-symbol-made-from-string-interpolation ()
+(defun ruby-should-indent (content column)
+  (with-temp-buffer
+    (insert content)
+    (ruby-mode)
+    (ruby-indent-line)
+    (should (= (current-column) column))))
+
+(defun ruby-assert-state (content &rest values-plist)
+  "Assert syntax state values at the end of CONTENT.
+
+VALUES-PLIST is a list with alternating index and value elements."
+  (with-temp-buffer
+    (insert content)
+    (ruby-mode)
+    (syntax-propertize (point))
+    (while values-plist
+      (should (eq (nth (car values-plist)
+                       (parse-partial-sexp (point-min) (point)))
+                  (cadr values-plist)))
+      (setq values-plist (cddr values-plist)))))
+
+(ert-deftest ruby-indent-after-symbol-made-from-string-interpolation ()
   "It can indent the line after symbol made using string interpolation."
-  (let ((initial-content "def foo(suffix)\n  :\"bar#{suffix}\"\n")
-        (expected-content "def foo(suffix)\n  :\"bar#{suffix}\"\n  "))
-    (with-temp-buffer
-      (insert initial-content)
-      (ruby-indent-line) ; Doesn't rely on text properties or the syntax table.
-      (let ((buffer-content (buffer-substring-no-properties (point-min)
-                                                            (point-max))))
-        (should (string= buffer-content expected-content))))))
+  (ruby-should-indent "def foo(suffix)\n  :\"bar#{suffix}\"\n"
+                      ruby-indent-level))
+
+(ert-deftest ruby-indent-after-js-style-symbol-with-block-beg-name ()
+  "JS-style hash symbol can have keyword name."
+  (ruby-should-indent "link_to \"home\", home_path, class: \"foo\"\n" 0))
+
+(ert-deftest ruby-discern-singleton-class-from-heredoc ()
+  (ruby-assert-state "foo <<asd\n" 3 ?\n)
+  (ruby-assert-state "class <<asd\n" 3 nil))
 
 (provide 'ruby-mode-tests)