Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
lwm1986
roslyn
提交
aacc93a8
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,发现更多精彩内容 >>
提交
aacc93a8
编写于
7月 20, 2016
作者:
B
Balaji Krishnan
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Simplify code.
上级
575504d6
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
42 addition
and
35 deletion
+42
-35
src/Features/Core/Portable/CodeRefactorings/MoveType/AbstractMoveTypeService.MoveTypeEditor.cs
...orings/MoveType/AbstractMoveTypeService.MoveTypeEditor.cs
+35
-34
src/Features/Core/Portable/CodeRefactorings/MoveType/AbstractMoveTypeService.cs
...able/CodeRefactorings/MoveType/AbstractMoveTypeService.cs
+7
-1
未找到文件。
src/Features/Core/Portable/CodeRefactorings/MoveType/AbstractMoveTypeService.MoveTypeEditor.cs
浏览文件 @
aacc93a8
// 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.Collections.Generic
;
using
System.Linq
;
using
System.Threading
;
...
...
@@ -139,43 +140,43 @@ private async Task<IEnumerable<CodeActionOperation>> MoveTypeToNewFileAsync(stri
private
static
IEnumerable
<
SyntaxNode
>
GetMembersToRemove
(
SyntaxNode
root
,
TTypeDeclarationSyntax
typeNode
)
{
// the type node being moved and its container declarations should
// be kept.
var
ancestorsAndSelfToKeep
=
typeNode
.
AncestorsAndSelf
()
.
Where
(
n
=>
n
is
TNamespaceDeclarationSyntax
||
n
is
TTypeDeclarationSyntax
);
// while we need the ancestor container declarations,
// we do not need other declarations inside of these containers,
// that are not the type being moved.
var
ancestorsKept
=
ancestorsAndSelfToKeep
.
OfType
<
TTypeDeclarationSyntax
>()
.
Except
(
new
[]
{
typeNode
});
var
membersOfAncestorsKept
=
ancestorsKept
.
SelectMany
(
a
=>
a
.
DescendantNodes
()
.
OfType
<
TMemberDeclarationSyntax
>()
.
Where
(
t
=>
!
t
.
Equals
(
typeNode
)
&&
t
.
Parent
.
Equals
(
a
)));
// top level nodes other that are not in the ancestor chain
// of the type being moved should be removed.
var
topLevelMembersToRemove
=
root
.
DescendantNodesAndSelf
(
descendIntoChildren
:
_
=>
true
,
descendIntoTrivia
:
false
)
.
Where
(
n
=>
IsTopLevelNamespaceOrTypeNode
(
n
)
&&
!
ancestorsAndSelfToKeep
.
Contains
(
n
));
return
topLevelMembersToRemove
.
Concat
(
membersOfAncestorsKept
);
HashSet
<
SyntaxNode
>
spine
=
new
HashSet
<
SyntaxNode
>();
// collect the parent chain of declarations to keep.
SyntaxNode
node
=
typeNode
;
while
(
node
.
Parent
!=
null
&&
!(
node
.
Parent
is
TCompilationUnitSyntax
))
{
spine
.
Add
(
node
.
Parent
);
node
=
node
.
Parent
;
}
// get potential namespace, types and members to remove.
var
removableCandidates
=
root
.
DescendantNodes
(
n
=>
DescendIntoChildren
(
n
,
spine
.
Contains
(
n
)))
.
Where
(
n
=>
FilterToTopLevelMembers
(
n
,
typeNode
));
// diff candidates with items we want to keep.
return
removableCandidates
.
Except
(
spine
);
}
private
static
bool
DescendIntoChildren
(
SyntaxNode
node
,
bool
shouldDescendIntoType
)
{
// 1. get top level types and namespaces to remove.
// 2. descend into types and get members to remove, only if type is part of spine, which means
// we'll be keeping the type declaration but not other members, in the new file.
return
node
is
TCompilationUnitSyntax
||
node
is
TNamespaceDeclarationSyntax
||
(
node
is
TTypeDeclarationSyntax
&&
shouldDescendIntoType
);
}
private
static
bool
IsTopLevelNamespaceOrTypeNode
(
SyntaxNode
n
ode
)
private
static
bool
FilterToTopLevelMembers
(
SyntaxNode
node
,
SyntaxNode
typeN
ode
)
{
// check only for top level namespaces and types in the file.
// top level types are parented by either a namespace or compilation unit
// top level namespaces are parented by compilation unit.
return
node
is
TNamespaceDeclarationSyntax
?
node
.
Parent
is
TCompilationUnitSyntax
:
node
is
TTypeDeclarationSyntax
?
node
.
Parent
is
TNamespaceDeclarationSyntax
||
node
.
Parent
is
TCompilationUnitSyntax
:
false
;
// It is a type declaration that is not the node we've moving
// or its a container namespace, or a member declaration that is not a type,
// thereby ignoring other stuff like statements and identifiers.
return
node
is
TTypeDeclarationSyntax
?
!
node
.
Equals
(
typeNode
)
:
(
node
is
TNamespaceDeclarationSyntax
||
node
is
TMemberDeclarationSyntax
);
}
/// <summary>
...
...
src/Features/Core/Portable/CodeRefactorings/MoveType/AbstractMoveTypeService.cs
浏览文件 @
aacc93a8
...
...
@@ -16,6 +16,7 @@ internal abstract partial class AbstractMoveTypeService<TService, TTypeDeclarati
where
TTypeDeclarationSyntax
:
SyntaxNode
where
TNamespaceDeclarationSyntax
:
SyntaxNode
where
TMemberDeclarationSyntax
:
SyntaxNode
where
TCompilationUnitSyntax
:
SyntaxNode
{
internal
enum
OperationKind
{
...
...
@@ -37,8 +38,13 @@ protected virtual SyntaxNode GetNodetoAnalyze(SyntaxNode root, TextSpan span)
private
bool
IsNestedType
(
TTypeDeclarationSyntax
typeNode
)
=>
typeNode
.
Parent
is
TTypeDeclarationSyntax
;
/// <summary>
/// checks if there is a single top level type declaration in a document
/// </summary>
private
bool
IsSingleTypeDeclarationInSourceDocument
(
SyntaxNode
root
)
=>
root
.
DescendantNodes
().
OfType
<
TTypeDeclarationSyntax
>().
Count
()
==
1
;
root
.
DescendantNodes
()
.
OfType
<
TTypeDeclarationSyntax
>()
.
Count
()
==
1
;
public
async
Task
<
CodeRefactoring
>
GetRefactoringAsync
(
Document
document
,
TextSpan
textSpan
,
CancellationToken
cancellationToken
)
{
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录