Skip to content

Commit b28ccdb

Browse files
author
Samir L. Boulema
committed
No icon when working in "small modus" #108
1 parent 911bb73 commit b28ccdb

16 files changed

+253
-220
lines changed

CodeNav.Shared/CodeNav.Shared.projitems

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -161,9 +161,6 @@
161161
<DependentUpon>OptionsWindow.cs</DependentUpon>
162162
</Compile>
163163
</ItemGroup>
164-
<ItemGroup>
165-
<Content Include="$(MSBuildThisFileDirectory)ToolWindow\CodeNavToolWindowPackage.vsct" />
166-
</ItemGroup>
167164
<ItemGroup>
168165
<EmbeddedResource Include="$(MSBuildThisFileDirectory)ToolWindow\VSPackage.resx">
169166
<SubType>Designer</SubType>

CodeNav.Shared/CodeViewUserControl.xaml.cs

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -59,18 +59,6 @@ public async Task UpdateDocument(string filePath = "", bool forceUpdate = false)
5959
=> await DocumentHelper.UpdateDocument(this, _workspace, CodeDocumentViewModel, _cache,
6060
OutliningManagerService, _margin, _column, null, filePath, forceUpdate).ConfigureAwait(false);
6161

62-
#region Custom Items
63-
64-
/// <summary>
65-
/// Show an item to indicate that the user has to select an active code document to inspect
66-
/// </summary>
67-
public void ShowWaitingForDocument()
68-
{
69-
CodeDocumentViewModel.CodeDocument = PlaceholderHelper.CreateSelectDocumentItem();
70-
}
71-
72-
#endregion
73-
7462
public void HighlightCurrentItem()
7563
=> _ = HighlightHelper.HighlightCurrentItem(CodeDocumentViewModel);
7664
}

CodeNav.Shared/CodeViewUserControlTop.xaml.cs

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -61,18 +61,6 @@ public async Task UpdateDocument(string filePath = "", bool forceUpdate = false)
6161
=> await DocumentHelper.UpdateDocument(this, _workspace, CodeDocumentViewModel, _cache,
6262
OutliningManagerService, _margin, null, _row, filePath, forceUpdate).ConfigureAwait(false);
6363

64-
#region Custom Items
65-
66-
/// <summary>
67-
/// Show an item to indicate that the user has to select an active code document to inspect
68-
/// </summary>
69-
public void ShowWaitingForDocument()
70-
{
71-
CodeDocumentViewModel.CodeDocument = PlaceholderHelper.CreateSelectDocumentItem();
72-
}
73-
74-
#endregion
75-
7664
public void HighlightCurrentItem()
7765
{
7866
_ = HighlightHelper.HighlightCurrentItem(CodeDocumentViewModel);

CodeNav.Shared/Helpers/DocumentHelper.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,11 @@ public static async Task SelectLines(TextSpan textSpan)
113113

114114
public static async Task<Document> GetCodeAnalysisDocument(VisualStudioWorkspace workspace, string filePath = "")
115115
{
116+
if (workspace == null)
117+
{
118+
return null;
119+
}
120+
116121
if (string.IsNullOrEmpty(filePath))
117122
{
118123
filePath = await GetFilePath().ConfigureAwait(false);

CodeNav.Shared/Mappers/SyntaxMapper.cs

Lines changed: 19 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -64,27 +64,20 @@ public static List<CodeItem> MapDocumentVB(string filePath)
6464
/// <param name="workspace">Current Visual Studio workspace</param>
6565
/// <returns>List of found code items</returns>
6666
public static async Task<List<CodeItem>> MapDocument(ICodeViewUserControl control,
67-
VisualStudioWorkspace workspace, string filePath = "")
67+
VisualStudioWorkspace workspace = null, string filePath = "")
6868
{
6969
_control = control;
7070

71-
if (workspace == null)
72-
{
73-
return null;
74-
}
75-
76-
Document codeAnalysisDocument;
77-
7871
try
7972
{
80-
codeAnalysisDocument = await DocumentHelper.GetCodeAnalysisDocument(workspace, filePath);
73+
var codeAnalysisDocument = await DocumentHelper.GetCodeAnalysisDocument(workspace, filePath);
8174

8275
if (codeAnalysisDocument != null)
8376
{
8477
return await MapDocument(codeAnalysisDocument);
8578
}
8679

87-
return await MapDocument(workspace);
80+
return await MapDocument();
8881
}
8982
catch (Exception e)
9083
{
@@ -133,28 +126,32 @@ public static async Task<List<CodeItem>> MapDocument(Document codeAnalysisDocume
133126
}
134127

135128
/// <summary>
136-
/// Map the active document, used for files outside of the current solution eg. [from metadata]
129+
/// Map the active document without workspace
137130
/// </summary>
138131
/// <returns>List of found code items</returns>
139-
public static async Task<List<CodeItem>> MapDocument(VisualStudioWorkspace workspace)
132+
public static async Task<List<CodeItem>> MapDocument()
140133
{
141-
var text = await DocumentHelper.GetText();
134+
var filePath = await DocumentHelper.GetFilePath();
142135

143-
if (string.IsNullOrEmpty(text))
136+
if (string.IsNullOrEmpty(filePath))
144137
{
145138
return new List<CodeItem>();
146139
}
147140

148-
if (Path.GetExtension(await DocumentHelper.GetFilePath()).Equals(".js"))
141+
var fileExtension = Path.GetExtension(filePath);
142+
143+
var text = await DocumentHelper.GetText();
144+
145+
if (string.IsNullOrEmpty(text))
149146
{
150-
return SyntaxMapperJS.Map(await DocumentHelper.GetFilePath(), _control);
147+
return new List<CodeItem>();
151148
}
152149

153-
var language = await LanguageHelper.GetActiveDocumentLanguage(workspace);
154-
155-
switch (language)
150+
switch (fileExtension)
156151
{
157-
case LanguageEnum.CSharp:
152+
case ".js":
153+
return SyntaxMapperJS.Map(filePath, _control);
154+
case ".cs":
158155
_tree = CSharpSyntaxTree.ParseText(text);
159156

160157
try
@@ -171,7 +168,7 @@ public static async Task<List<CodeItem>> MapDocument(VisualStudioWorkspace works
171168
var root = (CompilationUnitSyntax)await _tree.GetRootAsync();
172169

173170
return root.Members.Select(MapMember).ToList();
174-
case LanguageEnum.VisualBasic:
171+
case ".vb":
175172
_tree = VisualBasic.VisualBasicSyntaxTree.ParseText(text);
176173

177174
try
@@ -190,7 +187,7 @@ public static async Task<List<CodeItem>> MapDocument(VisualStudioWorkspace works
190187
return rootVB.Members.Select(MapMember).ToList();
191188
default:
192189
return new List<CodeItem>();
193-
}
190+
}
194191
}
195192

196193
public static CodeItem MapMember(MemberDeclarationSyntax member)
Lines changed: 75 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -1,62 +1,58 @@
11
using System;
22
using CodeNav.Helpers;
3-
using EnvDTE;
4-
using Microsoft.VisualStudio.LanguageServices;
53
using Microsoft.VisualStudio.Text.Editor;
64
using Microsoft.VisualStudio.Text.Outlining;
7-
using Microsoft.VisualStudio.TextManager.Interop;
8-
using DefGuidList = Microsoft.VisualStudio.Editor.DefGuidList;
9-
using AsyncTask = System.Threading.Tasks.Task;
105
using System.Linq;
116
using System.Runtime.InteropServices;
127
using Community.VisualStudio.Toolkit;
138
using Microsoft.VisualStudio.Shell;
14-
using WindowEvents = Community.VisualStudio.Toolkit.WindowEvents;
15-
using DocumentEvents = Community.VisualStudio.Toolkit.DocumentEvents;
9+
using Microsoft.VisualStudio.Imaging;
10+
using System.Threading.Tasks;
11+
using System.Windows;
12+
using System.Threading;
13+
using Task = System.Threading.Tasks.Task;
1614

1715
namespace CodeNav.ToolWindow
1816
{
19-
[Guid("88d7674e-67d3-4835-9e0e-aa893dfc985a")]
20-
public class CodeNavToolWindow : ToolWindowPane
17+
public class CodeNavToolWindow : BaseToolWindow<CodeNavToolWindow>
2118
{
22-
private readonly CodeViewUserControl _control;
23-
private WindowEvents _windowEvents;
24-
private DocumentEvents _documentEvents;
25-
private VisualStudioWorkspace _workspace;
26-
27-
/// <summary>
28-
/// Initializes a new instance of the <see cref="CodeNavToolWindow"/> class.
29-
/// </summary>
30-
public CodeNavToolWindow() : base(null)
31-
{
32-
Caption = "CodeNav";
33-
_control = new CodeViewUserControl(null);
34-
Content = _control;
35-
}
19+
private CodeViewUserControl _control;
3620

37-
public override void OnToolWindowCreated()
38-
{
39-
var codeNavToolWindowPackage = Package as CodeNavToolWindowPackage;
40-
_workspace = codeNavToolWindowPackage.ComponentModel.GetService<VisualStudioWorkspace>();
21+
public override string GetTitle(int toolWindowId) => "CodeNav";
4122

23+
public override Type PaneType => typeof(Pane);
24+
25+
public override async Task<FrameworkElement> CreateAsync(int toolWindowId, CancellationToken cancellationToken)
26+
{
4227
RegisterEvents();
4328

44-
_control.ShowWaitingForDocument();
29+
await Package.JoinableTaskFactory.SwitchToMainThreadAsync();
30+
31+
_control = new CodeViewUserControl();
32+
33+
_control.CodeDocumentViewModel.CodeDocument = PlaceholderHelper.CreateSelectDocumentItem();
34+
35+
return _control;
4536
}
4637

47-
private void RegisterEvents()
38+
[Guid("88d7674e-67d3-4835-9e0e-aa893dfc985a")]
39+
public class Pane : ToolWindowPane
4840
{
49-
_documentEvents = VS.Events.DocumentEvents;
50-
_documentEvents.Saved += DocumentEvents_Saved;
51-
_documentEvents.Opened += DocumentEvents_Opened;
52-
53-
_windowEvents = VS.Events.WindowEvents;
54-
_windowEvents.FrameIsVisibleChanged += WindowEvents_FrameIsVisibleChanged;
41+
public Pane()
42+
{
43+
BitmapImageMoniker = KnownMonikers.DocumentOutline;
44+
}
5545
}
5646

57-
private void WindowEvents_FrameIsVisibleChanged(FrameVisibilityEventArgs obj)
58-
=> WindowEvents_WindowActivated();
47+
private void RegisterEvents()
48+
{
49+
VS.Events.DocumentEvents.Saved += DocumentEvents_Saved;
50+
VS.Events.DocumentEvents.Opened += DocumentEvents_Opened;
51+
VS.Events.WindowEvents.ActiveFrameChanged += WindowEvents_ActiveFrameChanged;
52+
}
5953

54+
private void WindowEvents_ActiveFrameChanged(ActiveFrameChangeEventArgs obj)
55+
=> _ = WindowEvents_WindowActivated(obj);
6056

6157
private void DocumentEvents_Opened(object sender, string e)
6258
=> UpdateDocument();
@@ -70,34 +66,50 @@ private void OutliningManager_RegionsCollapsed(object sender, RegionsCollapsedEv
7066
private void OutliningManager_RegionsExpanded(object sender, RegionsExpandedEventArgs e)
7167
=> _control.RegionsExpanded(e);
7268

73-
private void WindowEvents_WindowActivated()
69+
private async Task WindowEvents_WindowActivated(ActiveFrameChangeEventArgs obj)
7470
{
75-
// Wire up reference for Caret events
76-
var textViewHost = GetCurrentViewHost();
77-
if (textViewHost != null)
71+
if (obj.OldFrame == obj.NewFrame)
72+
{
73+
return;
74+
}
75+
76+
var documentView = await obj.NewFrame.GetDocumentViewAsync();
77+
78+
var filePath = documentView?.Document?.FilePath;
79+
80+
if (string.IsNullOrEmpty(filePath))
7881
{
79-
textViewHost.TextView.Caret.PositionChanged += Caret_PositionChanged;
80-
81-
if (Properties.Settings.Default.ShowHistoryIndicators)
82-
{
83-
textViewHost.TextView.TextBuffer.ChangedLowPriority += TextBuffer_ChangedLowPriority;
84-
}
85-
86-
// Subscribe to Outlining events
87-
var outliningManagerService = OutliningHelper.GetOutliningManagerService(Package as IServiceProvider);
88-
var outliningManager = OutliningHelper.GetOutliningManager(outliningManagerService, GetCurrentViewHost().TextView);
89-
90-
if (outliningManager != null && outliningManagerService != null)
91-
{
92-
_control.OutliningManagerService = outliningManagerService;
93-
outliningManager.RegionsExpanded -= OutliningManager_RegionsExpanded;
94-
outliningManager.RegionsExpanded += OutliningManager_RegionsExpanded;
95-
outliningManager.RegionsCollapsed -= OutliningManager_RegionsCollapsed;
96-
outliningManager.RegionsCollapsed += OutliningManager_RegionsCollapsed;
97-
}
82+
return;
9883
}
9984

100-
UpdateDocument();
85+
var textView = documentView?.TextView;
86+
87+
if (textView == null)
88+
{
89+
return;
90+
}
91+
92+
textView.Caret.PositionChanged += Caret_PositionChanged;
93+
94+
if (Properties.Settings.Default.ShowHistoryIndicators)
95+
{
96+
textView.TextBuffer.ChangedLowPriority += TextBuffer_ChangedLowPriority;
97+
}
98+
99+
// Subscribe to Outlining events
100+
var outliningManagerService = OutliningHelper.GetOutliningManagerService(Package);
101+
var outliningManager = OutliningHelper.GetOutliningManager(outliningManagerService, textView);
102+
103+
if (outliningManager != null && outliningManagerService != null)
104+
{
105+
_control.OutliningManagerService = outliningManagerService;
106+
outliningManager.RegionsExpanded -= OutliningManager_RegionsExpanded;
107+
outliningManager.RegionsExpanded += OutliningManager_RegionsExpanded;
108+
outliningManager.RegionsCollapsed -= OutliningManager_RegionsCollapsed;
109+
outliningManager.RegionsCollapsed += OutliningManager_RegionsCollapsed;
110+
}
111+
112+
UpdateDocument(filePath);
101113
}
102114

103115
private void TextBuffer_ChangedLowPriority(object sender, Microsoft.VisualStudio.Text.TextContentChangedEventArgs e)
@@ -112,39 +124,16 @@ private void TextBuffer_ChangedLowPriority(object sender, Microsoft.VisualStudio
112124

113125
private void Caret_PositionChanged(object sender, CaretPositionChangedEventArgs e) => _control.HighlightCurrentItem();
114126

115-
private void UpdateDocument(bool forceUpdate = false)
127+
private void UpdateDocument(string filePath = "", bool forceUpdate = false)
116128
{
117129
try
118130
{
119-
_control.SetWorkspace(_workspace);
120-
_ = _control.UpdateDocument(forceUpdate: forceUpdate);
131+
_ = _control.UpdateDocument(filePath, forceUpdate);
121132
}
122133
catch (Exception e)
123134
{
124135
LogHelper.Log("Error updating document in ToolWindow", e);
125136
}
126137
}
127-
128-
private IWpfTextViewHost GetCurrentViewHost()
129-
{
130-
// code to get access to the editor's currently selected text cribbed from
131-
// http://msdn.microsoft.com/en-us/library/dd884850.aspx
132-
133-
var txtMgr = (IVsTextManager)GetService(typeof(SVsTextManager));
134-
var mustHaveFocus = 1;
135-
txtMgr.GetActiveView(mustHaveFocus, null, out IVsTextView vTextView);
136-
137-
if (!(vTextView is IVsUserData userData))
138-
{
139-
return null;
140-
}
141-
142-
IWpfTextViewHost viewHost;
143-
Guid guidViewHost = DefGuidList.guidIWpfTextViewHost;
144-
userData.GetData(ref guidViewHost, out var holder);
145-
viewHost = (IWpfTextViewHost)holder;
146-
147-
return viewHost;
148-
}
149138
}
150139
}

0 commit comments

Comments
 (0)