Fork me on GitHub
#vim
<
2022-06-14
>
ribelo17:06:28

Is there any magic way to automatically format the code while writing, or at least get it reasonably correct identation?

ribelo17:06:14

obviously the main problem is with macros

ribelo17:06:42

I'm not a big fan of parinfer, but in fact feel forced to use it because at least it takes care of the indentation of an entire block of code, not just one line

ribelo17:06:27

but it has a lot of other disadvantages, and still can't properly deal with macros

Noah Bogart18:06:26

What do you want to use to format?

ribelo18:06:38

lsp format

ribelo18:06:32

I don't really know what I mean, I just want it to be like in emacs 🙃

Noah Bogart18:06:05

If you have a command of some kind, you could do:

augroup cljfmt
  autocmd! BufWritePre <buffer> call cloure_format_function()
augroup END
and then every time you save it formats for you

Noah Bogart18:06:21

i forget the syntax for selecting only clojure files

ribelo18:06:32

actually have something similar, except that it formats on the exit from insert mode, but bugs the parinfer

ribelo18:06:46

vim + clojure is a tough job 😜

Noah Bogart18:06:26

haha yeah, it's complicated

Noah Bogart18:06:04

I set up a githook for zprint, which is what I and my company uses for formatting, so I mostly don't care until commit time, and then format all of the changed files

ribelo18:06:29

this is also a way out, but I have a some sort of compulsive neurosis and badly formatted code prevents me from concentrating on my work

😂 2
ribelo18:06:59

btw, I see in your dotfiles that you use both vim-iced and conjure

Noah Bogart18:06:34

i actually have stopped using vim-iced. i think the settings file is still hanging around in my dotfiles but i only use conjure.

ribelo18:06:35

does this not clash with each other?

Noah Bogart18:06:48

lol i have a lot of cruft in my dotfiles, i gotta clean it up

😄 1
Noah Bogart18:06:25

i like conjure because it doesn't try to do too much: it's simply a connection to the repl. (I do disable the auto completion and docs stuff). i prefer to rely on clojure-lsp for all of the docs and signature and other stuff

ribelo18:06:15

conjure is really cool, the only thing I miss compared to emacs and cider is the ability to inspect value

ribelo18:06:40

but if I really need to, I can always use a portal or something similar

ribelo18:06:40

I've added the parinfer to turn off before formatting and to turn back on after and maybe it will work ok

Noah Bogart18:06:03

glad to help!

sheluchin18:06:23

> I set up a githook for zprint, which is what I and my company uses for formatting, so I mostly don't care until commit time, and then format all of the changed files @UEENNMX0T doesn't this sometimes produce errors?

Noah Bogart18:06:48

what kind of errors are you thinking of?

sheluchin18:06:19

https://github.com/guns/vim-sexp#indent-commands-normal= f.ex sometimes when I use == to re-indent the current compound form, it'll do it improperly and I end up with code that either doesn't compile or puts values in incorrect positions. A githook for auto-formatting doesn't do things like that?

Noah Bogart18:06:02

I don't use parinfer or anything like that, so indentation doesn't affect the meaning of the code I write. and I use the zprint's -c flag, which returns a non-zero exit code if any files aren't correctly formatted, so it just exits the commit instead of committing. nothing happens automatically for me

sheluchin19:06:26

Hmm, okay. I'll have to add trying zprint to my list. Thanks for the explanation. Much to learn 😅

ribelo19:06:58

== aligns code according to shiftwidth

ribelo19:06:29

using it together with parinfer is not a good idea

ribelo19:06:49

lsp format works much smarter, and I also recommend you make a function that either disables parinfer before formatting, or switches mode from smart to parens @UPWHQK562

sheluchin20:06:17

Thank you for the advice @U0BBFDED7. Indeed, == just hasn't worked well with the rest of my settings. I'll try your approach with lsp format + paren mode.

ribelo20:06:37

Are you using vim or neovim?

ribelo20:06:49

(vim.api.nvim_create_autocmd
  :InsertLeave
  {:pattern [:*.clj :*.cljs :*.cljc :*.bb]
   :callback (fn [{: buf}]
               (when (. vim.lsp.handlers :textDocument/formatting)
                 (set vim.g.parinfer_enabled 0)
                 (vim.lsp.buf.format {:bufnr buf})
                 (set vim.g.parinfer_enabled 1)))})

ribelo20:06:35

so far it works for me and nothing has yet broken

ribelo20:06:57

but the feature is 1 hour old, so anything can still happen 🙃

ribelo20:06:28

I forgot it was fennel, but you can easily translate it to lua

sheluchin20:06:36

No worries, thanks very much. It's a good starting point for me. I really need to spend some time to learn basic lua or make the switch to fennel + conjure. One day lol 🙂

uwo16:06:48

> conjure is really cool, the only thing I miss compared to emacs and cider is the ability to inspect value @U0BBFDED7 out of curiosity, what kind of value inspection do you mean? When I want to inspect a var i just <leader>ew. (I also use zprint and a somewhat customized global .zprint.edn, which makes reading values in the conjure-log a pleasure.)

ribelo18:06:21

linked to a specific moment

🙏 1
practicalli-johnny07:06:11

I'd suggest using Portal with vim/neovim to add a data inspector, especially useful for larger or nested data sets. Its an external tool and you would need to at tap> around the code to inspect, although I am sure there is some vim/lua/fennel hackery that could be done to automatically do that Currently I add portal to the user namespace - https://practical.li/clojure/clojure-cli/data-browsers/portal.html#starting-portal-on-repl-startup

ribelo08:06:47

This is exactly what I do, but having everything in emacs was convenient

ribelo08:06:25

I am still missing the cider debugger, but I have not found any substitute here

sheluchin09:06:03

Just want to mention that vim-iced does implement the CIDER debugger https://liquidz.github.io/vim-iced/#debugging

sheluchin12:06:17

> lsp format works much smarter, and I also recommend you make a function that either disables parinfer before formatting, or switches mode from smart to parens @UPWHQK562 @U0BBFDED7 would you also recommend lsp formatting over cljfmt? I'm wondering if using this snippet as suggested by vim-iced, but with the parinfer toggle wrapping, is just as good, of if it's better to use lsp: https://liquidz.github.io/vim-iced/#format_on_writing_files

aug VimIcedAutoFormatOnWriting
  au!
  " Format whole buffer on writing files
  " au BufWritePre *.clj,*.cljs,*.cljc,*.edn execute ':IcedFormatSyncAll'

  " Format only current form on writing files
  let g:parinfer_enabled = 0
  au BufWritePre *.clj,*.cljs,*.cljc,*.edn execute ':IcedFormatSync'
  let g:parinfer_enabled = 1
aug END

ribelo16:06:41

clojure-lsp uses cljfmt

noisesmith21:06:05

I find that when my indent looks wrong I can use =- to indent that top level form I'm currently in.

noisesmith21:06:11

I"m a caveman though