Export RazorDocumentManager using roslyn services

This commit is contained in:
Ajay Bhargav Baaskaran 2017-12-06 16:54:43 -08:00
parent 7cd1b6a5d0
commit f708c463d9
5 changed files with 71 additions and 56 deletions

View File

@ -3,7 +3,6 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.Diagnostics; using System.Diagnostics;
using Microsoft.CodeAnalysis.Razor; using Microsoft.CodeAnalysis.Razor;
using Microsoft.VisualStudio.Text; using Microsoft.VisualStudio.Text;
@ -11,20 +10,22 @@ using Microsoft.VisualStudio.Text.Editor;
namespace Microsoft.VisualStudio.Editor.Razor namespace Microsoft.VisualStudio.Editor.Razor
{ {
[System.Composition.Shared]
[Export(typeof(RazorDocumentManager))]
internal class DefaultRazorDocumentManager : RazorDocumentManager internal class DefaultRazorDocumentManager : RazorDocumentManager
{ {
private readonly ForegroundDispatcher _foregroundDispatcher; private readonly ForegroundDispatcher _foregroundDispatcher;
private readonly RazorEditorFactoryService _editorFactoryService; private readonly RazorEditorFactoryService _editorFactoryService;
private readonly TextBufferProjectService _projectService; private readonly TextBufferProjectService _projectService;
[ImportingConstructor]
public DefaultRazorDocumentManager( public DefaultRazorDocumentManager(
ForegroundDispatcher dispatcher,
RazorEditorFactoryService editorFactoryService, RazorEditorFactoryService editorFactoryService,
TextBufferProjectService projectService, TextBufferProjectService projectService)
VisualStudioWorkspaceAccessor workspaceAccessor)
{ {
if (dispatcher == null)
{
throw new ArgumentNullException(nameof(dispatcher));
}
if (editorFactoryService == null) if (editorFactoryService == null)
{ {
throw new ArgumentNullException(nameof(editorFactoryService)); throw new ArgumentNullException(nameof(editorFactoryService));
@ -35,40 +36,9 @@ namespace Microsoft.VisualStudio.Editor.Razor
throw new ArgumentNullException(nameof(projectService)); throw new ArgumentNullException(nameof(projectService));
} }
if (workspaceAccessor == null) _foregroundDispatcher = dispatcher;
{
throw new ArgumentNullException(nameof(workspaceAccessor));
}
_editorFactoryService = editorFactoryService; _editorFactoryService = editorFactoryService;
_projectService = projectService; _projectService = projectService;
_foregroundDispatcher = workspaceAccessor.Workspace.Services.GetRequiredService<ForegroundDispatcher>();
}
// 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<ITextBuffer> subjectBuffers) public override void OnTextViewOpened(ITextView textView, IEnumerable<ITextBuffer> subjectBuffers)

View File

@ -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<ForegroundDispatcher>();
return new DefaultRazorDocumentManager(dispatcher, _editorFactoryService, _projectService);
}
}
}

View File

@ -2,12 +2,13 @@
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System.Collections.Generic; using System.Collections.Generic;
using Microsoft.CodeAnalysis.Host;
using Microsoft.VisualStudio.Text; using Microsoft.VisualStudio.Text;
using Microsoft.VisualStudio.Text.Editor; using Microsoft.VisualStudio.Text.Editor;
namespace Microsoft.VisualStudio.Editor.Razor namespace Microsoft.VisualStudio.Editor.Razor
{ {
internal abstract class RazorDocumentManager internal abstract class RazorDocumentManager : ILanguageService
{ {
public abstract void OnTextViewOpened(ITextView textView, IEnumerable<ITextBuffer> subjectBuffers); public abstract void OnTextViewOpened(ITextView textView, IEnumerable<ITextBuffer> subjectBuffers);

View File

@ -20,22 +20,17 @@ namespace Microsoft.VisualStudio.Editor.Razor
private readonly RazorDocumentManager _documentManager; private readonly RazorDocumentManager _documentManager;
[ImportingConstructor] [ImportingConstructor]
public RazorTextViewConnectionListener( public RazorTextViewConnectionListener(VisualStudioWorkspaceAccessor workspaceAccessor)
VisualStudioWorkspaceAccessor workspaceAccessor,
RazorDocumentManager documentManager)
{ {
if (workspaceAccessor == null) if (workspaceAccessor == null)
{ {
throw new ArgumentNullException(nameof(workspaceAccessor)); throw new ArgumentNullException(nameof(workspaceAccessor));
} }
if (documentManager == null)
{
throw new ArgumentNullException(nameof(documentManager));
}
_documentManager = documentManager;
_foregroundDispatcher = workspaceAccessor.Workspace.Services.GetRequiredService<ForegroundDispatcher>(); _foregroundDispatcher = workspaceAccessor.Workspace.Services.GetRequiredService<ForegroundDispatcher>();
var languageServices = workspaceAccessor.Workspace.Services.GetLanguageServices(RazorLanguage.Name);
_documentManager = languageServices.GetRequiredService<RazorDocumentManager>();
} }
// This is only for testing. We want to avoid using the actual Roslyn GetService methods in unit tests. // This is only for testing. We want to avoid using the actual Roslyn GetService methods in unit tests.

View File

@ -45,7 +45,7 @@ namespace Microsoft.VisualStudio.Editor.Razor
{ {
// Arrange // Arrange
var editorFactoryService = new Mock<RazorEditorFactoryService>(MockBehavior.Strict); var editorFactoryService = new Mock<RazorEditorFactoryService>(MockBehavior.Strict);
var documentManager = new DefaultRazorDocumentManager(editorFactoryService.Object, UnsupportedProjectService, Dispatcher); var documentManager = new DefaultRazorDocumentManager(Dispatcher, editorFactoryService.Object, UnsupportedProjectService);
var textView = Mock.Of<ITextView>(); var textView = Mock.Of<ITextView>();
var buffers = new Collection<ITextBuffer>() var buffers = new Collection<ITextBuffer>()
{ {
@ -61,7 +61,7 @@ namespace Microsoft.VisualStudio.Editor.Razor
{ {
// Arrange // Arrange
var editorFactoryService = new Mock<RazorEditorFactoryService>(MockBehavior.Strict); var editorFactoryService = new Mock<RazorEditorFactoryService>(MockBehavior.Strict);
var documentManager = new DefaultRazorDocumentManager(editorFactoryService.Object, SupportedProjectService, Dispatcher); var documentManager = new DefaultRazorDocumentManager(Dispatcher, editorFactoryService.Object, SupportedProjectService);
var textView = Mock.Of<ITextView>(); var textView = Mock.Of<ITextView>();
var buffers = new Collection<ITextBuffer>() var buffers = new Collection<ITextBuffer>()
{ {
@ -83,7 +83,7 @@ namespace Microsoft.VisualStudio.Editor.Razor
}; };
var documentTracker = new DefaultVisualStudioDocumentTracker(FilePath, ProjectPath, ProjectManager, EditorSettingsManager, Workspace, buffers[0], ImportDocumentManager) as VisualStudioDocumentTracker; var documentTracker = new DefaultVisualStudioDocumentTracker(FilePath, ProjectPath, ProjectManager, EditorSettingsManager, Workspace, buffers[0], ImportDocumentManager) as VisualStudioDocumentTracker;
var editorFactoryService = Mock.Of<RazorEditorFactoryService>(factoryService => factoryService.TryGetDocumentTracker(It.IsAny<ITextBuffer>(), out documentTracker) == true); var editorFactoryService = Mock.Of<RazorEditorFactoryService>(factoryService => factoryService.TryGetDocumentTracker(It.IsAny<ITextBuffer>(), out documentTracker) == true);
var documentManager = new DefaultRazorDocumentManager(editorFactoryService, SupportedProjectService, Dispatcher); var documentManager = new DefaultRazorDocumentManager(Dispatcher, editorFactoryService, SupportedProjectService);
// Act // Act
documentManager.OnTextViewOpened(textView, buffers); 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 documentTracker = new DefaultVisualStudioDocumentTracker(FilePath, ProjectPath, ProjectManager, EditorSettingsManager, Workspace, buffers[0], ImportDocumentManager) as VisualStudioDocumentTracker;
var editorFactoryService = Mock.Of<RazorEditorFactoryService>(f => f.TryGetDocumentTracker(It.IsAny<ITextBuffer>(), out documentTracker) == true); var editorFactoryService = Mock.Of<RazorEditorFactoryService>(f => f.TryGetDocumentTracker(It.IsAny<ITextBuffer>(), out documentTracker) == true);
var documentManager = new DefaultRazorDocumentManager(editorFactoryService, SupportedProjectService, Dispatcher); var documentManager = new DefaultRazorDocumentManager(Dispatcher, editorFactoryService, SupportedProjectService);
// Assert 1 // Assert 1
Assert.False(documentTracker.IsSupportedProject); Assert.False(documentTracker.IsSupportedProject);
@ -120,7 +120,7 @@ namespace Microsoft.VisualStudio.Editor.Razor
public void OnTextViewClosed_FoNonRazorCoreProject_DoesNothing() public void OnTextViewClosed_FoNonRazorCoreProject_DoesNothing()
{ {
// Arrange // Arrange
var documentManager = new DefaultRazorDocumentManager(Mock.Of<RazorEditorFactoryService>(), UnsupportedProjectService, Dispatcher); var documentManager = new DefaultRazorDocumentManager(Dispatcher, Mock.Of<RazorEditorFactoryService>(), UnsupportedProjectService);
var textView = Mock.Of<ITextView>(); var textView = Mock.Of<ITextView>();
var buffers = new Collection<ITextBuffer>() var buffers = new Collection<ITextBuffer>()
{ {
@ -138,7 +138,7 @@ namespace Microsoft.VisualStudio.Editor.Razor
public void OnTextViewClosed_TextViewWithoutDocumentTracker_DoesNothing() public void OnTextViewClosed_TextViewWithoutDocumentTracker_DoesNothing()
{ {
// Arrange // Arrange
var documentManager = new DefaultRazorDocumentManager(Mock.Of<RazorEditorFactoryService>(), SupportedProjectService, Dispatcher); var documentManager = new DefaultRazorDocumentManager(Dispatcher, Mock.Of<RazorEditorFactoryService>(), SupportedProjectService);
var textView = Mock.Of<ITextView>(); var textView = Mock.Of<ITextView>();
var buffers = new Collection<ITextBuffer>() var buffers = new Collection<ITextBuffer>()
{ {
@ -176,7 +176,7 @@ namespace Microsoft.VisualStudio.Editor.Razor
buffers[1].Properties.AddProperty(typeof(VisualStudioDocumentTracker), documentTracker); buffers[1].Properties.AddProperty(typeof(VisualStudioDocumentTracker), documentTracker);
var editorFactoryService = Mock.Of<RazorEditorFactoryService>(); var editorFactoryService = Mock.Of<RazorEditorFactoryService>();
var documentManager = new DefaultRazorDocumentManager(editorFactoryService, SupportedProjectService, Dispatcher); var documentManager = new DefaultRazorDocumentManager(Dispatcher, editorFactoryService, SupportedProjectService);
// Act // Act
documentManager.OnTextViewClosed(textView2, buffers); 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); var documentTracker = new DefaultVisualStudioDocumentTracker(FilePath, ProjectPath, ProjectManager, EditorSettingsManager, Workspace, buffers[0], ImportDocumentManager);
buffers[0].Properties.AddProperty(typeof(VisualStudioDocumentTracker), documentTracker); buffers[0].Properties.AddProperty(typeof(VisualStudioDocumentTracker), documentTracker);
var editorFactoryService = Mock.Of<RazorEditorFactoryService>(); var editorFactoryService = Mock.Of<RazorEditorFactoryService>();
var documentManager = new DefaultRazorDocumentManager(editorFactoryService, SupportedProjectService, Dispatcher); var documentManager = new DefaultRazorDocumentManager(Dispatcher, editorFactoryService, SupportedProjectService);
// Populate the text views // Populate the text views
documentTracker.Subscribe(); documentTracker.Subscribe();