lsp.fnl 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. ;;; lsp.fnl - lsp configurations
  2. (import-macros {: bind! : setup!} :macros)
  3. (fn on-attach [client buf]
  4. ((. (. (require :cmp) :setup) :buffer) {
  5. :sources [{ :name "nvim_lsp" :priority 1 :group 1 }
  6. { :name "snippy" :priority 1 :group 2 }
  7. { :name "buffer" :priority 0 :group 3 }
  8. { :name "path" :priority 0 :group 4 }]})
  9. (bind! :n :gD vim.lsp.buf.declaration buf)
  10. ; (bind! :n :gd vim.lsp.buf.definition buf)
  11. (bind! :n :K vim.lsp.buf.hover buf)
  12. ; (bind! :n :gI vim.lsp.buf.implementation buf)
  13. (bind! :n :<C-k> vim.lsp.buf.signature_help buf)
  14. (bind! :n :<leader>wa vim.lsp.buf.add_workspace_folder buf)
  15. (bind! :n :<leader>wr vim.lsp.buf.remove_workspace_folder buf)
  16. (bind! :n :<leader>wl #(print (vim.inspect
  17. (vim.lsp.buf.list_workspace_folders))) buf)
  18. ; (bind! :n :<leader>D vim.lsp.buf.type_definition buf)
  19. (bind! :n :<leader>rn vim.lsp.buf.rename buf)
  20. (bind! :n :<leader>cn vim.lsp.buf.code_action buf)
  21. ; (bind! :n :gr vim.lsp.buf.references buf)
  22. (bind! [ :n :v ] :<leader>o
  23. #(vim.lsp.buf.format { :filter
  24. ;; use null-ls for formatting
  25. (fn [client] (or
  26. (= client.name :null-ls)
  27. (= client.name :rust_analyzer)))
  28. :async true }) buf)
  29. ;; Use builtin formatexpr
  30. (vim.api.nvim_buf_set_option buf :formatexpr "")
  31. ;; Some telescope commands
  32. (bind! :n :<leader>s "<cmd>Telescope lsp_dynamic_workspace_symbols<cr>" buf)
  33. (bind! :n :<leader>fs "<cmd>Telescope lsp_dynamic_workspace_symbols<cr>" buf)
  34. (bind! :n :<leader>fS "<cmd>Telescope lsp_workspace_symbols<cr>" buf)
  35. ;(print (vim.inspect client.server_capabilities))
  36. (when client.server_capabilities.documentSymbolProvider
  37. (bind! :n :<leader>d "<cmd>Telescope lsp_document_symbols<cr>" buf)
  38. (bind! :n :<leader>fd "<cmd>Telescope lsp_document_symbols<cr>" buf))
  39. (bind! :n :gr "<cmd>Telescope lsp_references<cr>" buf)
  40. (bind! :n :gd "<cmd>Telescope lsp_definitions<cr>" buf)
  41. (bind! :n :gI "<cmd>Telescope lsp_implementations<cr>" buf)
  42. (bind! :n :D "<cmd>Telescope lsp_type_definitions<cr>" buf)
  43. (bind! :n :<leader>fq "<cmd>Telescope diagnostics<cr>" buf)
  44. ;; Some trouble commands
  45. (bind! :n :<leader>q (fn []
  46. (let [trouble (require :trouble)]
  47. (if (trouble.is_open)
  48. (trouble.close)
  49. true
  50. (trouble.open :document_diagnostics)))))
  51. (bind! :n :<leader>Q (fn []
  52. (let [trouble (require :trouble)]
  53. (if (trouble.is_open)
  54. (trouble.close)
  55. true
  56. (trouble.open :workspace_diagnostics))))))
  57. (fn get-data-dir [server root]
  58. (let [resolved_path (vim.fn.resolve root)
  59. joined_path (vim.fn.substitute resolved_path "\\/" "@" :g)]
  60. (.. (vim.fn.fnamemodify (.. "~/.local/nvim/lsp-cache/"
  61. server
  62. "/")
  63. ":p") joined_path)))
  64. (fn configure []
  65. (let [lsp (require :lspconfig)
  66. ;configs (require :lspconfig.configs)
  67. lsp-cap ((. (require :cmp_nvim_lsp) :default_capabilities))
  68. lsp-utils (require :plugin.lsp)]
  69. (macro setup-server! [name ...]
  70. (let [opts { :on_attach `lsp-utils.on-attach
  71. :capabilities `lsp-cap }]
  72. (var last-key nil)
  73. (each [_ val (ipairs [...])]
  74. (if last-key
  75. (do (tset opts last-key val)
  76. (set last-key nil))
  77. (set last-key val)))
  78. `((. (. lsp ,name) :setup) ,opts)))
  79. (setup-server! :clangd
  80. :on_attach (fn [client buf]
  81. (lsp-utils.on-attach client buf)
  82. (bind! :n :go :<cmd>ClangdSwitchSourceHeader<cr> buf))
  83. :cmd [ "clangd"
  84. "--header-insertion-decorators=0"
  85. "--background-index"
  86. "--clang-tidy"
  87. "--completion-style=bundled"
  88. "--function-arg-placeholders"
  89. "--header-insertion=never"
  90. "--malloc-trim"
  91. "--pch-storage=memory"
  92. "--offset-encoding=utf-16" ])
  93. (setup-server! :cmake)
  94. (setup-server! :vala_ls)
  95. (setup-server! :gopls)
  96. (setup-server! :rust_analyzer)
  97. (setup-server! :texlab)
  98. (setup-server! :pylsp)
  99. (setup-server! :lua_ls
  100. :settings {
  101. :Lua {
  102. :runtime {
  103. :version "LuaJIT" }
  104. :diagnostics {
  105. :globals [ "vim" ] }
  106. :workspace {
  107. :checkThirdParty false
  108. :library (vim.api.nvim_get_runtime_file "" true) }
  109. :telemetry {
  110. :enable false }}})
  111. (setup-server! :fennel_language_server
  112. :settings {
  113. :fennel {
  114. :workspace {
  115. :library (vim.api.nvim_list_runtime_paths) }
  116. :diagnostics {
  117. :globals [ "vim" ] }}})))
  118. (fn setup-null-ls []
  119. (let [builtins (. (require :null-ls) :builtins)]
  120. (setup! :null-ls
  121. :on_attach (fn [client buf]
  122. (bind! [ :n :v ]
  123. :<leader>o
  124. #(vim.lsp.buf.format
  125. { :filter
  126. ;; use null-ls for formatting
  127. (fn [client]
  128. (or
  129. (= client.name :null-ls)
  130. (= client.name :rust_analyzer)))
  131. :async true }) buf)
  132. (vim.api.nvim_buf_set_option buf :formatexpr ""))
  133. :sources [
  134. builtins.formatting.astyle
  135. builtins.formatting.prettier
  136. builtins.formatting.yapf
  137. builtins.formatting.fnlfmt
  138. builtins.formatting.shfmt
  139. builtins.formatting.cmake_format
  140. builtins.formatting.stylua
  141. (builtins.completion.spell.with { :filetypes [ :text
  142. :markdown
  143. :tex ]})
  144. builtins.diagnostics.cmake_lint
  145. ;;builtins.diagnostics.codespell
  146. ;;builtins.diagnostics.shellcheck
  147. (builtins.diagnostics.glslc.with
  148. { :extra_args [ "--target-env=opengl" ]})])))
  149. {: configure : on-attach : get-data-dir : setup-null-ls }