Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
lwm1986
roslyn
提交
549a46d7
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,发现更多精彩内容 >>
提交
549a46d7
编写于
4月 19, 2018
作者:
J
Julien Couvreur
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Factor common code out
上级
839e7fe4
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
40 addition
and
98 deletion
+40
-98
src/EditorFeatures/CSharpTest/CodeActions/UseExplicitType/UseExplicitTypeTests.cs
...pTest/CodeActions/UseExplicitType/UseExplicitTypeTests.cs
+0
-1
src/Features/CSharp/Portable/CodeRefactorings/UseExplicitType/UseExplicitTypeCodeRefactoringProvider.cs
...UseExplicitType/UseExplicitTypeCodeRefactoringProvider.cs
+28
-7
src/Features/CSharp/Portable/CodeRefactorings/UseImplicitType/UseImplicitTypeCodeRefactoringProvider.cs
...UseImplicitType/UseImplicitTypeCodeRefactoringProvider.cs
+12
-90
未找到文件。
src/EditorFeatures/CSharpTest/CodeActions/UseExplicitType/UseExplicitTypeTests.cs
浏览文件 @
549a46d7
...
...
@@ -38,7 +38,6 @@ protected override CodeRefactoringProvider CreateCodeRefactoringProvider(Workspa
SingleOption
(
CSharpCodeStyleOptions
.
UseImplicitTypeWhereApparent
,
onWithNone
),
SingleOption
(
CSharpCodeStyleOptions
.
UseImplicitTypeForIntrinsicTypes
,
onWithNone
));
[
Fact
]
public
async
Task
TestIntLocalDeclaration
()
{
...
...
src/Features/CSharp/Portable/CodeRefactorings/UseExplicitType/UseExplicitTypeCodeRefactoringProvider.cs
浏览文件 @
549a46d7
...
...
@@ -8,17 +8,39 @@
using
Microsoft.CodeAnalysis.CodeActions
;
using
Microsoft.CodeAnalysis.CodeRefactorings
;
using
Microsoft.CodeAnalysis.CSharp.Extensions
;
using
Microsoft.CodeAnalysis.CSharp.Syntax
;
using
Microsoft.CodeAnalysis.CSharp.TypeStyle
;
using
Microsoft.CodeAnalysis.CSharp.Utilities
;
using
Microsoft.CodeAnalysis.Editing
;
using
Microsoft.CodeAnalysis.Options
;
using
Microsoft.CodeAnalysis.Shared.Extensions
;
using
Microsoft.CodeAnalysis.Text
;
namespace
Microsoft.CodeAnalysis.CSharp.CodeRefactorings.UseExplicitType
{
[
ExportCodeRefactoringProvider
(
LanguageNames
.
CSharp
,
Name
=
PredefinedCodeRefactoringProviderNames
.
UseExplicitType
),
Shared
]
internal
partial
class
UseExplicitTypeCodeRefactoringProvider
:
CodeRefactoringProvider
internal
class
UseExplicitTypeCodeRefactoringProvider
:
AbstractUseType
CodeRefactoringProvider
{
protected
override
string
Title
=>
CSharpFeaturesResources
.
Use_explicit_type
;
protected
override
TypeSyntax
FindAnalyzableType
(
SyntaxNode
node
,
SemanticModel
semanticModel
,
CancellationToken
cancellationToken
)
=>
CSharpUseExplicitTypeHelper
.
Instance
.
FindAnalyzableType
(
node
,
semanticModel
,
cancellationToken
);
protected
override
TypeStyleResult
AnalyzeTypeName
(
TypeSyntax
typeName
,
SemanticModel
semanticModel
,
OptionSet
optionSet
,
CancellationToken
cancellationToken
)
=>
CSharpUseExplicitTypeHelper
.
Instance
.
AnalyzeTypeName
(
typeName
,
semanticModel
,
optionSet
,
cancellationToken
);
protected
override
Task
HandleDeclarationAsync
(
Document
document
,
SyntaxEditor
editor
,
SyntaxNode
node
,
CancellationToken
cancellationToken
)
=>
UseExplicitTypeCodeFixProvider
.
HandleDeclarationAsync
(
document
,
editor
,
node
,
cancellationToken
);
}
internal
abstract
class
AbstractUseTypeCodeRefactoringProvider
:
CodeRefactoringProvider
{
protected
abstract
Task
HandleDeclarationAsync
(
Document
document
,
SyntaxEditor
editor
,
SyntaxNode
node
,
CancellationToken
cancellationToken
);
protected
abstract
TypeSyntax
FindAnalyzableType
(
SyntaxNode
node
,
SemanticModel
semanticModel
,
CancellationToken
cancellationToken
);
protected
abstract
TypeStyleResult
AnalyzeTypeName
(
TypeSyntax
typeName
,
SemanticModel
semanticModel
,
OptionSet
optionSet
,
CancellationToken
cancellationToken
);
protected
abstract
string
Title
{
get
;
}
public
override
async
Task
ComputeRefactoringsAsync
(
CodeRefactoringContext
context
)
{
var
document
=
context
.
Document
;
...
...
@@ -47,7 +69,7 @@ public override async Task ComputeRefactoringsAsync(CodeRefactoringContext conte
Debug
.
Assert
(
declaration
.
IsKind
(
SyntaxKind
.
VariableDeclaration
,
SyntaxKind
.
ForEachStatement
,
SyntaxKind
.
DeclarationExpression
));
var
declaredType
=
CSharpUseExplicitTypeHelper
.
Instance
.
FindAnalyzableType
(
declaration
,
semanticModel
,
cancellationToken
);
var
declaredType
=
FindAnalyzableType
(
declaration
,
semanticModel
,
cancellationToken
);
if
(
declaredType
==
null
)
{
return
;
...
...
@@ -63,8 +85,7 @@ public override async Task ComputeRefactoringsAsync(CodeRefactoringContext conte
return
;
}
var
typeStyle
=
CSharpUseExplicitTypeHelper
.
Instance
.
AnalyzeTypeName
(
declaredType
,
semanticModel
,
optionSet
,
cancellationToken
);
var
typeStyle
=
AnalyzeTypeName
(
declaredType
,
semanticModel
,
optionSet
,
cancellationToken
);
if
(
typeStyle
.
IsStylePreferred
&&
typeStyle
.
Severity
!=
DiagnosticSeverity
.
Hidden
)
{
// the analyzer would handle this. So we do not.
...
...
@@ -78,7 +99,7 @@ public override async Task ComputeRefactoringsAsync(CodeRefactoringContext conte
context
.
RegisterRefactoring
(
new
MyCodeAction
(
CSharpFeaturesResources
.
Use_explicit_typ
e
,
Titl
e
,
c
=>
UpdateDocumentAsync
(
document
,
declaredType
,
c
)));
}
...
...
@@ -89,12 +110,12 @@ private static SyntaxNode GetDeclaration(SyntaxNode root, TextSpan textSpan)
a
=>
a
.
IsKind
(
SyntaxKind
.
DeclarationExpression
,
SyntaxKind
.
VariableDeclaration
,
SyntaxKind
.
ForEachStatement
));
}
private
static
async
Task
<
Document
>
UpdateDocumentAsync
(
Document
document
,
SyntaxNode
node
,
CancellationToken
cancellationToken
)
private
async
Task
<
Document
>
UpdateDocumentAsync
(
Document
document
,
SyntaxNode
node
,
CancellationToken
cancellationToken
)
{
var
root
=
await
document
.
GetSyntaxRootAsync
(
cancellationToken
).
ConfigureAwait
(
false
);
var
editor
=
new
SyntaxEditor
(
root
,
document
.
Project
.
Solution
.
Workspace
);
await
UseExplicitTypeCodeFixProvider
.
HandleDeclarationAsync
(
document
,
editor
,
node
,
cancellationToken
).
ConfigureAwait
(
false
);
await
HandleDeclarationAsync
(
document
,
editor
,
node
,
cancellationToken
).
ConfigureAwait
(
false
);
var
newRoot
=
editor
.
GetChangedRoot
();
return
document
.
WithSyntaxRoot
(
newRoot
);
...
...
src/Features/CSharp/Portable/CodeRefactorings/UseImplicitType/UseImplicitTypeCodeRefactoringProvider.cs
浏览文件 @
549a46d7
// 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.Composition
;
using
System.Diagnostics
;
using
System.Threading
;
using
System.Threading.Tasks
;
using
Microsoft.CodeAnalysis.CodeActions
;
using
Microsoft.CodeAnalysis.CodeRefactorings
;
using
Microsoft.CodeAnalysis.CSharp.
Extensions
;
using
Microsoft.CodeAnalysis.CSharp.
Syntax
;
using
Microsoft.CodeAnalysis.CSharp.TypeStyle
;
using
Microsoft.CodeAnalysis.CSharp.Utilities
;
using
Microsoft.CodeAnalysis.Editing
;
using
Microsoft.CodeAnalysis.Shared.Extensions
;
using
Microsoft.CodeAnalysis.Text
;
using
Microsoft.CodeAnalysis.Options
;
namespace
Microsoft.CodeAnalysis.CSharp.CodeRefactorings.UseImplicitType
{
[
ExportCodeRefactoringProvider
(
LanguageNames
.
CSharp
,
Name
=
PredefinedCodeRefactoringProviderNames
.
UseImplicitType
),
Shared
]
internal
partial
class
UseImplicitTypeCodeRefactoringProvider
:
CodeRefactoringProvider
internal
partial
class
UseImplicitTypeCodeRefactoringProvider
:
UseExplicitType
.
AbstractUseType
CodeRefactoringProvider
{
public
override
async
Task
ComputeRefactoringsAsync
(
CodeRefactoringContext
context
)
{
var
document
=
context
.
Document
;
var
textSpan
=
context
.
Span
;
var
cancellationToken
=
context
.
CancellationToken
;
if
(!
textSpan
.
IsEmpty
)
{
return
;
}
if
(
document
.
Project
.
Solution
.
Workspace
.
Kind
==
WorkspaceKind
.
MiscellaneousFiles
)
{
return
;
}
var
root
=
await
document
.
GetSyntaxRootAsync
(
cancellationToken
).
ConfigureAwait
(
false
);
var
semanticModel
=
await
document
.
GetSemanticModelAsync
(
cancellationToken
).
ConfigureAwait
(
false
);
var
optionSet
=
await
document
.
GetOptionsAsync
(
cancellationToken
).
ConfigureAwait
(
false
);
var
declaration
=
GetDeclaration
(
root
,
textSpan
);
if
(
declaration
==
null
)
{
return
;
}
Debug
.
Assert
(
declaration
.
IsKind
(
SyntaxKind
.
VariableDeclaration
,
SyntaxKind
.
ForEachStatement
,
SyntaxKind
.
DeclarationExpression
));
var
declaredType
=
CSharpUseImplicitTypeHelper
.
Instance
.
FindAnalyzableType
(
declaration
,
semanticModel
,
cancellationToken
);
if
(
declaredType
==
null
)
{
return
;
}
if
(
declaredType
.
OverlapsHiddenPosition
(
cancellationToken
))
{
return
;
}
if
(!
declaredType
.
Span
.
IntersectsWith
(
textSpan
.
Start
))
{
return
;
}
var
typeStyle
=
CSharpUseImplicitTypeHelper
.
Instance
.
AnalyzeTypeName
(
declaredType
,
semanticModel
,
optionSet
,
cancellationToken
);
if
(
typeStyle
.
IsStylePreferred
&&
typeStyle
.
Severity
!=
DiagnosticSeverity
.
Hidden
)
{
// the analyzer would handle this. So we do not.
return
;
}
protected
override
string
Title
=>
CSharpFeaturesResources
.
Use_implicit_type
;
if
(!
typeStyle
.
CanConvert
())
{
return
;
}
protected
override
TypeSyntax
FindAnalyzableType
(
SyntaxNode
node
,
SemanticModel
semanticModel
,
CancellationToken
cancellationToken
)
=>
CSharpUseImplicitTypeHelper
.
Instance
.
FindAnalyzableType
(
node
,
semanticModel
,
cancellationToken
);
context
.
RegisterRefactoring
(
new
MyCodeAction
(
CSharpFeaturesResources
.
Use_implicit_type
,
c
=>
UpdateDocumentAsync
(
document
,
declaredType
,
c
)));
}
private
static
SyntaxNode
GetDeclaration
(
SyntaxNode
root
,
TextSpan
textSpan
)
{
var
token
=
root
.
FindToken
(
textSpan
.
Start
);
return
token
.
Parent
?.
FirstAncestorOrSelf
<
SyntaxNode
>(
a
=>
a
.
IsKind
(
SyntaxKind
.
DeclarationExpression
,
SyntaxKind
.
VariableDeclaration
,
SyntaxKind
.
ForEachStatement
));
}
private
static
async
Task
<
Document
>
UpdateDocumentAsync
(
Document
document
,
SyntaxNode
node
,
CancellationToken
cancellationToken
)
{
var
root
=
await
document
.
GetSyntaxRootAsync
(
cancellationToken
).
ConfigureAwait
(
false
);
var
editor
=
new
SyntaxEditor
(
root
,
document
.
Project
.
Solution
.
Workspace
);
UseImplicitTypeCodeFixProvider
.
HandleDeclaration
(
editor
,
node
);
var
newRoot
=
editor
.
GetChangedRoot
();
return
document
.
WithSyntaxRoot
(
newRoot
);
}
protected
override
TypeStyleResult
AnalyzeTypeName
(
TypeSyntax
typeName
,
SemanticModel
semanticModel
,
OptionSet
optionSet
,
CancellationToken
cancellationToken
)
=>
CSharpUseImplicitTypeHelper
.
Instance
.
AnalyzeTypeName
(
typeName
,
semanticModel
,
optionSet
,
cancellationToken
);
pr
ivate
class
MyCodeAction
:
CodeAction
.
DocumentChangeAction
pr
otected
override
Task
HandleDeclarationAsync
(
Document
document
,
SyntaxEditor
editor
,
SyntaxNode
node
,
CancellationToken
cancellationToken
)
{
public
MyCodeAction
(
string
title
,
Func
<
CancellationToken
,
Task
<
Document
>>
createChangedDocument
)
:
base
(
title
,
createChangedDocument
)
{
}
UseImplicitTypeCodeFixProvider
.
HandleDeclaration
(
editor
,
node
);
return
Task
.
CompletedTask
;
}
}
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录