Skip to content

Commit e261c18

Browse files
Merge pull request #74741 from CyrusNajmabadi/useImmutableArray
2 parents a187b89 + f59ad0e commit e261c18

File tree

10 files changed

+20
-33
lines changed

10 files changed

+20
-33
lines changed

src/LanguageServer/Microsoft.CodeAnalysis.LanguageServer.UnitTests/LspFileChangeWatcherTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ public async Task CreatingDirectoryWatchRequestsDirectoryWatch()
6161
var tempDirectory = tempRoot.CreateDirectory();
6262

6363
// Try creating a context and ensure we created the registration
64-
var context = lspFileChangeWatcher.CreateContext(new ProjectSystem.WatchedDirectory(tempDirectory.Path, extensionFilter: null));
64+
var context = lspFileChangeWatcher.CreateContext([new ProjectSystem.WatchedDirectory(tempDirectory.Path, extensionFilter: null)]);
6565
await WaitForFileWatcherAsync(testLspServer);
6666

6767
var watcher = GetSingleFileWatcher(dynamicCapabilitiesRpcTarget);
@@ -92,7 +92,7 @@ public async Task CreatingFileWatchRequestsFileWatch()
9292
var tempDirectory = tempRoot.CreateDirectory();
9393

9494
// Try creating a single file watch and ensure we created the registration
95-
var context = lspFileChangeWatcher.CreateContext();
95+
var context = lspFileChangeWatcher.CreateContext([]);
9696
var watchedFile = context.EnqueueWatchingFile("Z:\\SingleFile.txt");
9797
await WaitForFileWatcherAsync(testLspServer);
9898

src/LanguageServer/Microsoft.CodeAnalysis.LanguageServer/HostWorkspace/FileWatching/DelegatingFileChangeWatcher.cs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
using Microsoft.Extensions.Logging;
99
using Microsoft.CodeAnalysis.Host.Mef;
1010
using Microsoft.CodeAnalysis.Shared.TestHooks;
11+
using System.Collections.Immutable;
1112

1213
namespace Microsoft.CodeAnalysis.LanguageServer.HostWorkspace.FileWatching;
1314

@@ -50,8 +51,6 @@ private IFileChangeWatcher CreateFileWatcher()
5051
}
5152
}
5253

53-
public IFileChangeContext CreateContext(params WatchedDirectory[] watchedDirectories)
54-
{
55-
return _underlyingFileWatcher.Value.CreateContext(watchedDirectories);
56-
}
54+
public IFileChangeContext CreateContext(ImmutableArray<WatchedDirectory> watchedDirectories)
55+
=> _underlyingFileWatcher.Value.CreateContext(watchedDirectories);
5756
}

src/LanguageServer/Microsoft.CodeAnalysis.LanguageServer/HostWorkspace/FileWatching/LspFileChangeWatcher.cs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,10 @@ public static bool SupportsLanguageServerHost(LanguageServerHost languageServerH
4040
return clientCapabilitiesProvider.GetClientCapabilities().Workspace?.DidChangeWatchedFiles?.DynamicRegistration ?? false;
4141
}
4242

43-
public IFileChangeContext CreateContext(params WatchedDirectory[] watchedDirectories)
44-
{
45-
return new FileChangeContext([.. watchedDirectories], this);
46-
}
43+
public IFileChangeContext CreateContext(ImmutableArray<WatchedDirectory> watchedDirectories)
44+
=> new FileChangeContext(watchedDirectories, this);
4745

48-
private class FileChangeContext : IFileChangeContext
46+
private sealed class FileChangeContext : IFileChangeContext
4947
{
5048
private readonly ImmutableArray<WatchedDirectory> _watchedDirectories;
5149
private readonly LspFileChangeWatcher _lspFileChangeWatcher;

src/LanguageServer/Microsoft.CodeAnalysis.LanguageServer/HostWorkspace/FileWatching/SimpleFileChangeWatcher.cs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,10 @@ namespace Microsoft.CodeAnalysis.LanguageServer.HostWorkspace.FileWatching;
1717
/// </remarks>
1818
internal sealed class SimpleFileChangeWatcher : IFileChangeWatcher
1919
{
20-
public IFileChangeContext CreateContext(params WatchedDirectory[] watchedDirectories)
21-
{
22-
return new FileChangeContext([.. watchedDirectories]);
23-
}
20+
public IFileChangeContext CreateContext(ImmutableArray<WatchedDirectory> watchedDirectories)
21+
=> new FileChangeContext(watchedDirectories);
2422

25-
private class FileChangeContext : IFileChangeContext
23+
private sealed class FileChangeContext : IFileChangeContext
2624
{
2725
private readonly ImmutableArray<WatchedDirectory> _watchedDirectories;
2826

src/LanguageServer/Microsoft.CodeAnalysis.LanguageServer/HostWorkspace/LoadedProject.cs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,14 +43,12 @@ public LoadedProject(ProjectSystemProject projectSystemProject, SolutionServices
4343
// We'll watch the directory for all source file changes
4444
// TODO: we only should listen for add/removals here, but we can't specify such a filter now
4545
var projectDirectory = Path.GetDirectoryName(projectSystemProject.FilePath)!;
46-
var watchedDirectories = new WatchedDirectory[]
47-
{
46+
47+
_fileChangeContext = fileWatcher.CreateContext([
4848
new(projectDirectory, ".cs"),
4949
new(projectDirectory, ".cshtml"),
5050
new(projectDirectory, ".razor")
51-
};
52-
53-
_fileChangeContext = fileWatcher.CreateContext(watchedDirectories);
51+
]);
5452
_fileChangeContext.FileChanged += FileChangedContext_FileChanged;
5553

5654
// Start watching for file changes for the project file as well

src/VisualStudio/Core/Def/ProjectSystem/FileChangeWatcher.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -83,10 +83,8 @@ static int FindCombinableRange(ImmutableSegmentedList<WatcherOperation> operatio
8383
}
8484
}
8585

86-
public IFileChangeContext CreateContext(params WatchedDirectory[] watchedDirectories)
87-
{
88-
return new Context(this, watchedDirectories.ToImmutableArray());
89-
}
86+
public IFileChangeContext CreateContext(ImmutableArray<WatchedDirectory> watchedDirectories)
87+
=> new Context(this, watchedDirectories);
9088

9189
/// <summary>
9290
/// Represents an operation to subscribe or unsubscribe from <see cref="IVsAsyncFileChangeEx2"/> events. The

src/VisualStudio/Core/Def/ProjectSystem/RuleSets/VisualStudioRuleSetManager.RuleSetFile.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ public void InitializeFileTracking(IFileChangeWatcher fileChangeWatcher)
6666
includes = [FilePath];
6767
}
6868

69-
_fileChangeContext = fileChangeWatcher.CreateContext();
69+
_fileChangeContext = fileChangeWatcher.CreateContext([]);
7070
_fileChangeContext.FileChanged += IncludeUpdated;
7171

7272
foreach (var include in includes)

src/Workspaces/Core/Portable/Workspace/ProjectSystem/FileWatchedPortableExecutableReferenceFactory.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ public FileWatchedPortableExecutableReferenceFactory(
8787
referenceDirectories.Add(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), ".nuget", "packages"));
8888
}
8989

90-
var directoriesToWatch = referenceDirectories.Select(static d => new WatchedDirectory(d, ".dll")).ToArray();
90+
var directoriesToWatch = referenceDirectories.SelectAsArray(static d => new WatchedDirectory(d, ".dll"));
9191
var fileReferenceChangeContext = fileChangeWatcher.CreateContext(directoriesToWatch);
9292
fileReferenceChangeContext.FileChanged += FileReferenceChangeContext_FileChanged;
9393
return fileReferenceChangeContext;

src/Workspaces/Core/Portable/Workspace/ProjectSystem/IFileChangeWatcher.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ namespace Microsoft.CodeAnalysis.ProjectSystem;
99

1010
internal interface IFileChangeWatcher
1111
{
12-
IFileChangeContext CreateContext(params WatchedDirectory[] watchedDirectories);
12+
IFileChangeContext CreateContext(ImmutableArray<WatchedDirectory> watchedDirectories);
1313
}
1414

1515
/// <summary>

src/Workspaces/Core/Portable/Workspace/ProjectSystem/ProjectSystemProject.cs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -203,18 +203,14 @@ internal ProjectSystemProject(
203203
_documentFileChangeContext = _projectSystemProjectFactory.FileChangeWatcher.CreateContext(watchedDirectories);
204204
_documentFileChangeContext.FileChanged += DocumentFileChangeContext_FileChanged;
205205

206-
static WatchedDirectory[] GetWatchedDirectories(string? language, string? filePath)
206+
static ImmutableArray<WatchedDirectory> GetWatchedDirectories(string? language, string? filePath)
207207
{
208208
if (filePath is null)
209-
{
210209
return [];
211-
}
212210

213211
var rootPath = Path.GetDirectoryName(filePath);
214212
if (rootPath is null)
215-
{
216213
return [];
217-
}
218214

219215
return language switch
220216
{

0 commit comments

Comments
 (0)