-
Notifications
You must be signed in to change notification settings - Fork 4
/
CollapseLevel.cs
240 lines (203 loc) · 8.1 KB
/
CollapseLevel.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
using CollapseLevel.Core;
using Microsoft.VisualStudio.ComponentModelHost;
using Microsoft.VisualStudio.Shell;
using Microsoft.VisualStudio.Shell.Interop;
using Microsoft.VisualStudio.Text;
using Microsoft.VisualStudio.Text.Outlining;
using Microsoft.VisualStudio.TextManager.Interop;
using System;
using System.Collections.Generic;
using System.ComponentModel.Design;
namespace CollapseLevel
{
/// <summary>
/// Command handler
/// </summary>
internal sealed class CollapseLevel
{
/// <summary>
/// Command ID. Maps command number to the internal command id. <see cref="MenuItemCallback"/>
/// </summary>
public static readonly Dictionary<int, int> CommandIdMapping = new Dictionary<int, int>()
{
{0x101, 1},
{0x102, 2},
{0x103, 3},
{0x104, 4},
{0x105, 5},
{0x106, 6},
{0x107, 7},
{0x108, 8},
{0x109, 9},
};
/// <summary>
/// Command menu group.
/// </summary>
public static readonly Guid CommandSet = new Guid("40b66aa9-8017-4fb7-a3b5-765d2f97b2b1");
private const int MaxCollapseLevel = 30;
/// <summary>
/// VS Package that provides this command.
/// </summary>
private readonly Package package;
/// <summary>
/// Text manager to get information about active views.
/// </summary>
private readonly IVsTextManager textManager;
/// <summary>
/// Initializes a new instance of the <see cref="CollapseLevel"/> class.
/// Adds our command handlers for menu (commands must exist in the command table file)
/// </summary>
private CollapseLevel(Package package, IVsTextManager textManager)
{
this.package = package ?? throw new ArgumentNullException("package");
this.textManager = textManager;
OleMenuCommandService commandService = this.ServiceProvider.GetService(typeof(IMenuCommandService)) as OleMenuCommandService;
if (commandService != null)
{
foreach (var pair in CommandIdMapping)
{
var menuCommandID = new CommandID(CommandSet, pair.Key);
var menuItem = new MenuCommand(this.MenuItemCallback, menuCommandID);
commandService.AddCommand(menuItem);
}
}
}
/// <summary>
/// Gets the instance of the command.
/// </summary>
public static CollapseLevel Instance
{
get;
private set;
}
/// <summary>
/// Keep information about last input for "Custom level" command.
/// </summary>
private int? LastCustomLevel { get; set; }
private IOutliningManager OutliningManager { get; set; }
/// <summary>
/// Gets the service provider from the owner package.
/// </summary>
private IServiceProvider ServiceProvider
{
get
{
return this.package;
}
}
/// <summary>
/// Visual Studio's status bar. Allows to display status.
/// </summary>
private IVsStatusbar StatusBar
{
get
{
return (IVsStatusbar)ServiceProvider.GetService(typeof(SVsStatusbar));
}
}
/// <summary>
/// Initializes the singleton instance of the command.
/// </summary>
/// <param name="package">Owner package, not null.</param>
public static void Initialize(Package package, IVsTextManager textManager)
{
Instance = new CollapseLevel(package, textManager);
}
/// <summary>
/// Walk through all the regions, calculate their levels and collapse all regions where n = level +1.
/// </summary>
private void Collapse(int level)
{
var tv = TextViewRegistry.GetActiveTextView(this.textManager);
if (tv == null)
return;
var componentModel = this.ServiceProvider.GetService(typeof(SComponentModel)) as IComponentModel;
if (componentModel != null)
{
var outliningManagerService = componentModel.GetService<IOutliningManagerService>();
if (outliningManagerService != null)
OutliningManager = outliningManagerService.GetOutliningManager(tv);
}
if (OutliningManager != null)
{
try
{
var length = tv.TextSnapshot.Length;
var textViewSpan = new SnapshotSpan(tv.TextSnapshot, 0, length);
var regions = OutliningManager.GetAllRegions(textViewSpan);
Dictionary<ICollapsible, int> regionCount = new Dictionary<ICollapsible, int>();
foreach (var region in regions)
{
var extent = region.Extent;
var span = extent.GetSpan(tv.TextSnapshot);
foreach (var compareTo in regions)
{
if (compareTo.Equals(region))
continue;
if (!regionCount.ContainsKey(region))
regionCount[region] = 1;
var compareToSpan = compareTo.Extent.GetSpan(tv.TextSnapshot);
var regionSpan = region.Extent.GetSpan(tv.TextSnapshot);
if (compareToSpan.Contains(regionSpan))
{
regionCount[region] = regionCount[region] + 1;
}
}
}
OutliningManager.ExpandAll(textViewSpan, (y) => (true));
foreach (var pair in regionCount)
{
if (pair.Value == level)
{
OutliningManager.TryCollapse(pair.Key);
}
}
}
catch (ObjectDisposedException)
{
// TODO Investigate, happened once while testing some early version.
}
}
}
/// <summary>
/// This function is the callback used to execute the command when the menu item is clicked.
/// </summary>
private void MenuItemCallback(object sender, EventArgs e)
{
MenuCommand command = sender as MenuCommand;
int commandId = CommandIdMapping[command.CommandID.ID]; // Find folding level
switch (commandId)
{
case 9: // Custom level
var level = PromptForCustomLevel();
if (level != null)
{
Collapse(level.Value);
NotifySuccess(level.Value);
}
break;
default: // Levels mapped in *.vsct file - in this case commandId is the level.
Collapse(commandId);
NotifySuccess(commandId);
break;
}
}
private void NotifySuccess(int level)
{
StatusBar.SetText($"Collapse Level: Collapsed everything above level {level} .");
}
private int? PromptForCustomLevel()
{
int level = LastCustomLevel ?? 3;
var response = Microsoft.VisualBasic.Interaction.InputBox("Please specify collapse level", "Collapse Level", level.ToString(), -1, -1);
int readLevel;
if (string.IsNullOrWhiteSpace(response) || !int.TryParse(response, out readLevel) || !(readLevel >= 0 && readLevel <= MaxCollapseLevel))
{
StatusBar.SetText($"Collapse Level: Please specify number between 1 - {MaxCollapseLevel}");
return null;
}
LastCustomLevel = readLevel;
return readLevel;
}
}
}