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

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

[nongnu] elpa/clojure-ts-mode b708c7f15f 047/210: Rewrite the indent log


From: ELPA Syncer
Subject: [nongnu] elpa/clojure-ts-mode b708c7f15f 047/210: Rewrite the indent logic
Date: Thu, 31 Oct 2024 18:59:38 -0400 (EDT)

branch: elpa/clojure-ts-mode
commit b708c7f15fb22b8b4547f8ffa8d2a84faa0422c9
Author: Artur Malabarba <bruce.connor.am@gmail.com>
Commit: Artur Malabarba <bruce.connor.am@gmail.com>

    Rewrite the indent logic
    
    [Fix #282 Fix #296] Use custom-code to calculate normal-indent.
    
    Add tests for backtracking indent and several other scenarios.
    
    Support a new notation for indent specs.
    An indent spec can be:
    - `nil` (or absent), meaning “indent like a regular function”.
    - A list/vector meaning that this function/macro takes a number of
      “special” arguments which are indented by more spaces (in CIDER
      that's 4 spaces), and then all other arguments are indented like a
      body (in CIDER that's 2 spaces).
      - The first element is a number indicating how many "special"
        arguments this function/macro takes.
      - Each following element is an indent spec on its own, and it
        applies to the argument on the same position as this element. So,
        when the argument is a form, it specifies how to indent that
        argument internally (if it's not a form the spec is irrelevant).
      - If the function/macro has more aguments than the list has
        elements, the last element of the list applies to all remaining
        arguments.
    
    So, for instance, if I specify the `deftype` spec as `[2 nil nil fn]`
    (equivalent to `[2 nil nil [1]]`), it would be indented like this:
    
    ```
    (deftype
        ImageSelection
        [data]
      Transferable
      (getTransferDataFlavors [this]
        (some-function))
      SomethingElse
      (isDataFlavorSupported [this flavor]
        (= imageFlavor flavor)))
    ```
    (I put `ImageSelection` and `[data]` on their own lines just to show the 
indentation).
    
    Another example, `reify` as `[1 nil fn]`
    ```
    (reify Object
      (toString [this]
        (f)
        asodkaodkap))
    ```
    
    Or something more complicated, `letfn` as `[1 [fn] nil]`
    ```
    (letfn [(twice [x]
              (* x 2))
            (six-times [y]
              (* (twice y) 3))]
      (println "Twice 15 =" (twice 15))
      (println "Six times 15 =" (six-times 15)))
    ```
---
 clojure-mode-indentation-test.el | 85 +++++++++++++++++++++++++++++++++++++---
 1 file changed, 79 insertions(+), 6 deletions(-)

diff --git a/clojure-mode-indentation-test.el b/clojure-mode-indentation-test.el
index 93de734750..01f4ebd7aa 100644
--- a/clojure-mode-indentation-test.el
+++ b/clojure-mode-indentation-test.el
@@ -110,28 +110,28 @@ values of customisable variables."
 (check-indentation doc-strings-without-indent-specified
   "
 (defn some-fn
-|\"some doc string\""
+|\"some doc string\")"
   "
 (defn some-fn
-  |\"some doc string\"")
+  |\"some doc string\")")
 
 (check-indentation doc-strings-with-correct-indent-specified
   "
 (defn some-fn
-  |\"some doc string\""
+  |\"some doc string\")"
   "
 (defn some-fn
-  |\"some doc string\"")
+  |\"some doc string\")")
 
 (check-indentation doc-strings-with-additional-indent-specified
   "
 (defn some-fn
   |\"some doc string
-    - some note\""
+    - some note\")"
   "
 (defn some-fn
   |\"some doc string
-    - some note\"")
+    - some note\")")
 
 ;; we can specify different indentation for symbol with some ns prefix
 (put-clojure-indent 'bala 0)
@@ -189,6 +189,79 @@ values of customisable variables."
                        |(* x 2))]
   :a)")
 
+(check-indentation fixed-normal-indent
+  "(cond
+  (or 1
+      2) 3
+|:else 4)"
+  "(cond
+  (or 1
+      2) 3
+  |:else 4)")
+
+(check-indentation fixed-normal-indent-2
+  "(fact {:spec-type
+       :charnock-column-id} #{\"charnock\"}
+|{:spec-type
+       :charnock-column-id} #{\"current_charnock\"})"
+  "(fact {:spec-type
+       :charnock-column-id} #{\"charnock\"}
+      |{:spec-type
+       :charnock-column-id} #{\"current_charnock\"})")
+
+
+;;; Backtracking indent
+(defmacro def-full-indent-test (name form)
+  "Verify that FORM corresponds to a properly indented sexp."
+  (declare (indent 1))
+  `(ert-deftest ,(intern (format "test-backtracking-%s" name)) ()
+     (with-temp-buffer
+       (clojure-mode)
+       (insert "\n" ,(replace-regexp-in-string "\n +" "\n " form))
+       (indent-region (point-min) (point-max))
+       (should (equal (buffer-string)
+                      ,(concat "\n" form))))))
+
+(def-full-indent-test closing-paren
+  "(ns ca
+  (:gen-class)
+  )")
+
+(def-full-indent-test non-symbol-at-start
+  "{\"1\" 2
+ *3 4}")
+
+(def-full-indent-test non-symbol-at-start-2
+  "(\"1\" 2
+ *3 4)")
+
+(def-full-indent-test defrecord
+  "(defrecord TheNameOfTheRecord
+    [a pretty long argument list]
+  SomeType
+  (assoc [_ x]
+    (.assoc pretty x 10)))")
+
+(def-full-indent-test defrecord-2
+  "(defrecord TheNameOfTheRecord [a pretty long argument list]
+  SomeType (assoc [_ x]
+             (.assoc pretty x 10)))")
+
+(def-full-indent-test letfn
+  "(letfn [(f [x]
+          (* x 2))
+        (f [x]
+          (* x 2))]
+  (a b
+     c) (d)
+  e)")
+
+(def-full-indent-test reify
+  "(reify
+    Object
+  (x [_]
+    1))")
+
 (provide 'clojure-mode-indentation-test)
 
 ;; Local Variables:



reply via email to

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