5
5
using System . Collections . Immutable ;
6
6
using Microsoft . CodeAnalysis . Host ;
7
7
using Microsoft . CodeAnalysis . LanguageServer . Handler . DebugConfiguration ;
8
+ using Microsoft . CodeAnalysis . LanguageServer . HostWorkspace . FileWatching ;
8
9
using Microsoft . CodeAnalysis . LanguageServer . HostWorkspace . ProjectTelemetry ;
9
10
using Microsoft . CodeAnalysis . MSBuild ;
10
11
using Microsoft . CodeAnalysis . ProjectSystem ;
@@ -25,7 +26,8 @@ internal sealed class LoadedProject : IDisposable
25
26
26
27
private readonly ProjectSystemProject _projectSystemProject ;
27
28
private readonly ProjectSystemProjectOptionsProcessor _optionsProcessor ;
28
- private readonly IFileChangeContext _fileChangeContext ;
29
+ private readonly IFileChangeContext _sourceFileChangeContext ;
30
+ private readonly IFileChangeContext _projectFileChangeContext ;
29
31
private readonly ProjectTargetFrameworkManager _targetFrameworkManager ;
30
32
31
33
/// <summary>
@@ -53,22 +55,16 @@ public LoadedProject(ProjectSystemProject projectSystemProject, SolutionServices
53
55
// TODO: we only should listen for add/removals here, but we can't specify such a filter now
54
56
_projectDirectory = Path . GetDirectoryName ( _projectFilePath ) ! ;
55
57
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 ;
58
60
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 ) ;
61
64
}
62
65
63
- private void FileChangedContext_FileChanged ( object ? sender , string filePath )
66
+ private void SourceFileChangeContext_FileChanged ( object ? sender , string filePath )
64
67
{
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
-
72
68
var matchers = _mostRecentFileMatchers ? . Value ;
73
69
if ( matchers is null )
74
70
{
@@ -92,6 +88,11 @@ private void FileChangedContext_FileChanged(object? sender, string filePath)
92
88
}
93
89
}
94
90
91
+ private void ProjectFileChangeContext_FileChanged ( object ? sender , string filePath )
92
+ {
93
+ NeedsReload ? . Invoke ( this , EventArgs . Empty ) ;
94
+ }
95
+
95
96
public event EventHandler ? NeedsReload ;
96
97
97
98
public string ? GetTargetFramework ( )
@@ -105,7 +106,8 @@ private void FileChangedContext_FileChanged(object? sender, string filePath)
105
106
/// </summary>
106
107
public void Dispose ( )
107
108
{
108
- _fileChangeContext . Dispose ( ) ;
109
+ _sourceFileChangeContext . Dispose ( ) ;
110
+ _projectFileChangeContext . Dispose ( ) ;
109
111
_optionsProcessor . Dispose ( ) ;
110
112
_projectSystemProject . RemoveFromWorkspace ( ) ;
111
113
}
@@ -221,7 +223,7 @@ public void Dispose()
221
223
document => _projectSystemProject . RemoveDynamicSourceFile ( document . FilePath ) ,
222
224
"Project {0} now has {1} dynamic file(s)." ) ;
223
225
224
- WatchProjectAssetsFile ( newProjectInfo , _fileChangeContext ) ;
226
+ WatchProjectAssetsFile ( newProjectInfo ) ;
225
227
226
228
var needsRestore = ProjectDependencyHelper . NeedsRestore ( newProjectInfo , _mostRecentFileInfo , logger ) ;
227
229
@@ -272,7 +274,7 @@ void UpdateProjectSystemProjectCollection<T>(IEnumerable<T> loadedCollection, IE
272
274
logger . LogTrace ( logMessage , projectFullPathWithTargetFramework , newItems . Count ) ;
273
275
}
274
276
275
- void WatchProjectAssetsFile ( ProjectFileInfo currentProjectInfo , IFileChangeContext fileChangeContext )
277
+ void WatchProjectAssetsFile ( ProjectFileInfo currentProjectInfo )
276
278
{
277
279
if ( _mostRecentFileInfo ? . ProjectAssetsFilePath == currentProjectInfo . ProjectAssetsFilePath )
278
280
{
@@ -282,14 +284,9 @@ void WatchProjectAssetsFile(ProjectFileInfo currentProjectInfo, IFileChangeConte
282
284
283
285
// Dispose of the last once since we're changing the file we're watching.
284
286
_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 ;
293
290
}
294
291
}
295
292
0 commit comments