rust.lua 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. local M = {}
  2. M.config = function()
  3. O.lang.rust = {
  4. rust_tools = {
  5. active = false,
  6. parameter_hints_prefix = "<-",
  7. other_hints_prefix = "=>", -- prefix for all the other hints (type, chaining)
  8. },
  9. -- @usage can be clippy
  10. formatter = {
  11. exe = "rustfmt",
  12. args = { "--emit=stdout", "--edition=2018" },
  13. stdin = true,
  14. },
  15. linter = "",
  16. diagnostics = {
  17. virtual_text = { spacing = 0, prefix = "" },
  18. signs = true,
  19. underline = true,
  20. },
  21. lsp = {
  22. path = DATA_PATH .. "/lspinstall/rust/rust-analyzer",
  23. },
  24. }
  25. end
  26. M.format = function()
  27. O.formatters.filetype["rust"] = {
  28. function()
  29. return {
  30. exe = O.lang.rust.formatter.exe,
  31. args = O.lang.rust.formatter.args,
  32. stdin = O.lang.rust.formatter.stdin,
  33. }
  34. end,
  35. }
  36. require("formatter.config").set_defaults {
  37. logging = false,
  38. filetype = O.formatters.filetype,
  39. }
  40. end
  41. M.lint = function()
  42. -- TODO: implement linters (if applicable)
  43. return "No linters configured!"
  44. end
  45. M.lsp = function()
  46. if require("lv-utils").check_lsp_client_active "rust_analyzer" then
  47. return
  48. end
  49. if O.lang.rust.rust_tools.active then
  50. local opts = {
  51. tools = { -- rust-tools options
  52. -- automatically set inlay hints (type hints)
  53. -- There is an issue due to which the hints are not applied on the first
  54. -- opened file. For now, write to the file to trigger a reapplication of
  55. -- the hints or just run :RustSetInlayHints.
  56. -- default: true
  57. autoSetHints = true,
  58. -- whether to show hover actions inside the hover window
  59. -- this overrides the default hover handler
  60. -- default: true
  61. hover_with_actions = true,
  62. runnables = {
  63. -- whether to use telescope for selection menu or not
  64. -- default: true
  65. use_telescope = true,
  66. -- rest of the opts are forwarded to telescope
  67. },
  68. inlay_hints = {
  69. -- wheter to show parameter hints with the inlay hints or not
  70. -- default: true
  71. show_parameter_hints = true,
  72. -- prefix for parameter hints
  73. -- default: "<-"
  74. parameter_hints_prefix = O.lang.rust.rust_tools.parameter_hints_prefix,
  75. -- prefix for all the other hints (type, chaining)
  76. -- default: "=>"
  77. other_hints_prefix = O.lang.rust.rust_tools.other_hints_prefix,
  78. -- whether to align to the lenght of the longest line in the file
  79. max_len_align = false,
  80. -- padding from the left if max_len_align is true
  81. max_len_align_padding = 1,
  82. -- whether to align to the extreme right or not
  83. right_align = false,
  84. -- padding from the right if right_align is true
  85. right_align_padding = 7,
  86. },
  87. hover_actions = {
  88. -- the border that is used for the hover window
  89. -- see vim.api.nvim_open_win()
  90. border = {
  91. { "╭", "FloatBorder" },
  92. { "─", "FloatBorder" },
  93. { "╮", "FloatBorder" },
  94. { "│", "FloatBorder" },
  95. { "╯", "FloatBorder" },
  96. { "─", "FloatBorder" },
  97. { "╰", "FloatBorder" },
  98. { "│", "FloatBorder" },
  99. },
  100. },
  101. },
  102. -- all the opts to send to nvim-lspconfig
  103. -- these override the defaults set by rust-tools.nvim
  104. -- see https://github.com/neovim/nvim-lspconfig/blob/master/CONFIG.md#rust_analyzer
  105. server = {
  106. cmd = { O.lang.rust.lsp.path },
  107. on_attach = require("lsp").common_on_attach,
  108. }, -- rust-analyser options
  109. }
  110. require("rust-tools").setup(opts)
  111. else
  112. require("lspconfig").rust_analyzer.setup {
  113. cmd = { O.lang.rust.lsp.path },
  114. on_attach = require("lsp").common_on_attach,
  115. filetypes = { "rust" },
  116. root_dir = require("lspconfig.util").root_pattern("Cargo.toml", "rust-project.json"),
  117. }
  118. end
  119. -- TODO: fix these mappings
  120. vim.api.nvim_exec(
  121. [[
  122. autocmd Filetype rust nnoremap <leader>lm <Cmd>RustExpandMacro<CR>
  123. autocmd Filetype rust nnoremap <leader>lH <Cmd>RustToggleInlayHints<CR>
  124. autocmd Filetype rust nnoremap <leader>le <Cmd>RustRunnables<CR>
  125. autocmd Filetype rust nnoremap <leader>lh <Cmd>RustHoverActions<CR>
  126. ]],
  127. true
  128. )
  129. end
  130. M.dap = function()
  131. -- TODO: implement dap
  132. return "No DAP configured!"
  133. end
  134. return M