Fork me on GitHub
#emacs
<
2021-10-08
>
onetom01:10:31

can anyone recommend me clojure source code formatting/indentation settings for mixed intellij/cursive and emacs/cider(/clojure-mode) teams? lot of discussions has happened about it here: https://github.com/bbatsov/clojure-style-guide/issues/126 but no conclusion seems to be reached yet. there is also tonsky's radically simpler solution https://tonsky.me/blog/clojurefmt/ which can already been achieved via cursive's Default to Only indent + Force Only Indent for all forms setting (https://twitter.com/CursiveIDE/status/1070984513163997189) i couldn't reproduce this simplified indentation in emacs, but i would be happy with any kind of auto-format-setting combinations, which yield equivalent results across these 2 specific environments.

onetom01:10:09

i've considered using some external auto-formatter, like cljstyle tried it with the options recommended here: https://bogoyavlensky.com/blog/clojure-formatting-cljstyle/ but it was not completely adhering to the simple tonsky-style, so i had to tweak it, which was a pain because it's based on cljfmt, but diverged from it. anyway, the options are summarized here: https://github.com/greglook/cljstyle/blob/main/doc/configuration.md and these settings - almost - worked for me:

$ cat .cljstyle                         
{:rules
 {:indentation
  {:indents
   {#"^:?require$" [[:block 0]],
    #"^:?import$" [[:block 0]],
    #"^:?use$" [[:block 0]],
    #"^[^ \[]" [[:inner 0]]}},
  :vars {:enabled? false},
  :functions {:enabled? false},
  :namespaces {:enabled? false},
  :blank-lines {:trim-consecutive? false
                :insert-padding? false}}}
(:time-consecutive? and :insert-padding? r my additions)

onetom01:10:54

btw, cljstyle can be integrated with this small package:

(straight-use-package
 '(cljstyle-mode :host github :type git :repo "jstokes/cljstyle-mode"))

onetom01:10:13

Finally, I also realized that one thing is the full document (or only lines changed compared to version control) formatting but there is an other formatting, which is happening in a more localized way, during editing, which only affects the current or next line or the current lisp form. this latter feature is commanded by completely different code, with different rules. think of stuff like tab-always-indent, Electric Indent (https://www.gnu.org/software/emacs/manual/html_node/emacs/Indent-Convenience.html) minor mode, C-M-q (`indent-sexp`, or indent-pp-sexp), indent-region, indent-code-rigidly, clojure-align, clojure-indent-region, etc. These features are exposed in different places in the different Emacs "distros", so i've noticed that users are not even aware of what package is providing the underlying functionality.

onetom01:10:01

in summary, please HELP! 😄 by sharing your experience and settings

vemv13:10:17

cljfmt very nearly matches emacs (clojure-mode) and is actually used by cider (cider-format) One certainly can make Cursive like cljfmt-like natively Both emacs and cursive will need the occasional tweak to satisfy cljfmt. I think that's normal - it's bit of a fool's errand to try independent parties/technologies to agree on the same exact formatting rules So an external process such as cljfmt is a simple 'arbiter' :)

vemv13:10:06

cljfmt has a native image if you're into that sort of thing. You can also invoke it from a repl for avoiding startup time. cljstyle is a fork of cljfmt, IMO that wasn't classy and whatever it provides should be created as PRs against cljfmt if we want to keep the community simple instead of more fragmented.

vemv13:10:25

(dubious) source: I help out maintaining cider, have contributed to cljfmt and also have authored a little framework for running formatters.

onetom05:10:58

thank you for the insights! what formatter framework are you referring to? this one: https://github.com/nedap/formatting-stack/ ?

vemv07:10:05

haha, yes when I authored it we had a mixed Cursive - Emacs team. worked well for us, it remains in use at that company and a few others. I'm not exactly promoting it yet though, there are things that could be made easier (@UHJH8MG6S is adding a plugin-like system to it, inspired by Kaocha)

🙏 1
ericdallo12:10:40

Is there any command or something that move a pair or key and value of a map up and down? 🧵

ericdallo12:10:54

Considering | the cursor, running the command moving down for:

{:a 1
|:b 2
 :c 3}
would result on:
{:a 1
 :c 3
|:b 2}

ericdallo12:10:28

I found cljr--move-line-down that works perfectly

ericdallo12:10:55

and cljr--move-line-up

ericdallo13:10:28

probably a clojure-lsp command for that would be cool as well for other editors :)

djm13:10:35

I was going to suggest move-text-line-down (and up) from the move-text package. Not Clojure-aware, but I often find cljr slow and/or surprising. Great if it works well for this though.

ericdallo13:10:09

actually, cljr doesn't work quite well if the pair is on the end of the map :(

ericdallo13:10:47

like moving up this:

{:a 1
 :b 2
|:c 3}
will result in
{:a 1
|:c 3}
 :b 2
:(

djm13:10:50

Oh, sorry, move-text-up is line or region, so probably works better than the line version

ericdallo13:10:46

yeah, I was searching for something that doesn't need a region, just get the current cursor

djm13:10:01

I tried using mark-sexp a couple of times, followed by move-text-up, and the result wasn’t pretty - both pairs ended up on the same line, with the closing } on the line below.

djm13:10:20

I wonder if there’s a way to figure out how many sexps end on the current line, and slurp that many times

ericdallo13:10:12

that looks easier to do with rewrite-clj, that's why I think adding that support for clojure-lsp would be useful

djm13:10:54

mark-sexp x2, then sp-transpose-hybrid-sexp seems to work

djm13:10:35

For moving up from the end, anyway. I’m not sure how to generalise it though.

ericdallo13:10:29

Yeah, it works, thanks for the help :) I'll probably implement that on clojure-lsp later anyway

yuhan16:10:48

Paxedit has a command paxedit-transpose-forward / backward that implements this in maps and let bindings

ericdallo16:10:39

good to know thks @UCPS050BV!

yuhan16:10:56

I've had mixed results trying to integrate it into my workflow though, paxedit's conventions about cursor placement just aren't very intuitive when used with other structured editing modes - it probably works well if you adopt their entire editing paradigm

onetom05:10:18

im using this feature too, often enough in cursive to really miss it from emacs 🙂 never heard about paxedit before, thanks for mentioning it!