Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
lwm1986
roslyn
提交
b1265560
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,发现更多精彩内容 >>
提交
b1265560
编写于
6月 16, 2020
作者:
C
Cyrus Najmabadi
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
NRT and immutable array
上级
e666b945
变更
5
隐藏空白更改
内联
并排
Showing
5 changed file
with
75 addition
and
78 deletion
+75
-78
src/Features/CSharp/Portable/ReplacePropertyWithMethods/CSharpReplacePropertyWithMethodsService.cs
...rtyWithMethods/CSharpReplacePropertyWithMethodsService.cs
+23
-20
src/Features/Core/Portable/ReplacePropertyWithMethods/AbstractReplacePropertyWithMethodsService.cs
...yWithMethods/AbstractReplacePropertyWithMethodsService.cs
+6
-4
src/Features/Core/Portable/ReplacePropertyWithMethods/IReplacePropertyWithMethodsService.cs
...PropertyWithMethods/IReplacePropertyWithMethodsService.cs
+2
-2
src/Features/Core/Portable/ReplacePropertyWithMethods/ReplacePropertyWithMethodsCodeRefactoringProvider.cs
...hods/ReplacePropertyWithMethodsCodeRefactoringProvider.cs
+36
-45
src/Features/VisualBasic/Portable/ReplacePropertyWithMethods/VisualBasicReplacePropertyWithMethods.vb
...pertyWithMethods/VisualBasicReplacePropertyWithMethods.vb
+8
-7
未找到文件。
src/Features/CSharp/Portable/ReplacePropertyWithMethods/CSharpReplacePropertyWithMethodsService.cs
浏览文件 @
b1265560
...
...
@@ -2,8 +2,10 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
#
nullable
enable
using
System
;
using
System.Collections.
Generic
;
using
System.Collections.
Immutable
;
using
System.Composition
;
using
System.Linq
;
using
System.Threading
;
...
...
@@ -16,7 +18,9 @@
using
Microsoft.CodeAnalysis.Formatting
;
using
Microsoft.CodeAnalysis.Host.Mef
;
using
Microsoft.CodeAnalysis.Options
;
using
Microsoft.CodeAnalysis.PooledObjects
;
using
Microsoft.CodeAnalysis.ReplacePropertyWithMethods
;
using
Microsoft.CodeAnalysis.Shared.Extensions
;
using
Roslyn.Utilities
;
namespace
Microsoft.CodeAnalysis.CSharp.ReplacePropertyWithMethods
...
...
@@ -31,7 +35,7 @@ public CSharpReplacePropertyWithMethodsService()
{
}
public
override
async
Task
<
I
List
<
SyntaxNode
>>
GetReplacementMembersAsync
(
public
override
async
Task
<
I
mmutableArray
<
SyntaxNode
>>
GetReplacementMembersAsync
(
Document
document
,
IPropertySymbol
property
,
SyntaxNode
propertyDeclarationNode
,
...
...
@@ -41,12 +45,10 @@ public CSharpReplacePropertyWithMethodsService()
CancellationToken
cancellationToken
)
{
if
(!(
propertyDeclarationNode
is
PropertyDeclarationSyntax
propertyDeclaration
))
{
return
SpecializedCollections
.
EmptyList
<
SyntaxNode
>();
}
return
ImmutableArray
<
SyntaxNode
>.
Empty
;
var
documentOptions
=
await
document
.
GetOptionsAsync
(
cancellationToken
).
ConfigureAwait
(
false
);
var
syntaxTree
=
await
document
.
GetSyntaxTreeAsync
(
cancellationToken
).
ConfigureAwait
(
false
);
var
syntaxTree
=
await
document
.
Get
Required
SyntaxTreeAsync
(
cancellationToken
).
ConfigureAwait
(
false
);
var
parseOptions
=
syntaxTree
.
Options
;
return
ConvertPropertyToMembers
(
...
...
@@ -57,18 +59,18 @@ public CSharpReplacePropertyWithMethodsService()
cancellationToken
);
}
private
static
List
<
SyntaxNode
>
ConvertPropertyToMembers
(
private
static
ImmutableArray
<
SyntaxNode
>
ConvertPropertyToMembers
(
DocumentOptionSet
documentOptions
,
ParseOptions
parseOptions
,
SyntaxGenerator
generator
,
IPropertySymbol
property
,
PropertyDeclarationSyntax
propertyDeclaration
,
IFieldSymbol
propertyBackingField
,
IFieldSymbol
?
propertyBackingField
,
string
desiredGetMethodName
,
string
desiredSetMethodName
,
CancellationToken
cancellationToken
)
{
var
result
=
new
List
<
SyntaxNode
>(
);
using
var
_
=
ArrayBuilder
<
SyntaxNode
>.
GetInstance
(
out
var
result
);
if
(
propertyBackingField
!=
null
)
{
...
...
@@ -96,7 +98,7 @@ public CSharpReplacePropertyWithMethodsService()
cancellationToken
:
cancellationToken
));
}
return
result
;
return
result
.
ToImmutable
()
;
}
private
static
SyntaxNode
GetSetMethod
(
...
...
@@ -104,7 +106,7 @@ public CSharpReplacePropertyWithMethodsService()
ParseOptions
parseOptions
,
SyntaxGenerator
generator
,
PropertyDeclarationSyntax
propertyDeclaration
,
IFieldSymbol
propertyBackingField
,
IFieldSymbol
?
propertyBackingField
,
IMethodSymbol
setMethod
,
string
desiredSetMethodName
,
CancellationToken
cancellationToken
)
...
...
@@ -125,7 +127,7 @@ public CSharpReplacePropertyWithMethodsService()
private
static
MethodDeclarationSyntax
GetSetMethodWorker
(
SyntaxGenerator
generator
,
PropertyDeclarationSyntax
propertyDeclaration
,
IFieldSymbol
propertyBackingField
,
IFieldSymbol
?
propertyBackingField
,
IMethodSymbol
setMethod
,
string
desiredSetMethodName
,
CancellationToken
cancellationToken
)
...
...
@@ -168,7 +170,7 @@ public CSharpReplacePropertyWithMethodsService()
ParseOptions
parseOptions
,
SyntaxGenerator
generator
,
PropertyDeclarationSyntax
propertyDeclaration
,
IFieldSymbol
propertyBackingField
,
IFieldSymbol
?
propertyBackingField
,
IMethodSymbol
getMethod
,
string
desiredGetMethodName
,
CancellationToken
cancellationToken
)
...
...
@@ -207,8 +209,9 @@ private static SyntaxTrivia ConvertTrivia(SyntaxTrivia trivia, CSharpSyntaxRewri
private
static
SyntaxTrivia
ConvertDocumentationComment
(
SyntaxTrivia
trivia
,
CSharpSyntaxRewriter
rewriter
)
{
var
structure
=
trivia
.
GetStructure
();
var
updatedStructure
=
(
StructuredTriviaSyntax
)
rewriter
.
Visit
(
structure
);
return
SyntaxFactory
.
Trivia
(
updatedStructure
);
var
rewritten
=
rewriter
.
Visit
(
structure
);
Contract
.
ThrowIfNull
(
rewritten
);
return
SyntaxFactory
.
Trivia
((
StructuredTriviaSyntax
)
rewritten
);
}
private
static
SyntaxNode
UseExpressionOrBlockBodyIfDesired
(
...
...
@@ -216,7 +219,7 @@ private static SyntaxTrivia ConvertDocumentationComment(SyntaxTrivia trivia, CSh
MethodDeclarationSyntax
methodDeclaration
,
bool
createReturnStatementForExpression
)
{
var
expressionBodyPreference
=
documentOptions
.
GetOption
(
CSharpCodeStyleOptions
.
PreferExpressionBodiedMethods
).
Value
;
if
(
methodDeclaration
?
.
Body
!=
null
&&
expressionBodyPreference
!=
ExpressionBodyPreference
.
Never
)
if
(
methodDeclaration
.
Body
!=
null
&&
expressionBodyPreference
!=
ExpressionBodyPreference
.
Never
)
{
if
(
methodDeclaration
.
Body
.
TryConvertToArrowExpressionBody
(
methodDeclaration
.
Kind
(),
parseOptions
,
expressionBodyPreference
,
...
...
@@ -228,7 +231,7 @@ private static SyntaxTrivia ConvertDocumentationComment(SyntaxTrivia trivia, CSh
.
WithAdditionalAnnotations
(
Formatter
.
Annotation
);
}
}
else
if
(
methodDeclaration
?
.
ExpressionBody
!=
null
&&
expressionBodyPreference
==
ExpressionBodyPreference
.
Never
)
else
if
(
methodDeclaration
.
ExpressionBody
!=
null
&&
expressionBodyPreference
==
ExpressionBodyPreference
.
Never
)
{
if
(
methodDeclaration
.
ExpressionBody
.
TryConvertToBlock
(
methodDeclaration
.
SemicolonToken
,
createReturnStatementForExpression
,
out
var
block
))
...
...
@@ -246,7 +249,7 @@ private static SyntaxTrivia ConvertDocumentationComment(SyntaxTrivia trivia, CSh
private
static
MethodDeclarationSyntax
GetGetMethodWorker
(
SyntaxGenerator
generator
,
PropertyDeclarationSyntax
propertyDeclaration
,
IFieldSymbol
propertyBackingField
,
IFieldSymbol
?
propertyBackingField
,
IMethodSymbol
getMethod
,
string
desiredGetMethodName
,
CancellationToken
cancellationToken
)
...
...
@@ -305,10 +308,10 @@ public override SyntaxNode GetPropertyNodeToReplace(SyntaxNode propertyDeclarati
return
propertyDeclaration
;
}
protected
override
NameMemberCrefSyntax
TryGetCrefSyntax
(
IdentifierNameSyntax
identifierName
)
protected
override
NameMemberCrefSyntax
?
TryGetCrefSyntax
(
IdentifierNameSyntax
identifierName
)
=>
identifierName
.
Parent
as
NameMemberCrefSyntax
;
protected
override
NameMemberCrefSyntax
CreateCrefSyntax
(
NameMemberCrefSyntax
originalCref
,
SyntaxToken
identifierToken
,
SyntaxNode
parameterType
)
protected
override
NameMemberCrefSyntax
CreateCrefSyntax
(
NameMemberCrefSyntax
originalCref
,
SyntaxToken
identifierToken
,
SyntaxNode
?
parameterType
)
{
CrefParameterListSyntax
parameterList
;
if
(
parameterType
is
TypeSyntax
typeSyntax
)
...
...
src/Features/Core/Portable/ReplacePropertyWithMethods/AbstractReplacePropertyWithMethodsService.cs
浏览文件 @
b1265560
...
...
@@ -6,6 +6,7 @@
using
System
;
using
System.Collections.Generic
;
using
System.Collections.Immutable
;
using
System.Threading
;
using
System.Threading.Tasks
;
using
Microsoft.CodeAnalysis.CodeActions
;
...
...
@@ -27,9 +28,9 @@ internal abstract class AbstractReplacePropertyWithMethodsService<TIdentifierNam
where
TPropertySyntax
:
SyntaxNode
{
public
abstract
SyntaxNode
GetPropertyNodeToReplace
(
SyntaxNode
propertyDeclaration
);
public
abstract
Task
<
I
List
<
SyntaxNode
>>
GetReplacementMembersAsync
(
Document
document
,
IPropertySymbol
property
,
SyntaxNode
propertyDeclaration
,
IFieldSymbol
propertyBackingField
,
string
desiredGetMethodName
,
string
desiredSetMethodName
,
CancellationToken
cancellationToken
);
public
abstract
Task
<
I
mmutableArray
<
SyntaxNode
>>
GetReplacementMembersAsync
(
Document
document
,
IPropertySymbol
property
,
SyntaxNode
propertyDeclaration
,
IFieldSymbol
propertyBackingField
,
string
desiredGetMethodName
,
string
desiredSetMethodName
,
CancellationToken
cancellationToken
);
protected
abstract
TCrefSyntax
TryGetCrefSyntax
(
TIdentifierNameSyntax
identifierName
);
protected
abstract
TCrefSyntax
?
TryGetCrefSyntax
(
TIdentifierNameSyntax
identifierName
);
protected
abstract
TCrefSyntax
CreateCrefSyntax
(
TCrefSyntax
originalCref
,
SyntaxToken
identifierToken
,
SyntaxNode
?
parameterType
);
protected
abstract
TExpressionSyntax
UnwrapCompoundAssignment
(
SyntaxNode
compoundAssignment
,
TExpressionSyntax
readExpression
);
...
...
@@ -85,7 +86,7 @@ protected static SyntaxNode GetFieldReference(SyntaxGenerator generator, IFieldS
private
readonly
TIdentifierNameSyntax
_identifierName
;
private
readonly
TExpressionSyntax
_expression
;
private
readonly
TCrefSyntax
_cref
;
private
readonly
TCrefSyntax
?
_cref
;
private
readonly
CancellationToken
_cancellationToken
;
public
ReferenceReplacer
(
...
...
@@ -95,7 +96,8 @@ protected static SyntaxNode GetFieldReference(SyntaxGenerator generator, IFieldS
ISemanticFactsService
semanticFacts
,
SyntaxEditor
editor
,
TIdentifierNameSyntax
identifierName
,
IPropertySymbol
property
,
IFieldSymbol
propertyBackingField
,
IPropertySymbol
property
,
IFieldSymbol
propertyBackingField
,
string
desiredGetMethodName
,
string
desiredSetMethodName
,
CancellationToken
cancellationToken
)
...
...
src/Features/Core/Portable/ReplacePropertyWithMethods/IReplacePropertyWithMethodsService.cs
浏览文件 @
b1265560
...
...
@@ -2,7 +2,7 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using
System.Collections.
Generic
;
using
System.Collections.
Immutable
;
using
System.Threading
;
using
System.Threading.Tasks
;
using
Microsoft.CodeAnalysis.CodeRefactorings
;
...
...
@@ -22,7 +22,7 @@ internal interface IReplacePropertyWithMethodsService : ILanguageService
string
desiredGetMethodName
,
string
desiredSetMethodName
,
CancellationToken
cancellationToken
);
Task
<
I
List
<
SyntaxNode
>>
GetReplacementMembersAsync
(
Task
<
I
mmutableArray
<
SyntaxNode
>>
GetReplacementMembersAsync
(
Document
document
,
IPropertySymbol
property
,
SyntaxNode
propertyDeclaration
,
IFieldSymbol
propertyBackingField
,
...
...
src/Features/Core/Portable/ReplacePropertyWithMethods/ReplacePropertyWithMethodsCodeRefactoringProvider.cs
浏览文件 @
b1265560
...
...
@@ -2,6 +2,8 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
#
nullable
enable
using
System
;
using
System.Collections.Generic
;
using
System.Collections.Immutable
;
...
...
@@ -17,6 +19,7 @@
using
Microsoft.CodeAnalysis.Editing
;
using
Microsoft.CodeAnalysis.FindSymbols
;
using
Microsoft.CodeAnalysis.LanguageServices
;
using
Microsoft.CodeAnalysis.PooledObjects
;
using
Microsoft.CodeAnalysis.Shared.Extensions
;
using
Microsoft.CodeAnalysis.Shared.Utilities
;
using
Roslyn.Utilities
;
...
...
@@ -41,18 +44,16 @@ public override async Task ComputeRefactoringsAsync(CodeRefactoringContext conte
var
(
document
,
_
,
cancellationToken
)
=
context
;
var
service
=
document
.
GetLanguageService
<
IReplacePropertyWithMethodsService
>();
if
(
service
==
null
)
{
return
;
}
var
propertyDeclaration
=
await
service
.
GetPropertyDeclarationAsync
(
context
).
ConfigureAwait
(
false
);
if
(
propertyDeclaration
==
null
)
{
return
;
}
var
semanticModel
=
await
document
.
GetSemanticModelAsync
(
cancellationToken
).
ConfigureAwait
(
false
);
var
semanticModel
=
await
document
.
Get
Required
SemanticModelAsync
(
cancellationToken
).
ConfigureAwait
(
false
);
var
propertySymbol
=
semanticModel
.
GetDeclaredSymbol
(
propertyDeclaration
)
as
IPropertySymbol
;
Contract
.
ThrowIfNull
(
propertySymbol
);
var
propertyName
=
propertySymbol
.
Name
;
var
accessorCount
=
...
...
@@ -115,9 +116,9 @@ public override async Task ComputeRefactoringsAsync(CodeRefactoringContext conte
return
updatedSolution
;
}
private
static
Dictionary
<
IPropertySymbol
,
IFieldSymbol
>
CreateDefinitionToBackingFieldMap
(
IEnumerable
<
ReferencedSymbol
>
propertyReferences
)
private
static
ImmutableDictionary
<
IPropertySymbol
,
IFieldSymbol
?
>
CreateDefinitionToBackingFieldMap
(
IEnumerable
<
ReferencedSymbol
>
propertyReferences
)
{
var
definitionToBackingField
=
new
Dictionary
<
IPropertySymbol
,
IFieldSymbol
>(
SymbolEquivalenceComparer
.
Instance
);
var
definitionToBackingField
=
ImmutableDictionary
.
CreateBuilder
<
IPropertySymbol
,
IFieldSymbol
?
>(
SymbolEquivalenceComparer
.
Instance
);
foreach
(
var
reference
in
propertyReferences
)
{
...
...
@@ -128,7 +129,7 @@ public override async Task ComputeRefactoringsAsync(CodeRefactoringContext conte
}
}
return
definitionToBackingField
;
return
definitionToBackingField
.
ToImmutable
()
;
}
private
static
bool
HasAnyMatchingGetOrSetMethods
(
IPropertySymbol
property
,
string
name
)
...
...
@@ -156,21 +157,17 @@ private static bool HasAnyMatchingSetMethods(IPropertySymbol property, string na
comparer
.
Equals
(
m
.
Parameters
[
0
].
Type
,
property
.
Type
));
}
private
static
IFieldSymbol
GetBackingField
(
IPropertySymbol
property
)
private
static
IFieldSymbol
?
GetBackingField
(
IPropertySymbol
property
)
{
var
field
=
property
.
GetBackingFieldIfAny
();
if
(
field
==
null
)
{
return
null
;
}
// If the field is something can be referenced with the name it has, then just use
// it as the backing field we'll generate. This is the case in VB where the backing
// field can be referenced as is.
if
(
field
.
CanBeReferencedByName
)
{
return
field
;
}
// Otherwise, generate a good name for the backing field we're generating. This is
// the case for C# where we have mangled names for the backing field and need something
...
...
@@ -188,7 +185,7 @@ private static IFieldSymbol GetBackingField(IPropertySymbol property)
}
#pragma warning disable IDE0060 // Remove unused parameter - Method not completely implemented.
private
static
string
GetDefinitionIssues
(
IEnumerable
<
ReferencedSymbol
>
getMethodReferences
)
private
static
string
?
GetDefinitionIssues
(
IEnumerable
<
ReferencedSymbol
>
getMethodReferences
)
#pragma warning restore IDE0060 // Remove unused parameter
{
// TODO: add things to be concerned about here. For example:
...
...
@@ -201,7 +198,7 @@ private static string GetDefinitionIssues(IEnumerable<ReferencedSymbol> getMetho
private
static
async
Task
<
Solution
>
UpdateReferencesAsync
(
Solution
updatedSolution
,
ILookup
<
Document
,
(
IPropertySymbol
property
,
ReferenceLocation
location
)>
referencesByDocument
,
Dictionary
<
IPropertySymbol
,
IFieldSymbol
>
propertyToBackingField
,
ImmutableDictionary
<
IPropertySymbol
,
IFieldSymbol
?
>
propertyToBackingField
,
string
desiredGetMethodName
,
string
desiredSetMethodName
,
CancellationToken
cancellationToken
)
{
...
...
@@ -220,14 +217,14 @@ private static string GetDefinitionIssues(IEnumerable<ReferencedSymbol> getMetho
Solution
updatedSolution
,
Document
originalDocument
,
IEnumerable
<(
IPropertySymbol
property
,
ReferenceLocation
location
)>
references
,
Dictionary
<
IPropertySymbol
,
IFieldSymbol
>
propertyToBackingField
,
ImmutableDictionary
<
IPropertySymbol
,
IFieldSymbol
?
>
propertyToBackingField
,
string
desiredGetMethodName
,
string
desiredSetMethodName
,
CancellationToken
cancellationToken
)
{
var
root
=
await
originalDocument
.
GetSyntaxRootAsync
(
cancellationToken
).
ConfigureAwait
(
false
);
var
root
=
await
originalDocument
.
Get
Required
SyntaxRootAsync
(
cancellationToken
).
ConfigureAwait
(
false
);
var
editor
=
new
SyntaxEditor
(
root
,
originalDocument
.
Project
.
Solution
.
Workspace
);
var
service
=
originalDocument
.
GetLanguageService
<
IReplacePropertyWithMethodsService
>();
var
service
=
originalDocument
.
Get
Required
LanguageService
<
IReplacePropertyWithMethodsService
>();
await
ReplaceReferencesAsync
(
originalDocument
,
references
,
propertyToBackingField
,
root
,
editor
,
service
,
...
...
@@ -241,7 +238,7 @@ private static string GetDefinitionIssues(IEnumerable<ReferencedSymbol> getMetho
private
static
async
Task
ReplaceReferencesAsync
(
Document
originalDocument
,
IEnumerable
<(
IPropertySymbol
property
,
ReferenceLocation
location
)>
references
,
IDictionary
<
IPropertySymbol
,
IFieldSymbol
>
propertyToBackingField
,
IDictionary
<
IPropertySymbol
,
IFieldSymbol
?
>
propertyToBackingField
,
SyntaxNode
root
,
SyntaxEditor
editor
,
IReplacePropertyWithMethodsService
service
,
string
desiredGetMethodName
,
string
desiredSetMethodName
,
...
...
@@ -249,33 +246,32 @@ private static string GetDefinitionIssues(IEnumerable<ReferencedSymbol> getMetho
{
if
(
references
!=
null
)
{
var
syntaxFacts
=
originalDocument
.
GetLanguageService
<
ISyntaxFactsService
>();
var
syntaxFacts
=
originalDocument
.
Get
Required
LanguageService
<
ISyntaxFactsService
>();
foreach
(
var
tuple
in
references
)
foreach
(
var
(
property
,
referenceLocation
)
in
references
)
{
cancellationToken
.
ThrowIfCancellationRequested
();
var
property
=
tuple
.
property
;
var
referenceLocation
=
tuple
.
location
;
var
location
=
referenceLocation
.
Location
;
var
nameToken
=
root
.
FindToken
(
location
.
SourceSpan
.
Start
,
findInsideTrivia
:
true
);
var
parent
=
nameToken
.
Parent
;
Contract
.
ThrowIfNull
(
parent
);
if
(
referenceLocation
.
IsImplicit
||
!
syntaxFacts
.
IsIdentifierName
(
parent
))
{
// Warn the user that we can't properly replace this property with a method.
editor
.
ReplaceNode
(
parent
,
nameToken
.
P
arent
.
WithAdditionalAnnotations
(
editor
.
ReplaceNode
(
parent
,
p
arent
.
WithAdditionalAnnotations
(
ConflictAnnotation
.
Create
(
FeaturesResources
.
Property_referenced_implicitly
)));
}
else
if
(
syntaxFacts
.
IsObjectInitializerNamedAssignmentIdentifier
(
parent
))
{
editor
.
ReplaceNode
(
parent
,
nameToken
.
P
arent
.
WithAdditionalAnnotations
(
editor
.
ReplaceNode
(
parent
,
p
arent
.
WithAdditionalAnnotations
(
ConflictAnnotation
.
Create
(
FeaturesResources
.
Property_reference_cannot_be_updated
)));
}
else
{
var
fieldSymbol
=
propertyToBackingField
.
GetValueOrDefault
(
tuple
.
property
);
var
fieldSymbol
=
propertyToBackingField
.
GetValueOrDefault
(
property
);
await
service
.
ReplaceReferenceAsync
(
originalDocument
,
editor
,
parent
,
property
,
fieldSymbol
,
...
...
@@ -289,7 +285,7 @@ private static string GetDefinitionIssues(IEnumerable<ReferencedSymbol> getMetho
Solution
originalSolution
,
Solution
updatedSolution
,
IEnumerable
<
ReferencedSymbol
>
references
,
I
Dictionary
<
IPropertySymbol
,
IFieldSymbol
>
definitionToBackingField
,
I
mmutableDictionary
<
IPropertySymbol
,
IFieldSymbol
?
>
definitionToBackingField
,
string
desiredGetMethodName
,
string
desiredSetMethodName
,
CancellationToken
cancellationToken
)
{
...
...
@@ -339,21 +335,20 @@ private static string GetDefinitionIssues(IEnumerable<ReferencedSymbol> getMetho
Solution
updatedSolution
,
DocumentId
documentId
,
MultiDictionary
<
DocumentId
,
IPropertySymbol
>.
ValueSet
originalDefinitions
,
IDictionary
<
IPropertySymbol
,
IFieldSymbol
>
definitionToBackingField
,
IDictionary
<
IPropertySymbol
,
IFieldSymbol
?
>
definitionToBackingField
,
string
desiredGetMethodName
,
string
desiredSetMethodName
,
CancellationToken
cancellationToken
)
{
var
updatedDocument
=
updatedSolution
.
GetDocument
(
documentId
);
var
compilation
=
await
updatedDocument
.
Project
.
GetCompilation
Async
(
cancellationToken
).
ConfigureAwait
(
false
);
var
updatedDocument
=
updatedSolution
.
Get
Required
Document
(
documentId
);
var
semanticModel
=
await
updatedDocument
.
GetRequiredSemanticModel
Async
(
cancellationToken
).
ConfigureAwait
(
false
);
// We've already gone and updated all references. So now re-resolve all the definitions
// in the current compilation to find their updated location.
var
currentDefinitions
=
await
GetCurrentPropertiesAsync
(
updatedSolution
,
c
ompilation
,
documentId
,
originalDefinitions
,
cancellationToken
).
ConfigureAwait
(
false
);
updatedSolution
,
semanticModel
.
C
ompilation
,
documentId
,
originalDefinitions
,
cancellationToken
).
ConfigureAwait
(
false
);
var
service
=
updatedDocument
.
GetLanguageService
<
IReplacePropertyWithMethodsService
>();
var
service
=
updatedDocument
.
Get
Required
LanguageService
<
IReplacePropertyWithMethodsService
>();
var
semanticModel
=
await
updatedDocument
.
GetSemanticModelAsync
(
cancellationToken
).
ConfigureAwait
(
false
);
var
root
=
await
updatedDocument
.
GetSyntaxRootAsync
(
cancellationToken
).
ConfigureAwait
(
false
);
var
editor
=
new
SyntaxEditor
(
root
,
updatedSolution
.
Workspace
);
...
...
@@ -376,7 +371,7 @@ private static string GetDefinitionIssues(IEnumerable<ReferencedSymbol> getMetho
{
members
=
members
.
Select
(
editor
.
Generator
.
AsInterfaceMember
)
.
WhereNotNull
()
.
To
List
();
.
To
ImmutableArray
();
}
var
nodeToReplace
=
service
.
GetPropertyNodeToReplace
(
declaration
);
...
...
@@ -387,14 +382,14 @@ private static string GetDefinitionIssues(IEnumerable<ReferencedSymbol> getMetho
return
updatedSolution
.
WithDocumentSyntaxRoot
(
documentId
,
editor
.
GetChangedRoot
());
}
private
static
async
Task
<
List
<(
IPropertySymbol
property
,
SyntaxNode
declaration
)>>
GetCurrentPropertiesAsync
(
private
static
async
Task
<
ImmutableArray
<(
IPropertySymbol
property
,
SyntaxNode
declaration
)>>
GetCurrentPropertiesAsync
(
Solution
updatedSolution
,
Compilation
compilation
,
DocumentId
documentId
,
MultiDictionary
<
DocumentId
,
IPropertySymbol
>.
ValueSet
originalDefinitions
,
CancellationToken
cancellationToken
)
{
var
result
=
new
List
<(
IPropertySymbol
property
,
SyntaxNode
declaration
)>(
);
using
var
_
=
ArrayBuilder
<(
IPropertySymbol
property
,
SyntaxNode
declaration
)>.
GetInstance
(
out
var
result
);
foreach
(
var
originalDefinition
in
originalDefinitions
)
{
cancellationToken
.
ThrowIfCancellationRequested
();
...
...
@@ -402,29 +397,25 @@ private static string GetDefinitionIssues(IEnumerable<ReferencedSymbol> getMetho
var
property
=
GetSymbolInCurrentCompilation
(
compilation
,
originalDefinition
,
cancellationToken
);
var
declaration
=
await
GetPropertyDeclarationAsync
(
property
,
cancellationToken
).
ConfigureAwait
(
false
);
if
(
declaration
!=
null
&&
updatedSolution
.
GetDocument
(
declaration
.
SyntaxTree
)?.
Id
==
documentId
)
{
if
(
property
!=
null
&&
declaration
!=
null
&&
updatedSolution
.
GetDocument
(
declaration
.
SyntaxTree
)?.
Id
==
documentId
)
result
.
Add
((
property
,
declaration
));
}
}
return
result
;
return
result
.
ToImmutable
()
;
}
private
static
async
Task
<
SyntaxNode
>
GetPropertyDeclarationAsync
(
IPropertySymbol
property
,
CancellationToken
cancellationToken
)
private
static
async
Task
<
SyntaxNode
?
>
GetPropertyDeclarationAsync
(
IPropertySymbol
?
property
,
CancellationToken
cancellationToken
)
{
if
(
property
==
null
)
{
return
null
;
}
Debug
.
Assert
(
property
.
DeclaringSyntaxReferences
.
Length
==
1
);
var
reference
=
property
.
DeclaringSyntaxReferences
[
0
];
return
await
reference
.
GetSyntaxAsync
(
cancellationToken
).
ConfigureAwait
(
false
);
}
private
static
TSymbol
GetSymbolInCurrentCompilation
<
TSymbol
>(
Compilation
compilation
,
TSymbol
originalDefinition
,
CancellationToken
cancellationToken
)
private
static
TSymbol
?
GetSymbolInCurrentCompilation
<
TSymbol
>(
Compilation
compilation
,
TSymbol
originalDefinition
,
CancellationToken
cancellationToken
)
where
TSymbol
:
class
,
ISymbol
{
return
originalDefinition
.
GetSymbolKey
(
cancellationToken
).
Resolve
(
compilation
,
cancellationToken
:
cancellationToken
).
GetAnySymbol
()
as
TSymbol
;
...
...
src/Features/VisualBasic/Portable/ReplacePropertyWithMethods/VisualBasicReplacePropertyWithMethods.vb
浏览文件 @
b1265560
...
...
@@ -2,12 +2,14 @@
' The .NET Foundation licenses this file to you under the MIT license.
' See the LICENSE file in the project root for more information.
Imports
System.Collections.Immutable
Imports
System.Composition
Imports
System.Threading
Imports
Microsoft.CodeAnalysis.CodeGeneration
Imports
Microsoft.CodeAnalysis.Editing
Imports
Microsoft.CodeAnalysis.Formatting
Imports
Microsoft.CodeAnalysis.Host.Mef
Imports
Microsoft.CodeAnalysis.PooledObjects
Imports
Microsoft.CodeAnalysis.ReplacePropertyWithMethods
Imports
Microsoft.CodeAnalysis.VisualBasic.Syntax
...
...
@@ -28,11 +30,11 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.CodeRefactorings.ReplaceMethodWithP
propertyBackingField
As
IFieldSymbol
,
desiredGetMethodName
As
String
,
desiredSetMethodName
As
String
,
cancellationToken
As
CancellationToken
)
As
Task
(
Of
I
List
(
Of
SyntaxNode
))
cancellationToken
As
CancellationToken
)
As
Task
(
Of
I
mmutableArray
(
Of
SyntaxNode
))
Dim
propertyStatement
=
TryCast
(
propertyDeclarationNode
,
PropertyStatementSyntax
)
If
propertyStatement
Is
Nothing
Then
Return
Task
.
FromResult
(
SpecializedCollections
.
EmptyList
(
Of
SyntaxNode
)
)
Return
SpecializedTasks
.
EmptyImmutableArray
(
Of
SyntaxNode
)
End
If
Return
Task
.
FromResult
(
ConvertPropertyToMembers
(
...
...
@@ -49,10 +51,9 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.CodeRefactorings.ReplaceMethodWithP
propertyBackingField
As
IFieldSymbol
,
desiredGetMethodName
As
String
,
desiredSetMethodName
As
String
,
cancellationToken
As
CancellationToken
)
As
IList
(
Of
SyntaxNode
)
Dim
result
=
New
List
(
Of
SyntaxNode
)()
cancellationToken
As
CancellationToken
)
As
ImmutableArray
(
Of
SyntaxNode
)
Dim
result
=
ArrayBuilder
(
Of
SyntaxNode
).
GetInstance
()
If
propertyBackingField
IsNot
Nothing
Then
Dim
initializer
=
propertyStatement
.
Initializer
?
.
Value
result
.
Add
(
generator
.
FieldDeclaration
(
propertyBackingField
,
initializer
))
...
...
@@ -72,7 +73,7 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.CodeRefactorings.ReplaceMethodWithP
setMethod
,
desiredSetMethodName
,
cancellationToken
:
=
cancellationToken
))
End
If
Return
result
Return
result
.
ToImmutableAndFree
()
End
Function
Private
Shared
Function
GetGetMethod
(
...
...
@@ -155,7 +156,7 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.CodeRefactorings.ReplaceMethodWithP
' To do this we make the new method using the information from the implicit getter
' Function. However, we need to update the 'explicit interface implementations'
' of the implicit getter function so that they point to the updated interface method
' and not
ht
e old implicit interface method.
' and not
th
e old implicit interface method.
Dim
updatedImplementations
=
method
.
ExplicitInterfaceImplementations
.
SelectAsArray
(
Function
(
i
)
UpdateExplicitInterfaceImplementation
(
[
property
]
,
i
,
desiredName
))
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录