ignore file changes not in workspace

This commit is contained in:
Matt Parker
2025-11-23 18:30:44 +10:00
parent 06cd3a0357
commit 91f19a0552
2 changed files with 15 additions and 6 deletions

View File

@@ -920,7 +920,8 @@ public class RoslynAnalysis(ILogger<RoslynAnalysis> logger, BuildService buildSe
return null; return null;
} }
public async Task UpdateDocument(SharpIdeFile fileModel, string newContent) // Returns true if the document was found and updated, otherwise false
public async Task<bool> UpdateDocument(SharpIdeFile fileModel, string newContent)
{ {
using var _ = SharpIdeOtel.Source.StartActivity($"{nameof(RoslynAnalysis)}.{nameof(UpdateDocument)}"); using var _ = SharpIdeOtel.Source.StartActivity($"{nameof(RoslynAnalysis)}.{nameof(UpdateDocument)}");
await _solutionLoadedTcs.Task; await _solutionLoadedTcs.Task;
@@ -937,7 +938,7 @@ public class RoslynAnalysis(ILogger<RoslynAnalysis> logger, BuildService buildSe
if (documentId is null) if (documentId is null)
{ {
_logger.LogWarning("UpdateDocument failed: Document '{DocumentPath}' not found in workspace", fileModel.Path); _logger.LogWarning("UpdateDocument failed: Document '{DocumentPath}' not found in workspace", fileModel.Path);
return; return false;
} }
var newText = SourceText.From(newContent, Encoding.UTF8); var newText = SourceText.From(newContent, Encoding.UTF8);
@@ -957,6 +958,11 @@ public class RoslynAnalysis(ILogger<RoslynAnalysis> logger, BuildService buildSe
{ {
_workspace.OnAnalyzerConfigDocumentTextChanged(documentId, newText, PreservationMode.PreserveIdentity); _workspace.OnAnalyzerConfigDocumentTextChanged(documentId, newText, PreservationMode.PreserveIdentity);
} }
else
{
return false;
}
return true;
} }
public async Task AddDocument(SharpIdeFile fileModel, string content) public async Task AddDocument(SharpIdeFile fileModel, string content)
@@ -1022,7 +1028,7 @@ public class RoslynAnalysis(ILogger<RoslynAnalysis> logger, BuildService buildSe
}, WorkspaceChangeKind.DocumentAdded, documentId: documentId); }, WorkspaceChangeKind.DocumentAdded, documentId: documentId);
} }
public async Task RemoveDocument(SharpIdeFile fileModel) public async Task<bool> RemoveDocument(SharpIdeFile fileModel)
{ {
using var _ = SharpIdeOtel.Source.StartActivity($"{nameof(RoslynAnalysis)}.{nameof(AddDocument)}"); using var _ = SharpIdeOtel.Source.StartActivity($"{nameof(RoslynAnalysis)}.{nameof(AddDocument)}");
await _solutionLoadedTcs.Task; await _solutionLoadedTcs.Task;
@@ -1038,7 +1044,7 @@ public class RoslynAnalysis(ILogger<RoslynAnalysis> logger, BuildService buildSe
if (documentId is null) if (documentId is null)
{ {
_logger.LogWarning("RemoveDocument failed: Document '{DocumentPath}' not found in workspace", fileModel.Path); _logger.LogWarning("RemoveDocument failed: Document '{DocumentPath}' not found in workspace", fileModel.Path);
return; return false;
} }
var documentKind = _workspace.CurrentSolution.GetDocumentKind(documentId); var documentKind = _workspace.CurrentSolution.GetDocumentKind(documentId);
Guard.Against.Null(documentKind); Guard.Against.Null(documentKind);
@@ -1050,6 +1056,7 @@ public class RoslynAnalysis(ILogger<RoslynAnalysis> logger, BuildService buildSe
case TextDocumentKind.AnalyzerConfigDocument: _workspace.OnAnalyzerConfigDocumentRemoved(documentId); break; case TextDocumentKind.AnalyzerConfigDocument: _workspace.OnAnalyzerConfigDocumentRemoved(documentId); break;
default: throw new ArgumentOutOfRangeException(nameof(documentKind)); default: throw new ArgumentOutOfRangeException(nameof(documentKind));
} }
return true;
} }
public async Task MoveDocument(SharpIdeFile sharpIdeFile, string oldFilePath) public async Task MoveDocument(SharpIdeFile sharpIdeFile, string oldFilePath)

View File

@@ -132,7 +132,8 @@ public class FileChangedService
private async Task HandleWorkspaceFileChanged(SharpIdeFile file, string newContents) private async Task HandleWorkspaceFileChanged(SharpIdeFile file, string newContents)
{ {
await _roslynAnalysis.UpdateDocument(file, newContents); var fileUpdatedInWorkspace = await _roslynAnalysis.UpdateDocument(file, newContents);
if (fileUpdatedInWorkspace is false) return;
GlobalEvents.Instance.SolutionAltered.InvokeParallelFireAndForget(); GlobalEvents.Instance.SolutionAltered.InvokeParallelFireAndForget();
_updateSolutionDiagnosticsQueue.AddWork(); _updateSolutionDiagnosticsQueue.AddWork();
} }
@@ -146,7 +147,8 @@ public class FileChangedService
private async Task HandleWorkspaceFileRemoved(SharpIdeFile file) private async Task HandleWorkspaceFileRemoved(SharpIdeFile file)
{ {
await _roslynAnalysis.RemoveDocument(file); var success = await _roslynAnalysis.RemoveDocument(file);
if (success is false) return;
GlobalEvents.Instance.SolutionAltered.InvokeParallelFireAndForget(); GlobalEvents.Instance.SolutionAltered.InvokeParallelFireAndForget();
_updateSolutionDiagnosticsQueue.AddWork(); _updateSolutionDiagnosticsQueue.AddWork();
} }