emacs-diffs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Emacs-diffs] trunk r115020: * lisp/progmodes/ruby-mode.el (ruby-smie-gr


From: Dmitry Gutov
Subject: [Emacs-diffs] trunk r115020: * lisp/progmodes/ruby-mode.el (ruby-smie-grammar): Improve precedences
Date: Fri, 08 Nov 2013 02:31:57 +0000
User-agent: Bazaar (2.6b2)

------------------------------------------------------------
revno: 115020
revision-id: address@hidden
parent: address@hidden
committer: Dmitry Gutov <address@hidden>
branch nick: trunk
timestamp: Fri 2013-11-08 04:31:51 +0200
message:
  * lisp/progmodes/ruby-mode.el (ruby-smie-grammar): Improve precedences
  of "and", "or", "&&" and "||".
  (ruby-smie--args-separator-p): Prohibit keyword "do" as the first
  argument.  Prohibit opening curly brace because it could only be a
  block opener in that position.
  (ruby-smie--forward-token, ruby-smie--backward-token): Separate
  "|" from "&" or "*" going after it.  That can happen in block
  arguments.
  (ruby-smie--indent-to-stmt): New function, seeks the end of
  previous statement or beginning of buffer.
  (ruby-smie-rules): Use it.
  (ruby-smie-rules): Check if there's a ":" before a curly block
  opener candidate; if there is, it's a hash.
  
  * test/indent/ruby.rb: New examples.
modified:
  lisp/ChangeLog                 changelog-20091113204419-o5vbwnq5f7feedwu-1432
  lisp/progmodes/ruby-mode.el    
rubymode.el-20091113204419-o5vbwnq5f7feedwu-8804
  test/ChangeLog                 changelog-20091113204419-o5vbwnq5f7feedwu-8588
  test/indent/ruby.rb            ruby.rb-20120424165921-h044139hbrd7snvw-1
=== modified file 'lisp/ChangeLog'
--- a/lisp/ChangeLog    2013-11-07 19:30:43 +0000
+++ b/lisp/ChangeLog    2013-11-08 02:31:51 +0000
@@ -1,3 +1,19 @@
+2013-11-08  Dmitry Gutov  <address@hidden>
+
+       * progmodes/ruby-mode.el (ruby-smie-grammar): Improve precedences
+       of "and", "or", "&&" and "||".
+       (ruby-smie--args-separator-p): Prohibit keyword "do" as the first
+       argument.  Prohibit opening curly brace because it could only be a
+       block opener in that position.
+       (ruby-smie--forward-token, ruby-smie--backward-token): Separate
+       "|" from "&" or "*" going after it.  That can happen in block
+       arguments.
+       (ruby-smie--indent-to-stmt): New function, seeks the end of
+       previous statement or beginning of buffer.
+       (ruby-smie-rules): Use it.
+       (ruby-smie-rules): Check if there's a ":" before a curly block
+       opener candidate; if there is, it's a hash.
+
 2013-11-07  Stefan Monnier  <address@hidden>
 
        * emacs-lisp/cl-macs.el (cl-symbol-macrolet): Use macroexp-progn.

=== modified file 'lisp/progmodes/ruby-mode.el'
--- a/lisp/progmodes/ruby-mode.el       2013-11-07 03:02:01 +0000
+++ b/lisp/progmodes/ruby-mode.el       2013-11-08 02:31:51 +0000
@@ -277,7 +277,10 @@
     (smie-bnf->prec2
      '((id)
        (insts (inst) (insts ";" insts))
-       (inst (exp) (inst "iuwu-mod" exp))
+       (inst (exp) (inst "iuwu-mod" exp)
+             ;; Somewhat incorrect (both can be used multiple times),
+             ;; but avoids lots of conflicts:
+             (exp "and" exp) (exp "or" exp))
        (exp  (exp1) (exp "," exp) (exp "=" exp)
              (id " @ " exp)
              (exp "." id))
@@ -323,14 +326,13 @@
        (left "+" "-")
        (left "*" "/" "%" "**")
        ;; (left "|") ; FIXME: Conflicts with | after block parameters.
+       (left "&&" "||")
        (left "^" "&")
        (nonassoc "<=>")
        (nonassoc ">" ">=" "<" "<=")
        (nonassoc "==" "===" "!=")
        (nonassoc "=~" "!~")
-       (left "<<" ">>")
-       (left "&&" "||")
-       (left "and" "or"))))))
+       (left "<<" ">>"))))))
 
 (defun ruby-smie--bosp ()
   (save-excursion (skip-chars-backward " \t")
@@ -379,13 +381,11 @@
    (save-excursion
      (goto-char pos)
      (or (and (eq (char-syntax (char-after)) ?w)
-              ;; FIXME: Also "do".  But alas, that breaks some
-              ;; indentation cases.
               (not (looking-at (regexp-opt '("unless" "if" "while" "until"
-                                             "else" "elsif" "end" "and" "or")
+                                             "else" "elsif" "do" "end" "and" 
"or")
                                            'symbols))))
          (memq (syntax-after pos) '(7 15))
-         (looking-at "\\s(\\|[-+!~:]\\sw")))))
+         (looking-at "[([]\\|[-+!~:]\\sw")))))
 
 (defun ruby-smie--at-dot-call ()
   (and (eq ?w (char-syntax (following-char)))
@@ -424,7 +424,9 @@
            ((member tok '("unless" "if" "while" "until"))
             (if (save-excursion (forward-word -1) (ruby-smie--bosp))
                 tok "iuwu-mod"))
-           ((equal tok "|")
+           ((string-match "|[*&]?" tok)
+            (forward-char (- 1 (length tok)))
+            (setq tok "|")
             (if (ruby-smie--opening-pipe-p) "opening-|" tok))
            ((and (equal tok "") (looking-at "\\\\\n"))
             (goto-char (match-end 0)) (ruby-smie--forward-token))
@@ -466,6 +468,9 @@
               tok "iuwu-mod"))
          ((equal tok "|")
           (if (ruby-smie--opening-pipe-p) "opening-|" tok))
+         ((string-match-p "|[*&]" tok)
+          (forward-char 1)
+          (substring tok 1))
          ((and (equal tok "") (eq ?\\ (char-before)) (looking-at "\n"))
           (forward-char -1) (ruby-smie--backward-token))
          ((equal tok "do")
@@ -478,6 +483,16 @@
            (t ";")))
          (t tok)))))))
 
+(defun ruby-smie--indent-to-stmt ()
+  (save-excursion
+    (let (parent)
+      (while (not (or (eq (car parent) t)
+                      (equal (nth 2 parent) ";")))
+        (setq parent (let (smie--parent) (smie-indent--parent)))
+        (when (numberp (nth 1 parent))
+          (goto-char (nth 1 parent))))
+      (cons 'column (smie-indent-virtual)))))
+
 (defun ruby-smie-rules (kind token)
   (pcase (cons kind token)
     (`(:elem . basic) ruby-indent-level)
@@ -498,9 +513,12 @@
     (`(:before . ,(or `"(" `"[" `"{"))
      (cond
       ((and (equal token "{")
-            (not (smie-rule-prev-p "(" "{" "[" "," "=>" "=" "return" ";")))
+            (not (smie-rule-prev-p "(" "{" "[" "," "=>" "=" "return" ";"))
+            (save-excursion
+              (forward-comment -1)
+              (not (eq (preceding-char) ?:))))
        ;; Curly block opener.
-       (smie-rule-parent))
+       (ruby-smie--indent-to-stmt))
       ((smie-rule-hanging-p)
        ;; Treat purely syntactic block-constructs as being part of their 
parent,
        ;; when the opening statement is hanging.
@@ -508,7 +526,7 @@
          (when (eq t (car state)) (goto-char (cadr state))))
        (cons 'column  (smie-indent-virtual)))))
     (`(:after . " @ ") (smie-rule-parent))
-    (`(:before . "do") (smie-rule-parent))
+    (`(:before . "do") (ruby-smie--indent-to-stmt))
     (`(,(or :before :after) . ".")
      (unless (smie-rule-parent-p ".")
        (smie-rule-parent ruby-indent-level)))

=== modified file 'test/ChangeLog'
--- a/test/ChangeLog    2013-11-06 17:56:48 +0000
+++ b/test/ChangeLog    2013-11-08 02:31:51 +0000
@@ -1,3 +1,7 @@
+2013-11-08  Dmitry Gutov  <address@hidden>
+
+       * indent/ruby.rb: New examples.
+
 2013-11-06  Glenn Morris  <address@hidden>
 
        * automated/Makefile.in (setwins): Avoid accidental matches.

=== modified file 'test/indent/ruby.rb'
--- a/test/indent/ruby.rb       2013-11-07 03:02:01 +0000
+++ b/test/indent/ruby.rb       2013-11-08 02:31:51 +0000
@@ -117,6 +117,9 @@
 e = 8 + 9   \
     + 10         # '\' needed
 
+foo = obj.bar { |m| tee(m) } +
+      obj.qux { |m| hum(m) }
+
 begin
   foo
 ensure
@@ -210,6 +213,14 @@
 foo and
   bar
 
+foo > bar &&
+  tee < qux
+
+zux do
+  foo == bar and
+    tee == qux
+end
+
 foo ^
   bar
 
@@ -244,6 +255,14 @@
   qux
 end
 
+foo do |*args|
+  tee
+end
+
+bar do |&block|
+  tee
+end
+
 foo = [1, 2, 3].map do |i|
   i + 1
 end
@@ -252,8 +271,16 @@
   bar
 end
 
-# Examples below still fail with `ruby-use-smie' on:
-
-bar.foo(tee) do # "." is parent to "do"; it shouldn't be.
-  bar
+bar.foo(tee) do
+  bar
+end
+
+bar.foo(tee) {
+  bar
+}
+
+bar 1 do
+  foo 2 do
+    tee
+  end
 end


reply via email to

[Prev in Thread] Current Thread [Next in Thread]