Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
lwm1986
roslyn
提交
ae9dfe17
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,发现更多精彩内容 >>
提交
ae9dfe17
编写于
11月 18, 2016
作者:
M
Matt Warren
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Add sync path for loading text
上级
5353525d
变更
8
隐藏空白更改
内联
并排
Showing
8 changed file
with
166 addition
and
10 deletion
+166
-10
src/VisualStudio/Core/Def/Implementation/Preview/PreviewUpdater.PreviewDialogWorkspace.cs
...entation/Preview/PreviewUpdater.PreviewDialogWorkspace.cs
+6
-1
src/Workspaces/Core/Desktop/Workspace/FileTextLoader.cs
src/Workspaces/Core/Desktop/Workspace/FileTextLoader.cs
+28
-0
src/Workspaces/Core/Portable/Workspace/Solution/AdditionalTextDocument.cs
...ore/Portable/Workspace/Solution/AdditionalTextDocument.cs
+1
-1
src/Workspaces/Core/Portable/Workspace/Solution/RecoverableTextAndVersion.cs
.../Portable/Workspace/Solution/RecoverableTextAndVersion.cs
+0
-4
src/Workspaces/Core/Portable/Workspace/Solution/TextDocumentState.cs
...ces/Core/Portable/Workspace/Solution/TextDocumentState.cs
+59
-3
src/Workspaces/Core/Portable/Workspace/Solution/TextLoader.cs
...Workspaces/Core/Portable/Workspace/Solution/TextLoader.cs
+20
-1
src/Workspaces/Core/Portable/Workspace/Workspace_Editor.cs
src/Workspaces/Core/Portable/Workspace/Workspace_Editor.cs
+8
-0
src/Workspaces/CoreTest/WorkspaceTests/MSBuildWorkspaceTests.cs
...rkspaces/CoreTest/WorkspaceTests/MSBuildWorkspaceTests.cs
+44
-0
未找到文件。
src/VisualStudio/Core/Def/Implementation/Preview/PreviewUpdater.PreviewDialogWorkspace.cs
浏览文件 @
ae9dfe17
...
...
@@ -66,7 +66,12 @@ internal PreviewTextLoader(SourceText documentText)
public
override
Task
<
TextAndVersion
>
LoadTextAndVersionAsync
(
Workspace
workspace
,
DocumentId
documentId
,
CancellationToken
cancellationToken
)
{
return
Task
.
FromResult
(
TextAndVersion
.
Create
(
_text
,
VersionStamp
.
Create
()));
return
Task
.
FromResult
(
LoadTextAndVersion
(
workspace
,
documentId
,
cancellationToken
));
}
internal
override
TextAndVersion
LoadTextAndVersion
(
Workspace
workspace
,
DocumentId
documentId
,
CancellationToken
cancellationToken
)
{
return
TextAndVersion
.
Create
(
_text
,
VersionStamp
.
Create
());
}
}
}
...
...
src/Workspaces/Core/Desktop/Workspace/FileTextLoader.cs
浏览文件 @
ae9dfe17
...
...
@@ -101,6 +101,34 @@ public override async Task<TextAndVersion> LoadTextAndVersionAsync(Workspace wor
return
textAndVersion
;
}
internal
override
TextAndVersion
LoadTextAndVersion
(
Workspace
workspace
,
DocumentId
documentId
,
CancellationToken
cancellationToken
)
{
DateTime
prevLastWriteTime
=
FileUtilities
.
GetFileTimeStamp
(
_path
);
TextAndVersion
textAndVersion
;
// Open file for reading with FileShare mode read/write/delete so that we do not lock this file.
using
(
var
stream
=
FileUtilities
.
RethrowExceptionsAsIOException
(()
=>
new
FileStream
(
_path
,
FileMode
.
Open
,
FileAccess
.
Read
,
FileShare
.
ReadWrite
|
FileShare
.
Delete
,
bufferSize
:
4096
,
useAsync
:
false
)))
{
var
version
=
VersionStamp
.
Create
(
prevLastWriteTime
);
var
text
=
CreateText
(
stream
,
workspace
);
textAndVersion
=
TextAndVersion
.
Create
(
text
,
version
,
_path
);
}
// Check if the file was definitely modified and closed while we were reading. In this case, we know the read we got was
// probably invalid, so throw an IOException which indicates to our caller that we should automatically attempt a re-read.
// If the file hasn't been closed yet and there's another writer, we will rely on file change notifications to notify us
// and reload the file.
DateTime
newLastWriteTime
=
FileUtilities
.
GetFileTimeStamp
(
_path
);
if
(!
newLastWriteTime
.
Equals
(
prevLastWriteTime
))
{
var
message
=
string
.
Format
(
WorkspacesResources
.
File_was_externally_modified_colon_0
,
_path
);
throw
new
IOException
(
message
);
}
return
textAndVersion
;
}
private
string
GetDebuggerDisplay
()
{
return
nameof
(
Path
)
+
" = "
+
Path
;
...
...
src/Workspaces/Core/Portable/Workspace/Solution/AdditionalTextDocument.cs
浏览文件 @
ae9dfe17
...
...
@@ -31,7 +31,7 @@ public AdditionalTextDocument(TextDocumentState document)
/// </summary>
public
override
SourceText
GetText
(
CancellationToken
cancellationToken
=
default
(
CancellationToken
))
{
var
text
=
_document
.
GetText
(
cancellationToken
);
var
text
=
_document
.
GetText
Sync
(
cancellationToken
);
return
text
;
}
}
...
...
src/Workspaces/Core/Portable/Workspace/Solution/RecoverableTextAndVersion.cs
浏览文件 @
ae9dfe17
...
...
@@ -119,10 +119,6 @@ private sealed class RecoverableText : RecoverableWeakValueSource<SourceText>
public
RecoverableText
(
RecoverableTextAndVersion
parent
,
SourceText
text
)
:
base
(
new
ConstantValueSource
<
SourceText
>(
text
))
{
// TODO: refactor recoverable text like recoverable tree so that
// we can have tree/node concept in recoverable text as well.
// basically tree is handle that can live in memory and node is
// data that come and go.
_parent
=
parent
;
}
...
...
src/Workspaces/Core/Portable/Workspace/Solution/TextDocumentState.cs
浏览文件 @
ae9dfe17
...
...
@@ -105,7 +105,9 @@ protected static ValueSource<TextAndVersion> CreateStrongText(TextAndVersion tex
protected
static
ValueSource
<
TextAndVersion
>
CreateStrongText
(
TextLoader
loader
,
DocumentId
documentId
,
SolutionServices
services
,
bool
reportInvalidDataException
)
{
return
new
AsyncLazy
<
TextAndVersion
>(
c
=>
LoadTextAsync
(
loader
,
documentId
,
services
,
reportInvalidDataException
,
c
),
cacheResult
:
true
);
asynchronousComputeFunction
:
c
=>
LoadTextAsync
(
loader
,
documentId
,
services
,
reportInvalidDataException
,
c
),
synchronousComputeFunction
:
c
=>
LoadTextSync
(
loader
,
documentId
,
services
,
reportInvalidDataException
,
c
),
cacheResult
:
true
);
}
protected
static
ValueSource
<
TextAndVersion
>
CreateRecoverableText
(
TextAndVersion
text
,
SolutionServices
services
)
...
...
@@ -116,7 +118,10 @@ protected static ValueSource<TextAndVersion> CreateRecoverableText(TextAndVersio
protected
static
ValueSource
<
TextAndVersion
>
CreateRecoverableText
(
TextLoader
loader
,
DocumentId
documentId
,
SolutionServices
services
,
bool
reportInvalidDataException
)
{
return
new
RecoverableTextAndVersion
(
new
AsyncLazy
<
TextAndVersion
>(
c
=>
LoadTextAsync
(
loader
,
documentId
,
services
,
reportInvalidDataException
,
c
),
cacheResult
:
false
),
new
AsyncLazy
<
TextAndVersion
>(
asynchronousComputeFunction
:
c
=>
LoadTextAsync
(
loader
,
documentId
,
services
,
reportInvalidDataException
,
c
),
synchronousComputeFunction
:
c
=>
LoadTextSync
(
loader
,
documentId
,
services
,
reportInvalidDataException
,
c
),
cacheResult
:
false
),
services
.
TemporaryStorage
);
}
...
...
@@ -169,6 +174,51 @@ protected static async Task<TextAndVersion> LoadTextAsync(TextLoader loader, Doc
}
}
protected
static
TextAndVersion
LoadTextSync
(
TextLoader
loader
,
DocumentId
documentId
,
SolutionServices
services
,
bool
reportInvalidDataException
,
CancellationToken
cancellationToken
)
{
int
retries
=
0
;
while
(
true
)
{
try
{
using
(
ExceptionHelpers
.
SuppressFailFast
())
{
var
result
=
loader
.
LoadTextAndVersion
(
services
.
Workspace
,
documentId
,
cancellationToken
);
return
result
;
}
}
catch
(
OperationCanceledException
)
{
// if load text is failed due to a cancellation, make sure we propagate it out to the caller
throw
;
}
catch
(
IOException
e
)
{
if
(++
retries
>
MaxRetries
)
{
services
.
Workspace
.
OnWorkspaceFailed
(
new
DocumentDiagnostic
(
WorkspaceDiagnosticKind
.
Failure
,
e
.
Message
,
documentId
));
return
TextAndVersion
.
Create
(
SourceText
.
From
(
string
.
Empty
,
Encoding
.
UTF8
),
VersionStamp
.
Default
,
documentId
.
GetDebuggerDisplay
());
}
// fall out to try again
}
catch
(
InvalidDataException
e
)
{
// TODO: Adjust this behavior in the future if we add support for non-text additional files
if
(
reportInvalidDataException
)
{
services
.
Workspace
.
OnWorkspaceFailed
(
new
DocumentDiagnostic
(
WorkspaceDiagnosticKind
.
Failure
,
e
.
Message
,
documentId
));
}
return
TextAndVersion
.
Create
(
SourceText
.
From
(
string
.
Empty
,
Encoding
.
UTF8
),
VersionStamp
.
Default
,
documentId
.
GetDebuggerDisplay
());
}
// try again after a delay
Task
.
Delay
(
RetryDelay
).
Wait
(
cancellationToken
);
}
}
public
ITemporaryTextStorage
Storage
{
get
...
...
@@ -237,12 +287,18 @@ public async Task<SourceText> GetTextAsync(CancellationToken cancellationToken)
return
textAndVersion
.
Text
;
}
public
SourceText
GetText
(
CancellationToken
cancellationToken
)
public
SourceText
GetText
Sync
(
CancellationToken
cancellationToken
)
{
var
textAndVersion
=
this
.
textAndVersionSource
.
GetValue
(
cancellationToken
);
return
textAndVersion
.
Text
;
}
public
VersionStamp
GetVersionSync
(
CancellationToken
cancellationToken
)
{
var
textAndVersion
=
this
.
textAndVersionSource
.
GetValue
(
cancellationToken
);
return
textAndVersion
.
Version
;
}
public
async
Task
<
VersionStamp
>
GetTextVersionAsync
(
CancellationToken
cancellationToken
)
{
// try fast path first
...
...
src/Workspaces/Core/Portable/Workspace/Solution/TextLoader.cs
浏览文件 @
ae9dfe17
...
...
@@ -4,6 +4,7 @@
using
System.Threading
;
using
System.Threading.Tasks
;
using
Microsoft.CodeAnalysis.Text
;
using
Roslyn.Utilities
;
namespace
Microsoft.CodeAnalysis
{
...
...
@@ -17,6 +18,14 @@ public abstract class TextLoader
/// </summary>
public
abstract
Task
<
TextAndVersion
>
LoadTextAndVersionAsync
(
Workspace
workspace
,
DocumentId
documentId
,
CancellationToken
cancellationToken
);
/// <summary>
/// Load a text and a version of the document in the workspace.
/// </summary>
internal
virtual
TextAndVersion
LoadTextAndVersion
(
Workspace
workspace
,
DocumentId
documentId
,
CancellationToken
cancellationToken
)
{
return
LoadTextAndVersionAsync
(
workspace
,
documentId
,
cancellationToken
).
WaitAndGetResult_CanCallOnBackground
(
cancellationToken
);
}
/// <summary>
/// Creates a new TextLoader from an already existing source text and version.
/// </summary>
...
...
@@ -59,6 +68,11 @@ public override Task<TextAndVersion> LoadTextAndVersionAsync(Workspace workspace
{
return
Task
.
FromResult
(
_textAndVersion
);
}
internal
override
TextAndVersion
LoadTextAndVersion
(
Workspace
workspace
,
DocumentId
documentId
,
CancellationToken
cancellationToken
)
{
return
_textAndVersion
;
}
}
private
class
TextContainerLoader
:
TextLoader
...
...
@@ -76,7 +90,12 @@ internal TextContainerLoader(SourceTextContainer container, VersionStamp version
public
override
Task
<
TextAndVersion
>
LoadTextAndVersionAsync
(
Workspace
workspace
,
DocumentId
documentId
,
CancellationToken
cancellationToken
)
{
return
Task
.
FromResult
(
TextAndVersion
.
Create
(
_container
.
CurrentText
,
_version
,
_filePath
));
return
Task
.
FromResult
(
LoadTextAndVersion
(
workspace
,
documentId
,
cancellationToken
));
}
internal
override
TextAndVersion
LoadTextAndVersion
(
Workspace
workspace
,
DocumentId
documentId
,
CancellationToken
cancellationToken
)
{
return
TextAndVersion
.
Create
(
_container
.
CurrentText
,
_version
,
_filePath
);
}
}
}
...
...
src/Workspaces/Core/Portable/Workspace/Workspace_Editor.cs
浏览文件 @
ae9dfe17
...
...
@@ -475,6 +475,14 @@ public ReuseVersionLoader(DocumentState oldDocumentState, SourceText newText)
return
GetProperTextAndVersion
(
oldText
,
_newText
,
version
,
_oldDocumentState
.
FilePath
);
}
internal
override
TextAndVersion
LoadTextAndVersion
(
Workspace
workspace
,
DocumentId
documentId
,
CancellationToken
cancellationToken
)
{
var
oldText
=
_oldDocumentState
.
GetTextSync
(
cancellationToken
);
var
version
=
_oldDocumentState
.
GetVersionSync
(
cancellationToken
);
return
GetProperTextAndVersion
(
oldText
,
_newText
,
version
,
_oldDocumentState
.
FilePath
);
}
}
private
static
TextAndVersion
GetProperTextAndVersion
(
SourceText
oldText
,
SourceText
newText
,
VersionStamp
version
,
string
filePath
)
...
...
src/Workspaces/CoreTest/WorkspaceTests/MSBuildWorkspaceTests.cs
浏览文件 @
ae9dfe17
...
...
@@ -2257,6 +2257,50 @@ public async Task TestAdditionalFilesStandalone()
}
}
[
Fact
,
Trait
(
Traits
.
Feature
,
Traits
.
Features
.
Workspace
)]
public
void
TestLoadTextSync
()
{
var
files
=
GetAnalyzerReferenceSolutionFiles
();
CreateFiles
(
files
);
using
(
var
ws
=
new
AdhocWorkspace
(
DesktopMefHostServices
.
DefaultServices
))
{
var
projectFullPath
=
Path
.
Combine
(
this
.
SolutionDirectory
.
Path
,
@"AnalyzerSolution\CSharpProject_AnalyzerReference.csproj"
);
var
loader
=
new
MSBuildProjectLoader
(
ws
);
var
infos
=
loader
.
LoadProjectInfoAsync
(
projectFullPath
).
Result
;
var
doc
=
infos
[
0
].
Documents
[
0
];
var
tav
=
doc
.
TextLoader
.
LoadTextAndVersion
(
ws
,
doc
.
Id
,
CancellationToken
.
None
);
var
adoc
=
infos
[
0
].
AdditionalDocuments
.
First
(
a
=>
a
.
Name
==
"XamlFile.xaml"
);
var
atav
=
adoc
.
TextLoader
.
LoadTextAndVersion
(
ws
,
adoc
.
Id
,
CancellationToken
.
None
);
Assert
.
Contains
(
"Window"
,
atav
.
Text
.
ToString
(),
StringComparison
.
Ordinal
);
}
}
[
Fact
,
Trait
(
Traits
.
Feature
,
Traits
.
Features
.
Workspace
)]
public
void
TestGetTextSync
()
{
var
files
=
GetAnalyzerReferenceSolutionFiles
();
CreateFiles
(
files
);
using
(
var
ws
=
MSBuildWorkspace
.
Create
())
{
var
projectFullPath
=
Path
.
Combine
(
this
.
SolutionDirectory
.
Path
,
@"AnalyzerSolution\CSharpProject_AnalyzerReference.csproj"
);
var
proj
=
ws
.
OpenProjectAsync
(
projectFullPath
).
Result
;
var
doc
=
proj
.
Documents
.
First
();
var
text
=
doc
.
State
.
GetTextSync
(
CancellationToken
.
None
);
var
adoc
=
proj
.
AdditionalDocuments
.
First
(
a
=>
a
.
Name
==
"XamlFile.xaml"
);
var
atext
=
adoc
.
State
.
GetTextSync
(
CancellationToken
.
None
);
Assert
.
Contains
(
"Window"
,
atext
.
ToString
(),
StringComparison
.
Ordinal
);
}
}
[
Fact
,
Trait
(
Traits
.
Feature
,
Traits
.
Features
.
Workspace
),
WorkItem
(
546171
,
"http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/546171"
)]
public
void
TestCSharpExternAlias
()
{
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录