Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
lwm1986
roslyn
提交
bf904760
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,发现更多精彩内容 >>
提交
bf904760
编写于
5月 19, 2016
作者:
B
Bart Koelman
提交者:
Jonathon Marolf
5月 19, 2016
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Fixes duplicate namespace insertion (#9229)
* Fixes duplicate namespace insertion * Added test
上级
96f5ce2f
变更
2
显示空白变更内容
内联
并排
Showing
2 changed file
with
115 addition
and
1 deletion
+115
-1
src/Workspaces/CSharpTest/CodeGeneration/AddImportsTests.cs
src/Workspaces/CSharpTest/CodeGeneration/AddImportsTests.cs
+80
-0
src/Workspaces/Core/Portable/Editing/ImportAdderService.cs
src/Workspaces/Core/Portable/Editing/ImportAdderService.cs
+35
-1
未找到文件。
src/Workspaces/CSharpTest/CodeGeneration/AddImportsTests.cs
浏览文件 @
bf904760
// 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.Collections.Immutable
;
using
System.Linq
;
using
System.Threading
;
using
System.Threading.Tasks
;
using
Microsoft.CodeAnalysis.CSharp.Syntax
;
using
Microsoft.CodeAnalysis.Editing
;
using
Microsoft.CodeAnalysis.Formatting
;
using
Microsoft.CodeAnalysis.Options
;
...
...
@@ -476,5 +480,81 @@ class C
public List<int> F;
}"
);
}
[
Fact
]
[
WorkItem
(
9228
,
"https://github.com/dotnet/roslyn/issues/9228"
)]
public
async
Task
TestDoNotAddDuplicateImportIfNamespaceIsDefinedInSourceAndExternalAssembly
()
{
var
externalCode
=
@"namespace N.M { public class A : System.Attribute { } }"
;
var
code
=
@"using System;
using N.M;
class C
{
public void M1(String p1) { }
public void M2([A] String p2) { }
}"
;
var
otherAssemblyReference
=
GetInMemoryAssemblyReferenceForCode
(
externalCode
);
var
project
=
_emptyProject
.
AddMetadataReferences
(
new
[]
{
otherAssemblyReference
})
.
WithCompilationOptions
(
new
CSharpCompilationOptions
(
OutputKind
.
DynamicallyLinkedLibrary
));
project
=
project
.
AddDocument
(
"duplicate.cs"
,
externalCode
).
Project
;
var
document
=
project
.
AddDocument
(
"test.cs"
,
code
);
var
options
=
document
.
Project
.
Solution
.
Workspace
.
Options
;
var
compilation
=
await
document
.
Project
.
Solution
.
GetCompilationAsync
(
document
.
Project
,
CancellationToken
.
None
);
ImmutableArray
<
Diagnostic
>
compilerDiagnostics
=
compilation
.
GetDiagnostics
(
CancellationToken
.
None
);
Assert
.
Empty
(
compilerDiagnostics
.
Where
(
d
=>
d
.
Severity
==
DiagnosticSeverity
.
Error
));
var
attribute
=
compilation
.
GetTypeByMetadataName
(
"N.M.A"
);
var
syntaxRoot
=
await
document
.
GetSyntaxRootAsync
(
CancellationToken
.
None
).
ConfigureAwait
(
false
);
SyntaxNode
p1SyntaxNode
=
syntaxRoot
.
DescendantNodes
().
OfType
<
ParameterSyntax
>().
FirstOrDefault
();
// Add N.M.A attribute to p1.
var
editor
=
await
DocumentEditor
.
CreateAsync
(
document
,
CancellationToken
.
None
).
ConfigureAwait
(
false
);
SyntaxNode
attributeSyntax
=
editor
.
Generator
.
Attribute
(
editor
.
Generator
.
TypeExpression
(
attribute
));
editor
.
AddAttribute
(
p1SyntaxNode
,
attributeSyntax
);
Document
documentWithAttribute
=
editor
.
GetChangedDocument
();
// Add namespace import.
Document
imported
=
await
ImportAdder
.
AddImportsAsync
(
documentWithAttribute
,
null
,
CancellationToken
.
None
).
ConfigureAwait
(
false
);
var
formatted
=
await
Formatter
.
FormatAsync
(
imported
,
options
);
var
actualText
=
(
await
formatted
.
GetTextAsync
()).
ToString
();
Assert
.
Equal
(
actualText
,
@"using System;
using N.M;
class C
{
public void M1([global::N.M.A] String p1) { }
public void M2([A] String p2) { }
}"
);
}
private
static
MetadataReference
GetInMemoryAssemblyReferenceForCode
(
string
code
)
{
SyntaxTree
tree
=
CSharpSyntaxTree
.
ParseText
(
code
);
CSharpCompilation
compilation
=
CSharpCompilation
.
Create
(
"test.dll"
,
new
[]
{
tree
})
.
WithOptions
(
new
CSharpCompilationOptions
(
OutputKind
.
DynamicallyLinkedLibrary
))
.
AddReferences
(
TestReferences
.
NetFx
.
v4_0_30319
.
mscorlib
);
return
compilation
.
ToMetadataReference
();
}
}
}
src/Workspaces/Core/Portable/Editing/ImportAdderService.cs
浏览文件 @
bf904760
...
...
@@ -53,7 +53,7 @@ public async Task<Document> AddImportsAsync(Document document, IEnumerable<TextS
var
existingNamespaces
=
new
HashSet
<
INamespaceSymbol
>();
await
this
.
GetExistingImportedNamespacesAsync
(
document
,
model
,
existingNamespaces
,
cancellationToken
).
ConfigureAwait
(
false
);
var
namespacesToAdd
=
new
HashSet
<
INamespaceSymbol
>(
namespaces
);
var
namespacesToAdd
=
new
HashSet
<
INamespaceSymbol
>(
namespaces
,
NamespaceEqualityComparer
.
Singleton
);
namespacesToAdd
.
RemoveAll
(
existingNamespaces
);
var
root
=
await
model
.
SyntaxTree
.
GetRootAsync
(
cancellationToken
).
ConfigureAwait
(
false
);
...
...
@@ -95,5 +95,39 @@ public async Task<Document> AddImportsAsync(Document document, IEnumerable<TextS
protected
abstract
INamespaceSymbol
GetImportedNamespaceSymbol
(
SyntaxNode
import
,
SemanticModel
model
);
protected
abstract
INamespaceSymbol
GetExplicitNamespaceSymbol
(
SyntaxNode
node
,
SemanticModel
model
);
protected
abstract
SyntaxNode
InsertNamespaceImport
(
SyntaxNode
root
,
SyntaxGenerator
gen
,
SyntaxNode
import
,
OptionSet
options
);
private
sealed
class
NamespaceEqualityComparer
:
IEqualityComparer
<
INamespaceSymbol
>
{
public
static
readonly
NamespaceEqualityComparer
Singleton
=
new
NamespaceEqualityComparer
();
private
NamespaceEqualityComparer
()
{
}
public
bool
Equals
(
INamespaceSymbol
x
,
INamespaceSymbol
y
)
{
if
(
object
.
ReferenceEquals
(
x
,
y
))
{
return
true
;
}
if
(
object
.
ReferenceEquals
(
x
,
null
)
||
object
.
ReferenceEquals
(
y
,
null
))
{
return
false
;
}
return
x
.
ToDisplayString
(
SymbolDisplayFormat
.
FullyQualifiedFormat
)
==
y
.
ToDisplayString
(
SymbolDisplayFormat
.
FullyQualifiedFormat
);
}
public
int
GetHashCode
(
INamespaceSymbol
obj
)
{
if
(
object
.
ReferenceEquals
(
obj
,
null
))
{
return
0
;
}
return
obj
.
ToDisplayString
(
SymbolDisplayFormat
.
FullyQualifiedFormat
).
GetHashCode
();
}
}
}
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录