From efcda5cf6f7f47bc09a74c37d0906525d9e701a9 Mon Sep 17 00:00:00 2001 From: Li Peng Date: Fri, 3 Jan 2025 14:46:53 +0800 Subject: [PATCH 1/2] Setup LSP for Rust Signed-off-by: Li Peng --- lua/config/lsp.lua | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/lua/config/lsp.lua b/lua/config/lsp.lua index f2c8492c..77baa3ea 100644 --- a/lua/config/lsp.lua +++ b/lua/config/lsp.lua @@ -278,6 +278,32 @@ if utils.executable("lua-language-server") then } end +-- settings for rust-analyzer is copied from https://rust-analyzer.github.io/manual.html#nvim-lsp +if utils.executable("rust-analyzer") then + lspconfig.rust_analyzer.setup { + on_attach = custom_attach, + settings = { + ['rust-analyzer'] = { + imports = { + granularity = { + group = "module", + }, + prefix = "self", + }, + cargo = { + buildScripts = { + enable = true, + }, + }, + procMacro = { + enable = true + }, + }, + }, + capabilities = capabilities, + } +end + -- Change diagnostic signs. fn.sign_define("DiagnosticSignError", { text = "🆇", texthl = "DiagnosticSignError" }) fn.sign_define("DiagnosticSignWarn", { text = "⚠️", texthl = "DiagnosticSignWarn" }) From 074a53a66451d3445031633599c9aaa7a5c80f95 Mon Sep 17 00:00:00 2001 From: Li Peng Date: Sat, 4 Jan 2025 09:01:31 +0800 Subject: [PATCH 2/2] Only format C, CPP and Rust code --- lua/custom-autocmd.lua | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/lua/custom-autocmd.lua b/lua/custom-autocmd.lua index b3ec858b..c3751133 100644 --- a/lua/custom-autocmd.lua +++ b/lua/custom-autocmd.lua @@ -54,6 +54,13 @@ api.nvim_create_autocmd({ "BufWritePre" }, { end, }) +api.nvim_create_autocmd({ "BufWritePre" }, { + pattern = {"*.c", "*.cc", "*.cpp", "*.h", "*.rs", "*.py"}, + callback = function() + vim.lsp.buf.format { asnyc = false } + end, +}) + -- Automatically reload the file if it is changed outside of Nvim, see https://unix.stackexchange.com/a/383044/221410. -- It seems that `checktime` does not work in command line. We need to check if we are in command -- line before executing this command, see also https://vi.stackexchange.com/a/20397/15292 .