55using System . Collections . Immutable ;
66using Microsoft . CodeAnalysis . Host ;
77using Microsoft . CodeAnalysis . LanguageServer . Handler . DebugConfiguration ;
8+ using Microsoft . CodeAnalysis . LanguageServer . HostWorkspace . FileWatching ;
89using Microsoft . CodeAnalysis . LanguageServer . HostWorkspace . ProjectTelemetry ;
910using Microsoft . CodeAnalysis . MSBuild ;
1011using Microsoft . CodeAnalysis . ProjectSystem ;
@@ -25,7 +26,8 @@ internal sealed class LoadedProject : IDisposable
2526
2627 private readonly ProjectSystemProject _projectSystemProject ;
2728 private readonly ProjectSystemProjectOptionsProcessor _optionsProcessor ;
28- private readonly IFileChangeContext _fileChangeContext ;
29+ private readonly IFileChangeContext _sourceFileChangeContext ;
30+ private readonly IFileChangeContext _projectFileChangeContext ;
2931 private readonly ProjectTargetFrameworkManager _targetFrameworkManager ;
3032
3133 /// <summary>
@@ -53,22 +55,16 @@ public LoadedProject(ProjectSystemProject projectSystemProject, SolutionServices
5355 // TODO: we only should listen for add/removals here, but we can't specify such a filter now
5456 _projectDirectory = Path . GetDirectoryName ( _projectFilePath ) ! ;
5557
56- _fileChangeContext = fileWatcher . CreateContext ( [ new ( _projectDirectory , [ ".cs" , ".cshtml" , ".razor" ] ) ] ) ;
57- _fileChangeContext . FileChanged += FileChangedContext_FileChanged ;
58+ _sourceFileChangeContext = fileWatcher . CreateContext ( [ new ( _projectDirectory , [ ".cs" , ".cshtml" , ".razor" ] ) ] ) ;
59+ _sourceFileChangeContext . FileChanged += SourceFileChangeContext_FileChanged ;
5860
59- // Start watching for file changes for the project file as well
60- _fileChangeContext . EnqueueWatchingFile ( _projectFilePath ) ;
61+ _projectFileChangeContext = fileWatcher . CreateContext ( [ ] ) ;
62+ _projectFileChangeContext . FileChanged += ProjectFileChangeContext_FileChanged ;
63+ _projectFileChangeContext . EnqueueWatchingFile ( _projectFilePath ) ;
6164 }
6265
63- private void FileChangedContext_FileChanged ( object ? sender , string filePath )
66+ private void SourceFileChangeContext_FileChanged ( object ? sender , string filePath )
6467 {
65- // If the project file itself changed, we almost certainly need to reload the project.
66- if ( string . Equals ( filePath , _projectFilePath , StringComparison . OrdinalIgnoreCase ) )
67- {
68- NeedsReload ? . Invoke ( this , EventArgs . Empty ) ;
69- return ;
70- }
71-
7268 var matchers = _mostRecentFileMatchers ? . Value ;
7369 if ( matchers is null )
7470 {
@@ -92,6 +88,11 @@ private void FileChangedContext_FileChanged(object? sender, string filePath)
9288 }
9389 }
9490
91+ private void ProjectFileChangeContext_FileChanged ( object ? sender , string filePath )
92+ {
93+ NeedsReload ? . Invoke ( this , EventArgs . Empty ) ;
94+ }
95+
9596 public event EventHandler ? NeedsReload ;
9697
9798 public string ? GetTargetFramework ( )
@@ -105,7 +106,8 @@ private void FileChangedContext_FileChanged(object? sender, string filePath)
105106 /// </summary>
106107 public void Dispose ( )
107108 {
108- _fileChangeContext . Dispose ( ) ;
109+ _sourceFileChangeContext . Dispose ( ) ;
110+ _projectFileChangeContext . Dispose ( ) ;
109111 _optionsProcessor . Dispose ( ) ;
110112 _projectSystemProject . RemoveFromWorkspace ( ) ;
111113 }
@@ -221,7 +223,7 @@ public void Dispose()
221223 document => _projectSystemProject . RemoveDynamicSourceFile ( document . FilePath ) ,
222224 "Project {0} now has {1} dynamic file(s)." ) ;
223225
224- WatchProjectAssetsFile ( newProjectInfo , _fileChangeContext ) ;
226+ WatchProjectAssetsFile ( newProjectInfo ) ;
225227
226228 var needsRestore = ProjectDependencyHelper . NeedsRestore ( newProjectInfo , _mostRecentFileInfo , logger ) ;
227229
@@ -272,7 +274,7 @@ void UpdateProjectSystemProjectCollection<T>(IEnumerable<T> loadedCollection, IE
272274 logger . LogTrace ( logMessage , projectFullPathWithTargetFramework , newItems . Count ) ;
273275 }
274276
275- void WatchProjectAssetsFile ( ProjectFileInfo currentProjectInfo , IFileChangeContext fileChangeContext )
277+ void WatchProjectAssetsFile ( ProjectFileInfo currentProjectInfo )
276278 {
277279 if ( _mostRecentFileInfo ? . ProjectAssetsFilePath == currentProjectInfo . ProjectAssetsFilePath )
278280 {
@@ -282,14 +284,9 @@ void WatchProjectAssetsFile(ProjectFileInfo currentProjectInfo, IFileChangeConte
282284
283285 // Dispose of the last once since we're changing the file we're watching.
284286 _mostRecentProjectAssetsFileWatcher ? . Dispose ( ) ;
285-
286- IWatchedFile ? currentWatcher = null ;
287- if ( currentProjectInfo . ProjectAssetsFilePath != null )
288- {
289- currentWatcher = fileChangeContext . EnqueueWatchingFile ( currentProjectInfo . ProjectAssetsFilePath ) ;
290- }
291-
292- _mostRecentProjectAssetsFileWatcher = currentWatcher ;
287+ _mostRecentProjectAssetsFileWatcher = currentProjectInfo . ProjectAssetsFilePath is { } assetsFilePath
288+ ? _projectFileChangeContext . EnqueueWatchingFile ( assetsFilePath )
289+ : null ;
293290 }
294291 }
295292
0 commit comments