branch: elpa/nix-mode
commit bf027132d04a6b16aa49606b7e7ea7d7913cd9d4
Merge: e820157994 2bb0a0c1f4
Author: Elis Hirwing <[email protected]>
Commit: GitHub <[email protected]>
Merge pull request #83 from j-piecuch/smie-tweaks
Improve indentation of let expressions
---
nix-mode.el | 28 ++++++++++++++-------------
tests/nix-mode-tests.el | 4 ++++
tests/testcases/smie-let.nix | 45 ++++++++++++++++++++++++++++++++++++++++++++
3 files changed, 64 insertions(+), 13 deletions(-)
diff --git a/nix-mode.el b/nix-mode.el
index 4fa219ce14..6e354cc19e 100644
--- a/nix-mode.el
+++ b/nix-mode.el
@@ -433,15 +433,21 @@ STRING-TYPE type of string based off of Emacs syntax
table types"
(`(:after . ,(guard (string-match-p nix-smie-indent-tokens-re
token)))
(nix-smie--indent-anchor))
- (`(:after . "in")
- (cond
- ((bolp) '(column . 0))
- ((<= (line-beginning-position)
- (save-excursion
- (forward-word)
- (smie-backward-sexp t)
- (point)))
- (smie-rule-parent))))
+ (`(,_ . "in")
+ (let ((bol (line-beginning-position)))
+ (forward-word)
+ ;; Go back to the corresponding "let".
+ (smie-backward-sexp t)
+ (pcase kind
+ (:before
+ (if (smie-rule-hanging-p)
+ (nix-smie--indent-anchor 0)
+ `(column . ,(current-column))))
+ (:after
+ (cond
+ ((bolp) '(column . 0))
+ ((<= bol (point))
+ `(column . ,(current-column))))))))
(`(:after . "nonsep-;")
(forward-char)
(backward-sexp)
@@ -453,10 +459,6 @@ STRING-TYPE type of string based off of Emacs syntax table
types"
(nix-smie--indent-anchor)))
(`(:after . ",")
(smie-rule-parent tab-width))
- (`(:before . "in")
- (forward-word)
- (smie-backward-sexp t)
- (nix-smie--indent-anchor 0))
(`(:before . ",")
;; The parent is either the enclosing "{" or some previous ",".
;; In both cases this is what we want to align to.
diff --git a/tests/nix-mode-tests.el b/tests/nix-mode-tests.el
index 2a58e0d67c..f3e4a77b98 100644
--- a/tests/nix-mode-tests.el
+++ b/tests/nix-mode-tests.el
@@ -197,5 +197,9 @@ Related issue: https://github.com/NixOS/nix-mode/issues/72"
"Proper indentation of closing parentheses."
(with-nix-mode-test ("smie-close-parens.nix" :indent 'smie-indent-line)))
+(ert-deftest nix-mode-test-let-smie ()
+ "Proper indentation of let expressions."
+ (with-nix-mode-test ("smie-let.nix" :indent 'smie-indent-line)))
+
(provide 'nix-mode-tests)
;;; nix-mode-tests.el ends here
diff --git a/tests/testcases/smie-let.nix b/tests/testcases/smie-let.nix
new file mode 100644
index 0000000000..ab21db86b4
--- /dev/null
+++ b/tests/testcases/smie-let.nix
@@ -0,0 +1,45 @@
+let
+ foo = 42;
+ bar = let foo = 42; in
+ let bar = 42; in
+ 42;
+ baz = 42;
+in
+let foo = 42; in
+let foo = 42;
+ bar = 42;
+in
+let foo = let
+ bar = 42;
+ in
+ 42;
+ bar = 42;
+in
+let
+ foo = let
+ bar = 42;
+ in
+ 42;
+ bar = 42;
+in
+let foo = 42;
+in
+{
+ foo = let
+ bar = 42;
+ in
+ 42;
+
+ foo = let bar = 42;
+ in 42;
+
+ foo = let bar = 42; in
+ 42;
+
+ foo =
+ let
+ foo = 42;
+ bar = 42;
+ in
+ 42;
+}