rust.lua 4.3 KB

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