dotnet / fsharp

The F# compiler, F# core library, F# language service, and F# tooling integration for Visual Studio
https://dotnet.microsoft.com/languages/fsharp
MIT License
3.87k stars 781 forks source link

visualstudio open namespace codefix has wrong indentation in some cases #10521

Open smoothdeveloper opened 3 years ago

smoothdeveloper commented 3 years ago

In indented context, the open namespace code fix puts the declaration at wrong indentation level.

It doesn't always affect the code, but when there are type extensions, it breaks it.

module Foo =
  open System

  type DateTime with
    member x.X() = ()

  let a () =
    let t = Task.Factory.StartNew(fun () -> 1) // use the code fix on Task
    ()

open namespace indentiation glitch

Related information

VS2019 16.8

smoothdeveloper commented 3 years ago

Another case:

[<AutoOpen>] // will insert after the attribute
module Foo2 =
  // would prefer here if "Always place open statements at the top level" is unchecked
  let a () =
    let t = Task.Factory.StartNew(fun () -> 1) // use the code fix on Task
    ()

Currently Here it introduces open ... on a new line just after the attribute making the code invalid.

Given the settings, I'd prefer open ... to land on a new line just above let a () =

image

smoothdeveloper commented 3 years ago

@vzarytovskii/@cartermp do you know if there are tests for this code fix, is it easy to add one for each cases?

cartermp commented 3 years ago

This seems like a bug in the tryfindinsertioncontext routine here: https://github.com/dotnet/fsharp/blob/8b361cfc6eecbccfb2dc625faa7ff66e7e621be5/src/fsharp/service/ServiceAssemblyContent.fs#L994

It's complicated code unfortunately. I'm not sure if it's under test. We have decent testing for the analysis part, but not the insertion part.

psfinaki commented 1 year ago

Okay this still reproduces the "Always place open statements at the top level" off.

psfinaki commented 1 year ago

Fixed partially as of now - the code fix doesn't break the code anymore but the location of the inserted code is still not precise.