Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
lwm1986
roslyn
提交
60a6e4cc
R
roslyn
项目概览
lwm1986
/
roslyn
与 Fork 源项目一致
从无法访问的项目Fork
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
R
roslyn
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
60a6e4cc
编写于
9月 13, 2017
作者:
C
chborl
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Code Cleanup
上级
316327ef
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
36 addition
and
55 deletion
+36
-55
src/Features/CSharp/Portable/ConvertAutoPropertyToFullProperty/CSharpConvertAutoPropertyToFullPropertyCodeRefactoringProvider.cs
...nvertAutoPropertyToFullPropertyCodeRefactoringProvider.cs
+10
-13
src/Features/Core/Portable/ConvertAutoPropertyToFullProperty/AbstractConvertAutoPropertyToFullPropertyCodeRefactoringProvider.cs
...nvertAutoPropertyToFullPropertyCodeRefactoringProvider.cs
+11
-27
src/Features/VisualBasic/Portable/ConvertAutoPropertyToFullProperty/VisualBasicConvertAutoPropertyToFullProperty.vb
...lProperty/VisualBasicConvertAutoPropertyToFullProperty.vb
+15
-15
未找到文件。
src/Features/CSharp/Portable/ConvertAutoPropertyToFullProperty/CSharpConvertAutoPropertyToFullPropertyCodeRefactoringProvider.cs
浏览文件 @
60a6e4cc
...
...
@@ -47,6 +47,12 @@ internal override SyntaxNode GetProperty(SyntaxToken token)
return
containingProperty
;
}
internal
override
async
Task
<
string
>
GetFieldNameAsync
(
Document
document
,
IPropertySymbol
propertySymbol
,
CancellationToken
cancellationToken
)
{
var
rules
=
await
GetNamingRulesAsync
(
document
,
cancellationToken
).
ConfigureAwait
(
false
);
return
GenerateFieldName
(
propertySymbol
,
rules
);
}
/// <summary>
/// Get the user-specified naming rules, then add standard default naming rules
/// for both static and non-static fields. The standard naming rules are added at the end
...
...
@@ -62,12 +68,12 @@ internal override SyntaxNode GetProperty(SyntaxToken token)
var
optionSet
=
await
document
.
GetOptionsAsync
(
cancellationToken
).
ConfigureAwait
(
false
);
var
namingPreferencesOption
=
optionSet
.
GetOption
(
SimplificationOptions
.
NamingPreferences
);
var
rules
=
namingPreferencesOption
.
CreateRules
().
NamingRules
.
AddRange
(
GetDefault
Rule
(
ImmutableArray
.
Create
(
new
ModifierKind
(
ModifierKindEnum
.
IsStatic
)),
defaultStaticFieldPrefix
))
.
AddRange
(
GetDefault
Rule
(
ImmutableArray
.
Create
<
ModifierKind
>(),
defaultFieldPrefix
));
.
AddRange
(
CreateNew
Rule
(
ImmutableArray
.
Create
(
new
ModifierKind
(
ModifierKindEnum
.
IsStatic
)),
defaultStaticFieldPrefix
))
.
AddRange
(
CreateNew
Rule
(
ImmutableArray
.
Create
<
ModifierKind
>(),
defaultFieldPrefix
));
return
rules
;
}
private
static
ImmutableArray
<
NamingRule
>
GetDefault
Rule
(
private
static
ImmutableArray
<
NamingRule
>
CreateNew
Rule
(
ImmutableArray
<
ModifierKind
>
modifiers
,
string
prefix
)
{
...
...
@@ -101,7 +107,7 @@ private string GenerateFieldName(IPropertySymbol property, ImmutableArray<Naming
}
}
return
GetUniqueName
(
fieldName
,
property
);
return
NameGenerator
.
GenerateUniqueName
(
fieldName
,
n
=>
!
property
.
ContainingType
.
GetMembers
(
n
).
Any
()
);
}
internal
override
(
SyntaxNode
newGetAccessor
,
SyntaxNode
newSetAccessor
)
GetNewAccessors
(
...
...
@@ -207,8 +213,6 @@ internal ExpressionBodyPreference GetAccessorExpressionBodyPreference(DocumentOp
internal
ExpressionBodyPreference
GetPropertyExpressionBodyPreference
(
DocumentOptionSet
options
)
=>
options
.
GetOption
(
CSharpCodeStyleOptions
.
PreferExpressionBodiedProperties
).
Value
;
internal
string
GetUniqueName
(
string
fieldName
,
IPropertySymbol
property
)
=>
NameGenerator
.
GenerateUniqueName
(
fieldName
,
n
=>
!
property
.
ContainingType
.
GetMembers
(
n
).
Any
());
internal
override
SyntaxNode
GetTypeBlock
(
SyntaxNode
syntaxNode
)
=>
syntaxNode
;
...
...
@@ -218,12 +222,5 @@ internal override SyntaxNode GetInitializerValue(SyntaxNode property)
internal
override
SyntaxNode
GetPropertyWithoutInitializer
(
SyntaxNode
property
)
=>
((
PropertyDeclarationSyntax
)
property
).
WithInitializer
(
null
);
internal
override
async
Task
<
string
>
GetFieldNameAsync
(
Document
document
,
IPropertySymbol
propertySymbol
,
CancellationToken
cancellationToken
)
{
var
rules
=
await
GetNamingRulesAsync
(
document
,
cancellationToken
).
ConfigureAwait
(
false
);
var
fieldName
=
GenerateFieldName
(
propertySymbol
,
rules
);
return
fieldName
;
}
}
}
src/Features/Core/Portable/ConvertAutoPropertyToFullProperty/AbstractConvertAutoPropertyToFullPropertyCodeRefactoringProvider.cs
浏览文件 @
60a6e4cc
...
...
@@ -18,12 +18,12 @@ internal abstract class AbstractConvertAutoPropertyToFullPropertyCodeRefactoring
{
internal
abstract
SyntaxNode
GetProperty
(
SyntaxToken
token
);
internal
abstract
Task
<
string
>
GetFieldNameAsync
(
Document
document
,
IPropertySymbol
propertySymbol
,
CancellationToken
cancellationToken
);
internal
abstract
SyntaxNode
GetInitializerValue
(
SyntaxNode
property
);
internal
abstract
SyntaxNode
GetPropertyWithoutInitializer
(
SyntaxNode
property
);
internal
abstract
(
SyntaxNode
newGetAccessor
,
SyntaxNode
newSetAccessor
)
GetNewAccessors
(
DocumentOptionSet
options
,
SyntaxNode
property
,
string
fieldName
,
SyntaxGenerator
generator
);
internal
abstract
SyntaxNode
GetTypeBlock
(
SyntaxNode
syntaxNode
);
internal
abstract
SyntaxNode
GetPropertyWithoutInitializer
(
SyntaxNode
property
);
internal
abstract
SyntaxNode
GetInitializerValue
(
SyntaxNode
property
);
internal
abstract
SyntaxNode
ConvertPropertyToExpressionBodyIfDesired
(
DocumentOptionSet
options
,
SyntaxNode
fullProperty
);
internal
abstract
SyntaxNode
GetTypeBlock
(
SyntaxNode
syntaxNode
);
public
override
async
Task
ComputeRefactoringsAsync
(
CodeRefactoringContext
context
)
{
...
...
@@ -72,29 +72,13 @@ internal bool IsValidAutoProperty(SyntaxNode property, IPropertySymbol propertyS
CancellationToken
cancellationToken
)
{
var
generator
=
SyntaxGenerator
.
GetGenerator
(
document
);
var
newRoot
=
await
ExpandPropertyAndAddFieldAsync
(
document
,
property
,
root
,
propertySymbol
,
generator
,
cancellationToken
).
ConfigureAwait
(
false
);
return
document
.
WithSyntaxRoot
(
newRoot
);
}
private
async
Task
<
SyntaxNode
>
ExpandPropertyAndAddFieldAsync
(
Document
document
,
SyntaxNode
property
,
SyntaxNode
root
,
IPropertySymbol
propertySymbol
,
SyntaxGenerator
generator
,
CancellationToken
cancellationToken
)
{
var
workspace
=
document
.
Project
.
Solution
.
Workspace
;
var
options
=
await
document
.
GetOptionsAsync
(
cancellationToken
).
ConfigureAwait
(
false
);
// Create full property. If the auto property had an initial value
// we need to remove it and later add it to the backing field
var
fieldName
=
await
GetFieldNameAsync
(
document
,
propertySymbol
,
cancellationToken
).
ConfigureAwait
(
false
);
var
accessorTuple
=
GetNewAccessors
(
options
,
property
,
fieldName
,
generator
);
var
accessorTuple
=
GetNewAccessors
(
options
,
property
,
fieldName
,
generator
);
var
fullProperty
=
generator
.
WithAccessorDeclarations
(
GetPropertyWithoutInitializer
(
property
),
...
...
@@ -102,23 +86,23 @@ internal bool IsValidAutoProperty(SyntaxNode property, IPropertySymbol propertyS
?
new
SyntaxNode
[]
{
accessorTuple
.
newGetAccessor
}
:
new
SyntaxNode
[]
{
accessorTuple
.
newGetAccessor
,
accessorTuple
.
newSetAccessor
})
.
WithLeadingTrivia
(
property
.
GetLeadingTrivia
());
fullProperty
=
ConvertPropertyToExpressionBodyIfDesired
(
options
,
fullProperty
);
fullProperty
=
ConvertPropertyToExpressionBodyIfDesired
(
options
,
fullProperty
);
var
editor
=
new
SyntaxEditor
(
root
,
workspace
);
editor
.
ReplaceNode
(
property
,
fullProperty
.
WithAdditionalAnnotations
(
Formatter
.
Annotation
));
// add backing field, plus initializer if it exists
var
newField
=
CodeGenerationSymbolFactory
.
CreateFieldSymbol
(
default
,
Accessibility
.
Private
,
DeclarationModifiers
.
From
(
propertySymbol
),
propertySymbol
.
Type
,
fieldName
,
default
,
Accessibility
.
Private
,
DeclarationModifiers
.
From
(
propertySymbol
),
propertySymbol
.
Type
,
fieldName
,
initializer
:
GetInitializerValue
(
property
));
var
containingType
=
GetTypeBlock
(
propertySymbol
.
ContainingType
.
DeclaringSyntaxReferences
.
FirstOrDefault
().
GetSyntax
(
cancellationToken
));
editor
.
ReplaceNode
(
containingType
,
(
currentTypeDecl
,
_
)
editor
.
ReplaceNode
(
containingType
,
(
currentTypeDecl
,
_
)
=>
CodeGenerator
.
AddFieldDeclaration
(
currentTypeDecl
,
newField
,
workspace
)
.
WithAdditionalAnnotations
(
Formatter
.
Annotation
));
return
editor
.
GetChangedRoot
(
);
var
newRoot
=
editor
.
GetChangedRoot
();
return
document
.
WithSyntaxRoot
(
newRoot
);
}
private
class
ConvertAutoPropertyToFullPropertyCodeAction
:
CodeAction
.
DocumentChangeAction
...
...
src/Features/VisualBasic/Portable/ConvertAutoPropertyToFullProperty/VisualBasicConvertAutoPropertyToFullProperty.vb
浏览文件 @
60a6e4cc
...
...
@@ -39,6 +39,15 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.ConvertAutoPropertyToFullProperty
Return
containingProperty
End
Function
''' <summary>
''' In VB, auto properties have an implicit backing field that is named using the property
''' name preceded by an underscore. We will use this as the field name so we don't mess up
''' any existing references to this field.
''' </summary>
Friend
Overrides
Function
GetFieldNameAsync
(
document
As
Document
,
propertySymbol
As
IPropertySymbol
,
cancellationToken
As
CancellationToken
)
As
Task
(
Of
String
)
Return
Task
.
FromResult
(
Underscore
+
propertySymbol
.
Name
)
End
Function
Friend
Overrides
Function
GetNewAccessors
(
options
As
DocumentOptionSet
,
propertyNode
As
SyntaxNode
,
fieldName
As
String
,
generator
As
SyntaxGenerator
)
_
As
(
newGetAccessor
As
SyntaxNode
,
newSetAccessor
As
SyntaxNode
)
...
...
@@ -77,29 +86,20 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.ConvertAutoPropertyToFullProperty
Return
False
End
Function
''' <summary>
''' In VB, auto properties have an implicit backing field that is named using the property
''' name preceded by an underscore. We will use this as the field name so we don't mess up
''' any existing references to this field.
''' </summary>
Friend
Overrides
Function
GetFieldNameAsync
(
document
As
Document
,
propertySymbol
As
IPropertySymbol
,
cancellationToken
As
CancellationToken
)
As
Task
(
Of
String
)
Return
Task
.
FromResult
(
Underscore
+
propertySymbol
.
Name
)
End
Function
Friend
Overrides
Function
GetTypeBlock
(
syntaxNode
As
SyntaxNode
)
As
SyntaxNode
Return
DirectCast
(
syntaxNode
,
TypeStatementSyntax
).
Parent
Friend
Overrides
Function
GetPropertyWithoutInitializer
(
propertyNode
As
SyntaxNode
)
As
SyntaxNode
Return
DirectCast
(
propertyNode
,
PropertyStatementSyntax
).
WithInitializer
(
Nothing
)
End
Function
Friend
Overrides
Function
GetInitializerValue
(
propertyNode
As
SyntaxNode
)
As
SyntaxNode
Return
DirectCast
(
propertyNode
,
PropertyStatementSyntax
).
Initializer
?
.
Value
End
Function
Friend
Overrides
Function
GetPropertyWithoutInitializer
(
propertyNode
As
SyntaxNode
)
As
SyntaxNode
Return
DirectCast
(
propertyNode
,
PropertyStatementSyntax
).
WithInitializer
(
Nothing
)
End
Function
Friend
Overrides
Function
ConvertPropertyToExpressionBodyIfDesired
(
options
As
DocumentOptionSet
,
propertyNode
As
SyntaxNode
)
As
SyntaxNode
Return
propertyNode
End
Function
Friend
Overrides
Function
GetTypeBlock
(
syntaxNode
As
SyntaxNode
)
As
SyntaxNode
Return
DirectCast
(
syntaxNode
,
TypeStatementSyntax
).
Parent
End
Function
End
Class
End
Namespace
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录