vim 2024-01-11

Wanted to share a (fennel) snippet for nvim I came up with to run cljfmt on the top-level for which the cursor currently resides in:

(defn format_form []
  (let [row-and-col (nvim.win_get_cursor 0)]
    ;; select entire top form using `vim-sexp` plugin
    (nvim.eval "sexp#select_current_top_list('v', 0)")
    ;; run formatter
    (nvim.command "lua vim.lsp.buf.format()")
    ;; escape out of visual mode
    (nvim.feedkeys "" :n false)
    ;; restore curser location
    (nvim.win_set_cursor 0 row-and-col)))

(vim.keymap.set :n :<leader>ff #(format_form))
Depends on clojure-lsp and vim-sexp plugins I'd be curious to hear if there is a more straightforward way to achieve formatting a top-level form

👍🏻 1

Thanks for sharing! I'm not aware of an alternative way to do that, so I end up always formatting the entire buffer.

I haven't used neovim to write clojure for a while, but I used to do something like this:

(fn vis-op+ [op args]
  #(op
     [(vim.api.nvim_buf_get_mark 0 "<")
      (vim.api.nvim_buf_get_mark 0 ">")]
     args))

(vim.keymap.set :x := (vis-op+ vim.lsp.buf.format {:async true}))
https://github.com/D00mch/dotfiles/blob/master/.config/nvim/fnl/plugins/lsp_config.fnl