Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
lwm1986
roslyn
提交
ab9cc113
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,发现更多精彩内容 >>
提交
ab9cc113
编写于
9月 10, 2015
作者:
C
Cyrus Najmabadi
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Better handle C# 5 code.
上级
8b5da4b6
变更
7
显示空白变更内容
内联
并排
Showing
7 changed file
with
54 addition
and
23 deletion
+54
-23
src/EditorFeatures/CSharp/UseAutoProperty/UseAutoPropertyAnalyzer.cs
...eatures/CSharp/UseAutoProperty/UseAutoPropertyAnalyzer.cs
+1
-1
src/EditorFeatures/CSharp/UseAutoProperty/UseAutoPropertyCodeFixProvider.cs
.../CSharp/UseAutoProperty/UseAutoPropertyCodeFixProvider.cs
+24
-5
src/EditorFeatures/CSharpTest/Diagnostics/UseAutoProperty/UseAutoPropertyTests.cs
...pTest/Diagnostics/UseAutoProperty/UseAutoPropertyTests.cs
+11
-2
src/EditorFeatures/VisualBasic/UseAutoProperty/UseAutoPropertyAnalyzer.vb
...es/VisualBasic/UseAutoProperty/UseAutoPropertyAnalyzer.vb
+1
-1
src/EditorFeatures/VisualBasic/UseAutoProperty/UseAutoPropertyCodeFixProvider.vb
...alBasic/UseAutoProperty/UseAutoPropertyCodeFixProvider.vb
+1
-0
src/Features/Core/Portable/UseAutoProperty/AbstractUseAutoPropertyAnalyzer.cs
...rtable/UseAutoProperty/AbstractUseAutoPropertyAnalyzer.cs
+13
-11
src/Features/Core/Portable/UseAutoProperty/AbstractUseAutoPropertyCodeFixProvider.cs
...UseAutoProperty/AbstractUseAutoPropertyCodeFixProvider.cs
+3
-3
未找到文件。
src/EditorFeatures/CSharp/UseAutoProperty/UseAutoPropertyAnalyzer.cs
浏览文件 @
ab9cc113
...
...
@@ -14,7 +14,7 @@ namespace Microsoft.CodeAnalysis.Editor.CSharp.UseAutoProperty
[
DiagnosticAnalyzer
(
LanguageNames
.
CSharp
)]
internal
class
UseAutoPropertyAnalyzer
:
AbstractUseAutoPropertyAnalyzer
<
PropertyDeclarationSyntax
,
FieldDeclarationSyntax
,
VariableDeclaratorSyntax
,
ExpressionSyntax
>
{
protected
override
bool
IsLanguageVersionSupported
(
Compilation
compilation
)
protected
override
bool
SupportsReadOnlyProperties
(
Compilation
compilation
)
{
return
((
CSharpCompilation
)
compilation
).
LanguageVersion
>=
LanguageVersion
.
CSharp6
;
}
...
...
src/EditorFeatures/CSharp/UseAutoProperty/UseAutoPropertyCodeFixProvider.cs
浏览文件 @
ab9cc113
...
...
@@ -25,14 +25,14 @@ protected override SyntaxNode GetNodeToRemove(VariableDeclaratorSyntax declarato
}
protected
override
SyntaxNode
UpdateProperty
(
Project
project
,
IFieldSymbol
fieldSymbol
,
IPropertySymbol
propertySymbol
,
PropertyDeclarationSyntax
propertyDeclaration
,
bool
isWrittenOutsideOfConstructor
,
CancellationToken
cancellationToken
)
Project
project
,
Compilation
compilation
,
IFieldSymbol
fieldSymbol
,
IPropertySymbol
propertySymbol
,
PropertyDeclarationSyntax
propertyDeclaration
,
bool
isWrittenOutsideOfConstructor
,
CancellationToken
cancellationToken
)
{
var
updatedProperty
=
propertyDeclaration
.
WithAccessorList
(
UpdateAccessorList
(
propertyDeclaration
.
AccessorList
));
// We may need to add a setter if the field is written to outside of the constructor
// of it's class.
if
(
NeedsSetter
(
propertyDeclaration
,
isWrittenOutsideOfConstructor
))
if
(
NeedsSetter
(
compilation
,
propertyDeclaration
,
isWrittenOutsideOfConstructor
))
{
var
accessor
=
SyntaxFactory
.
AccessorDeclaration
(
SyntaxKind
.
SetAccessorDeclaration
)
.
WithSemicolonToken
(
SyntaxFactory
.
Token
(
SyntaxKind
.
SemicolonToken
));
...
...
@@ -49,9 +49,28 @@ protected override SyntaxNode GetNodeToRemove(VariableDeclaratorSyntax declarato
return
updatedProperty
;
}
private
bool
NeedsSetter
(
PropertyDeclarationSyntax
propertyDeclaration
,
bool
isWrittenOutsideOfConstructor
)
private
bool
NeedsSetter
(
Compilation
compilation
,
PropertyDeclarationSyntax
propertyDeclaration
,
bool
isWrittenOutsideOfConstructor
)
{
return
isWrittenOutsideOfConstructor
&&
!
propertyDeclaration
.
AccessorList
.
Accessors
.
Any
(
SyntaxKind
.
SetAccessorDeclaration
);
if
(
propertyDeclaration
.
AccessorList
.
Accessors
.
Any
(
SyntaxKind
.
SetAccessorDeclaration
))
{
// Already has a setter.
return
false
;
}
if
(!
SupportsReadOnlyProperties
(
compilation
))
{
// If the language doesn't have readonly properties, then we'll need a
// setter here.
return
true
;
}
// If we're written outside a constructor we need a setter.
return
isWrittenOutsideOfConstructor
;
}
private
bool
SupportsReadOnlyProperties
(
Compilation
compilation
)
{
return
((
CSharpCompilation
)
compilation
).
LanguageVersion
>=
LanguageVersion
.
CSharp6
;
}
private
AccessorListSyntax
UpdateAccessorList
(
AccessorListSyntax
accessorList
)
...
...
src/EditorFeatures/CSharpTest/Diagnostics/UseAutoProperty/UseAutoPropertyTests.cs
浏览文件 @
ab9cc113
...
...
@@ -25,10 +25,19 @@ public void TestSingleGetter1()
}
[
Fact
,
Trait
(
Traits
.
Feature
,
Traits
.
Features
.
CodeActionsUseAutoProperty
)]
public
void
TestCSharp5
()
public
void
TestCSharp5_1
()
{
Test
(
@"class Class { [|int i|]; public int P { get { return i; } } }"
,
@"class Class { public int P { get; private set; } }"
,
CSharpParseOptions
.
Default
.
WithLanguageVersion
(
LanguageVersion
.
CSharp5
));
}
[
Fact
,
Trait
(
Traits
.
Feature
,
Traits
.
Features
.
CodeActionsUseAutoProperty
)]
public
void
TestCSharp5_2
()
{
TestMissing
(
@"class Class { [|int i|]; int P { get { return i; } } }"
,
@"class Class { [|
readonly
int i|]; int P { get { return i; } } }"
,
CSharpParseOptions
.
Default
.
WithLanguageVersion
(
LanguageVersion
.
CSharp5
));
}
...
...
src/EditorFeatures/VisualBasic/UseAutoProperty/UseAutoPropertyAnalyzer.vb
浏览文件 @
ab9cc113
...
...
@@ -13,7 +13,7 @@ Namespace Microsoft.CodeAnalysis.Editor.VisualBasic.UseAutoProperty
Private
ReadOnly
semanticFacts
As
New
VisualBasicSemanticFactsService
()
Protected
Overrides
Function
IsLanguageVersionSupported
(
compilation
As
Compilation
)
As
Boolean
Protected
Overrides
Function
SupportsReadOnlyProperties
(
compilation
As
Compilation
)
As
Boolean
Return
True
End
Function
...
...
src/EditorFeatures/VisualBasic/UseAutoProperty/UseAutoPropertyCodeFixProvider.vb
浏览文件 @
ab9cc113
...
...
@@ -16,6 +16,7 @@ Namespace Microsoft.CodeAnalysis.Editor.VisualBasic.UseAutoProperty
End
Function
Protected
Overrides
Function
UpdateProperty
(
project
As
Project
,
compilation
As
Compilation
,
fieldSymbol
As
IFieldSymbol
,
propertySymbol
As
IPropertySymbol
,
propertyDeclaration
As
PropertyBlockSyntax
,
...
...
src/Features/Core/Portable/UseAutoProperty/AbstractUseAutoPropertyAnalyzer.cs
浏览文件 @
ab9cc113
...
...
@@ -27,15 +27,16 @@ internal abstract class AbstractUseAutoPropertyAnalyzer<TPropertyDeclaration, TF
public
sealed
override
ImmutableArray
<
DiagnosticDescriptor
>
SupportedDiagnostics
=>
ImmutableArray
.
Create
(
Descriptor
,
FadedTokenDescriptor
);
protected
abstract
void
RegisterIneligibleFieldsAction
(
CompilationStartAnalysisContext
context
,
ConcurrentBag
<
IFieldSymbol
>
ineligibleFields
);
protected
abstract
bool
SupportsReadOnlyProperties
(
Compilation
compilation
);
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
);
public
sealed
override
void
Initialize
(
AnalysisContext
context
)
{
context
.
RegisterCompilationStartAction
(
csac
=>
{
if
(!
IsLanguageVersionSupported
(
csac
.
Compilation
))
{
return
;
}
var
analysisResults
=
new
ConcurrentBag
<
AnalysisResult
>();
var
ineligibleFields
=
new
ConcurrentBag
<
IFieldSymbol
>();
...
...
@@ -46,12 +47,6 @@ public sealed override void Initialize(AnalysisContext context)
});
}
protected
abstract
bool
IsLanguageVersionSupported
(
Compilation
compilation
);
protected
abstract
void
RegisterIneligibleFieldsAction
(
CompilationStartAnalysisContext
context
,
ConcurrentBag
<
IFieldSymbol
>
ineligibleFields
);
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
);
private
void
AnalyzeProperty
(
ConcurrentBag
<
AnalysisResult
>
analysisResults
,
SymbolAnalysisContext
symbolContext
)
{
var
property
=
(
IPropertySymbol
)
symbolContext
.
Symbol
;
...
...
@@ -109,6 +104,13 @@ private void AnalyzeProperty(ConcurrentBag<AnalysisResult> analysisResults, Symb
return
;
}
// If the user made the field readonly, we only want to convert it to a property if we
// can keep it readonly.
if
(
getterField
.
IsReadOnly
&&
!
SupportsReadOnlyProperties
(
symbolContext
.
Compilation
))
{
return
;
}
if
(!
containingType
.
Equals
(
getterField
.
ContainingType
))
{
// Field and property have to be in the same type.
...
...
src/Features/Core/Portable/UseAutoProperty/AbstractUseAutoPropertyCodeFixProvider.cs
浏览文件 @
ab9cc113
...
...
@@ -30,8 +30,8 @@ internal abstract class AbstractUseAutoPropertyCodeFixProvider<TPropertyDeclarat
protected
abstract
SyntaxNode
GetNodeToRemove
(
TVariableDeclarator
declarator
);
protected
abstract
SyntaxNode
UpdateProperty
(
Project
project
,
IFieldSymbol
fieldSymbol
,
IPropertySymbol
propertySymbol
,
TPropertyDeclaration
propertyDeclaration
,
bool
isWrittenOutsideConstructor
,
CancellationToken
cancellationToken
);
Project
project
,
Compilation
compilation
,
IFieldSymbol
fieldSymbol
,
IPropertySymbol
propertySymbol
,
TPropertyDeclaration
propertyDeclaration
,
bool
isWrittenOutsideConstructor
,
CancellationToken
cancellationToken
);
public
sealed
override
Task
RegisterCodeFixesAsync
(
CodeFixContext
context
)
{
...
...
@@ -74,7 +74,7 @@ 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
,
fieldSymbol
,
propertySymbol
,
property
,
var
updatedProperty
=
UpdateProperty
(
project
,
compilation
,
fieldSymbol
,
propertySymbol
,
property
,
IsWrittenToOutsideOfConstructorOrProperty
(
fieldSymbol
,
fieldLocations
,
property
,
cancellationToken
),
cancellationToken
);
// Now, rename all usages of the field to point at the property. Except don't actually
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录