From f708c463d9a7603786a8e5d670dd9901e48c0a74 Mon Sep 17 00:00:00 2001 From: Ajay Bhargav Baaskaran Date: Wed, 6 Dec 2017 16:54:43 -0800 Subject: [PATCH] Export RazorDocumentManager using roslyn services --- .../DefaultRazorDocumentManager.cs | 46 +++-------------- .../DefaultRazorDocumentManagerFactory.cs | 49 +++++++++++++++++++ .../RazorDocumentManager.cs | 3 +- .../RazorTextViewConnectionListener.cs | 13 ++--- .../DefaultRazorDocumentManagerTest.cs | 16 +++--- 5 files changed, 71 insertions(+), 56 deletions(-) create mode 100644 src/Microsoft.VisualStudio.Editor.Razor/DefaultRazorDocumentManagerFactory.cs diff --git a/src/Microsoft.VisualStudio.Editor.Razor/DefaultRazorDocumentManager.cs b/src/Microsoft.VisualStudio.Editor.Razor/DefaultRazorDocumentManager.cs index 768d2523e8..13f1c15e6d 100644 --- a/src/Microsoft.VisualStudio.Editor.Razor/DefaultRazorDocumentManager.cs +++ b/src/Microsoft.VisualStudio.Editor.Razor/DefaultRazorDocumentManager.cs @@ -3,7 +3,6 @@ using System; using System.Collections.Generic; -using System.ComponentModel.Composition; using System.Diagnostics; using Microsoft.CodeAnalysis.Razor; using Microsoft.VisualStudio.Text; @@ -11,20 +10,22 @@ using Microsoft.VisualStudio.Text.Editor; namespace Microsoft.VisualStudio.Editor.Razor { - [System.Composition.Shared] - [Export(typeof(RazorDocumentManager))] internal class DefaultRazorDocumentManager : RazorDocumentManager { private readonly ForegroundDispatcher _foregroundDispatcher; private readonly RazorEditorFactoryService _editorFactoryService; private readonly TextBufferProjectService _projectService; - [ImportingConstructor] public DefaultRazorDocumentManager( + ForegroundDispatcher dispatcher, RazorEditorFactoryService editorFactoryService, - TextBufferProjectService projectService, - VisualStudioWorkspaceAccessor workspaceAccessor) + TextBufferProjectService projectService) { + if (dispatcher == null) + { + throw new ArgumentNullException(nameof(dispatcher)); + } + if (editorFactoryService == null) { throw new ArgumentNullException(nameof(editorFactoryService)); @@ -35,40 +36,9 @@ namespace Microsoft.VisualStudio.Editor.Razor throw new ArgumentNullException(nameof(projectService)); } - if (workspaceAccessor == null) - { - throw new ArgumentNullException(nameof(workspaceAccessor)); - } - + _foregroundDispatcher = dispatcher; _editorFactoryService = editorFactoryService; _projectService = projectService; - _foregroundDispatcher = workspaceAccessor.Workspace.Services.GetRequiredService(); - } - - // This is only for testing. We want to avoid using the actual Roslyn GetService methods in unit tests. - internal DefaultRazorDocumentManager( - RazorEditorFactoryService editorFactoryService, - TextBufferProjectService projectService, - ForegroundDispatcher foregroundDispatcher) - { - if (editorFactoryService == null) - { - throw new ArgumentNullException(nameof(editorFactoryService)); - } - - if (projectService == null) - { - throw new ArgumentNullException(nameof(projectService)); - } - - if (foregroundDispatcher == null) - { - throw new ArgumentNullException(nameof(foregroundDispatcher)); - } - - _editorFactoryService = editorFactoryService; - _projectService = projectService; - _foregroundDispatcher = foregroundDispatcher; } public override void OnTextViewOpened(ITextView textView, IEnumerable subjectBuffers) diff --git a/src/Microsoft.VisualStudio.Editor.Razor/DefaultRazorDocumentManagerFactory.cs b/src/Microsoft.VisualStudio.Editor.Razor/DefaultRazorDocumentManagerFactory.cs new file mode 100644 index 0000000000..ac6eb886bd --- /dev/null +++ b/src/Microsoft.VisualStudio.Editor.Razor/DefaultRazorDocumentManagerFactory.cs @@ -0,0 +1,49 @@ +// Copyright (c) .NET Foundation. All rights reserved. +// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. + +using System; +using System.Composition; +using Microsoft.CodeAnalysis.Host; +using Microsoft.CodeAnalysis.Host.Mef; +using Microsoft.CodeAnalysis.Razor; + +namespace Microsoft.VisualStudio.Editor.Razor +{ + [Shared] + [ExportLanguageServiceFactory(typeof(RazorDocumentManager), RazorLanguage.Name, ServiceLayer.Default)] + internal class DefaultRazorDocumentManagerFactory : ILanguageServiceFactory + { + private readonly RazorEditorFactoryService _editorFactoryService; + private readonly TextBufferProjectService _projectService; + + [ImportingConstructor] + public DefaultRazorDocumentManagerFactory( + RazorEditorFactoryService editorFactoryService, + TextBufferProjectService projectService) + { + if (editorFactoryService == null) + { + throw new ArgumentNullException(nameof(editorFactoryService)); + } + + if (projectService == null) + { + throw new ArgumentNullException(nameof(projectService)); + } + + _editorFactoryService = editorFactoryService; + _projectService = projectService; + } + + public ILanguageService CreateLanguageService(HostLanguageServices languageServices) + { + if (languageServices == null) + { + throw new ArgumentNullException(nameof(languageServices)); + } + + var dispatcher = languageServices.WorkspaceServices.GetRequiredService(); + return new DefaultRazorDocumentManager(dispatcher, _editorFactoryService, _projectService); + } + } +} diff --git a/src/Microsoft.VisualStudio.Editor.Razor/RazorDocumentManager.cs b/src/Microsoft.VisualStudio.Editor.Razor/RazorDocumentManager.cs index 0e59a391bb..2f4dd3c577 100644 --- a/src/Microsoft.VisualStudio.Editor.Razor/RazorDocumentManager.cs +++ b/src/Microsoft.VisualStudio.Editor.Razor/RazorDocumentManager.cs @@ -2,12 +2,13 @@ // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. using System.Collections.Generic; +using Microsoft.CodeAnalysis.Host; using Microsoft.VisualStudio.Text; using Microsoft.VisualStudio.Text.Editor; namespace Microsoft.VisualStudio.Editor.Razor { - internal abstract class RazorDocumentManager + internal abstract class RazorDocumentManager : ILanguageService { public abstract void OnTextViewOpened(ITextView textView, IEnumerable subjectBuffers); diff --git a/src/Microsoft.VisualStudio.Editor.Razor/RazorTextViewConnectionListener.cs b/src/Microsoft.VisualStudio.Editor.Razor/RazorTextViewConnectionListener.cs index fe1ed057a9..7fefae0842 100644 --- a/src/Microsoft.VisualStudio.Editor.Razor/RazorTextViewConnectionListener.cs +++ b/src/Microsoft.VisualStudio.Editor.Razor/RazorTextViewConnectionListener.cs @@ -20,22 +20,17 @@ namespace Microsoft.VisualStudio.Editor.Razor private readonly RazorDocumentManager _documentManager; [ImportingConstructor] - public RazorTextViewConnectionListener( - VisualStudioWorkspaceAccessor workspaceAccessor, - RazorDocumentManager documentManager) + public RazorTextViewConnectionListener(VisualStudioWorkspaceAccessor workspaceAccessor) { if (workspaceAccessor == null) { throw new ArgumentNullException(nameof(workspaceAccessor)); } - if (documentManager == null) - { - throw new ArgumentNullException(nameof(documentManager)); - } - - _documentManager = documentManager; _foregroundDispatcher = workspaceAccessor.Workspace.Services.GetRequiredService(); + + var languageServices = workspaceAccessor.Workspace.Services.GetLanguageServices(RazorLanguage.Name); + _documentManager = languageServices.GetRequiredService(); } // This is only for testing. We want to avoid using the actual Roslyn GetService methods in unit tests. diff --git a/test/Microsoft.VisualStudio.Editor.Razor.Test/DefaultRazorDocumentManagerTest.cs b/test/Microsoft.VisualStudio.Editor.Razor.Test/DefaultRazorDocumentManagerTest.cs index 2c9db9152a..0ce1f73545 100644 --- a/test/Microsoft.VisualStudio.Editor.Razor.Test/DefaultRazorDocumentManagerTest.cs +++ b/test/Microsoft.VisualStudio.Editor.Razor.Test/DefaultRazorDocumentManagerTest.cs @@ -45,7 +45,7 @@ namespace Microsoft.VisualStudio.Editor.Razor { // Arrange var editorFactoryService = new Mock(MockBehavior.Strict); - var documentManager = new DefaultRazorDocumentManager(editorFactoryService.Object, UnsupportedProjectService, Dispatcher); + var documentManager = new DefaultRazorDocumentManager(Dispatcher, editorFactoryService.Object, UnsupportedProjectService); var textView = Mock.Of(); var buffers = new Collection() { @@ -61,7 +61,7 @@ namespace Microsoft.VisualStudio.Editor.Razor { // Arrange var editorFactoryService = new Mock(MockBehavior.Strict); - var documentManager = new DefaultRazorDocumentManager(editorFactoryService.Object, SupportedProjectService, Dispatcher); + var documentManager = new DefaultRazorDocumentManager(Dispatcher, editorFactoryService.Object, SupportedProjectService); var textView = Mock.Of(); var buffers = new Collection() { @@ -83,7 +83,7 @@ namespace Microsoft.VisualStudio.Editor.Razor }; var documentTracker = new DefaultVisualStudioDocumentTracker(FilePath, ProjectPath, ProjectManager, EditorSettingsManager, Workspace, buffers[0], ImportDocumentManager) as VisualStudioDocumentTracker; var editorFactoryService = Mock.Of(factoryService => factoryService.TryGetDocumentTracker(It.IsAny(), out documentTracker) == true); - var documentManager = new DefaultRazorDocumentManager(editorFactoryService, SupportedProjectService, Dispatcher); + var documentManager = new DefaultRazorDocumentManager(Dispatcher, editorFactoryService, SupportedProjectService); // Act documentManager.OnTextViewOpened(textView, buffers); @@ -104,7 +104,7 @@ namespace Microsoft.VisualStudio.Editor.Razor }; var documentTracker = new DefaultVisualStudioDocumentTracker(FilePath, ProjectPath, ProjectManager, EditorSettingsManager, Workspace, buffers[0], ImportDocumentManager) as VisualStudioDocumentTracker; var editorFactoryService = Mock.Of(f => f.TryGetDocumentTracker(It.IsAny(), out documentTracker) == true); - var documentManager = new DefaultRazorDocumentManager(editorFactoryService, SupportedProjectService, Dispatcher); + var documentManager = new DefaultRazorDocumentManager(Dispatcher, editorFactoryService, SupportedProjectService); // Assert 1 Assert.False(documentTracker.IsSupportedProject); @@ -120,7 +120,7 @@ namespace Microsoft.VisualStudio.Editor.Razor public void OnTextViewClosed_FoNonRazorCoreProject_DoesNothing() { // Arrange - var documentManager = new DefaultRazorDocumentManager(Mock.Of(), UnsupportedProjectService, Dispatcher); + var documentManager = new DefaultRazorDocumentManager(Dispatcher, Mock.Of(), UnsupportedProjectService); var textView = Mock.Of(); var buffers = new Collection() { @@ -138,7 +138,7 @@ namespace Microsoft.VisualStudio.Editor.Razor public void OnTextViewClosed_TextViewWithoutDocumentTracker_DoesNothing() { // Arrange - var documentManager = new DefaultRazorDocumentManager(Mock.Of(), SupportedProjectService, Dispatcher); + var documentManager = new DefaultRazorDocumentManager(Dispatcher, Mock.Of(), SupportedProjectService); var textView = Mock.Of(); var buffers = new Collection() { @@ -176,7 +176,7 @@ namespace Microsoft.VisualStudio.Editor.Razor buffers[1].Properties.AddProperty(typeof(VisualStudioDocumentTracker), documentTracker); var editorFactoryService = Mock.Of(); - var documentManager = new DefaultRazorDocumentManager(editorFactoryService, SupportedProjectService, Dispatcher); + var documentManager = new DefaultRazorDocumentManager(Dispatcher, editorFactoryService, SupportedProjectService); // Act documentManager.OnTextViewClosed(textView2, buffers); @@ -203,7 +203,7 @@ namespace Microsoft.VisualStudio.Editor.Razor var documentTracker = new DefaultVisualStudioDocumentTracker(FilePath, ProjectPath, ProjectManager, EditorSettingsManager, Workspace, buffers[0], ImportDocumentManager); buffers[0].Properties.AddProperty(typeof(VisualStudioDocumentTracker), documentTracker); var editorFactoryService = Mock.Of(); - var documentManager = new DefaultRazorDocumentManager(editorFactoryService, SupportedProjectService, Dispatcher); + var documentManager = new DefaultRazorDocumentManager(Dispatcher, editorFactoryService, SupportedProjectService); // Populate the text views documentTracker.Subscribe();