Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Linter for cshtml (Razor) files #546

Closed
Craige opened this issue Dec 30, 2019 · 20 comments
Closed

Linter for cshtml (Razor) files #546

Craige opened this issue Dec 30, 2019 · 20 comments

Comments

@Craige
Copy link

Craige commented Dec 30, 2019

.cs files are setup with an Ale linter that works great, but .cshtml files are not. The Omnisharp linter should work just fine if configured for .cshtml files as well.

@nickspoons
Copy link
Member

Unfortunately, OmniSharp doesn't support razor files by itself. VSCode has support using an extra plugin, but it is not simple getting them to work together - if you try in VSCode and check the OmniSharp logs, you can see that there are temp files being generated and sent to OmniSharp-roslyn. For example, an Index.cshtml file has an associated Index.cshtml__virtual.cs. This is not being generated by OmniSharp-roslyn but a standalone Razor plugin.

I have no idea how or if we can do something similar here, if someone wants to try then a PR would be great. But just telling OmniSharp-vim to lint .cshtml files like .cs files does not work (OmniSharp-roslyn throws exceptions).

@cordiosd
Copy link

This capability would be amazing.

@nickspoons
Copy link
Member

I've had a slightly more detailed look into this, and my conclusion is still that this is not something that this repo can support.

Here's what VSCode does with razor files as far as I can tell:

  1. The OmniSharp-roslyn server is started up as usual, except it is also passed in the path to a plugin dll, which I believe contains references for the translated razor .cs (see 3.)
  2. A razor language server is also started, rzls.exe
  3. When a .cshtml file needs to be linted or requests completions etc., rzls is used to create a tmp .cs file (in memory, it's not actually written to disk), e.g. Index.cshtml generates Index.cshtml__virtual.cs
  4. The tmp file is sent to OmniSharp-roslyn
  5. The response values are converted back to razor files by rzls

I'm not 100% about this flow but those elements are all involved.

We could handle some of this but I don't think we should start secondary language servers in this plugin. If someone wants to work on razor support, my suggestion is to create a new plugin which relies on OmniSharp-vim for the OmniSharp-roslyn part.

However it is possible that there are licensing issues preventing rzls from being used outside VSCode (I have no idea if this is the case, I just know that the VSCode debugger is licensed that way).

If anyone is interested, this is what the translated .cshtml file looks like:

Index.cshtml

@page
@model IndexModel
@{
    ViewData["Title"] = "Home page";
}

<div class="text-center">
    <h1 class="display-4">Welcome</h1>
    <p>Learn about <a href="https://docs.microsoft.com/aspnet/core">building Web apps with ASP.NET Core</a>.</p>
</div>

Index.cshtml__virtual.cs

// <auto-generated/>
#pragma warning disable 1591
namespace razor.Pages
{
    #line hidden
    using TModel = global::System.Object;
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Threading.Tasks;
    using Microsoft.AspNetCore.Mvc;
    using Microsoft.AspNetCore.Mvc.Rendering;
    using Microsoft.AspNetCore.Mvc.ViewFeatures;
#nullable restore
#line 1 "c:/code/test/razor/Pages/_ViewImports.cshtml"
using razor;

#line default
#line hidden
#nullable disable
    public class Pages_Index : global::Microsoft.AspNetCore.Mvc.RazorPages.Page
    {
        #pragma warning disable 219
        private void __RazorDirectiveTokenHelpers__() {
        ((System.Action)(() => {
#nullable restore
#line 2 "c:/code/test/razor/Pages/Index.cshtml"
IndexModel __typeHelper = default!;

#line default
#line hidden
#nullable disable
        }
        ))();
        }
        #pragma warning restore 219
        #pragma warning disable 0414
        private static System.Object __o = null;
        #pragma warning restore 0414
        #pragma warning disable 1998
        public async override global::System.Threading.Tasks.Task ExecuteAsync()
        {
#nullable restore
#line 3 "c:/code/test/razor/Pages/Index.cshtml"
  
    ViewData["Title"] = "Home page";

#line default
#line hidden
#nullable disable
        }
        #pragma warning restore 1998
        [global::Microsoft.AspNetCore.Mvc.Razor.Internal.RazorInjectAttribute]
        public global::Microsoft.AspNetCore.Mvc.ViewFeatures.IModelExpressionProvider ModelExpressionProvider { get; private set; }
        [global::Microsoft.AspNetCore.Mvc.Razor.Internal.RazorInjectAttribute]
        public global::Microsoft.AspNetCore.Mvc.IUrlHelper Url { get; private set; }
        [global::Microsoft.AspNetCore.Mvc.Razor.Internal.RazorInjectAttribute]
        public global::Microsoft.AspNetCore.Mvc.IViewComponentHelper Component { get; private set; }
        [global::Microsoft.AspNetCore.Mvc.Razor.Internal.RazorInjectAttribute]
        public global::Microsoft.AspNetCore.Mvc.Rendering.IJsonHelper Json { get; private set; }
        [global::Microsoft.AspNetCore.Mvc.Razor.Internal.RazorInjectAttribute]
        public global::Microsoft.AspNetCore.Mvc.Rendering.IHtmlHelper<IndexModel> Html { get; private set; }
        public global::Microsoft.AspNetCore.Mvc.ViewFeatures.ViewDataDictionary<IndexModel> ViewData => (global::Microsoft.AspNetCore.Mvc.ViewFeatures.ViewDataDictionary<IndexModel>)PageContext?.ViewData;
        public IndexModel Model => ViewData.Model;
    }
}
#pragma warning restore 1591

// 1

@cordiosd
Copy link

@nickspoons Thank you for looking into that. In the meantime, I am just creating a vim plugin that will automate the creation of .razor files with code behind files from vim.

omnisharp-vim is an amazing plugin with coc and ale. It is unfortunate that coc recommends coc-omnisharp instead. I was sticking with vscode until I discovered that using omnisharp-vim instead of coc-omnisharp was the way to go.

As I am just starting to get the skills necessary to know how to do most things in vim, I have just written my first vim scripts for the ftplugins and compiler directories to automate csharp workflow. i.e. (:help 41.12, :help 41.13). Off the top of your head, do you know if I am just rewriting what already exists?
The gist so far is just supporting the use of :make from within vim from .cs and .razor files.

@nickspoons
Copy link
Member

Hi @dball-adashi,

Working out scripts like that is a very good way to get into vimscript. If you want some inspiration, have a look here

@EdwardKerckhof
Copy link

Any news on this? Or steps on how I can implement this?

@Tantol
Copy link

Tantol commented Aug 24, 2022

Will blazor be supported in any future?

@nickspoons
Copy link
Member

The same answers as previously, but in short:

  1. I don't (currently) use razor or blazor personally so don't have the drive to work on it
  2. It appears to be pretty complicated involving a lot of hidden buffer manipulation
  3. If anyone does the work and makes a PR that works, I'll merge it

@InfinytRam
Copy link

InfinytRam commented Oct 6, 2023

+1 on this.

I don't want to go back to vscode or visual studio, just got addicted to neovim recently.

This capability of supporting .razor files would be extremely appreciated given that I work with dotnet and C# daily for my work.

@nickspoons, any possible hints here in that issue? sublimelsp/LSP-OmniSharp#3

cshtml seems to be handled by "Razor". It's an additional plugin that needs to be downloaded: https://github.com/OmniSharp/omnisharp-vscode/blob/6608b739146123e920f410da5f29b9abfa8309eb/src/omnisharp/server.ts#L322-L326

@skttlock
Copy link

Another +1!

@ninbura
Copy link

ninbura commented Jan 9, 2024

Would be awesome. A lack of .razor support is what's keeping me from leaving Visual Studio hell.

@daveHylde
Copy link

+1 here

@nickspoons
Copy link
Member

Can people stop adding comments saying "+1" please? I've explained why this is not trivial, and that I'm not going to do it. If anyone wants to do the work I'm more than happy to help and provide advice, testing and debug support, whatever you need. Until then "+1" isn't getting us anywhere.

@Cris-lml007
Copy link

I will try to make a blazor plugin, but I require more information regarding how the LSP error alert system works. If you could provide me with a guide about it it would be great.

@haras-unicorn
Copy link

if ur able to make something for vim this would be a huge win for all non vscode editors
firstly neovim but im using helix and if it works with vim/neovim it should work with helix as well

@haras-unicorn
Copy link

ok so getting rzls to build and work wasnt so hard (i just cloned razor and dotnet publish to get rzls) but it seems it has a ton of custom LSP messages and that language server wont work with a general LSP client implementation - it seems it needs custom handlers for a lot of messages

@tris203
Copy link

tris203 commented Jun 3, 2024

I am working on a plugin to hopefully work towards support for this is anyone is looking to collaborate

https://github.com/tris203/rzls.nvim

@InfinytRam
Copy link

InfinytRam commented Jun 22, 2024

@tris203, With a recent Neovim version for your plugin, how do we configure it with With the "LspAttach" event.
See :h LspAttach.

Not sure what to pass to on_attach when using "LspAttach" event.

@tris203
Copy link

tris203 commented Jun 22, 2024

@tris203, With a recent Neovim version for your plugin, how do we configure it with With the "LspAttach" event.
See :h LspAttach.

Create an issue in the repo if you need more but at the moment it's done through a setup function which creates the autocmds and starts the server etc. The details are in the README

It is by no means production ready yet though

@InfinytRam
Copy link

@tris203, thanks. I couldn't get your plugin to work :(

I created an issue on your repository. Help is appreciated.

Looking forward to get this plugin working and enhance the dev exprience for C# community.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests