-
Notifications
You must be signed in to change notification settings - Fork 417
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Listen for file renames #987
Changes from 21 commits
70f8486
47804cf
2459ea0
1096cd6
c46fad5
2f2ea4c
d5a8477
e9e1fd7
2882a33
c9db400
634be55
4db48c7
9847dc4
2b19763
c7884fe
b3cb182
3ef284e
14e9626
b122830
9802d7e
9796a50
e8652fa
9b96a8d
f305c86
36c145b
5d0b91b
858bcbb
259bc76
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,20 @@ | ||
using System; | ||
using OmniSharp.Models.FilesChanged; | ||
|
||
namespace OmniSharp.FileWatching | ||
{ | ||
// TODO: Flesh out this API more | ||
public interface IFileSystemWatcher | ||
{ | ||
void Watch(string path, Action<string> callback); | ||
void Watch(string path, Action<string, FileChangeType> callback); | ||
|
||
void TriggerChange(string path); | ||
/// <summary> | ||
/// Called when a file is created, changed, or deleted. | ||
/// </summary> | ||
/// <param name="path">The path to the file</param> | ||
/// <param name="changeType">The type of change. Hosts are not required to pass a change type</param> | ||
void TriggerChange(string path, FileChangeType changeType); | ||
|
||
void WatchDirectory(string path, Action<string, FileChangeType> callback); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
namespace OmniSharp.Models.FilesChanged | ||
{ | ||
public enum FileChangeType | ||
{ | ||
Unspecified = 0, | ||
Change, | ||
Create, | ||
Delete | ||
} | ||
} |
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,44 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.IO; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sort usings so |
||
using OmniSharp.Models.FilesChanged; | ||
|
||
namespace OmniSharp.FileWatching | ||
{ | ||
public class ManualFileSystemWatcher : IFileSystemWatcher | ||
{ | ||
private readonly Dictionary<string, Action<string>> _callbacks = new Dictionary<string, Action<string>>(); | ||
private readonly Dictionary<string, Action<string, FileChangeType>> _callbacks = new Dictionary<string, Action<string, FileChangeType>>(); | ||
private readonly Dictionary<string, Action<string, FileChangeType>> _directoryCallBacks = new Dictionary<string, Action<string, FileChangeType>>(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. One concern I have here is that only a single callback can be called for a particular path. And, the last callback registered will win. I don't think this will be a problem so far, but it may be in the future -- especially since we're watching directories. I can easily imagine some other project system (like script or cake) wanting to listen to the same directories that the MSBuild project system does. Should we combine callbacks using There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That's a good point--will add this |
||
|
||
public void TriggerChange(string path) | ||
public void TriggerChange(string path, FileChangeType changeType) | ||
{ | ||
Action<string> callback; | ||
if (_callbacks.TryGetValue(path, out callback)) | ||
if (_callbacks.TryGetValue(path, out var callback)) | ||
{ | ||
callback(path); | ||
callback(path, changeType); | ||
} | ||
|
||
var directoryPath = Path.GetDirectoryName(path); | ||
if (_directoryCallBacks.TryGetValue(directoryPath, out var fileCallback)) | ||
{ | ||
fileCallback(path, changeType); | ||
} | ||
} | ||
|
||
public void Watch(string path, Action<string> callback) | ||
public void Watch(string path, Action<string, FileChangeType> callback) | ||
{ | ||
_callbacks[path] = callback; | ||
} | ||
|
||
public void WatchDirectory(string path, Action<string, FileChangeType> callback) | ||
{ | ||
if (_directoryCallBacks.TryGetValue(path, out var existingCallback)) | ||
{ | ||
_directoryCallBacks[path] = (Action<string, FileChangeType>)Delegate.Combine(callback, existingCallback); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm OK leaving this as is, but it's also possible to just write: _directoryCallBacks[path] = callback + existingCallback; Believe it or not, The More You Know There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So much nicer. Fixing. |
||
} | ||
else | ||
{ | ||
_directoryCallBacks[path] = callback; | ||
} | ||
} | ||
} | ||
} | ||
} |
This file was deleted.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This will serialize as an int (I think, I didn't see any where we configure
Newtonsoft.Json
)