Skip to content

Commit

Permalink
Improve Update logic
Browse files Browse the repository at this point in the history
  • Loading branch information
sboulema committed Dec 19, 2016
1 parent 484fdc7 commit 8dedac3
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 15 deletions.
36 changes: 25 additions & 11 deletions CodeNav/CodeNav.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ internal class CodeNav : DockPanel, IWpfTextViewMargin
private readonly DocumentEvents _documentEvents;
private List<string> _highlightedItems;
private readonly BackgroundWorker _backgroundWorker;
private Dictionary<string, List<CodeItem>> _cache;

public CodeNav(IWpfTextViewHost textViewHost, DTE dte)
{
Expand All @@ -40,6 +41,7 @@ public CodeNav(IWpfTextViewHost textViewHost, DTE dte)
_textView = textViewHost.TextView;
_documentEvents = dte.Events.DocumentEvents;
_codeDocumentVm = new CodeDocumentViewModel();
_cache = new Dictionary<string, List<CodeItem>>();

_backgroundWorker = new BackgroundWorker {WorkerSupportsCancellation = true};
_backgroundWorker.DoWork += _backgroundWorker_DoWork;
Expand All @@ -56,7 +58,11 @@ private void _backgroundWorker_RunWorkerCompleted(object sender, RunWorkerComple
{
Dispatcher.BeginInvoke(DispatcherPriority.Background, new Action(() =>
{
_codeDocumentVm.CodeDocument = (List<CodeItem>) e.Result;
var areEqual = _codeDocumentVm.CodeDocument.SequenceEqual((List<CodeItem>)e.Result, new CodeItemComparer());
if (areEqual) return;

_codeDocumentVm.CodeDocument = (List<CodeItem>)e.Result;
_cache[_dte.ActiveDocument.Path] = (List<CodeItem>)e.Result;
((Grid)Children[0]).ColumnDefinitions[0].Width = !_codeDocumentVm.CodeDocument.Any() ? new GridLength(0) : new GridLength(Settings.Default.Width);
} ));
}
Expand Down Expand Up @@ -200,18 +206,26 @@ private void UpdateDocument(Window gotFocus = null)
_backgroundWorker.CancelAsync();
}

_codeDocumentVm.CodeDocument = new List<CodeItem>
if (_cache.ContainsKey(_dte.ActiveDocument.Path))
{
new CodeClassItem
_codeDocumentVm.CodeDocument = _cache[_dte.ActiveDocument.Path];
}

if (_codeDocumentVm.CodeDocument == null)
{
_codeDocumentVm.CodeDocument = new List<CodeItem>
{
Name = "Loading...",
FullName = "Loading...",
Id = "Loading...",
Foreground = new SolidColorBrush(Colors.Black),
BorderBrush = new SolidColorBrush(Colors.DarkGray),
IconPath = "Icons/Refresh/Refresh_16x.xaml"
}
};
new CodeClassItem
{
Name = "Loading...",
FullName = "Loading...",
Id = "Loading...",
Foreground = new SolidColorBrush(Colors.Black),
BorderBrush = new SolidColorBrush(Colors.DarkGray),
IconPath = "Icons/Refresh/Refresh_16x.xaml"
}
};
}

_backgroundWorker.RunWorkerAsync(elements);
}
Expand Down
4 changes: 2 additions & 2 deletions CodeNav/Models/CodeClassItem.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using System.Collections.Generic;
using System.ComponentModel;
using System;
using System.Collections.Generic;
using System.Windows.Media;

namespace CodeNav.Models
Expand Down
35 changes: 34 additions & 1 deletion CodeNav/Models/CodeItem.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System.ComponentModel;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Windows.Media;
using Caliburn.Micro;
using EnvDTE;
Expand Down Expand Up @@ -28,4 +30,35 @@ public SolidColorBrush Foreground
}
}
}

public class CodeItemComparer : IEqualityComparer<CodeItem>
{

public bool Equals(CodeItem x, CodeItem y)
{
//Check whether the objects are the same object.
if (ReferenceEquals(x, y)) return true;

//Check whether the products' properties are equal.
var membersAreEqual = true;
if (x is CodeClassItem && y is CodeClassItem)
{
membersAreEqual = (x as CodeClassItem).Members.SequenceEqual((y as CodeClassItem).Members, new CodeItemComparer());
}

return x != null && y != null && x.Id.Equals(y.Id) && membersAreEqual;
}

public int GetHashCode(CodeItem obj)
{
//Get hash code for the Name field if it is not null.
int hashProductName = obj.Name == null ? 0 : obj.Name.GetHashCode();

//Get hash code for the Code field.
int hashProductCode = obj.Id.GetHashCode();

//Calculate the hash code for the product.
return hashProductName ^ hashProductCode;
}
}
}
2 changes: 1 addition & 1 deletion CodeNav/source.extension.vsixmanifest
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<PackageManifest Version="2.0.0" xmlns="http://schemas.microsoft.com/developer/vsx-schema/2011" xmlns:d="http://schemas.microsoft.com/developer/vsx-schema-design/2011">
<Metadata>
<Identity Id="CodeNav.Samir Boulema.19687465-dc94-413d-ad72-6141e90c94d4" Version="0.12" Language="en-US" Publisher="Samir Boulema" />
<Identity Id="CodeNav.Samir Boulema.19687465-dc94-413d-ad72-6141e90c94d4" Version="1.0" Language="en-US" Publisher="Samir Boulema" />
<DisplayName>CodeNav</DisplayName>
<Description xml:space="preserve">Show the code structure of your current document</Description>
<MoreInfo>https://github.com/sboulema/CodeNav</MoreInfo>
Expand Down

0 comments on commit 8dedac3

Please sign in to comment.