提交 4a2cd9cf 编写于 作者: G Gen Lu

Exclude changes to unchangeable documents when applied in VS workspace

上级 feddb18e
......@@ -72,6 +72,8 @@ protected override void OnDocumentClosing(DocumentId documentId)
}
}
internal override bool IgnoreUnchangeableDocumentsWhenApplyingChanges => true;
/// <summary>
/// Returns the hierarchy for a given project.
/// </summary>
......
......@@ -378,7 +378,8 @@ public override EnvDTE.FileCodeModel GetFileCodeModel(DocumentId documentId)
var projectChanges = newSolution.GetChanges(currentSolution).GetProjectChanges().ToList();
// first make sure we can edit the document we will be updating (check them out from source control, etc)
var changedDocs = projectChanges.SelectMany(pd => pd.GetChangedDocuments(true).Concat(pd.GetChangedAdditionalDocuments())).Where(CanApplyChange).ToList();
var changedDocs = projectChanges.SelectMany(pd => pd.GetChangedDocuments(onlyGetDocumentsWithTextChanges: true).Concat(pd.GetChangedAdditionalDocuments()))
.Where(CanApplyChange).ToList();
if (changedDocs.Count > 0)
{
this.EnsureEditableDocuments(changedDocs);
......
......@@ -225,7 +225,7 @@ protected async Task<Solution> PostProcessChangesAsync(Solution changedSolution,
// process changed projects
foreach (var projectChanges in solutionChanges.GetProjectChanges())
{
var documentsToProcess = projectChanges.GetChangedDocuments(true).Concat(
var documentsToProcess = projectChanges.GetChangedDocuments(onlyGetDocumentsWithTextChanges: true).Concat(
projectChanges.GetAddedDocuments());
foreach (var documentId in documentsToProcess)
......
......@@ -132,9 +132,7 @@ public IEnumerable<DocumentId> GetAddedAnalyzerConfigDocuments()
/// </summary>
/// <returns></returns>
public IEnumerable<DocumentId> GetChangedDocuments()
{
return GetChangedDocuments(false);
}
=> GetChangedDocuments(onlyGetDocumentsWithTextChanges: false, ignoreUnchangeableDocuments: false);
/// <summary>
/// Get Changed Documents:
......@@ -145,6 +143,9 @@ public IEnumerable<DocumentId> GetChangedDocuments()
/// <param name="onlyGetDocumentsWithTextChanges"></param>
/// <returns></returns>
public IEnumerable<DocumentId> GetChangedDocuments(bool onlyGetDocumentsWithTextChanges)
=> GetChangedDocuments(onlyGetDocumentsWithTextChanges, ignoreUnchangeableDocuments: false);
internal IEnumerable<DocumentId> GetChangedDocuments(bool onlyGetDocumentsWithTextChanges, bool ignoreUnchangeableDocuments)
{
foreach (var id in _newProject.DocumentIds)
{
......@@ -155,7 +156,7 @@ public IEnumerable<DocumentId> GetChangedDocuments(bool onlyGetDocumentsWithText
{
if (onlyGetDocumentsWithTextChanges)
{
if (newState.HasTextChanged(oldState))
if (newState.HasTextChanged(oldState, ignoreUnchangeableDocuments))
yield return id;
}
else
......
......@@ -129,9 +129,9 @@ internal virtual bool HasInfoChanged(TextDocument otherTextDocument)
/// <summary>
/// True if the text of the document change.
/// </summary>
internal bool HasTextChanged(TextDocument otherTextDocument)
internal bool HasTextChanged(TextDocument otherTextDocument, bool ignoreUnchangeableDocument)
{
return State.HasTextChanged(otherTextDocument.State);
return State.HasTextChanged(otherTextDocument.State, ignoreUnchangeableDocument);
}
}
}
......@@ -375,10 +375,14 @@ public virtual async Task<VersionStamp> GetTopLevelChangeTextVersionAsync(Cancel
return textAndVersion.Version;
}
public bool HasTextChanged(TextDocumentState oldState)
public bool HasTextChanged(TextDocumentState oldState, bool ignoreUnchangeableDocument)
{
return oldState.sourceText != sourceText
|| oldState.TextAndVersionSource != TextAndVersionSource;
if (ignoreUnchangeableDocument && !oldState.CanApplyChange())
{
return false;
}
return oldState.TextAndVersionSource != TextAndVersionSource;
}
public bool HasInfoChanged(TextDocumentState oldState)
......
......@@ -52,6 +52,8 @@ public abstract partial class Workspace : IDisposable
internal bool TestHookPartialSolutionsDisabled { get; set; }
internal virtual bool IgnoreUnchangeableDocumentsWhenApplyingChanges { get; } = false;
private Action<string>? _testMessageLogger;
/// <summary>
......@@ -1231,12 +1233,23 @@ private void CheckAllowedProjectChanges(ProjectChanges projectChanges)
throw new NotSupportedException(WorkspacesResources.Changing_document_property_is_not_supported);
}
if (!this.CanApplyChange(ApplyChangesKind.ChangeDocument)
&& projectChanges.GetChangedDocuments(onlyGetDocumentsWithTextChanges: true).Any())
var changedDocumentIds = projectChanges.GetChangedDocuments(onlyGetDocumentsWithTextChanges: true, IgnoreUnchangeableDocumentsWhenApplyingChanges).ToImmutableArray();
if (!this.CanApplyChange(ApplyChangesKind.ChangeDocument) && changedDocumentIds.Length > 0)
{
throw new NotSupportedException(WorkspacesResources.Changing_documents_is_not_supported);
}
// Checking for unchangeable documents will only be done if we were asked not to ignore them.
foreach (var documentId in changedDocumentIds)
{
var document = projectChanges.OldProject.GetDocumentState(documentId) ?? projectChanges.NewProject.GetDocumentState(documentId)!;
if (!document.CanApplyChange())
{
throw new NotSupportedException(string.Format(WorkspacesResources.Changing_document_0_is_not_supported, document.FilePath ?? document.Name));
}
}
if (projectChanges.GetAddedAdditionalDocuments().Any() && !this.CanApplyChange(ApplyChangesKind.AddAdditionalDocument))
{
throw new NotSupportedException(WorkspacesResources.Adding_additional_documents_is_not_supported);
......@@ -1296,15 +1309,6 @@ private void CheckAllowedProjectChanges(ProjectChanges projectChanges)
{
throw new NotSupportedException(WorkspacesResources.Removing_analyzer_references_is_not_supported);
}
foreach (var documentId in projectChanges.GetChangedDocuments())
{
var document = projectChanges.OldProject.GetDocumentState(documentId) ?? projectChanges.NewProject.GetDocumentState(documentId)!;
if (!document.CanApplyChange())
{
throw new NotSupportedException(string.Format(WorkspacesResources.Changing_document_0_is_not_supported, document.FilePath ?? document.Name));
}
}
}
protected virtual bool CanApplyCompilationOptionChange(CompilationOptions oldOptions, CompilationOptions newOptions, Project project)
......@@ -1452,8 +1456,8 @@ protected virtual void ApplyProjectChanges(ProjectChanges projectChanges)
var oldDoc = projectChanges.OldProject.GetDocument(documentId)!;
var newDoc = projectChanges.NewProject.GetDocument(documentId)!;
// update text if changed
if (newDoc.HasTextChanged(oldDoc))
// update text if it's changed (unless it's unchangeable and we were asked to exclude them)
if (newDoc.HasTextChanged(oldDoc, IgnoreUnchangeableDocumentsWhenApplyingChanges))
{
// What we'd like to do here is figure out what actual text changes occurred and pass them on to the host.
// However, since it is likely that the change was done by replacing the syntax tree, getting the actual text changes is non trivial.
......
......@@ -70,7 +70,7 @@ public static IEnumerable<DocumentId> GetTextChangedDocuments(Solution oldSoluti
var projectsDifference = GetChangedProjectChanges(oldSolution, newSolution);
foreach (var projectDifference in projectsDifference)
{
changedDocuments.AddRange(projectDifference.GetChangedDocuments(true));
changedDocuments.AddRange(projectDifference.GetChangedDocuments(onlyGetDocumentsWithTextChanges: true));
}
return changedDocuments;
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册