Skip to content

Commit b16b89d

Browse files
Merge pull request #74542 from CyrusNajmabadi/saveFix
2 parents ed5fa5e + 093c8ff commit b16b89d

File tree

7 files changed

+45
-49
lines changed

7 files changed

+45
-49
lines changed

src/VisualStudio/Core/Def/ProjectSystem/IOpenTextBufferEventListener.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,12 @@ internal interface IOpenTextBufferEventListener
2929
/// <param name="moniker">the moniker of the closed document.</param>
3030
void OnCloseDocument(string moniker);
3131

32+
/// <summary>
33+
/// Triggered when a document is saved.
34+
/// </summary>
35+
/// <param name="moniker">the moniker of the saved document.</param>
36+
void OnSaveDocument(string moniker);
37+
3238
/// <summary>
3339
/// Triggered when a document context is refreshed with a new hierarchy.
3440
/// </summary>

src/VisualStudio/Core/Def/ProjectSystem/MiscellaneousFilesWorkspace.cs

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -81,17 +81,9 @@ public async Task InitializeAsync()
8181
}
8282

8383
void IOpenTextBufferEventListener.OnOpenDocument(string moniker, ITextBuffer textBuffer, IVsHierarchy _) => TrackOpenedDocument(moniker, textBuffer);
84-
void IOpenTextBufferEventListener.OnDocumentOpenedIntoWindowFrame(string moniker, IVsWindowFrame windowFrame) { }
8584

8685
void IOpenTextBufferEventListener.OnCloseDocument(string moniker) => TryUntrackClosingDocument(moniker);
8786

88-
/// <summary>
89-
/// File hierarchy events are not relevant to the misc workspace.
90-
/// </summary>
91-
void IOpenTextBufferEventListener.OnRefreshDocumentContext(string moniker, IVsHierarchy hierarchy)
92-
{
93-
}
94-
9587
void IOpenTextBufferEventListener.OnRenameDocument(string newMoniker, string oldMoniker, ITextBuffer buffer)
9688
{
9789
// We want to consider this file to be added in one of two situations:
@@ -106,6 +98,21 @@ void IOpenTextBufferEventListener.OnRenameDocument(string newMoniker, string old
10698
}
10799
}
108100

101+
/// <summary>
102+
/// Not relevant to the misc workspace.
103+
/// </summary>
104+
void IOpenTextBufferEventListener.OnRefreshDocumentContext(string moniker, IVsHierarchy hierarchy) { }
105+
106+
/// <summary>
107+
/// Not relevant to the misc workspace.
108+
/// </summary>
109+
void IOpenTextBufferEventListener.OnDocumentOpenedIntoWindowFrame(string moniker, IVsWindowFrame windowFrame) { }
110+
111+
/// <summary>
112+
/// Not relevant to the misc workspace.
113+
/// </summary>
114+
void IOpenTextBufferEventListener.OnSaveDocument(string moniker) { }
115+
109116
public void RegisterLanguage(Guid languageGuid, string languageName, string scriptExtension)
110117
=> _languageInformationByLanguageGuid.Add(languageGuid, new LanguageInformation(languageName, scriptExtension));
111118

src/VisualStudio/Core/Def/ProjectSystem/OpenTextBufferProvider.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,15 @@ public int OnBeforeLastDocumentUnlock(uint docCookie, uint dwRDTLockType, uint d
131131
}
132132

133133
public int OnAfterSave(uint docCookie)
134-
=> VSConstants.E_NOTIMPL;
134+
{
135+
if (_runningDocumentTable.IsDocumentInitialized(docCookie))
136+
{
137+
var moniker = _runningDocumentTable.GetDocumentMoniker(docCookie);
138+
RaiseEventForEachListener(l => l.OnSaveDocument(moniker));
139+
}
140+
141+
return VSConstants.S_OK;
142+
}
135143

136144
public int OnAfterAttributeChange(uint docCookie, uint grfAttribs)
137145
=> VSConstants.E_NOTIMPL;

src/VisualStudio/Core/Def/ProjectSystem/VisualStudioWorkspaceImpl.OpenFileTracker.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ internal partial class VisualStudioWorkspaceImpl
3131
/// <summary>
3232
/// Singleton the updates the workspace in response to files being opened or closed.
3333
/// </summary>
34-
public sealed class OpenFileTracker : IOpenTextBufferEventListener
34+
public sealed partial class OpenFileTracker : IOpenTextBufferEventListener
3535
{
3636
private readonly VisualStudioWorkspaceImpl _workspace;
3737
private readonly ProjectSystemProjectFactory _projectSystemProjectFactory;

src/VisualStudio/Core/Def/ProjectSystem/VisualStudioWorkspaceImpl_SourceGenerators.cs

Lines changed: 7 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,11 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33
// See the LICENSE file in the project root for more information.
44

5-
using System;
6-
using System.ComponentModel.Composition;
75
using System.Linq;
8-
using Microsoft.CodeAnalysis.Editor;
96
using Microsoft.CodeAnalysis.Editor.Shared.Extensions;
107
using Microsoft.CodeAnalysis.Host;
11-
using Microsoft.CodeAnalysis.Host.Mef;
12-
using Microsoft.CodeAnalysis.Text;
13-
using Microsoft.VisualStudio.Commanding;
148
using Microsoft.VisualStudio.Shell;
159
using Microsoft.VisualStudio.Shell.Interop;
16-
using Microsoft.VisualStudio.Text.Editor.Commanding.Commands;
17-
using Microsoft.VisualStudio.Utilities;
1810

1911
namespace Microsoft.VisualStudio.LanguageServices.Implementation.ProjectSystem;
2012

@@ -69,31 +61,16 @@ public void SubscribeToSourceGeneratorImpactingEvents()
6961
this.EnqueueUpdateSourceGeneratorVersion(projectId: null, forceRegeneration: false);
7062
}
7163

72-
[Export(typeof(ICommandHandler))]
73-
[ContentType(ContentTypeNames.RoslynContentType)]
74-
[ContentType(ContentTypeNames.XamlContentType)]
75-
[Name(PredefinedCommandHandlerNames.SourceGeneratorSave)]
76-
[method: ImportingConstructor]
77-
[method: Obsolete(MefConstruction.ImportingConstructorMessage, error: true)]
78-
internal sealed class SaveCommandHandler() : IChainedCommandHandler<SaveCommandArgs>
64+
public sealed partial class OpenFileTracker
7965
{
80-
public string DisplayName => ServicesVSResources.Roslyn_save_command_handler;
81-
82-
public CommandState GetCommandState(SaveCommandArgs args, Func<CommandState> nextCommandHandler)
83-
=> nextCommandHandler();
84-
85-
public void ExecuteCommand(SaveCommandArgs args, Action nextCommandHandler, CommandExecutionContext executionContext)
66+
void IOpenTextBufferEventListener.OnSaveDocument(string moniker)
8667
{
87-
nextCommandHandler();
68+
// Note: this will find docs, additional docs, and analyzer config docs. Thats good. We do want changing
69+
// any of those to cause rerunning generators in any affected project.
70+
var documentIds = _workspace.CurrentSolution.GetDocumentIdsWithFilePath(moniker);
8871

89-
// After a save happens, enqueue a request to run generators on the projects impacted by the save.
90-
foreach (var projectGroup in args.SubjectBuffer.GetRelatedDocuments().GroupBy(d => d.Project))
91-
{
92-
if (projectGroup.Key.Solution.Workspace is VisualStudioWorkspaceImpl visualStudioWorkspace)
93-
{
94-
visualStudioWorkspace.EnqueueUpdateSourceGeneratorVersion(projectGroup.Key.Id, forceRegeneration: false);
95-
}
96-
}
72+
foreach (var projectId in documentIds.Select(i => i.ProjectId).Distinct())
73+
_workspace.EnqueueUpdateSourceGeneratorVersion(projectId, forceRegeneration: false);
9774
}
9875
}
9976
}

src/VisualStudio/Core/Def/Workspace/SourceGeneratedFileManager.cs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -224,13 +224,9 @@ void IOpenTextBufferEventListener.OnCloseDocument(string moniker)
224224
}
225225
}
226226

227-
void IOpenTextBufferEventListener.OnRefreshDocumentContext(string moniker, IVsHierarchy hierarchy)
228-
{
229-
}
230-
231-
void IOpenTextBufferEventListener.OnRenameDocument(string newMoniker, string oldMoniker, ITextBuffer textBuffer)
232-
{
233-
}
227+
void IOpenTextBufferEventListener.OnRefreshDocumentContext(string moniker, IVsHierarchy hierarchy) { }
228+
void IOpenTextBufferEventListener.OnRenameDocument(string newMoniker, string oldMoniker, ITextBuffer textBuffer) { }
229+
void IOpenTextBufferEventListener.OnSaveDocument(string moniker) { }
234230

235231
private sealed class OpenSourceGeneratedFile : IDisposable
236232
{

src/VisualStudio/LiveShare/Impl/Client/RemoteLanguageServiceWorkspace.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,8 +98,6 @@ public RemoteLanguageServiceWorkspace(
9898

9999
void IOpenTextBufferEventListener.OnOpenDocument(string moniker, ITextBuffer textBuffer, IVsHierarchy? hierarchy) => NotifyOnDocumentOpened(moniker, textBuffer);
100100

101-
void IOpenTextBufferEventListener.OnDocumentOpenedIntoWindowFrame(string moniker, IVsWindowFrame windowFrame) { }
102-
103101
void IOpenTextBufferEventListener.OnCloseDocument(string moniker) => NotifyOnDocumentClosing(moniker);
104102

105103
void IOpenTextBufferEventListener.OnRefreshDocumentContext(string moniker, IVsHierarchy hierarchy)
@@ -112,6 +110,10 @@ void IOpenTextBufferEventListener.OnRenameDocument(string newMoniker, string old
112110
// Handled by Add/Remove.
113111
}
114112

113+
void IOpenTextBufferEventListener.OnDocumentOpenedIntoWindowFrame(string moniker, IVsWindowFrame windowFrame) { }
114+
115+
void IOpenTextBufferEventListener.OnSaveDocument(string moniker) { }
116+
115117
public async Task SetSessionAsync(CollaborationSession session)
116118
{
117119
_session = session;

0 commit comments

Comments
 (0)