Remove ProjectService requirements for Razor text buffer initialization.

- Now that we have our own content type the editor does the work of determining if a Razor file opened in a core project should flow to our code.
This commit is contained in:
N. Taylor Mullen 2018-05-03 11:09:01 -07:00
parent e0612d7e07
commit dbed73da32
2 changed files with 7 additions and 81 deletions

View File

@ -17,13 +17,11 @@ namespace Microsoft.VisualStudio.Editor.Razor
{
private readonly ForegroundDispatcher _foregroundDispatcher;
private readonly RazorEditorFactoryService _editorFactoryService;
private readonly TextBufferProjectService _projectService;
[ImportingConstructor]
public DefaultRazorDocumentManager(
ForegroundDispatcher dispatcher,
RazorEditorFactoryService editorFactoryService,
TextBufferProjectService projectService)
RazorEditorFactoryService editorFactoryService)
{
if (dispatcher == null)
{
@ -35,14 +33,8 @@ namespace Microsoft.VisualStudio.Editor.Razor
throw new ArgumentNullException(nameof(editorFactoryService));
}
if (projectService == null)
{
throw new ArgumentNullException(nameof(projectService));
}
_foregroundDispatcher = dispatcher;
_editorFactoryService = editorFactoryService;
_projectService = projectService;
}
public override void OnTextViewOpened(ITextView textView, IEnumerable<ITextBuffer> subjectBuffers)
@ -66,11 +58,6 @@ namespace Microsoft.VisualStudio.Editor.Razor
continue;
}
if (!IsSupportedProject(textBuffer))
{
return;
}
if (!_editorFactoryService.TryGetDocumentTracker(textBuffer, out var documentTracker) ||
!(documentTracker is DefaultVisualStudioDocumentTracker tracker))
{
@ -120,25 +107,5 @@ namespace Microsoft.VisualStudio.Editor.Razor
}
}
}
private bool IsSupportedProject(ITextBuffer textBuffer)
{
// Fundamentally we have a Razor half of the world as soon as the document is open - and then later
// the C# half of the world will be initialized. This code is in general pretty tolerant of
// unexpected /impossible states.
//
// We also want to successfully shut down if the buffer is something other than .cshtml.
object project = null;
var isSupportedProject = false;
// We expect the document to have a hierarchy even if it's not a real 'project'.
// However the hierarchy can be null when the document is in the process of closing.
if ((project = _projectService.GetHostProject(textBuffer)) != null)
{
isSupportedProject = _projectService.IsSupportedProject(project);
}
return isSupportedProject;
}
}
}

View File

@ -33,35 +33,12 @@ namespace Microsoft.VisualStudio.Editor.Razor
private Workspace Workspace => TestWorkspace.Create();
private TextBufferProjectService SupportedProjectService { get; } = Mock.Of<TextBufferProjectService>(
s => s.GetHostProject(It.IsAny<ITextBuffer>()) == Mock.Of<object>() &&
s.IsSupportedProject(It.IsAny<object>()) == true &&
s.GetProjectPath(It.IsAny<object>()) == "C:/Some/Path/TestProject.csproj");
private TextBufferProjectService UnsupportedProjectService { get; } = Mock.Of<TextBufferProjectService>(s => s.IsSupportedProject(It.IsAny<object>()) == false);
[ForegroundFact]
public void OnTextViewOpened_ForNonRazorCoreProject_DoesNothing()
{
// Arrange
var editorFactoryService = new Mock<RazorEditorFactoryService>(MockBehavior.Strict);
var documentManager = new DefaultRazorDocumentManager(Dispatcher, editorFactoryService.Object, UnsupportedProjectService);
var textView = Mock.Of<ITextView>();
var buffers = new Collection<ITextBuffer>()
{
Mock.Of<ITextBuffer>(b => b.ContentType == RazorCoreContentType && b.Properties == new PropertyCollection()),
};
// Act & Assert
documentManager.OnTextViewOpened(textView, buffers);
}
[ForegroundFact]
public void OnTextViewOpened_ForNonRazorTextBuffer_DoesNothing()
{
// Arrange
var editorFactoryService = new Mock<RazorEditorFactoryService>(MockBehavior.Strict);
var documentManager = new DefaultRazorDocumentManager(Dispatcher, editorFactoryService.Object, SupportedProjectService);
var documentManager = new DefaultRazorDocumentManager(Dispatcher, editorFactoryService.Object);
var textView = Mock.Of<ITextView>();
var buffers = new Collection<ITextBuffer>()
{
@ -83,7 +60,7 @@ namespace Microsoft.VisualStudio.Editor.Razor
};
var documentTracker = new DefaultVisualStudioDocumentTracker(Dispatcher, FilePath, ProjectPath, ProjectManager, WorkspaceEditorSettings, Workspace, buffers[0], ImportDocumentManager) as VisualStudioDocumentTracker;
var editorFactoryService = Mock.Of<RazorEditorFactoryService>(factoryService => factoryService.TryGetDocumentTracker(It.IsAny<ITextBuffer>(), out documentTracker) == true);
var documentManager = new DefaultRazorDocumentManager(Dispatcher, editorFactoryService, SupportedProjectService);
var documentManager = new DefaultRazorDocumentManager(Dispatcher, editorFactoryService);
// Act
documentManager.OnTextViewOpened(textView, buffers);
@ -104,7 +81,7 @@ namespace Microsoft.VisualStudio.Editor.Razor
};
var documentTracker = new DefaultVisualStudioDocumentTracker(Dispatcher, FilePath, ProjectPath, ProjectManager, WorkspaceEditorSettings, Workspace, buffers[0], ImportDocumentManager) as VisualStudioDocumentTracker;
var editorFactoryService = Mock.Of<RazorEditorFactoryService>(f => f.TryGetDocumentTracker(It.IsAny<ITextBuffer>(), out documentTracker) == true);
var documentManager = new DefaultRazorDocumentManager(Dispatcher, editorFactoryService, SupportedProjectService);
var documentManager = new DefaultRazorDocumentManager(Dispatcher, editorFactoryService);
// Assert 1
Assert.False(documentTracker.IsSupportedProject);
@ -116,29 +93,11 @@ namespace Microsoft.VisualStudio.Editor.Razor
Assert.True(documentTracker.IsSupportedProject);
}
[ForegroundFact]
public void OnTextViewClosed_FoNonRazorCoreProject_DoesNothing()
{
// Arrange
var documentManager = new DefaultRazorDocumentManager(Dispatcher, Mock.Of<RazorEditorFactoryService>(), UnsupportedProjectService);
var textView = Mock.Of<ITextView>();
var buffers = new Collection<ITextBuffer>()
{
Mock.Of<ITextBuffer>(b => b.ContentType == RazorCoreContentType && b.Properties == new PropertyCollection()),
};
// Act
documentManager.OnTextViewClosed(textView, buffers);
// Assert
Assert.False(buffers[0].Properties.ContainsProperty(typeof(VisualStudioDocumentTracker)));
}
[ForegroundFact]
public void OnTextViewClosed_TextViewWithoutDocumentTracker_DoesNothing()
{
// Arrange
var documentManager = new DefaultRazorDocumentManager(Dispatcher, Mock.Of<RazorEditorFactoryService>(), SupportedProjectService);
var documentManager = new DefaultRazorDocumentManager(Dispatcher, Mock.Of<RazorEditorFactoryService>());
var textView = Mock.Of<ITextView>();
var buffers = new Collection<ITextBuffer>()
{
@ -176,7 +135,7 @@ namespace Microsoft.VisualStudio.Editor.Razor
buffers[1].Properties.AddProperty(typeof(VisualStudioDocumentTracker), documentTracker);
var editorFactoryService = Mock.Of<RazorEditorFactoryService>();
var documentManager = new DefaultRazorDocumentManager(Dispatcher, editorFactoryService, SupportedProjectService);
var documentManager = new DefaultRazorDocumentManager(Dispatcher, editorFactoryService);
// Act
documentManager.OnTextViewClosed(textView2, buffers);
@ -203,7 +162,7 @@ namespace Microsoft.VisualStudio.Editor.Razor
var documentTracker = new DefaultVisualStudioDocumentTracker(Dispatcher, FilePath, ProjectPath, ProjectManager, WorkspaceEditorSettings, Workspace, buffers[0], ImportDocumentManager);
buffers[0].Properties.AddProperty(typeof(VisualStudioDocumentTracker), documentTracker);
var editorFactoryService = Mock.Of<RazorEditorFactoryService>();
var documentManager = new DefaultRazorDocumentManager(Dispatcher, editorFactoryService, SupportedProjectService);
var documentManager = new DefaultRazorDocumentManager(Dispatcher, editorFactoryService);
// Populate the text views
documentTracker.Subscribe();