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

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

[nongnu] elpa/nix-mode 98426b94e5 280/500: Add function to indent things


From: ELPA Syncer
Subject: [nongnu] elpa/nix-mode 98426b94e5 280/500: Add function to indent things in blocks for nix-indent-line
Date: Sat, 29 Jan 2022 08:27:14 -0500 (EST)

branch: elpa/nix-mode
commit 98426b94e53ddea5e835d6cad58474affca59572
Author: Elis Hirwing <elis@hirwing.se>
Commit: Elis Hirwing <elis@hirwing.se>

    Add function to indent things in blocks for nix-indent-line
    
    Add a function `nix-indent-first-line-in-block` that checks if the
    last word on the previous line is one of "let, import, [, =, (, {".
    Then it adds two spaces of indent to the one of the previous line.
    
    And if you stand on the first line of a file, it defaults to indenting
    the line to zero spaces.
---
 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]