diff --git a/CodeNav/CodeNav.cs b/CodeNav/CodeNav.cs index fe70228..58c2e80 100644 --- a/CodeNav/CodeNav.cs +++ b/CodeNav/CodeNav.cs @@ -29,6 +29,7 @@ internal class CodeNav : DockPanel, IWpfTextViewMargin private readonly DocumentEvents _documentEvents; private List _highlightedItems; private readonly BackgroundWorker _backgroundWorker; + private Dictionary> _cache; public CodeNav(IWpfTextViewHost textViewHost, DTE dte) { @@ -40,6 +41,7 @@ public CodeNav(IWpfTextViewHost textViewHost, DTE dte) _textView = textViewHost.TextView; _documentEvents = dte.Events.DocumentEvents; _codeDocumentVm = new CodeDocumentViewModel(); + _cache = new Dictionary>(); _backgroundWorker = new BackgroundWorker {WorkerSupportsCancellation = true}; _backgroundWorker.DoWork += _backgroundWorker_DoWork; @@ -56,7 +58,11 @@ private void _backgroundWorker_RunWorkerCompleted(object sender, RunWorkerComple { Dispatcher.BeginInvoke(DispatcherPriority.Background, new Action(() => { - _codeDocumentVm.CodeDocument = (List) e.Result; + var areEqual = _codeDocumentVm.CodeDocument.SequenceEqual((List)e.Result, new CodeItemComparer()); + if (areEqual) return; + + _codeDocumentVm.CodeDocument = (List)e.Result; + _cache[_dte.ActiveDocument.Path] = (List)e.Result; ((Grid)Children[0]).ColumnDefinitions[0].Width = !_codeDocumentVm.CodeDocument.Any() ? new GridLength(0) : new GridLength(Settings.Default.Width); } )); } @@ -200,18 +206,26 @@ private void UpdateDocument(Window gotFocus = null) _backgroundWorker.CancelAsync(); } - _codeDocumentVm.CodeDocument = new List + if (_cache.ContainsKey(_dte.ActiveDocument.Path)) { - new CodeClassItem + _codeDocumentVm.CodeDocument = _cache[_dte.ActiveDocument.Path]; + } + + if (_codeDocumentVm.CodeDocument == null) + { + _codeDocumentVm.CodeDocument = new List { - 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); } diff --git a/CodeNav/Models/CodeClassItem.cs b/CodeNav/Models/CodeClassItem.cs index 660b68b..1adbd41 100644 --- a/CodeNav/Models/CodeClassItem.cs +++ b/CodeNav/Models/CodeClassItem.cs @@ -1,5 +1,5 @@ -using System.Collections.Generic; -using System.ComponentModel; +using System; +using System.Collections.Generic; using System.Windows.Media; namespace CodeNav.Models diff --git a/CodeNav/Models/CodeItem.cs b/CodeNav/Models/CodeItem.cs index 23f272d..79b305c 100644 --- a/CodeNav/Models/CodeItem.cs +++ b/CodeNav/Models/CodeItem.cs @@ -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; @@ -28,4 +30,35 @@ public SolidColorBrush Foreground } } } + + public class CodeItemComparer : IEqualityComparer + { + + 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; + } + } } diff --git a/CodeNav/source.extension.vsixmanifest b/CodeNav/source.extension.vsixmanifest index 7f2e487..7c875e0 100644 --- a/CodeNav/source.extension.vsixmanifest +++ b/CodeNav/source.extension.vsixmanifest @@ -1,7 +1,7 @@  - + CodeNav Show the code structure of your current document https://github.com/sboulema/CodeNav