165 lines
5.0 KiB
Markdown
165 lines
5.0 KiB
Markdown
# Using C# with Neovim
|
|
|
|
So I have come to enjoy the modern C# somewhat, even though it is not my favourite language.
|
|
I do like the `dotnet` cli when working on Linux, and when using it with Neovim and the omnisharp_roslyn, it works quite well.
|
|
|
|
Here is just a small guide to get going with it.
|
|
|
|
## install omnisharp
|
|
Download the latest version of OmniSharp on their [Github Page]("https://github.com/OmniSharp/omnisharp-roslyn") under releases.
|
|
|
|
I downloaded the x64.tar.gz version for linux. then I untarred it to .local/share/omnisharp
|
|
then i symlinked it to path, here I did it to /usr/bin/local
|
|
with something like this
|
|
|
|
```bash
|
|
sudo ln -s ~/.local/share/omnisharp/OmniSharp /usr/local/bin/omnisharp
|
|
```
|
|
|
|
Note that Neovim wants the OmniSharp binary to be with lower case letters, that is why i symlink OmniSharp -> omnisharp.
|
|
|
|
Then in nvim.lua file i add something like this, and just notice the omnisharp config in the language server section.
|
|
That should do it. Pretty "sharp" C# setup on Linux, if I should say so myself. I do kind of enjoy it now.
|
|
|
|
```lua
|
|
do
|
|
vim.pack.add({ gh("j-hui/fidget.nvim") })
|
|
require("fidget").setup({})
|
|
|
|
vim.api.nvim_create_autocmd("LspAttach", {
|
|
group = vim.api.nvim_create_augroup("kickstart-lsp-attach", { clear = true }),
|
|
callback = function(event)
|
|
local map = function(keys, func, desc, mode)
|
|
mode = mode or "n"
|
|
vim.keymap.set(mode, keys, func, { buffer = event.buf, desc = "LSP: " .. desc })
|
|
end
|
|
|
|
map("grn", vim.lsp.buf.rename, "[R]e[n]ame")
|
|
|
|
map("gra", vim.lsp.buf.code_action, "[G]oto Code [A]ction", { "n", "x" })
|
|
|
|
map("grD", vim.lsp.buf.declaration, "[G]oto [D]eclaration")
|
|
|
|
local client = vim.lsp.get_client_by_id(event.data.client_id)
|
|
if client and client:supports_method("textDocument/documentHighlight", event.buf) then
|
|
local highlight_augroup = vim.api.nvim_create_augroup("kickstart-lsp-highlight", { clear = false })
|
|
vim.api.nvim_create_autocmd({ "CursorHold", "CursorHoldI" }, {
|
|
buffer = event.buf,
|
|
group = highlight_augroup,
|
|
callback = vim.lsp.buf.document_highlight,
|
|
})
|
|
|
|
vim.api.nvim_create_autocmd({ "CursorMoved", "CursorMovedI" }, {
|
|
buffer = event.buf,
|
|
group = highlight_augroup,
|
|
callback = vim.lsp.buf.clear_references,
|
|
})
|
|
|
|
vim.api.nvim_create_autocmd("LspDetach", {
|
|
group = vim.api.nvim_create_augroup("kickstart-lsp-detach", { clear = true }),
|
|
callback = function(event2)
|
|
vim.lsp.buf.clear_references()
|
|
vim.api.nvim_clear_autocmds({ group = "kickstart-lsp-highlight", buffer = event2.buf })
|
|
end,
|
|
})
|
|
end
|
|
|
|
if client and client:supports_method("textDocument/inlayHint", event.buf) then
|
|
map("<leader>th", function()
|
|
vim.lsp.inlay_hint.enable(not vim.lsp.inlay_hint.is_enabled({ bufnr = event.buf }))
|
|
end, "[T]oggle Inlay [H]ints")
|
|
end
|
|
end,
|
|
})
|
|
|
|
-- Enable the following language servers
|
|
-- Feel free to add/remove any LSPs that you want here. They will automatically be installed.
|
|
-- See `:help lsp-config` for information about keys and how to configure
|
|
---@type table<string, vim.lsp.Config>
|
|
local servers = {
|
|
clangd = {},
|
|
gopls = {},
|
|
pyright = {},
|
|
rust_analyzer = {},
|
|
ts_ls = {},
|
|
stylua = {},
|
|
jdtls = {},
|
|
omnisharp = {
|
|
cmd = { "omnisharp", "--languageserver" },
|
|
settings = {
|
|
FormattingOptions = {
|
|
EnableEditorConfigSupport = true,
|
|
},
|
|
RoslynExtensionsOptions = {
|
|
EnableAnalyzersSupport = true,
|
|
EnableImportCompletion = true,
|
|
},
|
|
Sdk = {
|
|
OmniSharp = {
|
|
UseModernNet = true,
|
|
},
|
|
},
|
|
},
|
|
},
|
|
lua_ls = {
|
|
on_init = function(client)
|
|
client.server_capabilities.documentFormattingProvider = false
|
|
|
|
if client.workspace_folders then
|
|
local path = client.workspace_folders[1].name
|
|
if
|
|
path ~= vim.fn.stdpath("config")
|
|
and (vim.uv.fs_stat(path .. "/.luarc.json") or vim.uv.fs_stat(path .. "/.luarc.jsonc"))
|
|
then
|
|
return
|
|
end
|
|
end
|
|
|
|
local current_settings = client.config.settings --[[@as lspconfig.settings.lua_ls]]
|
|
client.config.settings.Lua = vim.tbl_deep_extend("force", current_settings.Lua, {
|
|
runtime = {
|
|
version = "LuaJIT",
|
|
path = { "lua/?.lua", "lua/?/init.lua" },
|
|
},
|
|
workspace = {
|
|
checkThirdParty = false,
|
|
library = vim.api.nvim_get_runtime_file("", true),
|
|
},
|
|
})
|
|
end,
|
|
---@type lspconfig.settings.lua_ls
|
|
settings = {
|
|
Lua = {
|
|
format = { enable = false },
|
|
},
|
|
},
|
|
},
|
|
}
|
|
|
|
vim.pack.add({
|
|
gh("neovim/nvim-lspconfig"),
|
|
gh("mason-org/mason.nvim"),
|
|
gh("mason-org/mason-lspconfig.nvim"),
|
|
gh("WhoIsSethDaniel/mason-tool-installer.nvim"),
|
|
})
|
|
|
|
require("mason").setup({})
|
|
|
|
require("mason-lspconfig").setup({
|
|
automatic_enable = false,
|
|
})
|
|
|
|
local ensure_installed = vim.tbl_keys(servers or {})
|
|
vim.list_extend(ensure_installed, {})
|
|
|
|
require("mason-tool-installer").setup({ ensure_installed = ensure_installed })
|
|
|
|
for name, server in pairs(servers) do
|
|
vim.lsp.config(name, server)
|
|
vim.lsp.enable(name)
|
|
end
|
|
end
|
|
|
|
```
|
|
|