Skip to content

Commit

Permalink
Invert if statement to reduce nesting
Browse files Browse the repository at this point in the history
  • Loading branch information
laurentkempe committed Aug 25, 2018
1 parent f160567 commit 6a3891e
Show file tree
Hide file tree
Showing 6 changed files with 195 additions and 207 deletions.
25 changes: 13 additions & 12 deletions GitDiffMargin/Core/BackgroundParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -157,18 +157,19 @@ private void TryReparse(bool forceReparse)
if (DateTimeOffset.Now - _lastEdit < ReparseDelay)
return;

if (Interlocked.CompareExchange(ref _parsing, 1, 0) == 0)
try
{
var task = Task.Factory.StartNew(ReParse, CancellationToken.None, TaskCreationOptions.None,
_taskScheduler);
task.ContinueWith(_ => _parsing = 0);
}
catch
{
_parsing = 0;
throw;
}
if (Interlocked.CompareExchange(ref _parsing, 1, 0) != 0) return;

try
{
var task = Task.Factory.StartNew(ReParse, CancellationToken.None, TaskCreationOptions.None,
_taskScheduler);
task.ContinueWith(_ => _parsing = 0);
}
catch
{
_parsing = 0;
throw;
}
}

private void ReParse()
Expand Down
45 changes: 21 additions & 24 deletions GitDiffMargin/Core/DiffUpdateBackgroundParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,25 +26,23 @@ internal DiffUpdateBackgroundParser(ITextBuffer textBuffer, ITextBuffer document
_commands = commands;
ReparseDelay = TimeSpan.FromMilliseconds(500);

if (TextDocumentFactoryService.TryGetTextDocument(_documentBuffer, out _textDocument))
{
_originalPath = originalPath;
if (_commands.IsGitRepository(_textDocument.FilePath, _originalPath))
{
_textDocument.FileActionOccurred += OnFileActionOccurred;

var repositoryDirectory = _commands.GetGitRepository(_textDocument.FilePath, _originalPath);
if (repositoryDirectory != null)
{
_watcher = new FileSystemWatcher(repositoryDirectory);
_watcher.Changed += HandleFileSystemChanged;
_watcher.Created += HandleFileSystemChanged;
_watcher.Deleted += HandleFileSystemChanged;
_watcher.Renamed += HandleFileSystemChanged;
_watcher.EnableRaisingEvents = true;
}
}
}
if (!TextDocumentFactoryService.TryGetTextDocument(_documentBuffer, out _textDocument)) return;

_originalPath = originalPath;

if (!_commands.IsGitRepository(_textDocument.FilePath, _originalPath)) return;

_textDocument.FileActionOccurred += OnFileActionOccurred;

var repositoryDirectory = _commands.GetGitRepository(_textDocument.FilePath, _originalPath);
if (repositoryDirectory == null) return;

_watcher = new FileSystemWatcher(repositoryDirectory);
_watcher.Changed += HandleFileSystemChanged;
_watcher.Created += HandleFileSystemChanged;
_watcher.Deleted += HandleFileSystemChanged;
_watcher.Renamed += HandleFileSystemChanged;
_watcher.EnableRaisingEvents = true;
}

public override string Name => "Git Diff Analyzer";
Expand Down Expand Up @@ -106,11 +104,10 @@ protected override void Dispose(bool disposing)
{
base.Dispose(disposing);

if (disposing)
{
if (_textDocument != null) _textDocument.FileActionOccurred -= OnFileActionOccurred;
_watcher?.Dispose();
}
if (!disposing) return;

if (_textDocument != null) _textDocument.FileActionOccurred -= OnFileActionOccurred;
_watcher?.Dispose();
}
}
}
19 changes: 10 additions & 9 deletions GitDiffMargin/Core/MarginCore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -225,25 +225,26 @@ private void OnBrushesChanged(EventArgs e)

private static Brush GetBrush(ResourceDictionary properties)
{
if (properties == null)
return Brushes.Transparent;

if (properties.Contains(EditorFormatDefinition.BackgroundColorId))
Brush GetBrush()
{
var color = (Color) properties[EditorFormatDefinition.BackgroundColorId];
var brush = new SolidColorBrush(color);
var brush = (Brush) properties[EditorFormatDefinition.BackgroundBrushId];
if (brush.CanFreeze) brush.Freeze();
return brush;
}

if (properties.Contains(EditorFormatDefinition.BackgroundBrushId))
Brush GetSolidColorBrush()
{
var brush = (Brush) properties[EditorFormatDefinition.BackgroundBrushId];
var color = (Color) properties[EditorFormatDefinition.BackgroundColorId];
var brush = new SolidColorBrush(color);
if (brush.CanFreeze) brush.Freeze();
return brush;
}

return Brushes.Transparent;
if (properties == null) return Brushes.Transparent;

if (properties.Contains(EditorFormatDefinition.BackgroundColorId)) return GetSolidColorBrush();

return properties.Contains(EditorFormatDefinition.BackgroundBrushId) ? GetBrush() : Brushes.Transparent;
}

private void HandleParseComplete(object sender, ParseResultEventArgs e)
Expand Down
20 changes: 7 additions & 13 deletions GitDiffMargin/Git/GitCommands.cs
Original file line number Diff line number Diff line change
Expand Up @@ -197,13 +197,11 @@ public void StartExternalDiff(ITextDocument textDocument, string originalPath)
public bool TryGetOriginalPath(string path, out string originalPath)
{
originalPath = null;
if (GetGitRepository(path, ref originalPath) == null)
{
originalPath = path;
return false;
}
if (GetGitRepository(path, ref originalPath) != null) return true;

originalPath = path;
return false;

This comment has been minimized.

Copy link
@sharwell

sharwell Aug 25, 2018

Collaborator

💭 to me this feels weird because the main success case returns in the middle.


return true;
}

/// <inheritdoc />
Expand Down Expand Up @@ -280,13 +278,9 @@ private string GetGitRepository(string path, ref string originalPath)

private static Encoding GetEncoding(string file)
{
if (File.Exists(file))
{
var encoding = Encoding.UTF8;
if (HasPreamble(file, encoding)) return encoding;
}

return Encoding.Default;
return File.Exists(file)
? (HasPreamble(file, Encoding.UTF8) ? Encoding.UTF8 : Encoding.Default)
: Encoding.Default;
}

private static bool HasPreamble(string file, Encoding encoding)
Expand Down
Loading

0 comments on commit 6a3891e

Please sign in to comment.