Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
lwm1986
roslyn
提交
17a9ee46
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,发现更多精彩内容 >>
提交
17a9ee46
编写于
4月 03, 2020
作者:
M
Manish Vasani
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Port MisplacedUsingDirectives to shared layer
上级
13532741
变更
6
隐藏空白更改
内联
并排
Showing
6 changed file
with
30 addition
and
24 deletion
+30
-24
src/Analyzers/CSharp/Analyzers/CSharpAnalyzers.projitems
src/Analyzers/CSharp/Analyzers/CSharpAnalyzers.projitems
+1
-0
src/Analyzers/CSharp/Analyzers/MisplacedUsingDirectives/MisplacedUsingDirectivesDiagnosticAnalyzer.cs
...gDirectives/MisplacedUsingDirectivesDiagnosticAnalyzer.cs
+3
-3
src/Analyzers/CSharp/CodeFixes/CSharpCodeFixes.projitems
src/Analyzers/CSharp/CodeFixes/CSharpCodeFixes.projitems
+1
-0
src/Analyzers/CSharp/CodeFixes/MisplacedUsingDirectives/MisplacedUsingDirectivesCodeFixProvider.cs
...singDirectives/MisplacedUsingDirectivesCodeFixProvider.cs
+16
-7
src/Analyzers/CSharp/Tests/CSharpAnalyzers.UnitTests.projitems
...nalyzers/CSharp/Tests/CSharpAnalyzers.UnitTests.projitems
+1
-0
src/Analyzers/CSharp/Tests/MisplacedUsingDirectives/MisplacedUsingDirectivesCodeFixProviderTests.cs
...irectives/MisplacedUsingDirectivesCodeFixProviderTests.cs
+8
-14
未找到文件。
src/Analyzers/CSharp/Analyzers/CSharpAnalyzers.projitems
浏览文件 @
17a9ee46
...
...
@@ -24,6 +24,7 @@
<Compile
Include=
"$(MSBuildThisFileDirectory)ConvertSwitchStatementToExpression\ConvertSwitchStatementToExpressionDiagnosticAnalyzer.cs"
/>
<Compile
Include=
"$(MSBuildThisFileDirectory)FileHeaders\CSharpFileHeaderDiagnosticAnalyzer.cs"
/>
<Compile
Include=
"$(MSBuildThisFileDirectory)FileHeaders\CSharpFileHeaderHelper.cs"
/>
<Compile
Include=
"$(MSBuildThisFileDirectory)MisplacedUsingDirectives\MisplacedUsingDirectivesDiagnosticAnalyzer.cs"
/>
<Compile
Include=
"$(MSBuildThisFileDirectory)NamingStyle\CSharpNamingStyleDiagnosticAnalyzer.cs"
/>
<Compile
Include=
"$(MSBuildThisFileDirectory)OrderModifiers\CSharpOrderModifiersDiagnosticAnalyzer.cs"
/>
<Compile
Include=
"$(MSBuildThisFileDirectory)OrderModifiers\CSharpOrderModifiersHelper.cs"
/>
...
...
src/
Features/CSharp/Portable
/MisplacedUsingDirectives/MisplacedUsingDirectivesDiagnosticAnalyzer.cs
→
src/
Analyzers/CSharp/Analyzers
/MisplacedUsingDirectives/MisplacedUsingDirectivesDiagnosticAnalyzer.cs
浏览文件 @
17a9ee46
...
...
@@ -19,16 +19,16 @@ namespace Microsoft.CodeAnalysis.CSharp.MisplacedUsingDirectives
internal
sealed
class
MisplacedUsingDirectivesDiagnosticAnalyzer
:
AbstractBuiltInCodeStyleDiagnosticAnalyzer
{
private
static
readonly
LocalizableResourceString
s_localizableTitle
=
new
LocalizableResourceString
(
nameof
(
CSharp
FeaturesResources
.
Misplaced_using_directive
),
CSharpFeaturesResources
.
ResourceManager
,
typeof
(
CSharpFeature
sResources
));
nameof
(
CSharp
AnalyzersResources
.
Misplaced_using_directive
),
CSharpAnalyzersResources
.
ResourceManager
,
typeof
(
CSharpAnalyzer
sResources
));
private
static
readonly
LocalizableResourceString
s_localizableOutsideMessage
=
new
LocalizableResourceString
(
nameof
(
CSharp
FeaturesResources
.
Using_directives_must_be_placed_outside_of_a_namespace_declaration
),
CSharpFeaturesResources
.
ResourceManager
,
typeof
(
CSharpFeature
sResources
));
nameof
(
CSharp
AnalyzersResources
.
Using_directives_must_be_placed_outside_of_a_namespace_declaration
),
CSharpAnalyzersResources
.
ResourceManager
,
typeof
(
CSharpAnalyzer
sResources
));
private
static
readonly
DiagnosticDescriptor
s_outsideDiagnosticDescriptor
=
CreateDescriptorWithId
(
IDEDiagnosticIds
.
MoveMisplacedUsingDirectivesDiagnosticId
,
s_localizableTitle
,
s_localizableOutsideMessage
);
private
static
readonly
LocalizableResourceString
s_localizableInsideMessage
=
new
LocalizableResourceString
(
nameof
(
CSharp
FeaturesResources
.
Using_directives_must_be_placed_inside_of_a_namespace_declaration
),
CSharpFeaturesResources
.
ResourceManager
,
typeof
(
CSharpFeature
sResources
));
nameof
(
CSharp
AnalyzersResources
.
Using_directives_must_be_placed_inside_of_a_namespace_declaration
),
CSharpAnalyzersResources
.
ResourceManager
,
typeof
(
CSharpAnalyzer
sResources
));
private
static
readonly
DiagnosticDescriptor
s_insideDiagnosticDescriptor
=
CreateDescriptorWithId
(
IDEDiagnosticIds
.
MoveMisplacedUsingDirectivesDiagnosticId
,
s_localizableTitle
,
s_localizableInsideMessage
);
...
...
src/Analyzers/CSharp/CodeFixes/CSharpCodeFixes.projitems
浏览文件 @
17a9ee46
...
...
@@ -20,6 +20,7 @@
<Compile
Include=
"$(MSBuildThisFileDirectory)ConvertSwitchStatementToExpression\ConvertSwitchStatementToExpressionCodeFixProvider.Rewriter.cs"
/>
<Compile
Include=
"$(MSBuildThisFileDirectory)FileHeaders\CSharpFileHeaderCodeFixProvider.cs"
/>
<Compile
Include=
"$(MSBuildThisFileDirectory)MakeFieldReadonly\CSharpMakeFieldReadonlyCodeFixProvider.cs"
/>
<Compile
Include=
"$(MSBuildThisFileDirectory)MisplacedUsingDirectives\MisplacedUsingDirectivesCodeFixProvider.cs"
/>
<Compile
Include=
"$(MSBuildThisFileDirectory)OrderModifiers\CSharpOrderModifiersCodeFixProvider.cs"
/>
<Compile
Include=
"$(MSBuildThisFileDirectory)PopulateSwitch\CSharpPopulateSwitchExpressionCodeFixProvider.cs"
/>
<Compile
Include=
"$(MSBuildThisFileDirectory)PopulateSwitch\CSharpPopulateSwitchStatementCodeFixProvider.cs"
/>
...
...
src/
Features/CSharp/Portable
/MisplacedUsingDirectives/MisplacedUsingDirectivesCodeFixProvider.cs
→
src/
Analyzers/CSharp/CodeFixes
/MisplacedUsingDirectives/MisplacedUsingDirectivesCodeFixProvider.cs
浏览文件 @
17a9ee46
...
...
@@ -20,11 +20,15 @@
using
Microsoft.CodeAnalysis.Diagnostics
;
using
Microsoft.CodeAnalysis.Formatting
;
using
Microsoft.CodeAnalysis.LanguageServices
;
using
Microsoft.CodeAnalysis.Options
;
using
Microsoft.CodeAnalysis.Shared.Extensions
;
using
Microsoft.CodeAnalysis.Simplification
;
using
Roslyn.Utilities
;
using
static
Microsoft
.
CodeAnalysis
.
CodeActions
.
CodeAction
;
#if CODE_STYLE
using
OptionSet
=
Microsoft
.
CodeAnalysis
.
Diagnostics
.
AnalyzerConfigOptions
;
#else
using
Microsoft.CodeAnalysis.Options
;
#endif
namespace
Microsoft.CodeAnalysis.CSharp.MisplacedUsingDirectives
{
...
...
@@ -58,7 +62,12 @@ public override async Task RegisterCodeFixesAsync(CodeFixContext context)
var
syntaxRoot
=
await
document
.
GetSyntaxRootAsync
(
cancellationToken
).
ConfigureAwait
(
false
);
var
compilationUnit
=
(
CompilationUnitSyntax
)
syntaxRoot
;
#if CODE_STYLE
var
options
=
document
.
Project
.
AnalyzerOptions
.
GetAnalyzerOptionSet
(
syntaxRoot
.
SyntaxTree
,
cancellationToken
);
#else
var
options
=
await
document
.
GetOptionsAsync
(
cancellationToken
).
ConfigureAwait
(
false
);
#endif
// Read the preferred placement option and verify if it can be applied to this code file.
// There are cases where we will not be able to fix the diagnostic and the user will need to resolve
...
...
@@ -72,7 +81,7 @@ public override async Task RegisterCodeFixesAsync(CodeFixContext context)
foreach
(
var
diagnostic
in
context
.
Diagnostics
)
{
context
.
RegisterCodeFix
(
new
MoveMisplacedUsingsCodeAction
(
token
=>
GetTransformedDocumentAsync
(
document
,
compilationUnit
,
options
,
placement
,
token
)),
new
MoveMisplacedUsingsCodeAction
(
token
=>
GetTransformedDocumentAsync
(
document
,
compilationUnit
,
placement
,
token
)),
diagnostic
);
}
}
...
...
@@ -80,7 +89,6 @@ public override async Task RegisterCodeFixesAsync(CodeFixContext context)
private
static
async
Task
<
Document
>
GetTransformedDocumentAsync
(
Document
document
,
CompilationUnitSyntax
compilationUnit
,
OptionSet
options
,
AddImportPlacement
placement
,
CancellationToken
cancellationToken
)
{
...
...
@@ -93,7 +101,7 @@ public override async Task RegisterCodeFixesAsync(CodeFixContext context)
var
(
compilationUnitWithoutHeader
,
fileHeader
)
=
RemoveFileHeader
(
compilationUnitWithExpandedUsings
,
syntaxFactsService
);
// A blanket warning that this codefix may change code so that it does not compile.
var
warningAnnotation
=
WarningAnnotation
.
Create
(
CSharp
Feature
sResources
.
Warning_colon_Moving_using_directives_may_change_code_meaning
);
var
warningAnnotation
=
WarningAnnotation
.
Create
(
CSharp
Analyzer
sResources
.
Warning_colon_Moving_using_directives_may_change_code_meaning
);
var
newCompilationUnit
=
placement
==
AddImportPlacement
.
InsideNamespace
?
MoveUsingsInsideNamespace
(
compilationUnitWithoutHeader
,
warningAnnotation
)
...
...
@@ -104,6 +112,7 @@ public override async Task RegisterCodeFixesAsync(CodeFixContext context)
var
newDocument
=
document
.
WithSyntaxRoot
(
newCompilationUnitWithHeader
);
// Simplify usings now that they have been moved and are in the proper context.
var
options
=
await
document
.
GetOptionsAsync
(
cancellationToken
).
ConfigureAwait
(
false
);
return
await
Simplifier
.
ReduceAsync
(
newDocument
,
Simplifier
.
Annotation
,
options
,
cancellationToken
).
ConfigureAwait
(
false
);
}
...
...
@@ -389,10 +398,10 @@ private static CompilationUnitSyntax AddFileHeader(CompilationUnitSyntax compila
return
compilationUnit
.
ReplaceToken
(
firstToken
,
newFirstToken
);
}
private
class
MoveMisplacedUsingsCodeAction
:
DocumentChangeAction
private
class
MoveMisplacedUsingsCodeAction
:
CustomCodeActions
.
DocumentChangeAction
{
public
MoveMisplacedUsingsCodeAction
(
Func
<
CancellationToken
,
Task
<
Document
>>
createChangedDocument
)
:
base
(
CSharp
Feature
sResources
.
Move_misplaced_using_directives
,
createChangedDocument
)
:
base
(
CSharp
Analyzer
sResources
.
Move_misplaced_using_directives
,
createChangedDocument
)
{
}
}
...
...
src/Analyzers/CSharp/Tests/CSharpAnalyzers.UnitTests.projitems
浏览文件 @
17a9ee46
...
...
@@ -15,6 +15,7 @@
<Compile
Include=
"$(MSBuildThisFileDirectory)AddRequiredParentheses\AddRequiredParenthesesTests.cs"
/>
<Compile
Include=
"$(MSBuildThisFileDirectory)ConvertAnonymousTypeToTuple\ConvertAnonymousTypeToTupleTests.cs"
/>
<Compile
Include=
"$(MSBuildThisFileDirectory)MakeFieldReadonly\MakeFieldReadonlyTests.cs"
/>
<Compile
Include=
"$(MSBuildThisFileDirectory)MisplacedUsingDirectives\MisplacedUsingDirectivesCodeFixProviderTests.cs"
/>
<Compile
Include=
"$(MSBuildThisFileDirectory)OrderModifiers\OrderModifiersCompilerErrorTests.cs"
/>
<Compile
Include=
"$(MSBuildThisFileDirectory)OrderModifiers\OrderModifiersTests.cs"
/>
<Compile
Include=
"$(MSBuildThisFileDirectory)RemoveUnreachableCode\RemoveUnreachableCodeTests.cs"
/>
...
...
src/
EditorFeatures/CSharpTest
/MisplacedUsingDirectives/MisplacedUsingDirectivesCodeFixProviderTests.cs
→
src/
Analyzers/CSharp/Tests
/MisplacedUsingDirectives/MisplacedUsingDirectivesCodeFixProviderTests.cs
浏览文件 @
17a9ee46
...
...
@@ -17,11 +17,11 @@
namespace
Microsoft.CodeAnalysis.Editor.CSharp.UnitTests.MisplacedUsingDirectives
{
/// <summary>
/// Unit tests for the <see cref="MisplacedUsingDirectivesDiagnosticAnalyzer"/> and <see cref="MisplacedUsingDirectivesCodeFixProvider"/>.
/// </summary>
public
class
MisplacedUsingDirectivesInCompilationUnitCodeFixProviderTests
:
AbstractCSharpDiagnosticProviderBasedUserDiagnosticTest
{
internal
override
(
DiagnosticAnalyzer
,
CodeFixProvider
)
CreateDiagnosticProviderAndFixer
(
Workspace
workspace
)
=>
(
new
MisplacedUsingDirectivesDiagnosticAnalyzer
(),
new
MisplacedUsingDirectivesCodeFixProvider
());
internal
static
readonly
CodeStyleOption2
<
AddImportPlacement
>
OutsidePreferPreservationOption
=
new
CodeStyleOption2
<
AddImportPlacement
>(
AddImportPlacement
.
OutsideNamespace
,
NotificationOption2
.
None
);
...
...
@@ -54,17 +54,14 @@ public class MisplacedUsingDirectivesInCompilationUnitCodeFixProviderTests : Abs
protected
const
string
DelegateDefinition
=
@"public delegate void TestDelegate();"
;
private
static
TestParameters
GetTestParameters
(
CodeStyleOption2
<
AddImportPlacement
>
preferredPlacementOption
)
=>
new
TestParameters
(
options
:
OptionsSet
((
new
OptionKey2
(
CSharpCodeStyleOptions
.
PreferredUsingDirectivePlacement
),
preferredPlacementOption
)));
private
protected
Task
TestDiagnosticMissingAsync
(
string
initialMarkup
,
CodeStyleOption2
<
AddImportPlacement
>
preferredPlacementOption
)
{
var
options
=
OptionsSet
(
CSharpCodeStyleOptions
.
PreferredUsingDirectivePlacement
,
preferredPlacementOption
);
return
TestDiagnosticMissingAsync
(
initialMarkup
,
new
TestParameters
(
options
:
options
));
}
=>
TestDiagnosticMissingAsync
(
initialMarkup
,
GetTestParameters
(
preferredPlacementOption
));
private
protected
Task
TestMissingAsync
(
string
initialMarkup
,
CodeStyleOption2
<
AddImportPlacement
>
preferredPlacementOption
)
{
var
options
=
OptionsSet
(
CSharpCodeStyleOptions
.
PreferredUsingDirectivePlacement
,
preferredPlacementOption
);
return
TestMissingAsync
(
initialMarkup
,
new
TestParameters
(
options
:
options
));
}
=>
TestMissingAsync
(
initialMarkup
,
GetTestParameters
(
preferredPlacementOption
));
private
protected
Task
TestInRegularAndScriptAsync
(
string
initialMarkup
,
string
expectedMarkup
,
CodeStyleOption2
<
AddImportPlacement
>
preferredPlacementOption
,
bool
placeSystemNamespaceFirst
)
{
...
...
@@ -76,9 +73,6 @@ private protected Task TestInRegularAndScriptAsync(string initialMarkup, string
return
TestInRegularAndScriptAsync
(
initialMarkup
,
expectedMarkup
,
options
:
options
);
}
internal
override
(
DiagnosticAnalyzer
,
CodeFixProvider
)
CreateDiagnosticProviderAndFixer
(
Workspace
workspace
)
=>
(
new
MisplacedUsingDirectivesDiagnosticAnalyzer
(),
new
MisplacedUsingDirectivesCodeFixProvider
());
#
region
Test
Preserve
/// <summary>
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录