emacs-elpa-diffs
[Top][All Lists]
Advanced

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

[nongnu] elpa/nix-mode 7f968e8a7f 284/500: Merge pull request #61 from e


From: ELPA Syncer
Subject: [nongnu] elpa/nix-mode 7f968e8a7f 284/500: Merge pull request #61 from etu/master
Date: Sat, 29 Jan 2022 08:27:14 -0500 (EST)

branch: elpa/nix-mode
commit 7f968e8a7f955fb8f3aec7b00129cac3b3b5986f
Merge: 1512d02830 98426b94e5
Author: Matthew Bauer <mjbauer95@gmail.com>
Commit: GitHub <noreply@github.com>

    Merge pull request #61 from etu/master
    
    Add function to indent things in blocks for nix-indent-line
---
 nix-mode.el | 36 ++++++++++++++++++++++++++++++++++++
 1 file changed, 36 insertions(+)

diff --git a/nix-mode.el b/nix-mode.el
index d4bf6fc486..05ba967388 100644
--- a/nix-mode.el
+++ b/nix-mode.el
@@ -380,6 +380,39 @@ STRING-TYPE type of string based off of Emacs syntax table 
types"
                                     (current-indentation)))))
     (when matching-indentation (indent-line-to matching-indentation) t)))
 
+(defun nix-indent-first-line-in-block ()
+  "Indent the first line in a block."
+
+  (let ((matching-indentation (save-excursion
+                                ;; If we're on the first line of the buffer
+                                (if (= (line-number-at-pos) 1)
+                                    ;; Return that we want to ident to 
position 0 if we're on th
+                                    ;; first line. This fixes bad indent of 
things and avoid endless
+                                    ;; indent loop of tokens that would match 
below if we press tab
+                                    ;; on the first line and it happens to 
match any of the ones below.
+                                    0
+
+                                  ;; Go back to previous line that contain 
anything useful to check the
+                                  ;; contents of that line.
+                                  (beginning-of-line)
+                                  (skip-chars-backward "\n[:space:]")
+
+                                  ;; Grab the full string of the line before 
the one we're indenting
+                                  (let ((line (buffer-substring-no-properties 
(line-beginning-position) (line-end-position))))
+                                    ;; Then regex-match strings at the end of 
the line to detect if we need to indent the line after.
+                                    ;; We could probably add more things to 
look for here in the future.
+                                    (if (or (string-match "let$" line)
+                                            (string-match "import$" line)
+                                            (string-match "\\[$" line)
+                                            (string-match "=$" line)
+                                            (string-match "\($" line)
+                                            (string-match "\{$" line))
+
+                                        ;; If it matches any of the regexes 
above, grab the indent level
+                                        ;; of the line and add 2 to ident the 
line below this one.
+                                        (+ 2 (current-indentation))))))))
+    (when matching-indentation (indent-line-to matching-indentation) t)))
+
 (defun nix-mode-make-regexp (parts)
   "Combine the regexps into a single or-delimited regexp.
 PARTS a list of regexps"
@@ -514,6 +547,9 @@ PARTS a list of regexps"
                                             ) -1 0)
                                       )))))
 
+            ;; indent line after 'let', 'import', '[', '=', '(', '{'
+            ((nix-indent-first-line-in-block))
+
             ;; dedent '}', ']', ')' 'in'
             ((nix-indent-to-backward-match))
 



reply via email to

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