Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Self-versioned documents #10747

Merged
merged 18 commits into from
Aug 27, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ internal class DocumentSnapshot : IDocumentSnapshot
public IProjectSnapshot Project => ProjectInternal;
public bool SupportsOutput => true;

public int Version => State.Version;

public ProjectSnapshot ProjectInternal { get; }
public DocumentState State { get; }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,34 +28,51 @@ internal partial class DocumentState
private Task<TextAndVersion>? _loaderTask;
private SourceText? _sourceText;
private VersionStamp? _version;
private readonly int _numericVersion;
davidwengier marked this conversation as resolved.
Show resolved Hide resolved

public static DocumentState Create(
HostDocument hostDocument,
int numericVersion,
Func<Task<TextAndVersion>>? loader)
{
if (hostDocument is null)
{
throw new ArgumentNullException(nameof(hostDocument));
}

return new DocumentState(hostDocument, null, null, loader);
return new DocumentState(hostDocument, null, null, numericVersion, loader);
}

public static DocumentState Create(
HostDocument hostDocument,
Func<Task<TextAndVersion>>? loader)
{
if (hostDocument is null)
{
throw new ArgumentNullException(nameof(hostDocument));
}

return new DocumentState(hostDocument, null, null, 1, loader);
}

// Internal for testing
internal DocumentState(
HostDocument hostDocument,
SourceText? text,
VersionStamp? version,
int numericVersion,
Func<Task<TextAndVersion>>? loader)
{
HostDocument = hostDocument;
_sourceText = text;
_version = version;
_numericVersion = numericVersion;
_loader = loader ?? EmptyLoader;
_lock = new object();
}

public HostDocument HostDocument { get; }
public int Version => _numericVersion;

public bool IsGeneratedOutputResultAvailable => ComputedState.IsResultAvailable == true;

Expand Down Expand Up @@ -152,12 +169,12 @@ public bool TryGetTextVersion(out VersionStamp result)

public virtual DocumentState WithConfigurationChange()
{
var state = new DocumentState(HostDocument, _sourceText, _version, _loader)
var state = new DocumentState(HostDocument, _sourceText, _version, _numericVersion + 1, _loader)
{
// The source could not have possibly changed.
_sourceText = _sourceText,
_version = _version,
_loaderTask = _loaderTask
_loaderTask = _loaderTask,
};

// Do not cache computed state
Expand All @@ -167,7 +184,7 @@ public virtual DocumentState WithConfigurationChange()

public virtual DocumentState WithImportsChange()
{
var state = new DocumentState(HostDocument, _sourceText, _version, _loader)
var state = new DocumentState(HostDocument, _sourceText, _version, _numericVersion + 1, _loader)
{
// The source could not have possibly changed.
_sourceText = _sourceText,
Expand All @@ -183,7 +200,7 @@ public virtual DocumentState WithImportsChange()

public virtual DocumentState WithProjectWorkspaceStateChange()
{
var state = new DocumentState(HostDocument, _sourceText, _version, _loader)
var state = new DocumentState(HostDocument, _sourceText, _version, _numericVersion + 1, _loader)
{
// The source could not have possibly changed.
_sourceText = _sourceText,
Expand All @@ -206,7 +223,7 @@ public virtual DocumentState WithText(SourceText sourceText, VersionStamp versio

// Do not cache the computed state

return new DocumentState(HostDocument, sourceText, version, null);
return new DocumentState(HostDocument, sourceText, version, _numericVersion + 1, null);
}

public virtual DocumentState WithTextLoader(Func<Task<TextAndVersion>> loader)
Expand All @@ -218,7 +235,7 @@ public virtual DocumentState WithTextLoader(Func<Task<TextAndVersion>> loader)

// Do not cache the computed state

return new DocumentState(HostDocument, null, null, loader);
return new DocumentState(HostDocument, null, null, _numericVersion + 1, loader);
}

// Internal, because we are temporarily sharing code with CohostDocumentSnapshot
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ internal interface IDocumentSnapshot
IProjectSnapshot Project { get; }
bool SupportsOutput { get; }

int Version { get; }
davidwengier marked this conversation as resolved.
Show resolved Hide resolved

Task<SourceText> GetTextAsync();
Task<VersionStamp> GetTextVersionAsync();
Task<RazorCodeDocument> GetGeneratedOutputAsync();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ public ImportDocumentSnapshot(IProjectSnapshot project, RazorProjectItem item)
_version = VersionStamp.Default;
}

public int Version => 1;

public Task<RazorCodeDocument> GetGeneratedOutputAsync()
=> throw new NotSupportedException();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ public ProjectState WithAddedHostDocument(HostDocument hostDocument, Func<Task<T
return this;
}

var documents = Documents.Add(hostDocument.FilePath, DocumentState.Create(hostDocument, loader));
var documents = Documents.Add(hostDocument.FilePath, DocumentState.Create(hostDocument, 1, loader));

// Compute the effect on the import map
var importTargetPaths = GetImportDocumentTargetPaths(hostDocument);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ internal class RemoteDocumentSnapshot(TextDocument textDocument, RemoteProjectSn

public bool SupportsOutput => true;

public int Version => throw new NotImplementedException("We don't expect to use this in cohosting because we do not control generated document creation.");

public Task<SourceText> GetTextAsync() => _textDocument.GetTextAsync();

public Task<VersionStamp> GetTextVersionAsync() => _textDocument.GetTextVersionAsync();
Expand Down