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

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

[nongnu] elpa/clojure-ts-mode ff5d7e13dc 05/14: Semantic indentation of


From: ELPA Syncer
Subject: [nongnu] elpa/clojure-ts-mode ff5d7e13dc 05/14: Semantic indentation of method implementations
Date: Fri, 8 Sep 2023 18:59:18 -0400 (EDT)

branch: elpa/clojure-ts-mode
commit ff5d7e13dc53cc5da0e8139b04e02d90f61d9065
Author: Danny Freeman <danny@dfreeman.email>
Commit: Danny Freeman <danny@dfreeman.email>

    Semantic indentation of method implementations
    
    method implemenations like
    
    (deftype MyType
      Object
      (toString [this]
        "hello"))
    
    Look like regular function calls, but should be indented like defns.
    
    Found myself wanting to calling a node aunt or uncle, but nodes aren't
    gendered. Internet suggested auncle or pibling. Auncle sounds better to
    me.
---
 clojure-ts-mode.el | 25 +++++++++++++++++++++++--
 1 file changed, 23 insertions(+), 2 deletions(-)

diff --git a/clojure-ts-mode.el b/clojure-ts-mode.el
index 817e4721bb..9ff592f0e9 100644
--- a/clojure-ts-mode.el
+++ b/clojure-ts-mode.el
@@ -615,6 +615,26 @@ See `treesit-simple-indent-rules'."
        clojure-ts--symbols-with-body-expressions-regexp
        first-child)))))
 
+(defconst clojure-ts--reifying-symbol-regexp
+  (eval-and-compile
+    (rx line-start
+        (or "deftype" "defrecord"
+            "reify" "proxy" "extend-type" "extend-protocol")))
+  "A regular expression matching a symbol used to define a concrete type.")
+
+(defun clojure-ts--match-method-body (node parent _bol)
+  "Matches a `NODE' in the body of a `PARENT' method implementation.
+A method implementation referes to concrete implemntations being defined in
+forms like deftype, defrecord, reify, proxy, etc."
+  (and
+   (clojure-ts--list-node-p parent)
+   (let* ((grandparent (treesit-node-parent parent))
+          ;; auncle: gender neutral sibling of parent, aka child of grandparent
+          (first-auncle (treesit-node-child grandparent 0 t)))
+     (and (clojure-ts--list-node-p grandparent)
+          (clojure-ts--symbol-matches-p clojure-ts--reifying-symbol-regexp
+                                        first-auncle)))))
+
 (defvar clojure-ts--threading-macro
   (eval-and-compile
     (rx (and "->" (? ">") line-end)))
@@ -623,7 +643,7 @@ See `treesit-simple-indent-rules'."
 (defun clojure-ts--match-threading-macro-arg (_node parent _)
   "Match NODE if it is an argument to a PARENT threading macro."
   ;; We want threading macros to indent 2 only if the ->> is on it's own line.
-  ;; If not, then align functoin arg.
+  ;; If not, then align function arg.
   (and (clojure-ts--list-node-p parent)
        (let ((first-child (treesit-node-child parent 0 t)))
          (clojure-ts--symbol-matches-p
@@ -640,6 +660,7 @@ See `treesit-simple-indent-rules'."
   `((clojure
      ((parent-is "source") parent-bol 0)
      ;; https://guide.clojure.style/#body-indentation
+     (clojure-ts--match-method-body parent 2)
      (clojure-ts--match-expression-in-body parent 2)
      ;; https://guide.clojure.style/#threading-macros-alignment
      (clojure-ts--match-threading-macro-arg prev-sibling 0)
@@ -654,7 +675,7 @@ See `treesit-simple-indent-rules'."
 (defun clojure-ts--configured-indent-rules ()
   "Gets the configured choice of indent rules."
   (cond
-   ((eq clojure-ts-indent-style 'semantic) clojure-ts--semantic-indent-rules)
+   ((eq clojure-ts-indent-style 'semantic) (clojure-ts--semantic-indent-rules))
    ((eq clojure-ts-indent-style 'fixed) clojure-ts--fixed-indent-rules)
    (t (error
        (format



reply via email to

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