From 74881b6e068e209725527b3776aaaa7955d4fb4a Mon Sep 17 00:00:00 2001 From: Stefan Monnier Date: Fri, 21 Aug 2015 14:13:05 -0400 Subject: [PATCH] * lisp/emacs-lisp/smie.el (smie-indent-current-column): New fun (smie-indent-exps, smie-indent-keyword): Use it. * test/indent/css-mode.css: Test alignment with leading comment. --- lisp/emacs-lisp/smie.el | 25 +++++++++++++++++++++---- test/indent/css-mode.css | 2 +- 2 files changed, 22 insertions(+), 5 deletions(-) diff --git a/lisp/emacs-lisp/smie.el b/lisp/emacs-lisp/smie.el index 1bc5eb2872..0c24f796d1 100644 --- a/lisp/emacs-lisp/smie.el +++ b/lisp/emacs-lisp/smie.el @@ -1198,6 +1198,21 @@ Comments are treated as spaces." (forward-comment (- (point))) (<= (point) bol)))) +(defun smie-indent--current-column () + "Like `current-column', but if there's a comment before us, use that." + ;; This is used, so that when we align elements, we don't get + ;; toto = { /* foo, */ a, + ;; b } + ;; but + ;; toto = { /* foo, */ a, + ;; b } + (let ((pos (point)) + (lbp (line-beginning-position))) + (save-excursion + (unless (and (forward-comment -1) (>= (point) lbp)) + (goto-char pos)) + (current-column)))) + ;; Dynamically scoped. (defvar smie--parent) (defvar smie--after) (defvar smie--token) @@ -1577,7 +1592,9 @@ should not be computed on the basis of the following token." ;; So we use a heuristic here, which is that we only use virtual ;; if the parent is tightly linked to the child token (they're ;; part of the same BNF rule). - (if (car parent) (current-column) (smie-indent-virtual))))))))))) + (if (car parent) + (smie-indent--current-column) + (smie-indent-virtual))))))))))) (defun smie-indent-comment () "Compute indentation of a comment." @@ -1707,12 +1724,12 @@ should not be computed on the basis of the following token." ;; There's a previous element, and it's not special (it's not ;; the function), so let's just align with that one. (goto-char (car positions)) - (current-column)) + (smie-indent--current-column)) ((cdr positions) ;; We skipped some args plus the function and bumped into something. ;; Align with the first arg. (goto-char (cadr positions)) - (current-column)) + (smie-indent--current-column)) (positions ;; We're the first arg. (goto-char (car positions)) @@ -1720,7 +1737,7 @@ should not be computed on the basis of the following token." ;; We used to use (smie-indent-virtual), but that ;; doesn't seem right since it might then indent args less than ;; the function itself. - (current-column))))))) + (smie-indent--current-column))))))) (defvar smie-indent-functions '(smie-indent-fixindent smie-indent-bob smie-indent-close diff --git a/test/indent/css-mode.css b/test/indent/css-mode.css index faf91539d8..67a6b1e30b 100644 --- a/test/indent/css-mode.css +++ b/test/indent/css-mode.css @@ -29,7 +29,7 @@ a.b:c,d.e:f,g[h]:i,j[k]:l,.m.n:o,.p.q:r,.s[t]:u,.v[w]:x { /* bug:20282 */ } .x2 { - foo: bar; + /* foo: bar; */ foo2: bar2; bar: baz; } -- 2.39.2