提交 01a30990 编写于 作者: C CyrusNajmabadi

Use expression bodied constructors if desired.

上级 495658b9
...@@ -143,7 +143,8 @@ protected override TDeclarationNode AddMethod<TDeclarationNode>(TDeclarationNode ...@@ -143,7 +143,8 @@ protected override TDeclarationNode AddMethod<TDeclarationNode>(TDeclarationNode
{ {
if (method.IsConstructor()) if (method.IsConstructor())
{ {
return Cast<TDeclarationNode>(ConstructorGenerator.AddConstructorTo(typeDeclaration, method, options, availableIndices)); return Cast<TDeclarationNode>(ConstructorGenerator.AddConstructorTo(
typeDeclaration, method, Workspace, options, availableIndices));
} }
if (method.IsDestructor()) if (method.IsDestructor())
...@@ -616,7 +617,8 @@ public override SyntaxNode CreateFieldDeclaration(IFieldSymbol field, CodeGenera ...@@ -616,7 +617,8 @@ public override SyntaxNode CreateFieldDeclaration(IFieldSymbol field, CodeGenera
if (method.IsConstructor()) if (method.IsConstructor())
{ {
return ConstructorGenerator.GenerateConstructorDeclaration(method, destination, options); return ConstructorGenerator.GenerateConstructorDeclaration(
method, destination, Workspace, options);
} }
else if (method.IsDestructor()) else if (method.IsDestructor())
{ {
......
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. // Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using Microsoft.CodeAnalysis.CodeGeneration; using Microsoft.CodeAnalysis.CodeGeneration;
using Microsoft.CodeAnalysis.CSharp.CodeStyle;
using Microsoft.CodeAnalysis.CSharp.Extensions; using Microsoft.CodeAnalysis.CSharp.Extensions;
using Microsoft.CodeAnalysis.CSharp.Syntax; using Microsoft.CodeAnalysis.CSharp.Syntax;
...@@ -21,10 +23,12 @@ private static MemberDeclarationSyntax LastConstructorOrField(SyntaxList<MemberD ...@@ -21,10 +23,12 @@ private static MemberDeclarationSyntax LastConstructorOrField(SyntaxList<MemberD
internal static TypeDeclarationSyntax AddConstructorTo( internal static TypeDeclarationSyntax AddConstructorTo(
TypeDeclarationSyntax destination, TypeDeclarationSyntax destination,
IMethodSymbol constructor, IMethodSymbol constructor,
Workspace workspace,
CodeGenerationOptions options, CodeGenerationOptions options,
IList<bool> availableIndices) IList<bool> availableIndices)
{ {
var constructorDeclaration = GenerateConstructorDeclaration(constructor, GetDestination(destination), options); var constructorDeclaration = GenerateConstructorDeclaration(
constructor, GetDestination(destination), workspace, options);
// Generate after the last constructor, or after the last field, or at the start of the // Generate after the last constructor, or after the last field, or at the start of the
// type. // type.
...@@ -35,7 +39,8 @@ private static MemberDeclarationSyntax LastConstructorOrField(SyntaxList<MemberD ...@@ -35,7 +39,8 @@ private static MemberDeclarationSyntax LastConstructorOrField(SyntaxList<MemberD
} }
internal static ConstructorDeclarationSyntax GenerateConstructorDeclaration( internal static ConstructorDeclarationSyntax GenerateConstructorDeclaration(
IMethodSymbol constructor, CodeGenerationDestination destination, CodeGenerationOptions options) IMethodSymbol constructor, CodeGenerationDestination destination,
Workspace workspace, CodeGenerationOptions options)
{ {
options = options ?? CodeGenerationOptions.Default; options = options ?? CodeGenerationOptions.Default;
...@@ -56,10 +61,33 @@ private static MemberDeclarationSyntax LastConstructorOrField(SyntaxList<MemberD ...@@ -56,10 +61,33 @@ private static MemberDeclarationSyntax LastConstructorOrField(SyntaxList<MemberD
body: hasNoBody ? null : GenerateBlock(constructor), body: hasNoBody ? null : GenerateBlock(constructor),
semicolonToken: hasNoBody ? SyntaxFactory.Token(SyntaxKind.SemicolonToken) : default(SyntaxToken)); semicolonToken: hasNoBody ? SyntaxFactory.Token(SyntaxKind.SemicolonToken) : default(SyntaxToken));
declaration = UseExpressionBodyIfDesired(workspace, declaration);
return AddCleanupAnnotationsTo( return AddCleanupAnnotationsTo(
ConditionallyAddDocumentationCommentTo(declaration, constructor, options)); ConditionallyAddDocumentationCommentTo(declaration, constructor, options));
} }
private static ConstructorDeclarationSyntax UseExpressionBodyIfDesired(
Workspace workspace, ConstructorDeclarationSyntax declaration)
{
if (declaration.ExpressionBody == null)
{
var preferExpressionBody = workspace.Options.GetOption(CSharpCodeStyleOptions.PreferExpressionBodiedConstructors).Value;
if (preferExpressionBody)
{
var expressionBody = CodeGenerationHelpers.TryConvertToExpressionBody(declaration.Body);
if (expressionBody != null)
{
return declaration.WithBody(null)
.WithExpressionBody(expressionBody)
.WithSemicolonToken(SyntaxFactory.Token(SyntaxKind.SemicolonToken));
}
}
}
return declaration;
}
private static ConstructorInitializerSyntax GenerateConstructorInitializer( private static ConstructorInitializerSyntax GenerateConstructorInitializer(
IMethodSymbol constructor) IMethodSymbol constructor)
{ {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册