Skip to content

Commit 65e976f

Browse files
committed
Improve getting window from textviewhost
1 parent e7bcbd0 commit 65e976f

File tree

4 files changed

+35
-13
lines changed

4 files changed

+35
-13
lines changed

CodeNav/CodeNav.cs

Lines changed: 32 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
using CodeNav.Models;
1313
using CodeNav.Properties;
1414
using EnvDTE;
15+
using Microsoft.VisualStudio.Text;
1516
using Microsoft.VisualStudio.Text.Editor;
1617
using HorizontalAlignment = System.Windows.HorizontalAlignment;
1718
using Window = EnvDTE.Window;
@@ -32,12 +33,10 @@ public class CodeNav : DockPanel, IWpfTextViewMargin
3233
private List<string> _highlightedItems;
3334
private readonly BackgroundWorker _backgroundWorker;
3435
private readonly Dictionary<string, List<CodeItem>> _cache;
36+
private readonly Window _window;
3537

3638
public CodeNav(IWpfTextViewHost textViewHost, DTE dte)
3739
{
38-
// If there are no code elements in the document, don't do anything
39-
if (dte.ActiveDocument?.ProjectItem?.FileCodeModel?.CodeElements == null) return;
40-
4140
_highlightedItems = new List<string>();
4241
_codeDocumentVm = new CodeDocumentViewModel();
4342
_cache = new Dictionary<string, List<CodeItem>>();
@@ -47,13 +46,36 @@ public CodeNav(IWpfTextViewHost textViewHost, DTE dte)
4746
_textView = textViewHost.TextView;
4847
_documentEvents = dte.Events.DocumentEvents;
4948

49+
// Get window belonging to textViewHost
50+
ITextDocument document;
51+
textViewHost.TextView.TextDataModel.DocumentBuffer.Properties.TryGetProperty(typeof(ITextDocument), out document);
52+
53+
for (var i = 1; i < _dte.Windows.Count + 1; i++)
54+
{
55+
var window = _dte.Windows.Item(i);
56+
try
57+
{
58+
if (window.Document == null) continue;
59+
if (!window.Document.FullName.Equals(document.FilePath, StringComparison.InvariantCultureIgnoreCase)) continue;
60+
_window = window;
61+
break;
62+
}
63+
catch (Exception e)
64+
{
65+
LogHelper.Log($"Exception getting parent window: {e.Message}");
66+
}
67+
}
68+
5069
// Setup the backgroundworker that will map the document to the codeitems
5170
_backgroundWorker = new BackgroundWorker {WorkerSupportsCancellation = true};
5271
_backgroundWorker.DoWork += _backgroundWorker_DoWork;
5372
_backgroundWorker.RunWorkerCompleted += _backgroundWorker_RunWorkerCompleted;
5473

5574
// Add the view/content to the margin area
5675
Children.Add(CreateGrid(textViewHost, dte));
76+
77+
RegisterEvents();
78+
LogHelper.Log($"CodeNav initialized for {_window.Caption}");
5779
}
5880

5981
private void _backgroundWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
@@ -70,7 +92,7 @@ private void _backgroundWorker_RunWorkerCompleted(object sender, RunWorkerComple
7092
}
7193

7294
_codeDocumentVm.CodeDocument = (List<CodeItem>)e.Result;
73-
_cache[_dte.ActiveDocument.Path] = (List<CodeItem>)e.Result;
95+
_cache[_window.Document.Path] = (List<CodeItem>)e.Result;
7496
((Grid)Children[0]).ColumnDefinitions[0].Width = !_codeDocumentVm.CodeDocument.Any() ? new GridLength(0) : new GridLength(Settings.Default.Width);
7597

7698
stopwatch.Stop();
@@ -99,8 +121,8 @@ public void RegisterEvents()
99121
}
100122

101123
// Subscribe to Code window activated event
102-
if (_dte?.ActiveDocument?.ActiveWindow == null) return;
103-
_windowEvents = _dte.Events.WindowEvents[_dte.ActiveDocument.ActiveWindow];
124+
if (_window == null) return;
125+
_windowEvents = _dte.Events.WindowEvents[_window];
104126
_windowEvents.WindowActivated -= WindowEvents_WindowActivated;
105127
_windowEvents.WindowActivated += WindowEvents_WindowActivated;
106128
}
@@ -112,7 +134,7 @@ public void UnRegisterEvents()
112134
_windowEvents.WindowActivated -= WindowEvents_WindowActivated;
113135
}
114136

115-
private void DocumentEvents_DocumentSaved(Document document) => UpdateDocument(_dte.ActiveDocument.ActiveWindow);
137+
private void DocumentEvents_DocumentSaved(Document document) => UpdateDocument(_window);
116138
private void WindowEvents_WindowActivated(Window gotFocus, Window lostFocus) => UpdateDocument(gotFocus);
117139
private void Caret_PositionChanged(object sender, CaretPositionChangedEventArgs e) => UpdateCurrentItem();
118140

@@ -211,13 +233,13 @@ public void UpdateDocument(Window gotFocus = null)
211233
if (gotFocus?.Document == null) return;
212234

213235
// Do we have code items in the text document
214-
var elements = _dte.ActiveDocument?.ProjectItem?.FileCodeModel?.CodeElements;
236+
var elements = _window.ProjectItem.FileCodeModel?.CodeElements;
215237
if (elements == null) return;
216238

217239
// Do we have a cached version of this document
218-
if (_cache.ContainsKey(_dte.ActiveDocument.Path))
240+
if (_cache.ContainsKey(_window.Document.Path))
219241
{
220-
_codeDocumentVm.CodeDocument = _cache[_dte.ActiveDocument.Path];
242+
_codeDocumentVm.CodeDocument = _cache[_window.Document.Path];
221243
}
222244

223245
// If not show a loading item

CodeNav/CodeNavFactory.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.ComponentModel.Composition;
3+
using CodeNav.Helpers;
34
using EnvDTE;
45
using Microsoft.VisualStudio.Shell;
56
using Microsoft.VisualStudio.Text.Editor;
@@ -36,7 +37,6 @@ public IWpfTextViewMargin CreateMargin(IWpfTextViewHost wpfTextViewHost, IWpfTex
3637
var dte = (DTE)_serviceProvider.GetService(typeof(DTE));
3738
Logger.Initialize(_serviceProvider, "CodeNav");
3839
var codeNav = new CodeNav(wpfTextViewHost, dte);
39-
codeNav.RegisterEvents();
4040
return codeNav;
4141
}
4242

CodeNav/source.extension.vsixmanifest

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<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">
33
<Metadata>
4-
<Identity Id="CodeNav.Samir Boulema.19687465-dc94-413d-ad72-6141e90c94d4" Version="0.3" Language="en-US" Publisher="Samir Boulema" />
4+
<Identity Id="CodeNav.Samir Boulema.19687465-dc94-413d-ad72-6141e90c94d4" Version="1.2" Language="en-US" Publisher="Samir Boulema" />
55
<DisplayName>CodeNav</DisplayName>
66
<Description xml:space="preserve">Show the code structure of your current document</Description>
77
<MoreInfo>https://github.com/sboulema/CodeNav</MoreInfo>

appveyor.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
version: 1.1.{build}
1+
version: 1.2.{build}
22
configuration: Release
33
install:
44
- ps: (new-object Net.WebClient).DownloadString("https://raw.github.com/madskristensen/ExtensionScripts/master/AppVeyor/vsix.ps1") | iex

0 commit comments

Comments
 (0)