~havi/nvim.d

ref: 7a07607b39771459fab357da9d4a5440550111c3 nvim.d/after/ftplugin/rust.lua -rw-r--r-- 6.7 KiB
7a07607bhavi fix(after/ftplugin/rust): autoformat before save 7 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
-- vim: filetype=lua syntax=lua softtabstop=3 tabstop=3 shiftwidth=3 fileencoding=utf-8 smartindent autoindent expandtab
-- code: language=lua insertSpaces=true tabSize=3
-- ────────────────────────────────────────────────────────────
local pluginman = require("lib.plugin-manager")
local lsp = require("lib.lsp")
local logger = require("lib.logger")()
local msg = ""
-- ────────────────────────────────────────────────────────────
local to_require_map = {
   ["which-key.nvim"] = { ["which-key"] = {} },
   ["rust-tools.nvim"] = {
      ["rust-tools"] = {},
      ["rust-tools.inlay_hints"] = {},
      ["rust-tools.hover_actions"] = {},
      ["rust-tools.open_cargo_toml"] = {},
      ["rust-tools.parent_module"] = {},
      ["rust-tools.runnables"] = {},
   },
}
for plugin_name, modules in pairs(to_require_map) do
   for module_name, _ in pairs(modules) do
      local plug = pluginman:load_plugin(plugin_name, module_name)
      if not plug then
         msg = string.format("module < %s > from plugin <%s> could not get loaded", module_name, plugin_name)
        -- stylua: ignore start
        if logger then logger:warn(msg)  end
         -- stylua: ignore end
      end
      to_require_map[plugin_name][module_name] = plug
   end
end
-- ────────────────────────────────────────────────────────────
local rust_tools = to_require_map["rust-tools.nvim"]["rust-tools"]
if not rust_tools then
   msg = string.format("could not load 'rust-tools'")
   -- stylua: ignore start
   if logger then return logger:warn(msg) else return end
   -- stylua: ignore end
end
vim.cmd('autocmd FileType toml lua require("cmp").setup.buffer { sources = { { name = "crates" } } }')
-- ────────────────────────────────────────────────────────────
local lsp_server_name = "rust_analyzer"
---@diagnostic disable-next-line: missing-parameter
lsp:setup(lsp_server_name)
-- ────────────────────────────────────────────────────────────
local wk = to_require_map["which-key.nvim"]["which-key"]
if not wk then
   msg = string.format("could not load 'which-key'")
   -- stylua: ignore start
   if logger then return logger:warn(msg) else return end
   -- stylua: ignore end
end
local inlay_hints = to_require_map["rust-tools.nvim"]["rust-tools.inlay_hints"]
if not inlay_hints then
   msg = string.format("could not load 'rust-tools.inlay_hints'")
   -- stylua: ignore start
   if logger then return logger:warn(msg) end
   -- stylua: ignore end
end
local hover_actions = to_require_map["rust-tools.nvim"]["rust-tools.hover_actions"]
if not hover_actions then
   msg = string.format("could not load 'rust-tools.hover_actions'")
   -- stylua: ignore start
   if logger then return logger:warn(msg) end
   -- stylua: ignore end
end
local open_cargo_toml = to_require_map["rust-tools.nvim"]["rust-tools.open_cargo_toml"]
if not open_cargo_toml then
   msg = string.format("could not load 'rust-tools.open_cargo_toml'")
   -- stylua: ignore start
   if logger then return logger:warn(msg) end
   -- stylua: ignore end
end
local parent_module = to_require_map["rust-tools.nvim"]["rust-tools.parent_module"]
if not parent_module then
   msg = string.format("could not load 'rust-tools.parent_module'")
   -- stylua: ignore start
   if logger then return logger:warn(msg) end
   -- stylua: ignore end
end
local runnables = to_require_map["rust-tools.nvim"]["rust-tools.runnables"]
if not runnables then
   msg = string.format("could not load 'rust-tools.runnables'")
   -- stylua: ignore start
   if logger then return logger:warn(msg) end
   -- stylua: ignore end
end
wk.register({
   ["h"] = {
      function()
         if inlay_hints then
            inlay_hints.toggle_inlay_hints()
         end
      end,
      "Toggle inlay hints",
   },
   ["<SPACE>"] = {
      function()
         if hover_actions then
            hover_actions.hover_actions()
            vim.lsp.buf.hover()
         end
      end,
      "Toggle hover actions",
   },
   ["c"] = {
      function()
         if open_cargo_toml then
            open_cargo_toml.open_cargo_toml()
         end
      end,
      "Open Cargo.Toml",
   },
   ["p"] = {
      function()
         if parent_module then
            parent_module.parent_module()
         end
      end,
      "Open Parent Module",
   },
   ["r"] = {
      function()
         if runnables then
            runnables.runnables()
         end
      end,
      "Runnables",
   },
}, {
   mode = "n",
   prefix = "<LocalLeader>",
   buffer = vim.api.nvim_get_current_buf(),
   noremap = true,
})
wk.register({
   ["<C-h>"] = {
      function()
         if inlay_hints then
            inlay_hints.toggle_inlay_hints()
         end
      end,
      "Toggle inlay hints",
   },
   ["<C-SPACE>"] = {
      function()
         if hover_actions then
            -- https://www.reddit.com/r/neovim/comments/uk3xmq/comment/i7n6wr6/?utm_source=share&utm_medium=web2x&context=3
            -- https://vi.stackexchange.com/questions/31422/how-to-switch-mode-from-visual-visual-line-mode-to-normal-mode-in-lua
            -- https://vi.stackexchange.com/questions/31422/how-to-switch-mode-from-visual-visual-line-mode-to-normal-mode-in-lua
            vim.cmd("stopinsert")
            hover_actions.hover_actions()
            vim.lsp.buf.hover()
         end
      end,
      "Toggle hover actions",
   },
   ["<C-c>"] = {
      function()
         if open_cargo_toml then
            open_cargo_toml.open_cargo_toml()
         end
      end,
      "Open Cargo.Toml",
   },
   ["<C-p>"] = {
      function()
         if parent_module then
            parent_module.parent_module()
         end
      end,
      "Open Parent Module",
   },
   ["<C-r>"] = {
      function()
         if runnables then
            runnables.runnables()
         end
      end,
      "Runnables",
   },
}, {
   mode = "i",
   buffer = vim.api.nvim_get_current_buf(),
   noremap = true,
})
-- auto format before saving
-- vim.api.nvim_exec([[ autocmd BufWritePre *.rs :silent! lua vim.lsp.buf.format({async = true}) ]], false)
vim.cmd('autocmd BufWritePre *.rs :silent! lua vim.lsp.buf.format({async = false})')
-- -- show/refresh diagnostics after saving
-- https://github.com/neovim/neovim/issues/13324#issuecomment-847353681
-- vim.cmd('autocmd BufWritePost *.rs lua vim.diagnostic.setloclist()')