Skip to content

Commit

Permalink
Update the diff margin when changes are made to the repository (fixes j…
Browse files Browse the repository at this point in the history
  • Loading branch information
sharwell committed Mar 20, 2013
1 parent 1c38255 commit 87370df
Showing 1 changed file with 52 additions and 0 deletions.
52 changes: 52 additions & 0 deletions Diff/DiffUpdateBackgroundParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,41 @@
using System.Linq;
using System.Threading.Tasks;
using Microsoft.VisualStudio.Text;
using CancellationToken = System.Threading.CancellationToken;
using Constants = NGit.Constants;
using Directory = System.IO.Directory;
using FileSystemEventArgs = System.IO.FileSystemEventArgs;
using FileSystemWatcher = System.IO.FileSystemWatcher;
using Path = System.IO.Path;
using Stopwatch = System.Diagnostics.Stopwatch;
using WatcherChangeTypes = System.IO.WatcherChangeTypes;

public class DiffUpdateBackgroundParser : BackgroundParser
{
private readonly IGitCommands _commands;
private readonly FileSystemWatcher _watcher;

public DiffUpdateBackgroundParser(ITextBuffer textBuffer, TaskScheduler taskScheduler, ITextDocumentFactoryService textDocumentFactoryService, IGitCommands commands)
: base(textBuffer, taskScheduler, textDocumentFactoryService)
{
_commands = commands;
ReparseDelay = TimeSpan.FromMilliseconds(500);

ITextDocument textDocument;
if (TextDocumentFactoryService.TryGetTextDocument(TextBuffer, out textDocument))
{
GitFileStatusTracker tracker = new GitFileStatusTracker(Path.GetDirectoryName(textDocument.FilePath));
if (tracker.HasGitRepository && tracker.Repository.Resolve(Constants.HEAD) != null)
{
_watcher = new FileSystemWatcher(tracker.Repository.Directory.GetAbsolutePath());
_watcher.IncludeSubdirectories = true;
_watcher.Changed += HandleFileSystemChanged;
_watcher.Created += HandleFileSystemChanged;
_watcher.Deleted += HandleFileSystemChanged;
_watcher.Renamed += HandleFileSystemChanged;
_watcher.EnableRaisingEvents = true;
}
}
}

public override string Name
Expand All @@ -26,6 +50,34 @@ public override string Name
}
}

private void HandleFileSystemChanged(object sender, FileSystemEventArgs e)
{
Action action = () => ProcessFileSystemChange(e);
Task.Factory.StartNew(action, CancellationToken.None, TaskCreationOptions.None, SccProviderService.TaskScheduler)
.HandleNonCriticalExceptions();
}

private void ProcessFileSystemChange(FileSystemEventArgs e)
{
if (e.ChangeType == WatcherChangeTypes.Changed && Directory.Exists(e.FullPath))
return;

if (string.Equals(Path.GetExtension(e.Name), ".lock", StringComparison.OrdinalIgnoreCase))
return;

MarkDirty(true);
}

protected override void Dispose(bool disposing)
{
if (disposing)
{
_watcher.Dispose();
}

base.Dispose(disposing);
}

protected override void ReParseImpl()
{
try
Expand Down

0 comments on commit 87370df

Please sign in to comment.