Skip to content

Commit

Permalink
Merge pull request #1053 from mholo65/feature/watch-file-extension
Browse files Browse the repository at this point in the history
Support watching file extensions.
  • Loading branch information
david-driscoll authored Dec 12, 2017
2 parents e545d1c + 7eb6366 commit 077c1f3
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 5 deletions.
4 changes: 2 additions & 2 deletions src/OmniSharp.Abstractions/FileWatching/IFileSystemWatcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ public interface IFileSystemWatcher
/// <summary>
/// Call to watch a file or directory path for changes.
/// </summary>
/// <param name="fileOrDirectoryPath">The file or directory path to watch.</param>
/// <param name="pathOrExtension">The file path, directory path or file extension to watch.</param>
/// <param name="callback">The callback that will be invoked when a change occurs in the watched file or directory.</param>
void Watch(string fileOrDirectoryPath, FileSystemNotificationCallback callback);
void Watch(string pathOrExtension, FileSystemNotificationCallback callback);
}
}
13 changes: 10 additions & 3 deletions src/OmniSharp.Host/FileWatching/ManualFileSystemWatcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,19 +29,26 @@ public void Notify(string filePath, FileChangeType changeType = FileChangeType.U
{
directoryCallback(filePath, changeType);
}

var extension = Path.GetExtension(filePath);
if (!string.IsNullOrEmpty(extension) &&
_callbacks.TryGetValue(extension, out var extensionCallback))
{
extensionCallback(filePath, changeType);
}
}
}

public void Watch(string fileOrDirectoryPath, FileSystemNotificationCallback callback)
public void Watch(string pathOrExtension, FileSystemNotificationCallback callback)
{
lock (_gate)
{
if (_callbacks.TryGetValue(fileOrDirectoryPath, out var existingCallback))
if (_callbacks.TryGetValue(pathOrExtension, out var existingCallback))
{
callback = callback + existingCallback;
}

_callbacks[fileOrDirectoryPath] = callback;
_callbacks[pathOrExtension] = callback;
}
}
}
Expand Down
17 changes: 17 additions & 0 deletions tests/OmniSharp.Roslyn.CSharp.Tests/FilesChangedFacts.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,5 +55,22 @@ public void TestMultipleDirectoryWatchers()
Assert.True(secondWatcherCalled);
}
}

[Fact]
public void TestFileExtensionWatchers()
{
using (var host = CreateEmptyOmniSharpHost())
{
var watcher = host.GetExport<IFileSystemWatcher>();

var extensionWatcherCalled = false;
watcher.Watch(".cs", (path, changeType) => { extensionWatcherCalled = true; });

var handler = GetRequestHandler(host);
handler.Handle(new[] { new FilesChangedRequest() { FileName = "FileName.cs", ChangeType = FileChangeType.Create } });

Assert.True(extensionWatcherCalled);
}
}
}
}

0 comments on commit 077c1f3

Please sign in to comment.