From 32bc44a974da18ff3a3d0c0b0c45163a2a902307 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Matou=C5=A1ek?= Date: Wed, 7 Jul 2021 09:05:38 -0700 Subject: [PATCH] Capture file content on open instead of save (#54643) --- .../EditAndContinueSaveFileCommandHandler.cs | 62 ------------------- .../EditAndContinueWorkspaceService.cs | 2 +- ...lStudioWorkspaceEditAndContinueListener.cs | 56 +++++++++++++++++ 3 files changed, 57 insertions(+), 63 deletions(-) delete mode 100644 src/EditorFeatures/Core/Implementation/EditAndContinue/EditAndContinueSaveFileCommandHandler.cs create mode 100644 src/VisualStudio/Core/Def/Implementation/EditAndContinue/VisualStudioWorkspaceEditAndContinueListener.cs diff --git a/src/EditorFeatures/Core/Implementation/EditAndContinue/EditAndContinueSaveFileCommandHandler.cs b/src/EditorFeatures/Core/Implementation/EditAndContinue/EditAndContinueSaveFileCommandHandler.cs deleted file mode 100644 index a8c80e2830e9b..0000000000000 --- a/src/EditorFeatures/Core/Implementation/EditAndContinue/EditAndContinueSaveFileCommandHandler.cs +++ /dev/null @@ -1,62 +0,0 @@ -// Licensed to the .NET Foundation under one or more agreements. -// The .NET Foundation licenses this file to you under the MIT license. -// See the LICENSE file in the project root for more information. - -using System; -using System.ComponentModel.Composition; -using System.Threading; -using System.Threading.Tasks; -using Microsoft.CodeAnalysis.EditAndContinue; -using Microsoft.CodeAnalysis.Host.Mef; -using Microsoft.CodeAnalysis.Text; -using Microsoft.VisualStudio.Commanding; -using Microsoft.VisualStudio.Text.Editor.Commanding.Commands; -using Microsoft.VisualStudio.Utilities; -using Roslyn.Utilities; -using VSCommanding = Microsoft.VisualStudio.Commanding; - -namespace Microsoft.CodeAnalysis.Editor.Implementation.EditAndContinue -{ - [Export] - [Export(typeof(VSCommanding.ICommandHandler))] - [ContentType(ContentTypeNames.RoslynContentType)] - [Name(PredefinedCommandHandlerNames.EditAndContinueFileSave)] - internal sealed class EditAndContinueSaveFileCommandHandler : IChainedCommandHandler - { - [ImportingConstructor] - [Obsolete(MefConstruction.ImportingConstructorMessage, error: true)] - public EditAndContinueSaveFileCommandHandler() - { - } - - public string DisplayName => PredefinedCommandHandlerNames.EditAndContinueFileSave; - - void IChainedCommandHandler.ExecuteCommand(SaveCommandArgs args, Action nextCommandHandler, CommandExecutionContext executionContext) - { - var textContainer = args.SubjectBuffer.AsTextContainer(); - - if (Workspace.TryGetWorkspace(textContainer, out var workspace)) - { - var documentId = workspace.GetDocumentIdInCurrentContext(textContainer); - if (documentId != null) - { - // ignoring source-generated files since they shouldn't be modified and saved: - var currentDocument = workspace.CurrentSolution.GetDocument(documentId); - if (currentDocument != null) - { - var proxy = new RemoteEditAndContinueServiceProxy(workspace); - - // fire and forget - _ = Task.Run(() => proxy.OnSourceFileUpdatedAsync(currentDocument, CancellationToken.None)).ReportNonFatalErrorAsync(); - } - } - } - - nextCommandHandler(); - } - - public VSCommanding.CommandState GetCommandState(SaveCommandArgs args, Func nextCommandHandler) - => nextCommandHandler(); - } -} - diff --git a/src/Features/Core/Portable/EditAndContinue/EditAndContinueWorkspaceService.cs b/src/Features/Core/Portable/EditAndContinue/EditAndContinueWorkspaceService.cs index 06459d0085077..a2970db04abf6 100644 --- a/src/Features/Core/Portable/EditAndContinue/EditAndContinueWorkspaceService.cs +++ b/src/Features/Core/Portable/EditAndContinue/EditAndContinueWorkspaceService.cs @@ -97,7 +97,7 @@ public void OnSourceFileUpdated(Document document) foreach (var debuggingSession in GetActiveDebuggingSessions()) { // fire and forget - _ = Task.Run(() => debuggingSession.OnSourceFileUpdatedAsync(document)); + _ = Task.Run(() => debuggingSession.OnSourceFileUpdatedAsync(document)).ReportNonFatalErrorAsync(); } } diff --git a/src/VisualStudio/Core/Def/Implementation/EditAndContinue/VisualStudioWorkspaceEditAndContinueListener.cs b/src/VisualStudio/Core/Def/Implementation/EditAndContinue/VisualStudioWorkspaceEditAndContinueListener.cs new file mode 100644 index 0000000000000..bd737a536714d --- /dev/null +++ b/src/VisualStudio/Core/Def/Implementation/EditAndContinue/VisualStudioWorkspaceEditAndContinueListener.cs @@ -0,0 +1,56 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System; +using System.Composition; +using System.Threading; +using System.Threading.Tasks; +using Microsoft.CodeAnalysis; +using Microsoft.CodeAnalysis.EditAndContinue; +using Microsoft.CodeAnalysis.Host; +using Microsoft.CodeAnalysis.Host.Mef; +using Roslyn.Utilities; + +namespace Microsoft.VisualStudio.LanguageServices.EditAndContinue +{ + /// + /// Connects to the ServiceHub services. + /// Launches ServiceHub if it is not running yet and starts services that push information from to the ServiceHub process. + /// + [ExportEventListener(WellKnownEventListeners.Workspace, WorkspaceKind.Host), Shared] + internal sealed class VisualStudioWorkspaceEditAndContinueListener : IEventListener, IEventListenerStoppable + { + [ImportingConstructor] + [Obsolete(MefConstruction.ImportingConstructorMessage, error: true)] + public VisualStudioWorkspaceEditAndContinueListener() + { + } + + public void StartListening(Workspace workspace, object serviceOpt) + { + if (workspace is not VisualStudioWorkspace) + { + return; + } + + workspace.DocumentOpened += WorkspaceDocumentOpened; + } + + public void StopListening(Workspace workspace) + { + if (workspace is not VisualStudioWorkspace) + { + return; + } + + workspace.DocumentOpened -= WorkspaceDocumentOpened; + } + + private void WorkspaceDocumentOpened(object? sender, DocumentEventArgs e) + { + var proxy = new RemoteEditAndContinueServiceProxy(e.Document.Project.Solution.Workspace); + _ = Task.Run(() => proxy.OnSourceFileUpdatedAsync(e.Document, CancellationToken.None)).ReportNonFatalErrorAsync(); + } + } +}