rust.lua 4.3 KB

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