From 78cf5f0ec08db4e47cbd4ad06a1f0f2e4ca1c565 Mon Sep 17 00:00:00 2001 From: Siegfried Pammer Date: Mon, 18 Feb 2019 22:54:45 +0100 Subject: [PATCH] Add support for expression bodies to IndexerDeclaration. --- .../OutputVisitor/CSharpOutputVisitor.cs | 23 +++++++++++++------ .../Syntax/TypeMembers/IndexerDeclaration.cs | 13 ++++++++--- 2 files changed, 26 insertions(+), 10 deletions(-) diff --git a/ICSharpCode.Decompiler/CSharp/OutputVisitor/CSharpOutputVisitor.cs b/ICSharpCode.Decompiler/CSharp/OutputVisitor/CSharpOutputVisitor.cs index 2d96f0afb..9992fa33e 100644 --- a/ICSharpCode.Decompiler/CSharp/OutputVisitor/CSharpOutputVisitor.cs +++ b/ICSharpCode.Decompiler/CSharp/OutputVisitor/CSharpOutputVisitor.cs @@ -2096,15 +2096,24 @@ namespace ICSharpCode.Decompiler.CSharp.OutputVisitor WriteKeyword(IndexerDeclaration.ThisKeywordRole); Space(policy.SpaceBeforeMethodDeclarationParentheses); WriteCommaSeparatedListInBrackets(indexerDeclaration.Parameters, policy.SpaceWithinMethodDeclarationParentheses); - OpenBrace(policy.PropertyBraceStyle); - // output get/set in their original order - foreach (AstNode node in indexerDeclaration.Children) { - if (node.Role == IndexerDeclaration.GetterRole || node.Role == IndexerDeclaration.SetterRole) { - node.AcceptVisitor(this); + + if (indexerDeclaration.ExpressionBody.IsNull) { + OpenBrace(policy.PropertyBraceStyle); + // output get/set in their original order + foreach (AstNode node in indexerDeclaration.Children) { + if (node.Role == IndexerDeclaration.GetterRole || node.Role == IndexerDeclaration.SetterRole) { + node.AcceptVisitor(this); + } } + CloseBrace(policy.PropertyBraceStyle); + NewLine(); + } else { + Space(); + WriteToken(Roles.Arrow); + Space(); + indexerDeclaration.ExpressionBody.AcceptVisitor(this); + Semicolon(); } - CloseBrace(policy.PropertyBraceStyle); - NewLine(); EndNode(indexerDeclaration); } diff --git a/ICSharpCode.Decompiler/CSharp/Syntax/TypeMembers/IndexerDeclaration.cs b/ICSharpCode.Decompiler/CSharp/Syntax/TypeMembers/IndexerDeclaration.cs index e5ce84ba1..e4493a836 100644 --- a/ICSharpCode.Decompiler/CSharp/Syntax/TypeMembers/IndexerDeclaration.cs +++ b/ICSharpCode.Decompiler/CSharp/Syntax/TypeMembers/IndexerDeclaration.cs @@ -35,7 +35,8 @@ namespace ICSharpCode.Decompiler.CSharp.Syntax public static readonly TokenRole ThisKeywordRole = new TokenRole ("this"); public static readonly Role GetterRole = PropertyDeclaration.GetterRole; public static readonly Role SetterRole = PropertyDeclaration.SetterRole; - + public static readonly Role ExpressionBodyRole = new Role("ExpressionBody", Expression.Null); + public override SymbolKind SymbolKind { get { return SymbolKind.Indexer; } } @@ -93,7 +94,12 @@ namespace ICSharpCode.Decompiler.CSharp.Syntax public CSharpTokenNode RBraceToken { get { return GetChildByRole (Roles.RBrace); } } - + + public Expression ExpressionBody { + get { return GetChildByRole(ExpressionBodyRole); } + set { SetChildByRole(ExpressionBodyRole, value); } + } + public override void AcceptVisitor (IAstVisitor visitor) { visitor.VisitIndexerDeclaration (this); @@ -116,7 +122,8 @@ namespace ICSharpCode.Decompiler.CSharp.Syntax && this.MatchAttributesAndModifiers(o, match) && this.ReturnType.DoMatch(o.ReturnType, match) && this.PrivateImplementationType.DoMatch(o.PrivateImplementationType, match) && this.Parameters.DoMatch(o.Parameters, match) - && this.Getter.DoMatch(o.Getter, match) && this.Setter.DoMatch(o.Setter, match); + && this.Getter.DoMatch(o.Getter, match) && this.Setter.DoMatch(o.Setter, match) + && this.ExpressionBody.DoMatch(o.ExpressionBody, match); } } }