Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
lwm1986
roslyn
提交
9c182f5f
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,发现更多精彩内容 >>
提交
9c182f5f
编写于
7月 20, 2017
作者:
C
CyrusNajmabadi
提交者:
GitHub
7月 20, 2017
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #20994 from CyrusNajmabadi/removeUnusedVarWithDirectives
Preserve leading directives when removing an unused variable.
上级
41e3a476
3581b430
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
75 addition
and
12 deletion
+75
-12
src/EditorFeatures/CSharpTest/RemoveUnusedLocalFunction/RemoveUnusedLocalFunctionTests.cs
...moveUnusedLocalFunction/RemoveUnusedLocalFunctionTests.cs
+7
-7
src/EditorFeatures/CSharpTest/RemoveUnusedVariable/RemoveUnusedVariableTests.cs
...arpTest/RemoveUnusedVariable/RemoveUnusedVariableTests.cs
+56
-2
src/Features/Core/Portable/RemoveUnusedVariable/AbstractRemoveUnusedVariableCodeFixProvider.cs
...edVariable/AbstractRemoveUnusedVariableCodeFixProvider.cs
+12
-3
未找到文件。
src/EditorFeatures/CSharpTest/RemoveUnusedLocalFunction/RemoveUnusedLocalFunctionTest.cs
→
src/EditorFeatures/CSharpTest/RemoveUnusedLocalFunction/RemoveUnusedLocalFunctionTest
s
.cs
浏览文件 @
9c182f5f
...
...
@@ -10,12 +10,12 @@
namespace
Microsoft.CodeAnalysis.Editor.CSharp.UnitTests.RemoveUnusedLocalFunction
{
public
partial
class
RemoveUnused
VariableTest
:
AbstractCSharpDiagnosticProviderBasedUserDiagnosticTest
public
partial
class
RemoveUnused
LocalFunctionTests
:
AbstractCSharpDiagnosticProviderBasedUserDiagnosticTest
{
internal
override
(
DiagnosticAnalyzer
,
CodeFixProvider
)
CreateDiagnosticProviderAndFixer
(
Workspace
workspace
)
=>
(
null
,
new
CSharpRemoveUnusedLocalFunctionCodeFixProvider
());
[
Fact
,
Trait
(
Traits
.
Feature
,
Traits
.
Features
.
CodeActionsRemoveUnused
Variable
)]
[
Fact
,
Trait
(
Traits
.
Feature
,
Traits
.
Features
.
CodeActionsRemoveUnused
LocalFunction
)]
public
async
Task
RemoveUnusedLocalFunction
()
{
await
TestInRegularAndScriptAsync
(
...
...
@@ -34,7 +34,7 @@ void Method()
}"
);
}
[
Fact
,
Trait
(
Traits
.
Feature
,
Traits
.
Features
.
CodeActionsRemoveUnused
Variable
)]
[
Fact
,
Trait
(
Traits
.
Feature
,
Traits
.
Features
.
CodeActionsRemoveUnused
LocalFunction
)]
public
async
Task
RemoveUnusedLocalFunctionFixAll1
()
{
await
TestInRegularAndScriptAsync
(
...
...
@@ -54,7 +54,7 @@ void Method()
}"
);
}
[
Fact
,
Trait
(
Traits
.
Feature
,
Traits
.
Features
.
CodeActionsRemoveUnused
Variable
)]
[
Fact
,
Trait
(
Traits
.
Feature
,
Traits
.
Features
.
CodeActionsRemoveUnused
LocalFunction
)]
public
async
Task
RemoveUnusedLocalFunctionFixAll2
()
{
await
TestInRegularAndScriptAsync
(
...
...
@@ -74,7 +74,7 @@ void Method()
}"
);
}
[
Fact
,
Trait
(
Traits
.
Feature
,
Traits
.
Features
.
CodeActionsRemoveUnused
Variable
)]
[
Fact
,
Trait
(
Traits
.
Feature
,
Traits
.
Features
.
CodeActionsRemoveUnused
LocalFunction
)]
public
async
Task
RemoveUnusedLocalFunctionFixAll3
()
{
await
TestInRegularAndScriptAsync
(
...
...
@@ -93,7 +93,7 @@ void Method()
}"
);
}
[
Fact
,
Trait
(
Traits
.
Feature
,
Traits
.
Features
.
CodeActionsRemoveUnused
Variable
)]
[
Fact
,
Trait
(
Traits
.
Feature
,
Traits
.
Features
.
CodeActionsRemoveUnused
LocalFunction
)]
public
async
Task
RemoveUnusedLocalFunctionFixAll4
()
{
await
TestInRegularAndScriptAsync
(
...
...
@@ -112,4 +112,4 @@ void Method()
}"
);
}
}
}
\ No newline at end of file
}
src/EditorFeatures/CSharpTest/RemoveUnusedVariable/RemoveUnusedVariableTest.cs
→
src/EditorFeatures/CSharpTest/RemoveUnusedVariable/RemoveUnusedVariableTest
s
.cs
浏览文件 @
9c182f5f
...
...
@@ -10,7 +10,7 @@
namespace
Microsoft.CodeAnalysis.Editor.CSharp.UnitTests.RemoveUnusedVariable
{
public
partial
class
RemoveUnusedVariableTest
:
AbstractCSharpDiagnosticProviderBasedUserDiagnosticTest
public
partial
class
RemoveUnusedVariableTest
s
:
AbstractCSharpDiagnosticProviderBasedUserDiagnosticTest
{
internal
override
(
DiagnosticAnalyzer
,
CodeFixProvider
)
CreateDiagnosticProviderAndFixer
(
Workspace
workspace
)
=>
(
null
,
new
CSharpRemoveUnusedVariableCodeFixProvider
());
...
...
@@ -205,5 +205,59 @@ void Method()
}
}"
);
}
[
WorkItem
(
20987
,
"https://github.com/dotnet/roslyn/issues/20987"
)]
[
Fact
,
Trait
(
Traits
.
Feature
,
Traits
.
Features
.
CodeActionsRemoveUnusedVariable
)]
public
async
Task
LeadingDirectives
()
{
await
TestInRegularAndScriptAsync
(
@"
#define NET461
using System;
namespace ClassLibrary
{
public class Class1
{
public static string GetText()
{
#if NET461
return ""Hello from "" + Environment.OSVersion;
#elif NETSTANDARD1_4
return ""Hello from .NET Standard"";
#else
#error Unknown platform
#endif
int [|blah|] = 5;
}
}
}"
,
@"
#define NET461
using System;
namespace ClassLibrary
{
public class Class1
{
public static string GetText()
{
#if NET461
return ""Hello from "" + Environment.OSVersion;
#elif NETSTANDARD1_4
return ""Hello from .NET Standard"";
#else
#error Unknown platform
#endif
}
}
}"
,
ignoreTrivia
:
false
);
}
#if false
#endif
}
}
\ No newline at end of file
}
src/Features/Core/Portable/RemoveUnusedVariable/AbstractRemoveUnusedVariableCodeFixProvider.cs
浏览文件 @
9c182f5f
...
...
@@ -9,6 +9,7 @@
using
Microsoft.CodeAnalysis.CodeFixes
;
using
Microsoft.CodeAnalysis.Editing
;
using
Microsoft.CodeAnalysis.Formatting
;
using
Microsoft.CodeAnalysis.LanguageServices
;
using
Microsoft.CodeAnalysis.Shared.Extensions
;
using
Roslyn.Utilities
;
...
...
@@ -45,6 +46,7 @@ public async override Task RegisterCodeFixesAsync(CodeFixContext context)
protected
override
Task
FixAllAsync
(
Document
document
,
ImmutableArray
<
Diagnostic
>
diagnostics
,
SyntaxEditor
editor
,
CancellationToken
cancellationToken
)
{
var
syntaxFacts
=
document
.
GetLanguageService
<
ISyntaxFactsService
>();
var
root
=
editor
.
OriginalRoot
;
foreach
(
var
diagnostic
in
diagnostics
)
{
...
...
@@ -58,12 +60,19 @@ protected override Task FixAllAsync(Document document, ImmutableArray<Diagnostic
else
{
var
variableDeclarator
=
token
.
GetAncestor
<
TVariableDeclarator
>();
var
variableDeclarators
=
token
.
GetAncestor
<
TVariableDeclaration
>().
ChildNodes
().
Where
(
x
=>
x
is
TVariableDeclarator
);
if
(
variableDeclarators
.
Count
()
==
1
)
{
editor
.
RemoveNode
(
token
.
GetAncestor
<
TLocalDeclarationStatement
>());
var
localDeclaration
=
token
.
GetAncestor
<
TLocalDeclarationStatement
>();
var
removeOptions
=
SyntaxGenerator
.
DefaultRemoveOptions
;
if
(
localDeclaration
.
GetLeadingTrivia
().
Contains
(
t
=>
t
.
IsDirective
))
{
removeOptions
|=
SyntaxRemoveOptions
.
KeepLeadingTrivia
;
}
editor
.
RemoveNode
(
localDeclaration
,
removeOptions
);
}
else
if
(
variableDeclarators
.
Count
()
>
1
)
{
...
...
@@ -83,4 +92,4 @@ private class MyCodeAction : CodeAction.DocumentChangeAction
}
}
}
}
\ No newline at end of file
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录