Skip to content

Commit

Permalink
Fix overrides generation for "naked" single case unions (#530)
Browse files Browse the repository at this point in the history
Co-authored-by: Eugene Auduchinok <[email protected]>
  • Loading branch information
2 people authored and DedSec256 committed Jun 6, 2023
1 parent 9907074 commit 60366d1
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,14 @@ open JetBrains.ReSharper.Plugins.FSharp.Psi.Features.Generate
open JetBrains.ReSharper.Plugins.FSharp.Psi.Features.Util
open JetBrains.ReSharper.Plugins.FSharp.Psi.Impl.Cache2
open JetBrains.ReSharper.Plugins.FSharp.Psi.Impl.Tree
open JetBrains.ReSharper.Plugins.FSharp.Psi.Parsing
open JetBrains.ReSharper.Plugins.FSharp.Psi.Tree
open JetBrains.ReSharper.Plugins.FSharp.Psi.Util
open JetBrains.ReSharper.Plugins.FSharp.Util
open JetBrains.ReSharper.Psi
open JetBrains.ReSharper.Psi.DataContext
open JetBrains.ReSharper.Psi.ExtensionsAPI
open JetBrains.ReSharper.Psi.ExtensionsAPI.Tree
open JetBrains.ReSharper.Psi.Impl
open JetBrains.ReSharper.Psi.Tree
open JetBrains.ReSharper.Psi.Util
Expand Down Expand Up @@ -288,9 +290,10 @@ type FSharpOverridingMembersBuilder() =
normalizeReprEnd recordRepr.LeftBrace recordRepr.RightBrace
recordRepr.LeftBrace, recordRepr.RightBrace

| :? IUnionRepresentation as unionRepr ->
| :? IUnionRepresentation
| :? ITypeAbbreviationRepresentation as typeRepr ->
let diff = origTypeReprIndent - desiredIndent
if diff > 0 then shiftWithWhitespaceBefore -diff unionRepr
if diff > 0 then shiftWithWhitespaceBefore -diff typeRepr
null, null

| _ -> null, null
Expand All @@ -317,15 +320,23 @@ type FSharpOverridingMembersBuilder() =
use disableFormatter = new DisableCodeFormatter()

let typeDecl = context.Root :?> IFSharpTypeDeclaration
let typeRepr = typeDecl.TypeRepresentation

match typeRepr with
match typeDecl.TypeRepresentation with
| :? IUnionRepresentation as unionRepr ->
unionRepr.UnionCasesEnumerable
|> Seq.tryHead
|> Option.iter EnumCaseLikeDeclarationUtil.addBarIfNeeded
|> Option.iter EnumCaseLikeDeclarationUtil.addBarIfNeeded
| :? ITypeAbbreviationRepresentation as abbrRepr when abbrRepr.CanBeUnionCase ->
let factory = typeDecl.CreateElementFactory()
let caseName = FSharpNamingService.mangleNameIfNecessary abbrRepr.AbbreviatedTypeOrUnionCase.SourceName
let declGroup = factory.CreateModuleMember($"type U = | {caseName}") :?> ITypeDeclarationGroup
let typeDeclaration = declGroup.TypeDeclarations[0] :?> IFSharpTypeDeclaration
let repr = typeDeclaration.TypeRepresentation
let newRepr = typeDecl.SetTypeRepresentation(repr)
if context.Anchor == abbrRepr then context.Anchor <- newRepr
| _ -> ()

let typeRepr = typeDecl.TypeRepresentation
addNewLineBeforeReprIfNeeded typeDecl typeRepr

let anchor: ITreeNode =
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// ${KIND:Overrides}
// ${SELECT0:ToString():System.String}

type SingleCaseUnion = Case{caret}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
Provided elements:
0: ToString():System.String
1: Equals(System.Object):System.Boolean
2: GetHashCode():System.Int32
3: Finalize():System.Void

// ${KIND:Overrides}
// ${SELECT0:ToString():System.String}

type SingleCaseUnion =
| Case

override this.ToString() = {selstart}failwith "todo"{selend}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// ${KIND:Overrides}
// ${SELECT0:ToString():System.String}

type U = ``A{caret} B``
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
Provided elements:
0: ToString():System.String
1: Equals(System.Object):System.Boolean
2: GetHashCode():System.Int32
3: Finalize():System.Void

// ${KIND:Overrides}
// ${SELECT0:ToString():System.String}

type U =
| ``A B``

override this.ToString() = {selstart}failwith "todo"{selend}
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,8 @@ type FSharpGenerateOverridesTest() =
[<Test>] member x.``Repr - Union - No bar - Multiple 03``() = x.DoNamedTest()
[<Test>] member x.``Repr - Union - No bar - Single 01``() = x.DoNamedTest()
[<Test>] member x.``Repr - Union - No bar - Single 02``() = x.DoNamedTest()
[<Test>] member x.``Repr - Union - No bar - Single 03``() = x.DoNamedTest()
[<Test>] member x.``Repr - Union - No bar - Single 04``() = x.DoNamedTest()
[<Test>] member x.``Repr - Union 01``() = x.DoNamedTest()
[<Test>] member x.``Repr - Union 02``() = x.DoNamedTest()

Expand Down

0 comments on commit 60366d1

Please sign in to comment.