Skip to content

Commit ac1741f

Browse files
Merge pull request #270 from X-Sharp/master
Added code to limit the # of calls for DocumentEvents.Opened and Docu…
2 parents 2d3406d + cdf1373 commit ac1741f

File tree

2 files changed

+49
-10
lines changed

2 files changed

+49
-10
lines changed

demo/VSSDK.TestExtension/TestExtensionPackage.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ protected override async Task InitializeAsync(CancellationToken cancellationToke
3737
await this.RegisterCommandsAsync();
3838

3939
VS.Events.DocumentEvents.AfterDocumentWindowHide += DocumentEvents_AfterDocumentWindowHide;
40+
VS.Events.DocumentEvents.Opened += DocumentEvents_Opened;
41+
VS.Events.DocumentEvents.Closed += DocumentEvents_Closed;
4042
VS.Events.DocumentEvents.BeforeDocumentWindowShow += DocumentEvents_BeforeDocumentWindowShow;
4143
VS.Events.ProjectItemsEvents.AfterRenameProjectItems += ProjectItemsEvents_AfterRenameProjectItems;
4244
VS.Events.ProjectItemsEvents.AfterRemoveProjectItems += ProjectItemsEvents_AfterRemoveProjectItems;
@@ -46,6 +48,16 @@ protected override async Task InitializeAsync(CancellationToken cancellationToke
4648
VS.Events.BuildEvents.SolutionConfigurationChanged += BuildEvents_SolutionConfigurationChanged;
4749
}
4850

51+
private void DocumentEvents_Closed(string obj)
52+
{
53+
VS.StatusBar.ShowMessageAsync("Closed document " + obj ?? "no name").FireAndForget();
54+
}
55+
56+
private void DocumentEvents_Opened(string obj)
57+
{
58+
VS.StatusBar.ShowMessageAsync("Opened document " + obj ?? "no name").FireAndForget();
59+
}
60+
4961
private void BuildEvents_SolutionConfigurationChanged()
5062
{
5163
VS.StatusBar.ShowMessageAsync("Solution configuration changed").FireAndForget();

src/toolkit/Community.VisualStudio.Toolkit.Shared/Documents/DocumentEvents.cs

Lines changed: 37 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,25 @@ internal DocumentEvents()
3636
/// <summary>
3737
/// Fires after the document was opened in the editor.
3838
/// </summary>
39+
/// <remarks>
40+
/// The event is called for documents in the document well but also
41+
/// for project files and may also be called for solution files.<br/>
42+
/// The document name may also be a special (generated name) in the form of
43+
/// RDT_PROJ_MK::{A2FE74E1-B743-11d0-AE1A-00A0C90FFFC3} that is used by
44+
/// Visual Studio for the 'miscellaneous files' project
45+
/// </remarks>
3946
public event Action<string>? Opened;
4047

4148
/// <summary>
42-
/// Fires after the document was closed.s
49+
/// Fires after the document was closed.
4350
/// </summary>
51+
/// <remarks>
52+
/// The event is called for documents in the document well but also
53+
/// for project files and may also be called for solution files. <br/>
54+
/// The document name may also be a special (generated name) in the form of
55+
/// RDT_PROJ_MK::{A2FE74E1-B743-11d0-AE1A-00A0C90FFFC3} that is used by
56+
/// Visual Studio for the 'miscellaneous files' project
57+
/// </remarks>
4458
public event Action<string>? Closed;
4559

4660
/// <summary>
@@ -55,24 +69,37 @@ internal DocumentEvents()
5569

5670
int IVsRunningDocTableEvents.OnAfterFirstDocumentLock(uint docCookie, uint dwRDTLockType, uint dwReadLocksRemaining, uint dwEditLocksRemaining)
5771
{
58-
if (Opened != null)
72+
// Please note that this event is called multiple times when a document
73+
// is opened for editing.
74+
// This code tries to only call the Open Event once
75+
//
76+
if (dwEditLocksRemaining == 1 && dwReadLocksRemaining == 0)
5977
{
60-
string file = _rdt.GetDocumentInfo(docCookie).Moniker;
61-
Opened.Invoke(file);
78+
if (Opened != null)
79+
{
80+
string file = _rdt.GetDocumentInfo(docCookie).Moniker;
81+
Opened.Invoke(file);
82+
}
6283
}
6384

6485
return VSConstants.S_OK;
6586
}
6687

6788
int IVsRunningDocTableEvents.OnBeforeLastDocumentUnlock(uint docCookie, uint dwRDTLockType, uint dwReadLocksRemaining, uint dwEditLocksRemaining)
6889
{
69-
if (Closed != null)
90+
// Please note that this event is called multiple times when a document
91+
// is opened for editing.
92+
// This code tries to only call the Close Event once
93+
if (dwReadLocksRemaining == 0 && dwEditLocksRemaining == 0)
7094
{
71-
string file = _rdt.GetDocumentInfo(docCookie).Moniker;
72-
73-
if (!string.IsNullOrEmpty(file))
95+
if (Closed != null)
7496
{
75-
Closed.Invoke(file);
97+
string file = _rdt.GetDocumentInfo(docCookie).Moniker;
98+
99+
if (!string.IsNullOrEmpty(file))
100+
{
101+
Closed.Invoke(file);
102+
}
76103
}
77104
}
78105

@@ -97,7 +124,7 @@ int IVsRunningDocTableEvents.OnAfterAttributeChange(uint docCookie, uint grfAttr
97124

98125
int IVsRunningDocTableEvents.OnBeforeDocumentWindowShow(uint docCookie, int fFirstShow, IVsWindowFrame pFrame)
99126
{
100-
if (BeforeDocumentWindowShow != null)
127+
if (BeforeDocumentWindowShow != null && fFirstShow == 1)
101128
{
102129
DocumentView docView = new(pFrame);
103130
BeforeDocumentWindowShow.Invoke(docView);

0 commit comments

Comments
 (0)