Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
lwm1986
roslyn
提交
89e9b8b4
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,发现更多精彩内容 >>
提交
89e9b8b4
编写于
7月 06, 2018
作者:
C
Cyrus Najmabadi
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Properly show progress as we're performing cleanup.
上级
28688494
变更
7
隐藏空白更改
内联
并排
Showing
7 changed file
with
103 addition
and
26 deletion
+103
-26
src/EditorFeatures/Core/Implementation/Formatting/FormatCommandHandler.FormatDocument.cs
...ntation/Formatting/FormatCommandHandler.FormatDocument.cs
+21
-4
src/Features/CSharp/Portable/CodeCleanup/CSharpCodeCleanupService.cs
...s/CSharp/Portable/CodeCleanup/CSharpCodeCleanupService.cs
+47
-18
src/Features/Core/Portable/CodeCleanup/ICodeCleanupService.cs
...Features/Core/Portable/CodeCleanup/ICodeCleanupService.cs
+2
-1
src/Features/Core/Portable/FeaturesResources.Designer.cs
src/Features/Core/Portable/FeaturesResources.Designer.cs
+9
-0
src/Features/Core/Portable/FeaturesResources.resx
src/Features/Core/Portable/FeaturesResources.resx
+3
-0
src/Workspaces/Core/Portable/Shared/Utilities/IProgressTracker.cs
...spaces/Core/Portable/Shared/Utilities/IProgressTracker.cs
+1
-0
src/Workspaces/Core/Portable/Shared/Utilities/ProgressTracker.cs
...kspaces/Core/Portable/Shared/Utilities/ProgressTracker.cs
+20
-3
未找到文件。
src/EditorFeatures/Core/Implementation/Formatting/FormatCommandHandler.FormatDocument.cs
浏览文件 @
89e9b8b4
...
...
@@ -9,9 +9,11 @@
using
Microsoft.CodeAnalysis.Extensions
;
using
Microsoft.CodeAnalysis.Options
;
using
Microsoft.CodeAnalysis.Shared.Extensions
;
using
Microsoft.CodeAnalysis.Shared.Utilities
;
using
Microsoft.CodeAnalysis.Text
;
using
Microsoft.VisualStudio.Commanding
;
using
Microsoft.VisualStudio.Text.Editor.Commanding.Commands
;
using
Microsoft.VisualStudio.Utilities
;
using
Roslyn.Utilities
;
using
static
Microsoft
.
CodeAnalysis
.
Options
.
OptionServiceFactory
;
using
VSCommanding
=
Microsoft
.
VisualStudio
.
Commanding
;
...
...
@@ -74,7 +76,7 @@ public bool ExecuteCommand(FormatDocumentCommandArgs args, CommandExecutionConte
return
false
;
}
using
(
context
.
OperationContext
.
AddScope
(
allowCancellation
:
true
,
EditorFeaturesResources
.
Formatting_document
))
using
(
var
scope
=
context
.
OperationContext
.
AddScope
(
allowCancellation
:
true
,
EditorFeaturesResources
.
Formatting_document
))
{
var
cancellationToken
=
context
.
OperationContext
.
UserCancellationToken
;
...
...
@@ -95,7 +97,8 @@ public bool ExecuteCommand(FormatDocumentCommandArgs args, CommandExecutionConte
{
// Code cleanup
var
oldDoc
=
document
;
var
codeCleanupChanges
=
GetCodeCleanupAndFormatChangesAsync
(
document
,
codeCleanupService
,
cancellationToken
).
WaitAndGetResult
(
cancellationToken
);
var
codeCleanupChanges
=
GetCodeCleanupAndFormatChangesAsync
(
document
,
codeCleanupService
,
scope
,
cancellationToken
).
WaitAndGetResult
(
cancellationToken
);
if
(
codeCleanupChanges
!=
null
&&
codeCleanupChanges
.
Count
()
>
0
)
{
...
...
@@ -121,9 +124,23 @@ public bool ExecuteCommand(FormatDocumentCommandArgs args, CommandExecutionConte
return
true
;
}
private
async
Task
<
IEnumerable
<
TextChange
>>
GetCodeCleanupAndFormatChangesAsync
(
Document
document
,
ICodeCleanupService
codeCleanupService
,
CancellationToken
cancellationToken
)
private
async
Task
<
IEnumerable
<
TextChange
>>
GetCodeCleanupAndFormatChangesAsync
(
Document
document
,
ICodeCleanupService
codeCleanupService
,
IUIThreadOperationScope
scope
,
CancellationToken
cancellationToken
)
{
var
newDoc
=
await
codeCleanupService
.
CleanupAsync
(
document
,
cancellationToken
).
ConfigureAwait
(
false
);
var
progressTracker
=
new
ProgressTracker
((
desc
,
completed
,
total
)
=>
{
if
(
desc
!=
null
)
{
scope
.
Description
=
desc
;
if
(
total
!=
0
)
{
scope
.
Progress
.
Report
(
new
ProgressInfo
(
completed
,
total
));
}
}
});
var
newDoc
=
await
codeCleanupService
.
CleanupAsync
(
document
,
progressTracker
,
cancellationToken
).
ConfigureAwait
(
false
);
return
await
newDoc
.
GetTextChangesAsync
(
document
,
cancellationToken
).
ConfigureAwait
(
false
);
}
...
...
src/Features/CSharp/Portable/CodeCleanup/CSharpCodeCleanupService.cs
浏览文件 @
89e9b8b4
...
...
@@ -14,6 +14,7 @@
using
Microsoft.CodeAnalysis.Host.Mef
;
using
Microsoft.CodeAnalysis.Options
;
using
Microsoft.CodeAnalysis.OrganizeImports
;
using
Microsoft.CodeAnalysis.PooledObjects
;
using
Microsoft.CodeAnalysis.RemoveUnnecessaryImports
;
using
Microsoft.CodeAnalysis.Shared.Extensions
;
using
Microsoft.CodeAnalysis.Shared.Utilities
;
...
...
@@ -107,44 +108,69 @@ internal class CSharpCodeCleanupService : ICodeCleanupService
);
}
public
async
Task
<
Document
>
CleanupAsync
(
Document
document
,
CancellationToken
cancellationToken
)
public
async
Task
<
Document
>
CleanupAsync
(
Document
document
,
IProgressTracker
progressTracker
,
CancellationToken
cancellationToken
)
{
var
docOptions
=
await
document
.
GetOptionsAsync
(
cancellationToken
).
ConfigureAwait
(
false
);
// add one item for the 'Remove Usings', one for 'Sort using' and one item for the
// 'format' actions we'll do last
progressTracker
.
AddItems
(
3
);
if
(
_codeFixServiceOpt
!=
null
)
{
document
=
await
ApplyCodeFixesAsync
(
document
,
docOptions
,
cancellationToken
).
ConfigureAwait
(
false
);
document
=
await
ApplyCodeFixesAsync
(
document
,
progressTracker
,
docOptions
,
cancellationToken
).
ConfigureAwait
(
false
);
}
// do the remove usings after code fix, as code fix might remove some code which can results in unused usings.
document
=
await
RemoveSortUsingsAsync
(
document
,
docOptions
,
cancellationToken
).
ConfigureAwait
(
false
);
document
=
await
RemoveSortUsingsAsync
(
document
,
progressTracker
,
docOptions
,
cancellationToken
).
ConfigureAwait
(
false
);
progressTracker
.
Description
=
FeaturesResources
.
Formatting_document
;
var
result
=
await
Formatter
.
FormatAsync
(
document
).
ConfigureAwait
(
false
);
progressTracker
.
ItemCompleted
();
return
await
Formatter
.
FormatAsync
(
document
).
ConfigureAwait
(
false
)
;
return
result
;
}
private
async
Task
<
Document
>
RemoveSortUsingsAsync
(
Document
document
,
DocumentOptionSet
docOptions
,
CancellationToken
cancellationToken
)
private
async
Task
<
Document
>
RemoveSortUsingsAsync
(
Document
document
,
IProgressTracker
tracker
,
DocumentOptionSet
docOptions
,
CancellationToken
cancellationToken
)
{
// remove usings
if
(
docOptions
.
GetOption
(
CodeCleanupOptions
.
RemoveUnusedImports
))
{
var
removeUsingsService
=
document
.
GetLanguageService
<
IRemoveUnnecessaryImportsService
>();
if
(
removeUsingsService
!=
null
)
{
tracker
.
Description
=
CSharpFeaturesResources
.
Organize_Usings
;
document
=
await
removeUsingsService
.
RemoveUnnecessaryImportsAsync
(
document
,
cancellationToken
).
ConfigureAwait
(
false
);
}
}
// sort usings
// mark the remove usings command as done.
tracker
.
ItemCompleted
();
if
(
docOptions
.
GetOption
(
CodeCleanupOptions
.
SortImports
))
{
tracker
.
Description
=
CSharpFeaturesResources
.
Organize_Usings
;
document
=
await
OrganizeImportsService
.
OrganizeImportsAsync
(
document
,
cancellationToken
).
ConfigureAwait
(
false
);
}
// Mark the sort usings command as done.
tracker
.
ItemCompleted
();
return
document
;
}
private
async
Task
<
Document
>
ApplyCodeFixesAsync
(
Document
document
,
DocumentOptionSet
docOptions
,
CancellationToken
cancellationToken
)
private
async
Task
<
Document
>
ApplyCodeFixesAsync
(
Document
document
,
IProgressTracker
progressTracker
,
DocumentOptionSet
docOptions
,
CancellationToken
cancellationToken
)
{
var
diagnosticIDs
=
GetEnabledDiagnosticIds
(
docOptions
);
// Add a progress item for each diagnostic we're going to try to cleanup.
progressTracker
.
AddItems
(
diagnosticIDs
.
Length
);
var
fixAllService
=
document
.
Project
.
Solution
.
Workspace
.
Services
.
GetService
<
IFixAllGetFixesService
>();
var
dummy
=
new
ProgressTracker
();
...
...
@@ -156,24 +182,27 @@ private async Task<Document> ApplyCodeFixesAsync(Document document, DocumentOpti
var
textSpan
=
new
TextSpan
(
0
,
syntaxTree
.
Length
);
var
fixCollection
=
await
_codeFixServiceOpt
.
GetFixesAsync
(
document
,
textSpan
,
diagnosticId
,
cancellationToken
).
ConfigureAwait
(
false
);
if
(
fixCollection
==
null
)
if
(
fixCollection
!=
null
&&
fixCollection
.
Fixes
.
Length
>
0
)
{
continue
;
}
progressTracker
.
Description
=
fixCollection
.
Fixes
[
0
].
Action
.
Title
;
var
fixAll
=
fixCollection
.
FixAllState
;
var
solution
=
await
fixAllService
.
GetFixAllChangedSolutionAsync
(
fixAll
.
CreateFixAllContext
(
dummy
,
cancellationToken
)).
ConfigureAwait
(
false
);
var
fixAll
=
fixCollection
.
FixAllState
;
var
solution
=
await
fixAllService
.
GetFixAllChangedSolutionAsync
(
fixAll
.
CreateFixAllContext
(
dummy
,
cancellationToken
)).
ConfigureAwait
(
false
);
document
=
solution
.
GetDocument
(
document
.
Id
);
}
document
=
solution
.
GetDocument
(
document
.
Id
);
// Mark this progress item as being completed.
progressTracker
.
ItemCompleted
();
}
return
document
;
}
private
I
Enumerable
<
string
>
GetEnabledDiagnosticIds
(
DocumentOptionSet
docOptions
)
private
I
mmutableArray
<
string
>
GetEnabledDiagnosticIds
(
DocumentOptionSet
docOptions
)
{
var
diagnosticIds
=
new
List
<
string
>
();
var
diagnosticIds
=
ArrayBuilder
<
string
>.
GetInstance
();
foreach
(
var
tuple
in
_optionDiagnosticsMappings
)
{
...
...
@@ -183,7 +212,7 @@ private IEnumerable<string> GetEnabledDiagnosticIds(DocumentOptionSet docOptions
}
}
return
diagnosticIds
;
return
diagnosticIds
.
ToImmutableAndFree
()
;
}
}
}
src/Features/Core/Portable/CodeCleanup/ICodeCleanupService.cs
浏览文件 @
89e9b8b4
...
...
@@ -3,11 +3,12 @@
using
System.Threading
;
using
System.Threading.Tasks
;
using
Microsoft.CodeAnalysis.Host
;
using
Microsoft.CodeAnalysis.Shared.Utilities
;
namespace
Microsoft.CodeAnalysis.CodeCleanup
{
internal
interface
ICodeCleanupService
:
ILanguageService
{
Task
<
Document
>
CleanupAsync
(
Document
document
,
CancellationToken
cancellationToken
);
Task
<
Document
>
CleanupAsync
(
Document
document
,
IProgressTracker
progressTracker
,
CancellationToken
cancellationToken
);
}
}
src/Features/Core/Portable/FeaturesResources.Designer.cs
浏览文件 @
89e9b8b4
...
...
@@ -1314,6 +1314,15 @@ internal class FeaturesResources {
}
}
/// <summary>
/// Looks up a localized string similar to Formatting document.
/// </summary>
internal
static
string
Formatting_document
{
get
{
return
ResourceManager
.
GetString
(
"Formatting_document"
,
resourceCulture
);
}
}
/// <summary>
/// Looks up a localized string similar to from {0}.
/// </summary>
...
...
src/Features/Core/Portable/FeaturesResources.resx
浏览文件 @
89e9b8b4
...
...
@@ -1376,4 +1376,7 @@ This version used in: {2}</value>
<data
name=
"Convert_to_conditional_expression"
xml:space=
"preserve"
>
<value>
Convert to conditional expression
</value>
</data>
<data
name=
"Formatting_document"
xml:space=
"preserve"
>
<value>
Formatting document
</value>
</data>
</root>
\ No newline at end of file
src/Workspaces/Core/Portable/Shared/Utilities/IProgressTracker.cs
浏览文件 @
89e9b8b4
...
...
@@ -2,6 +2,7 @@
{
internal
interface
IProgressTracker
{
string
Description
{
get
;
set
;
}
int
CompletedItems
{
get
;
}
int
TotalItems
{
get
;
}
...
...
src/Workspaces/Core/Portable/Shared/Utilities/ProgressTracker.cs
浏览文件 @
89e9b8b4
...
...
@@ -10,21 +10,37 @@ namespace Microsoft.CodeAnalysis.Shared.Utilities
/// </summary>
internal
class
ProgressTracker
:
IProgressTracker
{
private
string
_description
;
private
int
_completedItems
;
private
int
_totalItems
;
private
readonly
Action
<
int
,
int
>
_updateActionOpt
;
private
readonly
Action
<
string
,
int
,
int
>
_updateActionOpt
;
public
ProgressTracker
()
:
this
(
null
)
:
this
(
default
(
Action
<
string
,
int
,
int
>)
)
{
}
public
ProgressTracker
(
Action
<
int
,
int
>
updateActionOpt
)
:
this
((
desc
,
completed
,
total
)
=>
updateActionOpt
?.
Invoke
(
completed
,
total
))
{
}
public
ProgressTracker
(
Action
<
string
,
int
,
int
>
updateActionOpt
)
{
_updateActionOpt
=
updateActionOpt
;
}
public
string
Description
{
get
=>
_description
;
set
{
_description
=
value
;
Update
();
}
}
public
int
CompletedItems
=>
_completedItems
;
public
int
TotalItems
=>
_totalItems
;
...
...
@@ -45,12 +61,13 @@ public void Clear()
{
_totalItems
=
0
;
_completedItems
=
0
;
_description
=
null
;
Update
();
}
private
void
Update
()
{
_updateActionOpt
?.
Invoke
(
_completedItems
,
_totalItems
);
_updateActionOpt
?.
Invoke
(
_
description
,
_
completedItems
,
_totalItems
);
}
}
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录