From 7025b5fe97f38041e868b381136b1f3ba2fb0ede Mon Sep 17 00:00:00 2001 From: "Alexandre Cavalheiro S. Tiago da Silva" Date: Tue, 11 Feb 2025 23:38:36 -0300 Subject: [PATCH] feat(neovim): add roslyn.nvim support + rzls.nvim --- .../home-manager/programs/neovim/default.nix | 4 +- modules/home-manager/programs/neovim/lsp.lua | 2 +- .../programs/neovim/lua/community.lua | 4 +- .../neovim/lua/plugins/roslyn-nvim.lua | 120 ++++++++++++++++++ .../neovim/lua/plugins/treesitter.lua | 99 ++++++++------- 5 files changed, 180 insertions(+), 49 deletions(-) create mode 100644 modules/home-manager/programs/neovim/lua/plugins/roslyn-nvim.lua diff --git a/modules/home-manager/programs/neovim/default.nix b/modules/home-manager/programs/neovim/default.nix index 367563f..f99486b 100644 --- a/modules/home-manager/programs/neovim/default.nix +++ b/modules/home-manager/programs/neovim/default.nix @@ -78,7 +78,9 @@ in vscode-extensions.ms-vscode.cpptools # C# - csharp-ls + #csharp-ls Testing roslyn.nvim + roslyn-ls + rzls csharpier netcoredbg diff --git a/modules/home-manager/programs/neovim/lsp.lua b/modules/home-manager/programs/neovim/lsp.lua index 8d617a6..65faa69 100644 --- a/modules/home-manager/programs/neovim/lsp.lua +++ b/modules/home-manager/programs/neovim/lsp.lua @@ -55,7 +55,7 @@ return { "basedpyright", "clangd", "cmake", - "csharp_ls", + --"csharp_ls", Testing roslyn.nvim "cssls", "denols", "eslint", diff --git a/modules/home-manager/programs/neovim/lua/community.lua b/modules/home-manager/programs/neovim/lua/community.lua index 13881f3..96d4822 100644 --- a/modules/home-manager/programs/neovim/lua/community.lua +++ b/modules/home-manager/programs/neovim/lua/community.lua @@ -18,9 +18,11 @@ return { { import = "astrocommunity.motion.mini-ai" }, { import = "astrocommunity.motion.mini-surround" }, + { import = "astrocommunity.test.neotest" }, + { import = "astrocommunity.pack.cmake" }, { import = "astrocommunity.pack.cpp" }, - { import = "astrocommunity.pack.cs" }, + -- { import = "astrocommunity.pack.cs" }, Trying out roslyn.nvim { import = "astrocommunity.pack.godot" }, { import = "astrocommunity.pack.html-css" }, { import = "astrocommunity.pack.json" }, diff --git a/modules/home-manager/programs/neovim/lua/plugins/roslyn-nvim.lua b/modules/home-manager/programs/neovim/lua/plugins/roslyn-nvim.lua new file mode 100644 index 0000000..1ebefc4 --- /dev/null +++ b/modules/home-manager/programs/neovim/lua/plugins/roslyn-nvim.lua @@ -0,0 +1,120 @@ +---@type LazySpec +return { + "seblyng/roslyn.nvim", + ft = { "cs", "razor" }, + commit = "490fd2d0f76249032ef6ce503e43ccdaeed9616e", + lazy = true, + dependencies = { + { + "tris203/rzls.nvim", + config = function() + ---@diagnostic disable-next-line: missing-fields + require("rzls").setup { + path = vim.fn.getnixpath "rzls" .. "/bin/rzls", + } + end, + }, + }, + opts = function(_, opts) + local rzlspath = vim.fn.getnixpath "rzls" + require("roslyn.config").get() + + opts = { + exe = "Microsoft.CodeAnalysis.LanguageServer", + args = { + "--logLevel=Information", + "--extensionLogDirectory=" .. vim.fs.dirname(vim.lsp.get_log_path()), + "--razorSourceGenerator=" .. rzlspath .. "/lib/rzls/Microsoft.CodeAnalysis.Razor.Compiler.dll", + "--razorDesignTimePath=" .. rzlspath .. "/lib/rzls/Targets/Microsoft.NET.Sdk.Razor.DesignTime.targets", + }, + ---@type vim.lsp.ClientConfig + ---@diagnostic disable-next-line: missing-fields + config = { + handlers = require "rzls.roslyn_handlers", + settings = { + ["csharp|inlay_hints"] = { + csharp_enable_inlay_hints_for_implicit_object_creation = true, + csharp_enable_inlay_hints_for_implicit_variable_types = true, + + csharp_enable_inlay_hints_for_lambda_parameter_types = true, + csharp_enable_inlay_hints_for_types = true, + dotnet_enable_inlay_hints_for_indexer_parameters = true, + dotnet_enable_inlay_hints_for_literal_parameters = true, + dotnet_enable_inlay_hints_for_object_creation_parameters = true, + dotnet_enable_inlay_hints_for_other_parameters = true, + dotnet_enable_inlay_hints_for_parameters = true, + dotnet_suppress_inlay_hints_for_parameters_that_differ_only_by_suffix = true, + dotnet_suppress_inlay_hints_for_parameters_that_match_argument_name = true, + dotnet_suppress_inlay_hints_for_parameters_that_match_method_intent = true, + }, + ["csharp|code_lens"] = { + dotnet_enable_references_code_lens = true, + }, + }, + ---@class RoslynPatchedClient: vim.lsp.Client + ---@field patched boolean? + + ---@param client RoslynPatchedClient + ---@param bufnr integer + on_attach = function(client, bufnr) + -- Call AstroLSP's on_attach so it registers mappings, formatting, etc. + require("astrolsp").on_attach(client, bufnr) + + -- HACK: Patch out the `roslyn-ls` LSP client to have proper + -- semantic tokens. + -- This is a snippet of code taken and modified from: + -- https://github.com/seblyng/roslyn.nvim/wiki#semantic-tokens + if client.patched then + return + else + client.patched = true + end + + -- let the runtime know the server can do semanticTokens/full now + client.server_capabilities = vim.tbl_deep_extend("force", client.server_capabilities, { + semanticTokensProvider = { + full = true, + }, + }) + + local lsp_request = client.request + + client.request = function(method, params, handler, req_bufnr) + if method ~= vim.lsp.protocol.Methods.textDocument_semanticTokens_full then + return lsp_request(method, params, handler, req_bufnr) + end + + local target_bufnr = vim.uri_to_bufnr(params.textDocument.uri) + local line_count = vim.api.nvim_buf_line_count(target_bufnr) + local last_line = vim.api.nvim_buf_get_lines(target_bufnr, line_count - 1, line_count, true)[1] + + local returnvalue = lsp_request("textDocument/semanticTokens/range", { + textDocument = params.textDocument, + range = { + ["start"] = { + line = 0, + character = 0, + }, + ["end"] = { + line = line_count - 1, + character = string.len(last_line) - 1, + }, + }, + }, handler, req_bufnr) + return returnvalue + end + end, + }, + } + + return opts + end, + init = function() + vim.filetype.add { + extension = { + razor = "razor", + cshtml = "razor", + }, + } + end, +} diff --git a/modules/home-manager/programs/neovim/lua/plugins/treesitter.lua b/modules/home-manager/programs/neovim/lua/plugins/treesitter.lua index ee535af..ea0dec6 100644 --- a/modules/home-manager/programs/neovim/lua/plugins/treesitter.lua +++ b/modules/home-manager/programs/neovim/lua/plugins/treesitter.lua @@ -2,50 +2,57 @@ ---@type LazySpec return { - "nvim-treesitter/nvim-treesitter", - opts = function(_, opts) - -- add more things to the ensure_installed table protecting against community packs modifying it - opts.ensure_installed = require("astrocore").list_insert_unique(opts.ensure_installed, { - -- Programming - "c", - "c_sharp", - "cmake", - "cpp", - "css", - "gdscript", - "godot_resource", - "html", - "hyprlang", - "javascript", - "jsdoc", - "lua", - "nim", - "nim_format_string", - "objc", - "proto", - "python", - "svelte", - "tsx", - "typescript", - "vue", - -- Scripting - "bash", - "glsl", - -- Configuring - "dockerfile", - "json", - "jsonc", - "nix", - "vhs", - "yaml", - -- Misc - "cuda", - "markdown", - "markdown_inline", - "query", - -- VIM - "vim", - "vimdoc", - }) - end, + "nvim-treesitter/nvim-treesitter", + ---@param _ LazyPlugin + ---@param opts TSConfig + opts = function(_, opts) + -- disable automatically installing parsers + opts.auto_install = false + + -- add more things to the ensure_installed table protecting against community packs modifying it + opts.ensure_installed = require("astrocore").list_insert_unique(opts.ensure_installed --[[@as string[]], { + -- Programming + "c", + "c_sharp", + "cmake", + "cpp", + "css", + "gdscript", + "godot_resource", + "html", + "hyprlang", + "javascript", + "jsdoc", + "lua", + "nim", + "nim_format_string", + "objc", + "proto", + "python", + "razor", + "svelte", + "tsx", + "typescript", + "vue", + -- Scripting + "bash", + "fish", + "glsl", + -- Configuring + "dockerfile", + "json", + "jsonc", + "nix", + "vhs", + "yaml", + -- Misc + "cuda", + "markdown", + "markdown_inline", + "query", + -- VIM + "vim", + "vimdoc", + }) + end, }