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

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

[nongnu] elpa/nix-mode bb602e160f 286/500: Cleanups


From: ELPA Syncer
Subject: [nongnu] elpa/nix-mode bb602e160f 286/500: Cleanups
Date: Sat, 29 Jan 2022 08:27:14 -0500 (EST)

branch: elpa/nix-mode
commit bb602e160fa5bb8ae15b113ebf308f0fb30b3aa9
Author: Matthew Bauer <mjbauer95@gmail.com>
Commit: Matthew Bauer <mjbauer95@gmail.com>

    Cleanups
    
    Get rid of extra customs
---
 nix-mode.el | 107 ++++++++++++++++++++----------------------------------------
 1 file changed, 36 insertions(+), 71 deletions(-)

diff --git a/nix-mode.el b/nix-mode.el
index 05ba967388..bb8f1c2f67 100644
--- a/nix-mode.el
+++ b/nix-mode.el
@@ -37,30 +37,6 @@ Valid functions for this are:
   :group 'nix-mode
   :type 'function)
 
-(defcustom nix-mode-caps
-  '(" =[ \n]" "\(" "\{" "\\[" "\\bwith\\b" "\\blet\\b" "\\binherit\\b")
-  "Regular expressions to consider expression caps."
-  :group 'nix-mode
-  :type '(repeat string))
-
-(defcustom nix-mode-ends
-  '(";" "\)" "\\]" "\}" "\\bin\\b")
-  "Regular expressions to consider expression ends."
-  :group 'nix-mode
-  :type '(repeat string))
-
-(defcustom nix-mode-quotes
-  '("''" "\"")
-  "Regular expressions to consider expression quotes."
-  :group 'nix-mode
-  :type '(repeat string))
-
-(defcustom nix-mode-comments
-  '("#" "/\\*" "\\*/")
-  "Regular expressions to consider comment codes."
-  :group 'nix-mode
-  :type '(repeat string))
-
 (defgroup nix-faces nil
   "Nix faces."
   :group 'nix-mode
@@ -96,12 +72,12 @@ Valid functions for this are:
   "Face used to highlight Nix antiquotes."
   :group 'nix-faces)
 
-(defvar nix-system-types
+;;; Constants
+
+(defconst nix-system-types
   '("x86_64-linux" "i686-linux" "aarch64-linux" "x86_64-darwin")
   "List of supported systems.")
 
-;;; Syntax coloring
-
 (defconst nix-keywords
   '("if" "then"
     "else" "with"
@@ -120,6 +96,8 @@ Valid functions for this are:
 (defconst nix-warning-keywords
   '("assert" "abort" "throw"))
 
+;;; Regexps
+
 (defconst nix-re-file-path
   "[a-zA-Z0-9._\\+-]*\\(/[a-zA-Z0-9._\\+-]+\\)+")
 
@@ -132,6 +110,15 @@ Valid functions for this are:
 (defconst nix-re-variable-assign
   "\\<\\([a-zA-Z_][a-zA-Z0-9_'\-\.]*\\)[ \t]*=[^=]")
 
+(defconst nix-re-caps
+  " =[ \n]\\|\(\\|\{\\|\\[\\|\\bwith\\b\\|\\blet\\b\\|\\binherit\\b")
+
+(defconst nix-re-ends ";\\|\)\\|\\]\\|\}\\|\\bin\\b")
+
+(defconst nix-re-quotes "''\\|\"")
+
+(defconst nix-re-comments "#\\|/\\*\\|\\*/")
+
 (defconst nix-font-lock-keywords
   `((,(regexp-opt nix-keywords 'symbols) 0 'nix-keyword-face)
     (,(regexp-opt nix-warning-keywords 'symbols) 0 'nix-keyword-warning-face)
@@ -149,8 +136,6 @@ Valid functions for this are:
   (make-abbrev-table)
   "Abbrev table for Nix mode.")
 
-(makunbound 'nix-mode-syntax-table)
-
 (defvar nix-mode-syntax-table
   (let ((table (make-syntax-table)))
     (modify-syntax-entry ?/ ". 14" table)
@@ -348,11 +333,6 @@ STRING-TYPE type of string based off of Emacs syntax table 
types"
 
 ;;; Indentation
 
-(defun nix--inside-string-or-comment ()
-  "Determine whether we are inside of a string or comment."
-  (or (nix--get-string-type (nix--get-parse-state (point)))
-      (nth 4 (syntax-ppss))))
-
 (defun nix-find-backward-matching-token ()
   "Find the previous Nix token."
   (cond
@@ -360,7 +340,8 @@ STRING-TYPE type of string based off of Emacs syntax table 
types"
     (let ((counter 1))
       (while (and (> counter 0)
                   (re-search-backward "\\b\\(let\\|in\\)\\b" nil t))
-        (unless (nix--inside-string-or-comment)
+        (unless (or (nix--get-string-type (nix--get-parse-state (point)))
+                    (nix-is-comment-p))
           (setq counter (cond ((looking-at "let") (- counter 1))
                               ((looking-at "in") (+ counter 1))))))
       counter ))
@@ -413,37 +394,12 @@ STRING-TYPE type of string based off of Emacs syntax 
table types"
                                         (+ 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"
-  (declare (indent defun))
-  (string-join parts "\\|"))
-
-(defun nix-mode-caps-regexp ()
-  "Return regexp for matching expression caps."
-  (nix-mode-make-regexp nix-mode-caps))
-
-(defun nix-mode-ends-regexp ()
-  "Return regexp for matching expression ends."
-  (nix-mode-make-regexp nix-mode-ends))
-
-(defun nix-mode-quotes-regexp ()
-  "Return regexp for matching string quotes."
-  (nix-mode-make-regexp nix-mode-quotes))
-
-(defun nix-mode-comments-regexp ()
-  "Return regexp for matching comments."
-  (nix-mode-make-regexp nix-mode-comments))
-
-(defun nix-mode-combined-regexp ()
-  "Return combined regexp for matching items of interest."
-  (nix-mode-make-regexp (append nix-mode-caps
-                                nix-mode-ends
-                                nix-mode-quotes)))
-
 (defun nix-mode-search-backward ()
   "Search backward for items of interest regarding indentation."
-  (re-search-backward (nix-mode-combined-regexp) nil t))
+  (re-search-backward nix-re-ends nil t)
+  (re-search-backward nix-re-quotes nil t)
+  (re-search-backward nix-re-comments nil t)
+  (re-search-backward nix-re-caps nil t))
 
 (defun nix-indent-expression-start ()
   "Indent the start of a nix expression."
@@ -460,17 +416,17 @@ PARTS a list of regexps"
       ;; end is found
       (while (and (not done) (nix-mode-search-backward))
         (cond
-         ((looking-at (nix-mode-quotes-regexp))
+         ((looking-at nix-re-quotes)
           ;; skip over strings entirely
-          (re-search-backward (nix-mode-quotes-regexp) nil t))
-         ((looking-at (nix-mode-comments-regexp))
+          (re-search-backward nix-re-quotes nil t))
+         ((looking-at nix-re-comments)
           ;; skip over comments entirely
-          (re-search-backward (nix-mode-comments-regexp) nil t))
-         ((looking-at (nix-mode-ends-regexp))
+          (re-search-backward nix-re-comments nil t))
+         ((looking-at nix-re-ends)
           ;; count the matched end
           ;; this means we expect to find at least one more cap
           (setq ends (+ ends 1)))
-         ((looking-at (nix-mode-caps-regexp))
+         ((looking-at nix-re-caps)
           ;; we found at least one cap
           ;; this means our function will return true
           ;; this signals to the caller we handled the indentation
@@ -525,7 +481,7 @@ PARTS a list of regexps"
             ;; comment
             ((save-excursion
                (beginning-of-line)
-               (nth 4 (syntax-ppss)))
+               (nix-is-comment-p))
              (indent-line-to (nix-indent-prev-level)))
 
             ;; string
@@ -562,6 +518,15 @@ PARTS a list of regexps"
            (point))))
     (when (> end-of-indentation (point)) (goto-char end-of-indentation))))
 
+(defun nix-is-comment-p ()
+  "Whether we are in a comment."
+  (nth 3 (syntax-ppss)))
+
+(defun nix-is-string-p ()
+  "Whether we are in a string."
+  (or (looking-at nix-re-quotes)
+      (nix--get-string-type (nix--get-parse-state (point)))))
+
 ;;;###autoload
 (defun nix-mode-ffap-nixpkgs-path (str)
   "Support `ffap' for <nixpkgs> declarations.



reply via email to

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