Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
lwm1986
roslyn
提交
2966c70a
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,发现更多精彩内容 >>
提交
2966c70a
编写于
11月 15, 2018
作者:
G
Gen Lu
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Address review comments
上级
21151ddd
变更
5
隐藏空白更改
内联
并排
Showing
5 changed file
with
74 addition
and
50 deletion
+74
-50
src/EditorFeatures/CSharpTest/CodeActions/SyncNamespace/CSharpSyncNamespaceTestsBase.cs
...CodeActions/SyncNamespace/CSharpSyncNamespaceTestsBase.cs
+21
-0
src/EditorFeatures/CSharpTest/CodeActions/SyncNamespace/SyncNamespaceTests_ChangeNamespace.cs
...tions/SyncNamespace/SyncNamespaceTests_ChangeNamespace.cs
+0
-12
src/Features/CSharp/Portable/CodeRefactorings/SyncNamespace/CSharpSyncNamespaceService.cs
...eRefactorings/SyncNamespace/CSharpSyncNamespaceService.cs
+11
-9
src/Features/Core/Portable/CodeRefactorings/SyncNamespace/AbstractSyncNamespaceService.ChangeNamespaceCodeAction.cs
...AbstractSyncNamespaceService.ChangeNamespaceCodeAction.cs
+5
-2
src/Features/Core/Portable/CodeRefactorings/SyncNamespace/AbstractSyncNamespaceService.State.cs
...rings/SyncNamespace/AbstractSyncNamespaceService.State.cs
+37
-27
未找到文件。
src/EditorFeatures/CSharpTest/CodeActions/SyncNamespace/CSharpSyncNamespaceTestsBase.cs
浏览文件 @
2966c70a
...
...
@@ -6,6 +6,7 @@
using
System.Linq
;
using
System.Threading
;
using
System.Threading.Tasks
;
using
Microsoft.CodeAnalysis.CodeActions
;
using
Microsoft.CodeAnalysis.CodeRefactorings
;
using
Microsoft.CodeAnalysis.CodeRefactorings.SyncNamespace
;
using
Microsoft.CodeAnalysis.CSharp.CodeRefactorings.SyncNamespace
;
...
...
@@ -172,6 +173,26 @@ protected async Task TestMoveFileToMatchNamespace(string initialMarkup, List<str
Assert
.
True
(
expectedSourceReference
==
null
||
changedDocumentIds
.
Contains
(
refDocumentId
),
"reference document was not changed."
);
var
modifiedOriginalDocument
=
newSolution
.
GetDocument
(
originalDocumentId
);
var
modifiedOringinalRoot
=
await
modifiedOriginalDocument
.
GetSyntaxRootAsync
();
// One node/token will contain the warning we attached for change namespace action.
Assert
.
Single
(
modifiedOringinalRoot
.
DescendantNodesAndTokensAndSelf
().
Where
(
n
=>
{
IEnumerable
<
SyntaxAnnotation
>
annotations
;
if
(
n
.
IsNode
)
{
annotations
=
n
.
AsNode
().
GetAnnotations
(
WarningAnnotation
.
Kind
);
}
else
{
annotations
=
n
.
AsToken
().
GetAnnotations
(
WarningAnnotation
.
Kind
);
}
return
annotations
.
Any
(
annotation
=>
WarningAnnotation
.
GetDescription
(
annotation
)
==
FeaturesResources
.
Warning_colon_changing_namespace_may_produce_invalid_code_and_change_code_meaning
);
}));
var
actualText
=
(
await
modifiedOriginalDocument
.
GetTextAsync
()).
ToString
();
Assert
.
Equal
(
expectedSourceOriginal
,
actualText
);
...
...
src/EditorFeatures/CSharpTest/CodeActions/SyncNamespace/SyncNamespaceTests_ChangeNamespace.cs
浏览文件 @
2966c70a
...
...
@@ -1125,18 +1125,6 @@ void M1()
await
TestChangeNamespaceAsync
(
code
,
expectedSourceOriginal
,
expectedSourceReference
);
}
[
WpfFact
,
Trait
(
Traits
.
Feature
,
Traits
.
Features
.
CodeActionsSyncNamespace
)]
public
async
Task
ChangeFromGlobalNamespace_SingleDocumentNoRef
()
{
...
...
src/Features/CSharp/Portable/CodeRefactorings/SyncNamespace/CSharpSyncNamespaceService.cs
浏览文件 @
2966c70a
...
...
@@ -136,9 +136,6 @@ protected override SyntaxList<MemberDeclarationSyntax> GetMemberDeclarationsInCo
// The name will be resolved by adding proper import.
old
=
@new
=
nameRef
;
return
false
;
bool
IsGlobalNamespace
(
ImmutableArray
<
string
>
parts
)
=>
parts
.
Length
==
1
&&
parts
[
0
].
Length
==
0
;
}
protected
override
string
EscapeIdentifier
(
string
identifier
)
...
...
@@ -178,12 +175,13 @@ protected override async Task<SyntaxNode> ShouldPositionTriggerRefactoringAsync(
SyntaxNode
GetTriggeringNode
(
CompilationUnitSyntax
compUnit
,
int
pos
)
{
var
namespaceDecls
=
compilationUnit
.
DescendantNodes
().
OfType
<
NamespaceDeclarationSyntax
>().
ToImmutableArray
();
var
namespaceDecls
=
compilationUnit
.
DescendantNodes
(
n
=>
n
is
CompilationUnitSyntax
||
n
is
NamespaceDeclarationSyntax
)
.
OfType
<
NamespaceDeclarationSyntax
>().
ToImmutableArray
();
if
(
namespaceDecls
.
Length
==
1
&&
compilationUnit
.
Members
.
Count
==
1
)
{
var
namespaceDeclaration
=
namespaceDecls
.
Single
()
;
Debug
.
Assert
(
namespaceDeclaration
==
compilationUnit
.
Members
.
Single
()
);
var
namespaceDeclaration
=
namespaceDecls
[
0
]
;
Debug
.
Assert
(
namespaceDeclaration
==
compilationUnit
.
Members
[
0
]
);
if
(
namespaceDeclaration
.
Name
.
Span
.
IntersectsWith
(
position
)
&&
namespaceDeclaration
.
Name
.
GetDiagnostics
().
All
(
diag
=>
diag
.
DefaultSeverity
!=
DiagnosticSeverity
.
Error
))
...
...
@@ -223,7 +221,7 @@ SyntaxNode GetTriggeringNode(CompilationUnitSyntax compUnit, int pos)
Debug
.
Assert
(!
declaredNamespaceParts
.
IsDefault
&&
!
targetNamespaceParts
.
IsDefault
);
// Move everything from global namespace to a namespace declaration
if
(
declaredNamespaceParts
.
Length
==
1
&&
declaredNamespaceParts
[
0
].
Length
==
0
)
if
(
IsGlobalNamespace
(
declaredNamespaceParts
)
)
{
var
targetNamespaceDecl
=
SyntaxFactory
.
NamespaceDeclaration
(
name
:
CreateNameSyntax
(
targetNamespaceParts
,
aliasQualifier
:
null
,
targetNamespaceParts
.
Length
-
1
)
...
...
@@ -238,7 +236,7 @@ SyntaxNode GetTriggeringNode(CompilationUnitSyntax compUnit, int pos)
var
namespaceDeclaration
=
compilationUnit
.
DescendantNodes
().
OfType
<
NamespaceDeclarationSyntax
>().
Single
();
// Move everything to global namespace
if
(
targetNamespaceParts
.
Length
==
1
&&
targetNamespaceParts
[
0
].
Length
==
0
)
if
(
IsGlobalNamespace
(
targetNamespaceParts
)
)
{
var
(
namespaceOpeningTrivia
,
namespaceClosingTrivia
)
=
GetOpeningAndClosingTriviaOfNamespaceDeclaration
(
namespaceDeclaration
);
...
...
@@ -288,9 +286,13 @@ SyntaxNode GetTriggeringNode(CompilationUnitSyntax compUnit, int pos)
return
compilationUnit
.
ReplaceNode
(
namespaceDeclaration
,
namespaceDeclaration
.
WithName
(
CreateNameSyntax
(
targetNamespaceParts
,
aliasQualifier
:
null
,
targetNamespaceParts
.
Length
-
1
)
.
WithTriviaFrom
(
namespaceDeclaration
.
Name
)));
.
WithTriviaFrom
(
namespaceDeclaration
.
Name
)
.
WithAdditionalAnnotations
(
WarningAnnotation
)));
}
private
static
bool
IsGlobalNamespace
(
ImmutableArray
<
string
>
parts
)
=>
parts
.
Length
==
1
&&
parts
[
0
].
Length
==
0
;
private
static
string
GetAliasQualifierOpt
(
SyntaxNode
name
)
{
while
(
true
)
...
...
src/Features/Core/Portable/CodeRefactorings/SyncNamespace/AbstractSyncNamespaceService.ChangeNamespaceCodeAction.cs
浏览文件 @
2966c70a
...
...
@@ -382,7 +382,7 @@ await FixReferencesAsync(document, addImportService, syncNamespaceService, refLo
{
var
editor
=
await
DocumentEditor
.
CreateAsync
(
document
,
cancellationToken
).
ConfigureAwait
(
false
);
var
root
=
editor
.
OriginalRoot
;
var
containers
=
new
HashSet
<
SyntaxNode
>
();
var
containers
=
PooledHashSet
<
SyntaxNode
>.
GetInstance
();
var
generator
=
SyntaxGenerator
.
GetGenerator
(
document
);
var
syntaxFacts
=
document
.
GetLanguageService
<
ISyntaxFactsService
>();
...
...
@@ -432,7 +432,10 @@ await FixReferencesAsync(document, addImportService, syncNamespaceService, refLo
var
fixedDocument
=
editor
.
GetChangedDocument
();
root
=
await
fixedDocument
.
GetSyntaxRootAsync
(
cancellationToken
).
ConfigureAwait
(
false
);
return
(
fixedDocument
,
containers
.
Select
(
c
=>
root
.
GetCurrentNode
(
c
)).
ToImmutableArray
());
var
result
=
(
fixedDocument
,
containers
.
SelectAsArray
(
c
=>
root
.
GetCurrentNode
(
c
)));
containers
.
Free
();
return
result
;
}
private
async
Task
<
Solution
>
RemoveUnnecessaryImportsAsync
(
...
...
src/Features/Core/Portable/CodeRefactorings/SyncNamespace/AbstractSyncNamespaceService.State.cs
浏览文件 @
2966c70a
...
...
@@ -9,6 +9,7 @@
using
System.Threading.Tasks
;
using
Microsoft.CodeAnalysis.Editing
;
using
Microsoft.CodeAnalysis.LanguageServices
;
using
Microsoft.CodeAnalysis.PooledObjects
;
using
Microsoft.CodeAnalysis.Shared.Extensions
;
using
Microsoft.CodeAnalysis.Text
;
using
Roslyn.Utilities
;
...
...
@@ -159,42 +160,51 @@ private static string GetDefaultNamespace(ImmutableArray<Document> documents, IS
// (See `ShouldPositionTriggerRefactoringAsync`), or we are getting different namespace declarations among
// those documents, then we know we can't make a proper code change. We will return false and the refactoring
// will then bail. We use span of namespace declaration found in each document to decide if they are identical.
var
spansForNamespaceDeclaration
=
new
Dictionary
<
TextSpan
,
TNamespaceDeclarationSyntax
>();
foreach
(
var
document
in
documents
)
{
var
compilationUnitOrNamespaceDeclOpt
=
await
service
.
ShouldPositionTriggerRefactoringAsync
(
document
,
textSpan
.
Start
,
cancellationToken
)
.
ConfigureAwait
(
false
);
if
(
compilationUnitOrNamespaceDeclOpt
is
TNamespaceDeclarationSyntax
namespaceDeclaration
)
{
spansForNamespaceDeclaration
[
namespaceDeclaration
.
Span
]
=
namespaceDeclaration
;
}
else
if
(
compilationUnitOrNamespaceDeclOpt
is
TCompilationUnitSyntax
)
var
spansForNamespaceDeclaration
=
PooledDictionary
<
TextSpan
,
TNamespaceDeclarationSyntax
>.
GetInstance
();
try
{
foreach
(
var
document
in
documents
)
{
// In case there's no namespace declaration in the document, we used an empty span as key,
// since a valid namespace declaration node can't have zero length.
spansForNamespaceDeclaration
[
default
]
=
null
;
var
compilationUnitOrNamespaceDeclOpt
=
await
service
.
ShouldPositionTriggerRefactoringAsync
(
document
,
textSpan
.
Start
,
cancellationToken
)
.
ConfigureAwait
(
false
);
if
(
compilationUnitOrNamespaceDeclOpt
is
TNamespaceDeclarationSyntax
namespaceDeclaration
)
{
spansForNamespaceDeclaration
[
namespaceDeclaration
.
Span
]
=
namespaceDeclaration
;
}
else
if
(
compilationUnitOrNamespaceDeclOpt
is
TCompilationUnitSyntax
)
{
// In case there's no namespace declaration in the document, we used an empty span as key,
// since a valid namespace declaration node can't have zero length.
spansForNamespaceDeclaration
[
default
]
=
null
;
}
else
{
return
default
;
}
}
else
if
(
spansForNamespaceDeclaration
.
Count
!=
1
)
{
return
default
;
}
}
if
(
spansForNamespaceDeclaration
.
Count
!=
1
)
var
namespaceDecl
=
spansForNamespaceDeclaration
.
Values
.
Single
();
var
declaredNamespace
=
namespaceDecl
==
null
// namespaceDecl == null means the target namespace is global namespace.
?
string
.
Empty
// Since the node in each document has identical type and span,
// they should have same name.
:
SyntaxGenerator
.
GetGenerator
(
documents
.
First
()).
GetName
(
namespaceDecl
);
return
(
true
,
declaredNamespace
);
}
finally
{
return
default
;
spansForNamespaceDeclaration
.
Free
()
;
}
var
namespaceDecl
=
spansForNamespaceDeclaration
.
Values
.
Single
();
var
declaredNamespace
=
namespaceDecl
==
null
// namespaceDecl == null means the target namespace is global namespace.
?
string
.
Empty
// Since the node in each document has identical type and span,
// they should have same name.
:
SyntaxGenerator
.
GetGenerator
(
documents
.
First
()).
GetName
(
namespaceDecl
);
return
(
true
,
declaredNamespace
);
}
public
static
async
Task
<
State
>
CreateAsync
(
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录