Search code examples
intellij-ideaclojurela-clojure

How to debug a Clojure file in IntelliJ?


No breakpoint can be set on line 5, which contains [x].

IntelliJ won't let me do so. I used different plugin, such as La Clojure and Cursive. Both stop at line 3 rather than line 5.

So, how people step into the code in Clojure?

Is there any syntax suggestion or maybe tool to help with?

(defn flattenlist
  ([x & more]
    (concat (if (vector? x)
              (apply flattenlist x)
              [x]
            )
            (if (= more nil)
              nil
              (apply flattenlist more))))
  )
(flattenlist [[1 [[2]]] 3 [4 5] 6])

Solution

  • First, by convention, all trailing parentheses are on the same line, something like this:

    (defn flattenlist
      ([x & more]
       (println x)
       (concat (if (vector? x)
                 (apply flattenlist x)
                 [x])
               (if (= more nil)
                 nil
                 (apply flattenlist more)))))
    
    (flattenlist [[1 [[2]]] 3 [4 5] 6])
    

    Secondly, when you use composable functions, it is easy to insert a println and run/test just that function because it is referentially transparent. I am only a Clojure hobbyist, but I typically debug with printlns and unit tests. Using breakpoints isn't really that reliable.

    If you really want something similar to setting a breakpoint, you can try using this debugging macro (not mine).