Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
lwm1986
roslyn
提交
6bb8d9f5
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,发现更多精彩内容 >>
提交
6bb8d9f5
编写于
9月 10, 2015
作者:
C
Cyrus Najmabadi
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
PR feedback. Also handle fields with initializers.
上级
ab9cc113
变更
8
隐藏空白更改
内联
并排
Showing
8 changed file
with
91 addition
and
12 deletion
+91
-12
src/EditorFeatures/CSharp/UseAutoProperty/UseAutoPropertyAnalyzer.cs
...eatures/CSharp/UseAutoProperty/UseAutoPropertyAnalyzer.cs
+10
-0
src/EditorFeatures/CSharp/UseAutoProperty/UseAutoPropertyCodeFixProvider.cs
.../CSharp/UseAutoProperty/UseAutoPropertyCodeFixProvider.cs
+17
-2
src/EditorFeatures/CSharpTest/Diagnostics/UseAutoProperty/UseAutoPropertyTests.cs
...pTest/Diagnostics/UseAutoProperty/UseAutoPropertyTests.cs
+16
-0
src/EditorFeatures/VisualBasic/UseAutoProperty/UseAutoPropertyAnalyzer.vb
...es/VisualBasic/UseAutoProperty/UseAutoPropertyAnalyzer.vb
+9
-0
src/EditorFeatures/VisualBasic/UseAutoProperty/UseAutoPropertyCodeFixProvider.vb
...alBasic/UseAutoProperty/UseAutoPropertyCodeFixProvider.vb
+20
-7
src/EditorFeatures/VisualBasicTest/Diagnostics/UseAutoProperty/UseAutoPropertyTests.vb
...cTest/Diagnostics/UseAutoProperty/UseAutoPropertyTests.vb
+7
-0
src/Features/Core/Portable/UseAutoProperty/AbstractUseAutoPropertyAnalyzer.cs
...rtable/UseAutoProperty/AbstractUseAutoPropertyAnalyzer.cs
+8
-0
src/Features/Core/Portable/UseAutoProperty/AbstractUseAutoPropertyCodeFixProvider.cs
...UseAutoProperty/AbstractUseAutoPropertyCodeFixProvider.cs
+4
-3
未找到文件。
src/EditorFeatures/CSharp/UseAutoProperty/UseAutoPropertyAnalyzer.cs
浏览文件 @
6bb8d9f5
...
...
@@ -19,11 +19,21 @@ protected override bool SupportsReadOnlyProperties(Compilation compilation)
return
((
CSharpCompilation
)
compilation
).
LanguageVersion
>=
LanguageVersion
.
CSharp6
;
}
protected
override
bool
SupportsPropertyInitializer
(
Compilation
compilation
)
{
return
((
CSharpCompilation
)
compilation
).
LanguageVersion
>=
LanguageVersion
.
CSharp6
;
}
protected
override
void
RegisterIneligibleFieldsAction
(
CompilationStartAnalysisContext
context
,
ConcurrentBag
<
IFieldSymbol
>
ineligibleFields
)
{
context
.
RegisterSyntaxNodeAction
(
snac
=>
AnalyzeArgument
(
ineligibleFields
,
snac
),
SyntaxKind
.
Argument
);
}
protected
override
ExpressionSyntax
GetFieldInitializer
(
VariableDeclaratorSyntax
variable
,
CancellationToken
cancellationToken
)
{
return
variable
.
Initializer
?.
Value
;
}
private
void
AnalyzeArgument
(
ConcurrentBag
<
IFieldSymbol
>
ineligibleFields
,
SyntaxNodeAnalysisContext
context
)
{
// An argument will disqualify a field if that field is used in a ref/out position.
...
...
src/EditorFeatures/CSharp/UseAutoProperty/UseAutoPropertyCodeFixProvider.cs
浏览文件 @
6bb8d9f5
using
System.Collections.Generic
;
using
System
;
using
System.Collections.Generic
;
using
System.Collections.Immutable
;
using
System.Composition
;
using
System.Linq
;
using
System.Threading
;
using
System.Threading.Tasks
;
using
Microsoft.CodeAnalysis.CodeFixes
;
using
Microsoft.CodeAnalysis.CSharp
;
using
Microsoft.CodeAnalysis.CSharp.Syntax
;
...
...
@@ -24,7 +26,7 @@ protected override SyntaxNode GetNodeToRemove(VariableDeclaratorSyntax declarato
return
nodeToRemove
;
}
protected
override
SyntaxNode
UpdateProperty
(
protected
override
async
Task
<
SyntaxNode
>
UpdatePropertyAsync
(
Project
project
,
Compilation
compilation
,
IFieldSymbol
fieldSymbol
,
IPropertySymbol
propertySymbol
,
PropertyDeclarationSyntax
propertyDeclaration
,
bool
isWrittenOutsideOfConstructor
,
CancellationToken
cancellationToken
)
{
...
...
@@ -46,9 +48,22 @@ protected override SyntaxNode GetNodeToRemove(VariableDeclaratorSyntax declarato
updatedProperty
=
updatedProperty
.
AddAccessorListAccessors
(
accessor
);
}
var
fieldInitializer
=
await
GetFieldInitializerAsync
(
fieldSymbol
,
cancellationToken
).
ConfigureAwait
(
false
);
if
(
fieldInitializer
!=
null
)
{
updatedProperty
=
updatedProperty
.
WithInitializer
(
SyntaxFactory
.
EqualsValueClause
(
fieldInitializer
))
.
WithSemicolonToken
(
SyntaxFactory
.
Token
(
SyntaxKind
.
SemicolonToken
));
}
return
updatedProperty
;
}
private
async
Task
<
ExpressionSyntax
>
GetFieldInitializerAsync
(
IFieldSymbol
fieldSymbol
,
CancellationToken
cancellationToken
)
{
var
variableDeclarator
=
(
VariableDeclaratorSyntax
)
await
fieldSymbol
.
DeclaringSyntaxReferences
[
0
].
GetSyntaxAsync
(
cancellationToken
).
ConfigureAwait
(
false
);
return
variableDeclarator
.
Initializer
?.
Value
;
}
private
bool
NeedsSetter
(
Compilation
compilation
,
PropertyDeclarationSyntax
propertyDeclaration
,
bool
isWrittenOutsideOfConstructor
)
{
if
(
propertyDeclaration
.
AccessorList
.
Accessors
.
Any
(
SyntaxKind
.
SetAccessorDeclaration
))
...
...
src/EditorFeatures/CSharpTest/Diagnostics/UseAutoProperty/UseAutoPropertyTests.cs
浏览文件 @
6bb8d9f5
...
...
@@ -41,6 +41,22 @@ public void TestCSharp5_2()
CSharpParseOptions
.
Default
.
WithLanguageVersion
(
LanguageVersion
.
CSharp5
));
}
[
Fact
,
Trait
(
Traits
.
Feature
,
Traits
.
Features
.
CodeActionsUseAutoProperty
)]
public
void
TestInitializer
()
{
Test
(
@"class Class { [|int i = 1|]; int P { get { return i; } } }"
,
@"class Class { int P { get; } = 1; }"
);
}
[
Fact
,
Trait
(
Traits
.
Feature
,
Traits
.
Features
.
CodeActionsUseAutoProperty
)]
public
void
TestInitializer_CSharp5
()
{
TestMissing
(
@"class Class { [|int i = 1|]; int P { get { return i; } } }"
,
CSharpParseOptions
.
Default
.
WithLanguageVersion
(
LanguageVersion
.
CSharp5
));
}
[
Fact
,
Trait
(
Traits
.
Feature
,
Traits
.
Features
.
CodeActionsUseAutoProperty
)]
public
void
TestSingleGetter2
()
{
...
...
src/EditorFeatures/VisualBasic/UseAutoProperty/UseAutoPropertyAnalyzer.vb
浏览文件 @
6bb8d9f5
...
...
@@ -17,9 +17,18 @@ Namespace Microsoft.CodeAnalysis.Editor.VisualBasic.UseAutoProperty
Return
True
End
Function
Protected
Overrides
Function
SupportsPropertyInitializer
(
compilation
As
Compilation
)
As
Boolean
Return
True
End
Function
Protected
Overrides
Sub
RegisterIneligibleFieldsAction
(
context
As
CompilationStartAnalysisContext
,
ineligibleFields
As
ConcurrentBag
(
Of
IFieldSymbol
))
End
Sub
Protected
Overrides
Function
GetFieldInitializer
(
variable
As
ModifiedIdentifierSyntax
,
cancellationToken
As
CancellationToken
)
As
ExpressionSyntax
Dim
declarator
=
TryCast
(
variable
.
Parent
,
VariableDeclaratorSyntax
)
Return
declarator
?
.
Initializer
?
.
Value
End
Function
Private
Function
CheckExpressionSyntactically
(
expression
As
ExpressionSyntax
)
As
Boolean
If
expression
?
.
Kind
()
=
SyntaxKind
.
SimpleMemberAccessExpression
Then
Dim
memberAccessExpression
=
DirectCast
(
expression
,
MemberAccessExpressionSyntax
)
...
...
src/EditorFeatures/VisualBasic/UseAutoProperty/UseAutoPropertyCodeFixProvider.vb
浏览文件 @
6bb8d9f5
Imports
System.Composition
Imports
System.Threading
Imports
System.Threading.Tasks
Imports
Microsoft.CodeAnalysis.CodeFixes
Imports
Microsoft.CodeAnalysis.Editing
Imports
Microsoft.CodeAnalysis.UseAutoProperty
...
...
@@ -15,19 +16,31 @@ Namespace Microsoft.CodeAnalysis.Editor.VisualBasic.UseAutoProperty
Return
Utilities
.
GetNodeToRemove
(
identifier
)
End
Function
Protected
Overrides
Function
UpdateProperty
(
project
As
Project
,
compilation
As
Compilation
,
fieldSymbol
As
IFieldSymbol
,
propertySymbol
As
IPropertySymbol
,
propertyDeclaration
As
PropertyBlockSyntax
,
isWrittenToOutsideOfConstructor
As
Boolean
,
cancellationToken
As
CancellationToken
)
As
SyntaxNode
Protected
Overrides
Async
Function
UpdatePropertyAsync
(
project
As
Project
,
compilation
As
Compilation
,
fieldSymbol
As
IFieldSymbol
,
propertySymbol
As
IPropertySymbol
,
propertyDeclaration
As
PropertyBlockSyntax
,
isWrittenToOutsideOfConstructor
As
Boolean
,
cancellationToken
As
CancellationToken
)
As
Task
(
Of
SyntaxNode
)
Dim
statement
=
propertyDeclaration
.
PropertyStatement
If
Not
isWrittenToOutsideOfConstructor
AndAlso
Not
propertyDeclaration
.
Accessors
.
Any
(
SyntaxKind
.
SetAccessorBlock
)
Then
Dim
generator
=
SyntaxGenerator
.
GetGenerator
(
project
)
statement
=
DirectCast
(
generator
.
WithModifiers
(
statement
,
DeclarationModifiers
.
ReadOnly
),
PropertyStatementSyntax
)
End
If
Dim
initializer
=
Await
GetFieldInitializer
(
fieldSymbol
,
cancellationToken
).
ConfigureAwait
(
False
)
If
initializer
IsNot
Nothing
Then
statement
=
statement
.
WithInitializer
(
SyntaxFactory
.
EqualsValue
(
initializer
))
End
If
Return
statement
End
Function
Private
Async
Function
GetFieldInitializer
(
fieldSymbol
As
IFieldSymbol
,
cancellationToken
As
CancellationToken
)
As
Task
(
Of
ExpressionSyntax
)
Dim
identifier
=
TryCast
(
Await
fieldSymbol
.
DeclaringSyntaxReferences
(
0
).
GetSyntaxAsync
(
cancellationToken
).
ConfigureAwait
(
False
),
ModifiedIdentifierSyntax
)
Dim
declarator
=
TryCast
(
identifier
?
.
Parent
,
VariableDeclaratorSyntax
)
Return
declarator
?
.
Initializer
?
.
Value
End
Function
End
Class
End
Namespace
\ No newline at end of file
src/EditorFeatures/VisualBasicTest/Diagnostics/UseAutoProperty/UseAutoPropertyTests.vb
浏览文件 @
6bb8d9f5
...
...
@@ -38,6 +38,13 @@ NewLines("class Class1 \n [|dim i as integer|] \n property P as Integer \n get \
NewLines
(
"class Class1 \n property P as Integer \n end class"
))
End
Sub
<
Fact
,
Trait
(
Traits
.
Feature
,
Traits
.
Features
.
CodeActionsUseAutoProperty
)
>
Public
Sub
TestInitializer
()
Test
(
NewLines
(
"class Class1 \n dim i as Integer = 1 \n [|readonly property P as integer \n get \n return i \n end get \n end property|] \n end class"
),
NewLines
(
"class Class1 \n readonly property P as integer = 1 \n end class"
))
End
Sub
<
Fact
,
Trait
(
Traits
.
Feature
,
Traits
.
Features
.
CodeActionsUseAutoProperty
)
>
Public
Sub
TestDifferentValueName
()
Test
(
...
...
src/Features/Core/Portable/UseAutoProperty/AbstractUseAutoPropertyAnalyzer.cs
浏览文件 @
6bb8d9f5
...
...
@@ -29,6 +29,8 @@ internal abstract class AbstractUseAutoPropertyAnalyzer<TPropertyDeclaration, TF
protected
abstract
void
RegisterIneligibleFieldsAction
(
CompilationStartAnalysisContext
context
,
ConcurrentBag
<
IFieldSymbol
>
ineligibleFields
);
protected
abstract
bool
SupportsReadOnlyProperties
(
Compilation
compilation
);
protected
abstract
bool
SupportsPropertyInitializer
(
Compilation
compilation
);
protected
abstract
TExpression
GetFieldInitializer
(
TVariableDeclarator
variable
,
CancellationToken
cancellationToken
);
protected
abstract
TExpression
GetGetterExpression
(
IMethodSymbol
getMethod
,
CancellationToken
cancellationToken
);
protected
abstract
TExpression
GetSetterExpression
(
IMethodSymbol
setMethod
,
SemanticModel
semanticModel
,
CancellationToken
cancellationToken
);
protected
abstract
SyntaxNode
GetNodeToFade
(
TFieldDeclaration
fieldDeclaration
,
TVariableDeclarator
variableDeclarator
);
...
...
@@ -160,6 +162,12 @@ private void AnalyzeProperty(ConcurrentBag<AnalysisResult> analysisResults, Symb
return
;
}
var
initializer
=
GetFieldInitializer
(
variableDeclarator
,
cancellationToken
);
if
(
initializer
!=
null
&&
!
SupportsPropertyInitializer
(
symbolContext
.
Compilation
))
{
return
;
}
var
fieldDeclaration
=
variableDeclarator
?.
Parent
?.
Parent
as
TFieldDeclaration
;
if
(
fieldDeclaration
==
null
)
{
...
...
src/Features/Core/Portable/UseAutoProperty/AbstractUseAutoPropertyCodeFixProvider.cs
浏览文件 @
6bb8d9f5
...
...
@@ -29,7 +29,7 @@ internal abstract class AbstractUseAutoPropertyCodeFixProvider<TPropertyDeclarat
protected
abstract
SyntaxNode
GetNodeToRemove
(
TVariableDeclarator
declarator
);
protected
abstract
SyntaxNode
UpdateProperty
(
protected
abstract
Task
<
SyntaxNode
>
UpdatePropertyAsync
(
Project
project
,
Compilation
compilation
,
IFieldSymbol
fieldSymbol
,
IPropertySymbol
propertySymbol
,
TPropertyDeclaration
propertyDeclaration
,
bool
isWrittenOutsideConstructor
,
CancellationToken
cancellationToken
);
...
...
@@ -74,8 +74,9 @@ private async Task<Solution> ProcessResult(CodeFixContext context, Diagnostic di
var
fieldLocations
=
await
Renamer
.
GetRenameLocationsAsync
(
solution
,
fieldSymbol
,
solution
.
Workspace
.
Options
,
cancellationToken
).
ConfigureAwait
(
false
);
// First, create the updated property we want to replace the old property with
var
updatedProperty
=
UpdateProperty
(
project
,
compilation
,
fieldSymbol
,
propertySymbol
,
property
,
IsWrittenToOutsideOfConstructorOrProperty
(
fieldSymbol
,
fieldLocations
,
property
,
cancellationToken
),
cancellationToken
);
var
isWrittenToOutsideOfConstructor
=
IsWrittenToOutsideOfConstructorOrProperty
(
fieldSymbol
,
fieldLocations
,
property
,
cancellationToken
);
var
updatedProperty
=
await
UpdatePropertyAsync
(
project
,
compilation
,
fieldSymbol
,
propertySymbol
,
property
,
isWrittenToOutsideOfConstructor
,
cancellationToken
).
ConfigureAwait
(
false
);
// Now, rename all usages of the field to point at the property. Except don't actually
// rename the field itself. We want to be able to find it again post rename.
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录