Fork me on GitHub
#lsp
<
2024-03-04
>
sheluchin00:03:37

Here's an example for setting up a mapping in neovim for the cycle-coll refactoring using fennel and the nvim native LSP client:

(module lsp-aug
  {autoload {eval conjure.eval
             str conjure.aniseed.string
             a conjure.aniseed.core}})

(defn cycle-coll []
  (let [uri (a.str "file://" (vim.api.nvim_buf_get_name 0))
        (row col) (unpack (vim.api.nvim_win_get_cursor 0))]
    (vim.lsp.buf.execute_command
      {:command "cycle-coll"
       :arguments [uri (- row 1) (- col 1)]})))

(vim.keymap.set :n :<leader>et #(cycle-coll))
There is probably a better way of doing it, but it works :man-shrugging: Took me some time to figure out, so I thought I'd share. Thanks for the help earlier, @ericdallo.

Jason Paterson10:03:06

This is what I've got in my config, making a user command to run the lsp commands. vim.lsp.util has some useful helpers for making args.

local commands = { "cycle-coll", ... }

vim.api.nvim_buf_create_user_command(0, "CljLsp", function(info)
  local command = info.args
  local position_params = vim.lsp.util.make_position_params()
  vim.lsp.buf.execute_command({
    command = command,
    arguments = {
      position_params.textDocument.uri,
      position_params.position.line,
      position_params.position.character,
    },
  })
end, {
  desc = "Run clojure-lsp command",
  nargs = 1,
  complete = function(arg_lead, cmd_line, cursor_pos)
    return vim.tbl_filter(function(s) return s:sub(1, #arg_lead) == arg_lead end, commands)
  end,
})

sheluchin14:03:26

@UF3DZS1DF Thanks for sharing! That's very helpful. There's not much Fennel-specific config discussion here so far, so here's my translated version for posterity:

(module lsp-aug-jp
  {autoload {eval conjure.eval
             str conjure.aniseed.string
             a conjure.aniseed.core}})

(local commands ["cycle-coll"
                 "thread-first"
                 "thread-first-all"])

(vim.api.nvim_create_user_command "CljLsp"
 (fn [info]
    (let [command (a.get info :args)
          position-params (vim.lsp.util.make_position_params)
          uri (a.get-in position-params [:textDocument :uri])
          line (a.get-in position-params [:position :line])
          character (- (a.get-in position-params [:position :character]) 1)]
      (vim.lsp.buf.execute_command
        {:command command
         :arguments [uri line character]})))
 {:desc "Run clojure-lsp command"
  :nargs 1
  :complete (fn [arg-lead cmd-line cursor-pos]
              (vim.tbl_filter
                (fn [s] (= (a.sub s 1 (a.count arg-lead)) arg-lead))
                commands))})

(vim.keymap.set :n :<leader>ez ":CljLsp cycle-coll<cr>")
(vim.keymap.set :n :<leader>et ":CljLsp thread-first<cr>")
(vim.keymap.set :n :<leader>el ":CljLsp thread-first-all<cr>")