languages/csharp: add razor support#1271
Conversation
|
Could you target the v0.8 branch? The LSP experience was mostly overhauled and improved, so this PR will likely benefit from the new API. |
|
I've noticed a lot has changed. I’m working on it! 😁 |
|
we merged 0.8 into main! So you can target main directly, now (sorry for the double switch) |
9e90201 to
c48c342
Compare
|
I figured it out! It wasn't a roslyn-ls version problem. Razor wasn't working correctly because the .razorExtension wasn't being mapped. Now everything works perfectly. |
c48c342 to
9887f11
Compare
9887f11 to
99f459f
Compare
|
I actually added roslyn-ls in v8.0 already, you should be able to use it now that's pulled into main. |
I noticed. It’s a pretty good implementation. I’ve just added Razor support. Your implementation doesn’t have that, and I need it for Blazor projects. |
horriblename
left a comment
There was a problem hiding this comment.
Thanks, but overall, I think we should wait for the nixpkgs PR to land first before adding razor support (see my other comments)
modules/plugins/languages/csharp.nix
Outdated
| (mkIf (cfg.lsp.enable && lib.elem "roslyn" cfg.lsp.servers) { | ||
| vim.luaConfigRC.roslyn-path = '' | ||
| -- NOTE: this is required by roslyn-nvim to find roslyn | ||
| vim.env.PATH = vim.env.PATH .. ":${pkgs.roslyn-ls}/bin" |
There was a problem hiding this comment.
- add the package to
vim.extraPackagesinstead
2. the package needs to be overridable somehow, preferably withdoesn't really apply if we wait for the nixpkgs PRvim.lsp.servers.roslyn.cmd, or if it's really not doable, add an optionvim.languages.csharp.lsp.package(this deviates from our usual pattern so an appropriate warning in casecmdis used would be appreciated)
modules/plugins/languages/csharp.nix
Outdated
| vim.env.MASON = vim.fn.expand("~/.local/share/nvf/mason") | ||
| local expanded_mason = vim.fn.expand("$MASON") | ||
| local mason = expanded_mason == "$MASON" | ||
| and vim.fs.joinpath(vim.fn.stdpath("data"), "mason") | ||
| or expanded_mason | ||
| local mason_packages = vim.fs.joinpath(mason, "packages") | ||
|
|
||
| -- Define target paths | ||
| local targets = { | ||
| vim.fs.joinpath(mason_packages, "roslyn", "libexec", ".razorExtension"), | ||
| vim.fs.joinpath(mason_packages, "roslyn-unstable", "libexec", ".razorExtension"), | ||
| } |
There was a problem hiding this comment.
use vim.fn.stdpath("data") .. "/mason" instead of hard-coding ~/.local/share
I don't really like this approach overall. I would rather wait until https://github.com/NixOS/nixpkgs/issues/472357 is merged. Will probably need to patch out the warning in roslyn-nvim unless they start supporting it themselves though
| omnisharp = ["omnisharp-extended-lsp-nvim"]; | ||
| csharp_ls = ["csharpls-extended-lsp-nvim"]; | ||
| roslyn_ls = []; | ||
| roslyn = ["roslyn-nvim"]; |
There was a problem hiding this comment.
this is technically a breaking change, a warning about removal of "roslyn_ls" in lsp.servers would be nice
There was a problem hiding this comment.
Now I warn it on lsp.servers. Just not sure if I did it the right way.
034f317 to
7f9bb33
Compare
7f9bb33 to
4cad097
Compare
I’ve rethought the symlink approach, and I now believe this solution is more reproducible and, overall, more stable. |
056078a to
416f232
Compare
|
is this ready for review now? |
| root_dir = mkLuaInline '' | ||
| function(bufnr, on_dir) | ||
| local function find_root_pattern(fname, lua_pattern) | ||
| return vim.fs.root(0, function(name, path) | ||
| return name:match(lua_pattern) | ||
| end) | ||
| end | ||
|
|
||
| local fname = vim.api.nvim_buf_get_name(bufnr) | ||
| on_dir(find_root_pattern(fname, "%.sln$") or find_root_pattern(fname, "%.csproj$")) | ||
| end | ||
| ''; |
There was a problem hiding this comment.
the original root_dir function and the new root_marks are not equivalent, the root_marks do not match ProjectName.csproj and ProjectName.sln, don't change these
modules/plugins/languages/csharp.nix
Outdated
|
|
||
| (mkIf (cfg.lsp.enable && lib.elem "roslyn" cfg.lsp.servers) | ||
| { | ||
| vim.extraPackages = [pkgs.roslyn-ls pkgs.vscode-langservers-extracted]; |
There was a problem hiding this comment.
Don't add the LSP package to PATH, set the cmd for roslyn directly with vim.lsp.servers.*.cmd
modules/plugins/languages/csharp.nix
Outdated
| # NOTE: roslyn-nvim requires razorExtension dll's | ||
| # See: https://github.com/seblyng/roslyn.nvim/blob/main/lua/roslyn/health.lua | ||
| vim.luaConfigRC.razorExtension = '' | ||
| vim.env.MASON = "${razorExtensions.${cfg.lsp.razorExtension}}" |
There was a problem hiding this comment.
I don't think abusing $MASON like this is a good idea. What if some other plugin sees $MASON and starts doing mason things it's not supposed to? What if another LSP needs this hack as well?
Adapting from NixOS/nixpkgs#472357 (comment), something like this should work (untested):
let
pkg = pkgs.vscode-extensions.ms-dotnettools.csharp;
pluginRoot = "${pkg}/share/vscode/extensions/ms-dotnettools.csharp";
exe ="${pluginRoot}/.roslyn/Microsoft.CodeAnalysis.LanguageServer":
razorSourceGenerator = "${pluginRoot}/.razorExtension/Microsoft.CodeAnalysis.LanguageServer":
razorDesignTimePath = "${pluginRoot}/.razorExtension/Targets/Microsoft.NET.Sdk.Razor.DesignTime.targets";
razorExtension = "${pluginRoot}/.razorExtension/Microsoft.VisualStudioCode.RazorExtension.dll";
in {
cmd = mkLuaInline ''
{
"${exe}",
"--stdio",
"--logLevel=Information",
"--extensionLogDirectory=" .. vim.fs.dirname(vim.lsp.get_log_path()),
"--razorSourceGenerator=${razorSourceGenerator}",
"--razorDesignTimePath=${razorDesignTimePath}",
"--extension=${razorExtension}",
}
'';
}set vim.lsp.servers.roslyn.cmd to the above
There was a problem hiding this comment.
This solution works perfectly. The only required adjustment is to prepend dotnet before "${exe}". Without this, roslyn-ls incorrectly reports that the .NET SDK is missing, even though it is installed.
There is one additional observation: I tested Razor support in csharp-ls, and while it does function, the implementation is quite minimal. It only recognizes .cshtml files and offers limited functionality beyond basic support. Nevertheless, I decided to include it.
416f232 to
d8406d5
Compare
d8406d5 to
0d1612c
Compare
e7fe14f to
cf2e3f5
Compare
0cc4836 to
9bf6f5b
Compare
9bf6f5b to
1ab4af5
Compare
1ab4af5 to
d8763e4
Compare
d8763e4 to
da68d95
Compare
7dda21a to
3c97a96
Compare
Adds razor support for `roslyn` and `csharp_ls` servers
3c97a96 to
dbcfb83
Compare
It adds the roslyn-ls configuration and the roslyn-nvim plugin. Razor support works partially. The roslyn-nvim team announced that they now support Razor, but I think the version of roslyn-ls available in nixpkgs isn’t up to date. Because of that, Razor files may still produce errors. Aside from this, everything works fine.
This PR should fix #1268
Sanity Checking
nix fmt).#nix(default package).#maximal.#docs-html(manual, must build).#docs-linkcheck(optional, please build if adding links)x86_64-linuxaarch64-linuxx86_64-darwinaarch64-darwin