Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
lwm1986
roslyn
提交
b086c6f4
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,发现更多精彩内容 >>
提交
b086c6f4
编写于
6月 20, 2019
作者:
P
Petr Houška
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Add suport for TextSpan stripping & wire it within refactoring.
上级
3ceb7233
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
131 addition
and
16 deletion
+131
-16
src/EditorFeatures/CSharpTest/CodeActions/ConvertLocalFunctionToMethod/ConvertLocalFunctionToMethodTests.cs
...ocalFunctionToMethod/ConvertLocalFunctionToMethodTests.cs
+75
-3
src/Features/CSharp/Portable/CodeRefactorings/ConvertLocalFunctionToMethod/CSharpConvertLocalFunctionToMethodCodeRefactoringProvider.cs
...arpConvertLocalFunctionToMethodCodeRefactoringProvider.cs
+9
-2
src/Features/Core/Portable/CodeRefactoringHelpers.cs
src/Features/Core/Portable/CodeRefactoringHelpers.cs
+47
-11
未找到文件。
src/EditorFeatures/CSharpTest/CodeActions/ConvertLocalFunctionToMethod/ConvertLocalFunctionToMethodTests.cs
浏览文件 @
b086c6f4
...
...
@@ -555,7 +555,7 @@ void M()
}
[
Fact
,
Trait
(
Traits
.
Feature
,
Traits
.
Features
.
CodeActionsConvertLocalFunctionToMethod
)]
public
async
Task
Test
WholeMethod
Selection1
()
public
async
Task
Test
MethodBlock
Selection1
()
{
await
TestInRegularAndScriptAsync
(
@"class C
...
...
@@ -582,7 +582,7 @@ private static C LocalFunction(C c)
}
[
Fact
,
Trait
(
Traits
.
Feature
,
Traits
.
Features
.
CodeActionsConvertLocalFunctionToMethod
)]
public
async
Task
Test
WholeMethod
Selection2
()
public
async
Task
Test
MethodBlock
Selection2
()
{
await
TestInRegularAndScriptAsync
(
...
...
@@ -610,7 +610,7 @@ private static C LocalFunction(C c)
}
[
Fact
,
Trait
(
Traits
.
Feature
,
Traits
.
Features
.
CodeActionsConvertLocalFunctionToMethod
)]
public
async
Task
Test
WholeMethod
Selection3
()
public
async
Task
Test
MethodBlock
Selection3
()
{
await
this
.
TestMissingAsync
(
...
...
@@ -629,5 +629,77 @@ C LocalFunction(C c)
}"
);
}
[
Fact
,
Trait
(
Traits
.
Feature
,
Traits
.
Features
.
CodeActionsConvertLocalFunctionToMethod
)]
public
async
Task
TestMethodBlockSelection4
()
{
await
TestInRegularAndScriptAsync
(
@"class C
{
void M()
{
[|
C LocalFunction(C c)
{
return null;
}
|]
}
}"
,
@"class C
{
void M()
{
}
private static C LocalFunction(C c)
{
return null;
}
}"
);
}
[
Fact
,
Trait
(
Traits
.
Feature
,
Traits
.
Features
.
CodeActionsConvertLocalFunctionToMethod
)]
public
async
Task
TestMethodBlockSelection5
()
{
await
this
.
TestMissingAsync
(
@"class C
{
void M()
{
object a = null[|;
C LocalFunction(C c)
{
return null;
}|]
}
}"
);
}
[
Fact
,
Trait
(
Traits
.
Feature
,
Traits
.
Features
.
CodeActionsConvertLocalFunctionToMethod
)]
public
async
Task
TestMethodBlockSelection6
()
{
await
this
.
TestMissingAsync
(
@"class C
{
void M()
{
[|;
C LocalFunction(C c)
{
return null;
}
object|] a = null
}
}"
);
}
}
}
src/Features/CSharp/Portable/CodeRefactorings/ConvertLocalFunctionToMethod/CSharpConvertLocalFunctionToMethodCodeRefactoringProvider.cs
浏览文件 @
b086c6f4
...
...
@@ -41,9 +41,14 @@ public override async Task ComputeRefactoringsAsync(CodeRefactoringContext conte
}
var
cancellationToken
=
context
.
CancellationToken
;
var
root
=
await
document
.
GetSyntaxRootAsync
(
cancellationToken
).
ConfigureAwait
(
false
);
var
localFunction
=
await
CodeRefactoringHelpers
.
TryGetSelectedNode
<
LocalFunctionStatementSyntax
>(
context
.
Span
,
root
,
cancellationToken
).
ConfigureAwait
(
false
);
var
localFunction
=
await
CodeRefactoringHelpers
.
TryGetSelectedNode
<
LocalFunctionStatementSyntax
>(
document
,
context
.
Span
,
cancellationToken
).
ConfigureAwait
(
false
);
if
(
localFunction
==
default
)
{
var
block
=
await
CodeRefactoringHelpers
.
TryGetSelectedNode
<
BlockSyntax
>(
document
,
context
.
Span
,
cancellationToken
).
ConfigureAwait
(
false
);
localFunction
=
block
.
Parent
as
LocalFunctionStatementSyntax
;
}
if
(
localFunction
==
default
)
{
return
;
...
...
@@ -54,6 +59,8 @@ public override async Task ComputeRefactoringsAsync(CodeRefactoringContext conte
return
;
}
var
root
=
await
document
.
GetSyntaxRootAsync
(
cancellationToken
).
ConfigureAwait
(
false
);
context
.
RegisterRefactoring
(
new
MyCodeAction
(
CSharpFeaturesResources
.
Convert_to_method
,
c
=>
UpdateDocumentAsync
(
root
,
document
,
parentBlock
,
localFunction
,
c
)));
}
...
...
src/Features/Core/Portable/CodeRefactoringHelpers.cs
浏览文件 @
b086c6f4
...
...
@@ -21,19 +21,22 @@ internal static class CodeRefactoringHelpers
/// direct parent is of type <typeparamref name="TSyntaxNode"/> or if a Node of said type is the smallest Node containing
/// the whole <paramref name="selection"/>. Otherwise returns <code>null</code>.
/// </para>
/// <para>
/// Note: this function strips all whitespace from both the beginning and end given <paramref name="selection"/>
/// and the stripped selection to determine the relevant Node.
/// </para>
/// </summary>
public
static
async
Task
<
TSyntaxNode
>
TryGetSelectedNode
<
TSyntaxNode
>(
TextSpan
selection
,
SyntaxNode
root
,
CancellationToken
cancellationToken
)
where
TSyntaxNode
:
SyntaxNode
Document
document
,
TextSpan
selection
,
CancellationToken
cancellationToken
)
where
TSyntaxNode
:
SyntaxNode
{
var
root
=
await
document
.
GetSyntaxRootAsync
(
cancellationToken
).
ConfigureAwait
(
false
);
var
selectionStripped
=
await
GetStrippedTextSpan
(
document
,
selection
,
cancellationToken
).
ConfigureAwait
(
false
);
// TODO: Add handling selected whitespace before / after the same way as helper methods below -> refactor to public
// helper.
var
node
=
root
.
FindNode
(
selection
)
as
TSyntaxNode
;
var
node
=
root
.
FindNode
(
selectionStripped
)
as
TSyntaxNode
;
if
(
node
==
null
)
{
// e.g. "C LocalFunction[||](C c)" -> root.FindNode return ParameterList but we still want to return LocalFunctionNode
var
identifier
=
await
root
.
SyntaxTree
.
GetTouchingTokenAsync
(
selection
.
Start
,
var
identifier
=
await
root
.
SyntaxTree
.
GetTouchingTokenAsync
(
selection
Stripped
.
Start
,
token
=>
token
.
Parent
is
TSyntaxNode
,
cancellationToken
).
ConfigureAwait
(
false
);
node
=
identifier
.
Parent
as
TSyntaxNode
;
}
...
...
@@ -131,14 +134,19 @@ internal static class CodeRefactoringHelpers
return
true
;
}
private
static
async
Task
<
TextSpan
>
GetExpandedNodeSpan
(
private
static
Task
<
TextSpan
>
GetExpandedNodeSpan
(
Document
document
,
SyntaxNode
node
,
CancellationToken
cancellationToken
)
{
return
GetExpandedTextSpan
(
document
,
node
.
Span
,
cancellationToken
);
}
private
static
async
Task
<
TextSpan
>
GetExpandedTextSpan
(
Document
document
,
TextSpan
span
,
CancellationToken
cancellationToken
)
{
var
sourceText
=
await
document
.
GetTextAsync
(
cancellationToken
).
ConfigureAwait
(
false
);
var
nodeStartLine
=
sourceText
.
Lines
.
GetLineFromPosition
(
node
.
Span
Start
);
var
nodeStartLine
=
sourceText
.
Lines
.
GetLineFromPosition
(
span
.
Start
);
// Enable vertical selections that catch the previous line break and perhaps some whitespace.
if
(
nodeStartLine
.
LineNumber
!=
0
)
...
...
@@ -146,10 +154,10 @@ internal static class CodeRefactoringHelpers
nodeStartLine
=
sourceText
.
Lines
[
nodeStartLine
.
LineNumber
-
1
];
}
var
nodeEndLine
=
sourceText
.
Lines
.
GetLineFromPosition
(
node
.
S
pan
.
End
);
var
nodeEndLine
=
sourceText
.
Lines
.
GetLineFromPosition
(
s
pan
.
End
);
var
start
=
node
.
Span
Start
;
var
end
=
node
.
S
pan
.
End
;
var
start
=
span
.
Start
;
var
end
=
s
pan
.
End
;
while
(
start
>
nodeStartLine
.
Start
&&
char
.
IsWhiteSpace
(
sourceText
[
start
-
1
]))
{
...
...
@@ -163,5 +171,33 @@ internal static class CodeRefactoringHelpers
return
TextSpan
.
FromBounds
(
start
,
end
);
}
private
static
async
Task
<
TextSpan
>
GetStrippedTextSpan
(
Document
document
,
TextSpan
span
,
CancellationToken
cancellationToken
)
{
var
sourceText
=
await
document
.
GetTextAsync
(
cancellationToken
).
ConfigureAwait
(
false
);
var
start
=
span
.
Start
;
var
end
=
span
.
End
;
if
(
span
.
IsEmpty
)
{
return
span
;
}
while
(
start
<
end
&&
char
.
IsWhiteSpace
(
sourceText
[
start
]))
{
start
++;
}
while
(
start
<
end
&&
char
.
IsWhiteSpace
(
sourceText
[
end
-
1
])
&&
char
.
IsWhiteSpace
(
sourceText
[
end
]))
{
end
--;
}
return
TextSpan
.
FromBounds
(
start
,
end
);
}
}
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录