Commit f069bba8 authored by Stefan Monnier's avatar Stefan Monnier

* lisp/progmodes/ruby-mode.el (ruby-smie-grammar): Add rule for formal

params of lambda expressions.
(ruby-smie--implicit-semi-p): Refine rule.
(ruby-smie--opening-pipe-p): New function.
(ruby-smie--forward-token, ruby-smie--backward-token): Handle Ruby
symbols and matched |...| for formal params.
(ruby-smie-rules): Don't let the formal params of a "do" prevent it
from being treated as hanging.  Handle "rescue".

Fixes: debbugs:15208
parent 6758b6a8
2013-08-29 Stefan Monnier <monnier@iro.umontreal.ca>
* progmodes/ruby-mode.el (ruby-smie-grammar): Add rule for formal
params of lambda expressions.
(ruby-smie--implicit-semi-p): Refine rule (bug#15208).
(ruby-smie--opening-pipe-p): New function.
(ruby-smie--forward-token, ruby-smie--backward-token): Handle Ruby
symbols and matched |...| for formal params.
(ruby-smie-rules): Don't let the formal params of a "do" prevent it
from being treated as hanging. Handle "rescue".
2013-08-29 Glenn Morris <rgm@gnu.org> 2013-08-29 Glenn Morris <rgm@gnu.org>
* progmodes/cc-engine.el (c-pull-open-brace): * progmodes/cc-engine.el (c-pull-open-brace):
......
...@@ -254,11 +254,13 @@ Also ignores spaces after parenthesis when 'space." ...@@ -254,11 +254,13 @@ Also ignores spaces after parenthesis when 'space."
("for" for-body "end") ("for" for-body "end")
("[" expseq "]") ("[" expseq "]")
("{" hashvals "}") ("{" hashvals "}")
("{" insts "}")
("while" insts "end") ("while" insts "end")
("until" insts "end") ("until" insts "end")
("unless" insts "end") ("unless" insts "end")
("if" if-body "end") ("if" if-body "end")
("case" cases "end")) ("case" cases "end"))
(formal-params ("opening-|" exp "|"))
(for-body (for-head ";" insts)) (for-body (for-head ";" insts))
(for-head (id "in" exp)) (for-head (id "in" exp))
(cases (exp "then" insts) ;; FIXME: Ruby also allows (exp ":" insts). (cases (exp "then" insts) ;; FIXME: Ruby also allows (exp ":" insts).
...@@ -285,10 +287,20 @@ Also ignores spaces after parenthesis when 'space." ...@@ -285,10 +287,20 @@ Also ignores spaces after parenthesis when 'space."
(save-excursion (save-excursion
(skip-chars-backward " \t") (skip-chars-backward " \t")
(not (or (bolp) (not (or (bolp)
(memq (char-before) '(?\; ?- ?+ ?* ?/ ?:)) (and (memq (char-before) '(?\; ?- ?+ ?* ?/ ?: ?.))
;; Make sure it's not the end of a regexp.
(not (eq (car (syntax-after (1- (point)))) 7)))
(and (memq (char-before) '(?\? ?=)) (and (memq (char-before) '(?\? ?=))
(not (memq (char-syntax (char-before (1- (point)))) (let ((tok (ruby-smie--backward-token)))
'(?w ?_)))))))) (or (equal tok "?")
(string-match "\\`\\s." tok))))))))
(defun ruby-smie--opening-pipe-p ()
(save-excursion
(if (eq ?| (char-before)) (forward-char -1))
(skip-chars-backward " \t\n")
(or (eq ?\{ (char-before))
(looking-back "\\_<do" (- (point) 2)))))
(defun ruby-smie--forward-token () (defun ruby-smie--forward-token ()
(skip-chars-forward " \t") (skip-chars-forward " \t")
...@@ -299,12 +311,16 @@ Also ignores spaces after parenthesis when 'space." ...@@ -299,12 +311,16 @@ Also ignores spaces after parenthesis when 'space."
(if (eolp) (forward-char 1) (forward-comment 1)) (if (eolp) (forward-char 1) (forward-comment 1))
";") ";")
(forward-comment (point-max)) (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))) (let ((tok (smie-default-forward-token)))
(cond (cond
((member tok '("unless" "if" "while" "until")) ((member tok '("unless" "if" "while" "until"))
(if (save-excursion (forward-word -1) (ruby-smie--bosp)) (if (save-excursion (forward-word -1) (ruby-smie--bosp))
tok "iuwu-mod")) tok "iuwu-mod"))
(t tok))))) ((equal tok "|")
(if (ruby-smie--opening-pipe-p) "opening-|" tok))
(t tok))))))
(defun ruby-smie--backward-token () (defun ruby-smie--backward-token ()
(let ((pos (point))) (let ((pos (point)))
...@@ -314,10 +330,14 @@ Also ignores spaces after parenthesis when 'space." ...@@ -314,10 +330,14 @@ Also ignores spaces after parenthesis when 'space."
(progn (skip-chars-forward " \t") (progn (skip-chars-forward " \t")
";") ";")
(let ((tok (smie-default-backward-token))) (let ((tok (smie-default-backward-token)))
(when (and (eq ?: (char-before)) (string-match "\\`\\s." tok))
(forward-char -1) (setq tok (concat ":" tok))) ;; bug#15208.
(cond (cond
((member tok '("unless" "if" "while" "until")) ((member tok '("unless" "if" "while" "until"))
(if (ruby-smie--bosp) (if (ruby-smie--bosp)
tok "iuwu-mod")) tok "iuwu-mod"))
((equal tok "|")
(if (ruby-smie--opening-pipe-p) "opening-|" tok))
(t tok)))))) (t tok))))))
(defun ruby-smie-rules (kind token) (defun ruby-smie-rules (kind token)
...@@ -332,7 +352,19 @@ Also ignores spaces after parenthesis when 'space." ...@@ -332,7 +352,19 @@ Also ignores spaces after parenthesis when 'space."
;; For (invalid) code between switch and case. ;; For (invalid) code between switch and case.
;; (if (smie-parent-p "switch") 4) ;; (if (smie-parent-p "switch") 4)
0)) 0))
(`(:before . ,(or `"else" `"then" `"elsif")) 0) (`(: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)))))
;; `(column . ,(smie-indent-virtual))
(smie-rule-parent)))
(`(:before . ,(or `"else" `"then" `"elsif" `"rescue")) 0)
(`(:before . ,(or `"when")) (`(:before . ,(or `"when"))
(if (not (smie-rule-sibling-p)) 0)) ;; ruby-indent-level (if (not (smie-rule-sibling-p)) 0)) ;; ruby-indent-level
;; Hack attack: Since newlines are separators, don't try to align args that ;; Hack attack: Since newlines are separators, don't try to align args that
......
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