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

Fix overrides generation for "naked" single case unions #530

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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