Skip to content

Commit

Permalink
Fix a bug in the Add Open code fix (#15998)
Browse files Browse the repository at this point in the history
* Fix a bug in an Add Open code fix

* fantomas
  • Loading branch information
psfinaki authored Sep 19, 2023
1 parent 24345e6 commit c4b56b4
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 12 deletions.
25 changes: 15 additions & 10 deletions vsintegration/src/FSharp.Editor/CodeFixes/AddOpenCodeFixProvider.fs
Original file line number Diff line number Diff line change
Expand Up @@ -55,21 +55,26 @@ type internal AddOpenCodeFixProvider [<ImportingConstructor>] (assemblyContentPr

// attribute, shouldn't be here
| line when line.StartsWith "[<" && line.EndsWith ">]" ->
let moduleDeclLineNumber =
let moduleDeclLineNumberOpt =
sourceText.Lines
|> Seq.skip insertionLineNumber
|> Seq.findIndex (fun line -> line.ToString().Contains "module")
// add back the skipped lines
|> fun i -> insertionLineNumber + i
|> Seq.tryFindIndex (fun line -> line.ToString().Contains "module")

let moduleDeclLineText = sourceText.Lines[ moduleDeclLineNumber ].ToString().Trim()
match moduleDeclLineNumberOpt with
// implicit top level module
| None -> insertionLineNumber, $"{margin}open {ns}{br}{br}"
// explicit top level module
| Some number ->
// add back the skipped lines
let moduleDeclLineNumber = insertionLineNumber + number
let moduleDeclLineText = sourceText.Lines[ moduleDeclLineNumber ].ToString().Trim()

if moduleDeclLineText.EndsWith "=" then
insertionLineNumber, $"{margin}open {ns}{br}{br}"
else
moduleDeclLineNumber + 2, $"{margin}open {ns}{br}{br}"
if moduleDeclLineText.EndsWith "=" then
insertionLineNumber, $"{margin}open {ns}{br}{br}"
else
moduleDeclLineNumber + 2, $"{margin}open {ns}{br}{br}"

// something else, shot in the dark
// implicit top level module
| _ -> insertionLineNumber, $"{margin}open {ns}{br}{br}"

| ScopeKind.Namespace -> insertionLineNumber + 3, $"{margin}open {ns}{br}{br}"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ let ``Fixes FS0039 for missing opens - nested module`` () =
Assert.Equal(expected, actual)

[<Fact>]
let ``Fixes FS0039 for missing opens - module has attributes`` () =
let ``Fixes FS0039 for missing opens - explicit module has attributes`` () =
let code =
"""
[<AutoOpen>]
Expand Down Expand Up @@ -187,6 +187,33 @@ Console.WriteLine 42

Assert.Equal(expected, actual)

[<Fact>]
let ``Fixes FS0039 for missing opens - implicit module has attributes`` () =
let code =
"""
[<Obsolete>]
type MyType() =
let now = DateTime.Now
"""

let expected =
Some
{
Message = "open System"
FixedCode =
"""
open System
[<Obsolete>]
type MyType() =
let now = DateTime.Now
"""
}

let actual = codeFix |> tryFix code Auto

Assert.Equal(expected, actual)

// TODO: the open statement should actually be within the module
[<Fact>]
let ``Fixes FS0039 for missing opens - nested module has attributes`` () =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ module Module1 =
Assert.Equal(expected, actual)

[<Fact>]
let ``Fixes FS0039 for missing opens - module has attributes`` () =
let ``Fixes FS0039 for missing opens - explicit module has attributes`` () =
let code =
"""
[<AutoOpen>]
Expand Down Expand Up @@ -181,6 +181,33 @@ Console.WriteLine 42

Assert.Equal(expected, actual)

[<Fact>]
let ``Fixes FS0039 for missing opens - implicit module has attributes`` () =
let code =
"""
[<Obsolete>]
type MyType() =
let now = DateTime.Now
"""

let expected =
Some
{
Message = "open System"
FixedCode =
"""
open System
[<Obsolete>]
type MyType() =
let now = DateTime.Now
"""
}

let actual = codeFix |> tryFix code Auto

Assert.Equal(expected, actual)

[<Fact>]
let ``Fixes FS0039 for missing opens - nested module has attributes`` () =
let code =
Expand Down

0 comments on commit c4b56b4

Please sign in to comment.