12
12
using CodeNav . Models ;
13
13
using CodeNav . Properties ;
14
14
using EnvDTE ;
15
+ using Microsoft . VisualStudio . Text ;
15
16
using Microsoft . VisualStudio . Text . Editor ;
16
17
using HorizontalAlignment = System . Windows . HorizontalAlignment ;
17
18
using Window = EnvDTE . Window ;
@@ -32,12 +33,10 @@ public class CodeNav : DockPanel, IWpfTextViewMargin
32
33
private List < string > _highlightedItems ;
33
34
private readonly BackgroundWorker _backgroundWorker ;
34
35
private readonly Dictionary < string , List < CodeItem > > _cache ;
36
+ private readonly Window _window ;
35
37
36
38
public CodeNav ( IWpfTextViewHost textViewHost , DTE dte )
37
39
{
38
- // If there are no code elements in the document, don't do anything
39
- if ( dte . ActiveDocument ? . ProjectItem ? . FileCodeModel ? . CodeElements == null ) return ;
40
-
41
40
_highlightedItems = new List < string > ( ) ;
42
41
_codeDocumentVm = new CodeDocumentViewModel ( ) ;
43
42
_cache = new Dictionary < string , List < CodeItem > > ( ) ;
@@ -47,13 +46,36 @@ public CodeNav(IWpfTextViewHost textViewHost, DTE dte)
47
46
_textView = textViewHost . TextView ;
48
47
_documentEvents = dte . Events . DocumentEvents ;
49
48
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
+
50
69
// Setup the backgroundworker that will map the document to the codeitems
51
70
_backgroundWorker = new BackgroundWorker { WorkerSupportsCancellation = true } ;
52
71
_backgroundWorker . DoWork += _backgroundWorker_DoWork ;
53
72
_backgroundWorker . RunWorkerCompleted += _backgroundWorker_RunWorkerCompleted ;
54
73
55
74
// Add the view/content to the margin area
56
75
Children . Add ( CreateGrid ( textViewHost , dte ) ) ;
76
+
77
+ RegisterEvents ( ) ;
78
+ LogHelper . Log ( $ "CodeNav initialized for { _window . Caption } ") ;
57
79
}
58
80
59
81
private void _backgroundWorker_RunWorkerCompleted ( object sender , RunWorkerCompletedEventArgs e )
@@ -70,7 +92,7 @@ private void _backgroundWorker_RunWorkerCompleted(object sender, RunWorkerComple
70
92
}
71
93
72
94
_codeDocumentVm . CodeDocument = ( List < CodeItem > ) e . Result ;
73
- _cache [ _dte . ActiveDocument . Path ] = ( List < CodeItem > ) e . Result ;
95
+ _cache [ _window . Document . Path ] = ( List < CodeItem > ) e . Result ;
74
96
( ( Grid ) Children [ 0 ] ) . ColumnDefinitions [ 0 ] . Width = ! _codeDocumentVm . CodeDocument . Any ( ) ? new GridLength ( 0 ) : new GridLength ( Settings . Default . Width ) ;
75
97
76
98
stopwatch . Stop ( ) ;
@@ -99,8 +121,8 @@ public void RegisterEvents()
99
121
}
100
122
101
123
// 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 ] ;
104
126
_windowEvents . WindowActivated -= WindowEvents_WindowActivated ;
105
127
_windowEvents . WindowActivated += WindowEvents_WindowActivated ;
106
128
}
@@ -112,7 +134,7 @@ public void UnRegisterEvents()
112
134
_windowEvents . WindowActivated -= WindowEvents_WindowActivated ;
113
135
}
114
136
115
- private void DocumentEvents_DocumentSaved ( Document document ) => UpdateDocument ( _dte . ActiveDocument . ActiveWindow ) ;
137
+ private void DocumentEvents_DocumentSaved ( Document document ) => UpdateDocument ( _window ) ;
116
138
private void WindowEvents_WindowActivated ( Window gotFocus , Window lostFocus ) => UpdateDocument ( gotFocus ) ;
117
139
private void Caret_PositionChanged ( object sender , CaretPositionChangedEventArgs e ) => UpdateCurrentItem ( ) ;
118
140
@@ -211,13 +233,13 @@ public void UpdateDocument(Window gotFocus = null)
211
233
if ( gotFocus ? . Document == null ) return ;
212
234
213
235
// Do we have code items in the text document
214
- var elements = _dte . ActiveDocument ? . ProjectItem ? . FileCodeModel ? . CodeElements ;
236
+ var elements = _window . ProjectItem . FileCodeModel ? . CodeElements ;
215
237
if ( elements == null ) return ;
216
238
217
239
// Do we have a cached version of this document
218
- if ( _cache . ContainsKey ( _dte . ActiveDocument . Path ) )
240
+ if ( _cache . ContainsKey ( _window . Document . Path ) )
219
241
{
220
- _codeDocumentVm . CodeDocument = _cache [ _dte . ActiveDocument . Path ] ;
242
+ _codeDocumentVm . CodeDocument = _cache [ _window . Document . Path ] ;
221
243
}
222
244
223
245
// If not show a loading item
0 commit comments