Something went wrong. Try again.
clojure plugin for neovim
Something went wrong. Try again.
4.1 kB · 115 lines
at main
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116; This Source Code Form is subject to the terms of the Mozilla Public; License, v. 2.0. If a copy of the MPL was not distributed with this; file, You can obtain one at https://mozilla.org/MPL/2.0/.
(local {: get-package-path} (require :clojure-nvim.fs))(local edn (require :clojure-nvim.vendor.edn))(local textcase (require :textcase))(import-macros {: callback : when-require} :clojure-nvim.util-macros)
(local clojure-lsp-commands (get-package-path "lua" "clojure-nvim" "clojure-lsp-commands.edn"))
(λ get-uri-and-pos [] (let [[row col] (vim.api.nvim_win_get_cursor 0) uri (vim.uri_from_bufnr 0)] [uri (- row 1) col]))
(λ get-client [name] (. (vim.lsp.get_clients {: name}) 1))
(λ execute-positional-command [cmd ?args] (let [client (get-client "clojure-lsp") [uri row col] (get-uri-and-pos)] (client:exec_cmd {:command (. cmd :command) :arguments [uri row col (if (= "string" (type ?args)) ?args (unpack (or ?args [])))]}) (vim.api.nvim__redraw {:buf 0 :flush true})))
(λ execute-prompt-command [cmd ?args] (let [prompt (. cmd :prompt)] (if (not ?args) (vim.ui.input {:prompt (.. prompt " ")} #(execute-positional-command cmd $)) (execute-positional-command cmd (?. ?args :args)))))
(λ execute-choice-command [cmd ?args] (let [{: prompt : choices} cmd] (if (not ?args) (vim.ui.select choices {: prompt} #(execute-positional-command cmd $)) (execute-positional-command cmd (?. ?args :args)))))
(λ get-command-fn [cmd] (case (. cmd :type) :positional (λ [_args] (execute-positional-command cmd nil)) :prompt (λ [args] (execute-prompt-command cmd args)) :choice (λ [args] (execute-choice-command cmd args))))
(λ register-keymaps [commands] (each [_ cmd (ipairs commands)] (when (. cmd :shortcut) (vim.keymap.set "n" (.. "<leader>cl" (. cmd :shortcut)) (get-command-fn cmd) {:silent true :noremap true :desc (.. "clojure-lsp-" (. cmd :command))}))))
(λ register-commands [commands] (each [_ cmd (ipairs commands)] (let [nargs (if (. cmd :positional) "0" "?")] (vim.api.nvim_create_user_command (.. "CljLsp" (textcase.api.to_pascal_case (. cmd :command))) (get-command-fn cmd) {: nargs}))) (vim.api.nvim_create_user_command :CljLspCursorInfo (λ [] (let [[uri row col] (get-uri-and-pos)] (vim.lsp.buf_notify 0 "clojure/cursorInfo/log" {:textDocument {: uri} :position {:line row :character col}}))) {:nargs 0}) (vim.api.nvim_create_user_command :CljLspServerInfo #(vim.lsp.buf_notify 0 "clojure/serverInfo/log") {:nargs 0}) (vim.api.nvim_create_user_command :CljLspProjectTree #(vim.lsp.buf_request_sync 0 "clojure/workspace/projectTree/nodes") {:nargs 0}) nil)
(local M {})
(local default-lsp-config {:cmd ["clojure-lsp"] :filetypes ["clojure"] :root_markers ["project.clj" "deps.edn" "build.boot" "shadow-cljs.edn" "bb.edn" ".git"]})
(λ M.set-config [?lsp-config] (let [lsp-config (or ?lsp-config {})] (vim.lsp.config :clojure-lsp (if (vim.tbl_isempty lsp-config) default-lsp-config (vim.tbl_deep_extend :force default-lsp-config lsp-config)))))
(λ M.enable-config [] (vim.lsp.enable :clojure-lsp))
(λ M.register-keys [config] (let [{:register-keymaps reg-keys :register-commands reg-cmds} config] (when (vim.uv.fs_stat clojure-lsp-commands) (let [commands (edn.decode (vim.fn.readblob clojure-lsp-commands))] (when reg-keys (register-keymaps commands)) (when reg-cmds (register-commands commands))))))
(λ M.initialize [config] (when (?. config :lsp :enable) (M.set-config (. config :lsp :config)) (M.enable-config) (M.register-keys (. config :lsp))))
M