Commit 7ccae3b1 authored by Stefan Monnier's avatar Stefan Monnier
Browse files

* lisp/progmodes/ruby-mode.el: Fix recently added tests.

(ruby-smie-grammar): Add - and +.
(ruby-smie--redundant-do-p, ruby-smie--forward-id)
(ruby-smie--backward-id): New functions.
(ruby-smie--forward-token, ruby-smie--backward-token): Use them.
(ruby-smie-rules): Handle hanging do.  Get rid of hack, not needed
any more.
* test/indent/ruby.rb: Add a few more tests; adjust some indentation.
parent c8af480d
2013-10-07 Stefan Monnier <monnier@iro.umontreal.ca>
* progmodes/ruby-mode.el: Fix recently added tests.
(ruby-smie-grammar): Add - and +.
(ruby-smie--redundant-do-p, ruby-smie--forward-id)
(ruby-smie--backward-id): New functions.
(ruby-smie--forward-token, ruby-smie--backward-token): Use them.
(ruby-smie-rules): Handle hanging do. Get rid of hack, not needed
any more.
2013-10-07 Leo Liu <sdl.web@gmail.com>
* register.el (register-preview-delay)
......
......@@ -246,7 +246,7 @@ Also ignores spaces after parenthesis when 'space."
'((id)
(insts (inst) (insts ";" insts))
(inst (exp) (inst "iuwu-mod" exp))
(exp (exp1) (exp "," exp) (exp "=" exp))
(exp (exp1) (exp "," exp) (exp "=" exp) (exp "-" exp) (exp "+" exp))
(exp1 (exp2) (exp2 "?" exp1 ":" exp1))
(exp2 ("def" insts "end")
("begin" insts-rescue-insts "end")
......@@ -274,7 +274,7 @@ Also ignores spaces after parenthesis when 'space."
(itheni (insts) (exp "then" insts))
(ielsei (itheni) (itheni "else" insts))
(if-body (ielsei) (if-body "elsif" if-body)))
'((nonassoc "in") (assoc ";") (assoc ",") (right "="))
'((nonassoc "in") (assoc ";") (assoc ",") (right "=") (assoc "-" "+"))
'((assoc "when"))
'((assoc "elsif"))
'((assoc "rescue" "ensure"))
......@@ -297,6 +297,11 @@ Also ignores spaces after parenthesis when 'space."
(or (equal tok "?")
(string-match "\\`\\s." tok))))))))
(defun ruby-smie--redundant-do-p (&optional skip)
(save-excursion
(if skip (backward-word 1))
(member (nth 2 (smie-backward-sexp ";")) '("while"))))
(defun ruby-smie--opening-pipe-p ()
(save-excursion
(if (eq ?| (char-before)) (forward-char -1))
......@@ -304,6 +309,15 @@ Also ignores spaces after parenthesis when 'space."
(or (eq ?\{ (char-before))
(looking-back "\\_<do" (- (point) 2)))))
(defun ruby-smie--forward-id ()
(when (and (not (eobp))
(eq ?w (char-syntax (char-after))))
(let ((tok (smie-default-forward-token)))
(when (eq ?. (char-after))
(forward-char 1)
(setq tok (concat tok "." (ruby-smie--forward-id))))
tok)))
(defun ruby-smie--forward-token ()
(skip-chars-forward " \t")
(cond
......@@ -316,17 +330,37 @@ Also ignores spaces after parenthesis when 'space."
(forward-comment (point-max))
(if (looking-at ":\\s.+")
(progn (goto-char (match-end 0)) (match-string 0)) ;; bug#15208.
(let ((tok (smie-default-forward-token)))
(cond
((member tok '("unless" "if" "while" "until"))
(if (save-excursion (forward-word -1) (ruby-smie--bosp))
tok "iuwu-mod"))
(let ((tok (smie-default-forward-token)))
(when (eq ?. (char-after))
(forward-char 1)
(setq tok (concat tok "." (ruby-smie--forward-id))))
(cond
((member tok '("unless" "if" "while" "until"))
(if (save-excursion (forward-word -1) (ruby-smie--bosp))
tok "iuwu-mod"))
((equal tok "|")
(if (ruby-smie--opening-pipe-p) "opening-|" tok))
((and (equal tok "") (looking-at "\\\\\n"))
(goto-char (match-end 0)) (ruby-smie--forward-token))
((equal tok "do")
(cond
((not (ruby-smie--redundant-do-p 'skip)) tok)
((> (save-excursion (forward-comment (point-max)) (point))
(line-end-position))
(ruby-smie--forward-token)) ;Fully redundant.
(t ";")))
((equal tok ".") (concat tok (ruby-smie--forward-id)))
(t tok)))))))
(defun ruby-smie--backward-id ()
(when (and (not (bobp))
(eq ?w (char-syntax (char-before))))
(let ((tok (smie-default-backward-token)))
(when (eq ?. (char-before))
(forward-char -1)
(setq tok (concat (ruby-smie--backward-id) "." tok)))
tok)))
(defun ruby-smie--backward-token ()
(let ((pos (point)))
(forward-comment (- (point)))
......@@ -336,6 +370,9 @@ Also ignores spaces after parenthesis when 'space."
((and (bolp) (not (bobp))) "") ;Presumably a heredoc.
(t
(let ((tok (smie-default-backward-token)))
(when (eq ?. (char-before))
(forward-char -1)
(setq tok (concat (ruby-smie--backward-id) "." tok)))
(when (and (eq ?: (char-before)) (string-match "\\`\\s." tok))
(forward-char -1) (setq tok (concat ":" tok))) ;; bug#15208.
(cond
......@@ -346,6 +383,16 @@ Also ignores spaces after parenthesis when 'space."
(if (ruby-smie--opening-pipe-p) "opening-|" tok))
((and (equal tok "") (eq ?\\ (char-before)) (looking-at "\n"))
(forward-char -1) (ruby-smie--backward-token))
((equal tok "do")
(cond
((not (ruby-smie--redundant-do-p)) tok)
((> (save-excursion (forward-word 1)
(forward-comment (point-max)) (point))
(line-end-position))
(ruby-smie--backward-token)) ;Fully redundant.
(t ";")))
((equal tok ".")
(concat (ruby-smie--backward-id) tok))
(t tok)))))))
(defun ruby-smie-rules (kind token)
......@@ -370,24 +417,22 @@ Also ignores spaces after parenthesis when 'space."
(if (smie-rule-hanging-p) (smie-rule-parent)))
(`(:after . "=") 2)
(`(:before . "do")
(when
(save-excursion
(forward-word 1) ;Skip "do"
(skip-chars-forward " \t")
(and (equal (save-excursion (ruby-smie--forward-token)) "opening-|")
(save-excursion (forward-sexp 1)
(skip-chars-forward " \t")
(or (eolp)
(looking-at comment-start-skip)))))
(when (or (smie-rule-hanging-p)
(save-excursion
(forward-word 1) ;Skip "do"
(skip-chars-forward " \t")
(and (equal (save-excursion (ruby-smie--forward-token))
"opening-|")
(save-excursion (forward-sexp 1)
(skip-chars-forward " \t")
(or (eolp)
(looking-at comment-start-skip))))))
;; `(column . ,(smie-indent-virtual))
(smie-rule-parent)))
(`(:before . ,(or `"else" `"then" `"elsif" `"rescue" `"ensure")) 0)
(`(:before . ,(or `"when"))
(if (not (smie-rule-sibling-p)) 0)) ;; ruby-indent-level
;; Hack attack: Since newlines are separators, don't try to align args that
;; appear on a separate line. "" is for the case where the "previous
;; separator" was not an implicit ";" but the BOB.
(`(:list-intro . ,(or `";" `"")) t)))
))
(defun ruby-imenu-create-index-in-block (prefix beg end)
"Create an imenu index of methods inside a block."
......
......@@ -99,6 +99,14 @@ def test2 (arg)
do_something
end
# Example from http://www.ruby-doc.org/docs/ProgrammingRuby/html/language.html
d = 4 + 5 + # no '\' needed
6 + 7
# Example from http://www.ruby-doc.org/docs/ProgrammingRuby/html/language.html
e = 8 + 9 \
+ 10 # '\' needed
begin
foo
ensure
......@@ -109,20 +117,27 @@ def test2 (arg)
MSG = 'Separate every 3 digits in the integer portion of a number' +
'with underscores(_).'
# Examples below fail with SMIE.
class C
def foo
self.end
D.new.class
end
end
a = foo(j, k) -
bar_tee
bar_tee
while a < b do # "do" is optional
foo
end
desc "foo foo" \
"bar bar"
"bar bar"
foo.
bar
# FIXME: is this really valid Ruby? Isn't the newline after "foo" treated as
# an implicit semi-colon?
foo
.bar
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment