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

Unable to remap projected document edits if they fall outside of known list of source mappings #4284

Closed
TanayParikh opened this issue Oct 1, 2020 · 3 comments · Fixed by #5748
Labels
enhancement Small improvement request

Comments

@TanayParikh
Copy link
Contributor

@ajaybhargavb and I were looking into why some edits were being lost during formatting. Issue seems to be due to how we handle projections which come at the end of a document / @code block.

Passing edits into the formatter which go beyond the last line of generated C# fails. This is because the edit starts 1 line after the last source mapping, and consequently the projection mapping fails.

Call here.

@NavMenuCssClass

@(NavMenuCssClass)

@code {
    int counter = 3;
}

Note: Code actions do not work with single line @code blocks as the mapping fails.

Edited C# & Generated C#

Edited C#:

// <auto-generated/>
#pragma warning disable 1591
namespace blazorserver6.Pages
{
    #line hidden
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Threading.Tasks;
    using Microsoft.AspNetCore.Components;
#nullable restore
#line 1 "c:/Users/taparik/dev/blazorserver6/_Imports.razor"
using System.Net.Http;

#line default
#line hidden
#nullable disable
#nullable restore
#line 2 "c:/Users/taparik/dev/blazorserver6/_Imports.razor"
using Microsoft.AspNetCore.Authorization;

#line default
#line hidden
#nullable disable
#nullable restore
#line 3 "c:/Users/taparik/dev/blazorserver6/_Imports.razor"
using Microsoft.AspNetCore.Components.Authorization;

#line default
#line hidden
#nullable disable
#nullable restore
#line 4 "c:/Users/taparik/dev/blazorserver6/_Imports.razor"
using Microsoft.AspNetCore.Components.Forms;

#line default
#line hidden
#nullable disable
#nullable restore
#line 5 "c:/Users/taparik/dev/blazorserver6/_Imports.razor"
using Microsoft.AspNetCore.Components.Routing;

#line default
#line hidden
#nullable disable
#nullable restore
#line 6 "c:/Users/taparik/dev/blazorserver6/_Imports.razor"
using Microsoft.AspNetCore.Components.Web;

#line default
#line hidden
#nullable disable
#nullable restore
#line 7 "c:/Users/taparik/dev/blazorserver6/_Imports.razor"
using Microsoft.JSInterop;

#line default
#line hidden
#nullable disable
#nullable restore
#line 8 "c:/Users/taparik/dev/blazorserver6/_Imports.razor"
using blazorserver6;

#line default
#line hidden
#nullable disable
#nullable restore
#line 9 "c:/Users/taparik/dev/blazorserver6/_Imports.razor"
using blazorserver6.Shared;

#line default
#line hidden
#nullable disable
    public partial class Component : Microsoft.AspNetCore.Components.ComponentBase
    {
        #pragma warning disable 219
        private void __RazorDirectiveTokenHelpers__() {
        }
        #pragma warning restore 219
        #pragma warning disable 0414
        private static System.Object __o = null;
        #pragma warning restore 0414
        #pragma warning disable 1998
        protected override void BuildRenderTree(Microsoft.AspNetCore.Components.Rendering.RenderTreeBuilder __builder)
        {
#nullable restore
#line 1 "c:/Users/taparik/dev/blazorserver6/Pages/Component.razor"
__o = NavMenuCssClass;

#line default
#line hidden
#nullable disable
#nullable restore
#line 3 "c:/Users/taparik/dev/blazorserver6/Pages/Component.razor"
__o = NavMenuCssClass;

#line default
#line hidden
#nullable disable
        }
        #pragma warning restore 1998
#nullable restore
#line 5 "c:/Users/taparik/dev/blazorserver6/Pages/Component.razor"
       
    int foo = 3;

        public Component(int foo)
        {
            this.foo = foo;
        }

#line default
#line hidden
#nullable disable
    }
}
#pragma warning restore 1591

Original:

// <auto-generated/>
#pragma warning disable 1591
namespace blazorserver6.Pages
{
    #line hidden
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Threading.Tasks;
    using Microsoft.AspNetCore.Components;
#nullable restore
#line 1 "c:/Users/taparik/dev/blazorserver6/_Imports.razor"
using System.Net.Http;

#line default
#line hidden
#nullable disable
#nullable restore
#line 2 "c:/Users/taparik/dev/blazorserver6/_Imports.razor"
using Microsoft.AspNetCore.Authorization;

#line default
#line hidden
#nullable disable
#nullable restore
#line 3 "c:/Users/taparik/dev/blazorserver6/_Imports.razor"
using Microsoft.AspNetCore.Components.Authorization;

#line default
#line hidden
#nullable disable
#nullable restore
#line 4 "c:/Users/taparik/dev/blazorserver6/_Imports.razor"
using Microsoft.AspNetCore.Components.Forms;

#line default
#line hidden
#nullable disable
#nullable restore
#line 5 "c:/Users/taparik/dev/blazorserver6/_Imports.razor"
using Microsoft.AspNetCore.Components.Routing;

#line default
#line hidden
#nullable disable
#nullable restore
#line 6 "c:/Users/taparik/dev/blazorserver6/_Imports.razor"
using Microsoft.AspNetCore.Components.Web;

#line default
#line hidden
#nullable disable
#nullable restore
#line 7 "c:/Users/taparik/dev/blazorserver6/_Imports.razor"
using Microsoft.JSInterop;

#line default
#line hidden
#nullable disable
#nullable restore
#line 8 "c:/Users/taparik/dev/blazorserver6/_Imports.razor"
using blazorserver6;

#line default
#line hidden
#nullable disable
#nullable restore
#line 9 "c:/Users/taparik/dev/blazorserver6/_Imports.razor"
using blazorserver6.Shared;

#line default
#line hidden
#nullable disable
    public partial class Component : Microsoft.AspNetCore.Components.ComponentBase
    {
        #pragma warning disable 219
        private void __RazorDirectiveTokenHelpers__() {
        }
        #pragma warning restore 219
        #pragma warning disable 0414
        private static System.Object __o = null;
        #pragma warning restore 0414
        #pragma warning disable 1998
        protected override void BuildRenderTree(Microsoft.AspNetCore.Components.Rendering.RenderTreeBuilder __builder)
        {
#nullable restore
#line 1 "c:/Users/taparik/dev/blazorserver6/Pages/Component.razor"
__o = NavMenuCssClass;

#line default
#line hidden
#nullable disable
#nullable restore
#line 3 "c:/Users/taparik/dev/blazorserver6/Pages/Component.razor"
__o = NavMenuCssClass;

#line default
#line hidden
#nullable disable
        }
        #pragma warning restore 1998
#nullable restore
#line 5 "c:/Users/taparik/dev/blazorserver6/Pages/Component.razor"
       
    int foo = 3;

#line default
#line hidden
#nullable disable
    }
}
#pragma warning restore 1591

Another manifestation of this issue:

@functions {
    class Foo
    {

    }
}

Invoke generate constructor code action.

Expected:

@functions {
    class Foo
    {
        public Foo()
        {

        }
    }
}

Actual:

@functions {
    {
        public Foo()
        {
        }
    }
}
@ajaybhargavb
Copy link
Contributor

Updating the issue title to better represent the underlying problem.

This is more of a design limitation than a bug. Essentially, the issue is that we can't map a position/range that is not in our SourceMappings. In some cases, edits returned by Roslyn for code actions end up going to places we don't have mapping for and thus doesn't get applied. There is no easy way to get around this unless we make our mapping system more complex and make it understand more than verbatim source mappings.

@ajaybhargavb ajaybhargavb changed the title DocumentMappingService.TryMapFromProjectedDocumentRange Mapping Fails When At End of Document Unable to remap projected document edits if they fall outside of known list of source mappings Oct 1, 2020
@TanayParikh
Copy link
Contributor Author

https://github.com/dotnet/aspnetcore/issues/26867 contains a targeted quick mitigation for this. The long term solution for code actions will involve heuristically determining the correct place within the razor syntax tree to insert the complete code action.

@ghost
Copy link

ghost commented Oct 21, 2020

We've moved this issue to the Backlog milestone. This means that it is not going to be worked on for the coming release. We will reassess the backlog following the current release and consider this item at that time. To learn more about our issue management process and to have better expectation regarding different types of issues you can read our Triage Process.

@allisonchou allisonchou transferred this issue from dotnet/aspnetcore Oct 1, 2021
@allisonchou allisonchou added the enhancement Small improvement request label Oct 1, 2021
@allisonchou allisonchou modified the milestones: Next Preview Candidates, 17.1 Oct 1, 2021
@ghost ghost locked as resolved and limited conversation to collaborators Dec 18, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
enhancement Small improvement request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants