Skip to content

Commit

Permalink
Add .razor files as dynamic items for Razor Components support (#4523)
Browse files Browse the repository at this point in the history
  • Loading branch information
davidwengier authored Feb 9, 2019
1 parent d4730b4 commit 50a3f40
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,22 @@ public static IWorkspaceProjectContext Create()
return new IWorkspaceProjectContextMock().Object;
}

public static IWorkspaceProjectContext CreateForDynamicFiles(UnconfiguredProject project, Action<string> addDynamicFile = null)
{
var context = new IWorkspaceProjectContextMock();

context.SetupGet(c => c.ProjectFilePath)
.Returns(project.FullPath);

if (addDynamicFile != null)
{
context.Setup(c => c.AddDynamicFile(It.IsAny<string>(), It.IsAny<IEnumerable<string>>()))
.Callback<string, IEnumerable<string>>((p1, p2) => addDynamicFile(p1));
}

return context.Object;
}

public static IWorkspaceProjectContext CreateForSourceFiles(UnconfiguredProject project, Action<string> addSourceFile = null, Action<string> removeSourceFile = null)
{
var context = new IWorkspaceProjectContextMock();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,39 @@
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System.Collections.Generic;
using Microsoft.VisualStudio.LanguageServices.ProjectSystem;
using Xunit;

namespace Microsoft.VisualStudio.ProjectSystem.LanguageServices.Handlers
{
public class DynamicItemHandlerTests : EvaluationHandlerTestBase
{
[Fact]
public void Handle_RazorAndCshtmlFiles_AddsToContext()
{
var dynamicFiles = new HashSet<string>(StringComparers.Paths);
void onDynamicFileAdded(string s) => Assert.True(dynamicFiles.Add(s));

var project = UnconfiguredProjectFactory.Create(filePath: @"C:\Myproject.csproj");
var context = IWorkspaceProjectContextMockFactory.CreateForDynamicFiles(project, onDynamicFileAdded);

var handler = CreateInstance(project, context);

var projectChange = IProjectChangeDescriptionFactory.FromJson(
@"{
""Difference"": {
""AnyChanges"": true,
""AddedItems"": [ ""File1.razor"", ""File1.cshtml"", ""File1.cs"" ]
}
}");

Handle(handler, projectChange);

Assert.Equal(2, dynamicFiles.Count);
Assert.Contains(@"C:\File1.razor", dynamicFiles);
Assert.Contains(@"C:\File1.cshtml", dynamicFiles);
}

internal override IProjectEvaluationHandler CreateInstance()
{
return CreateInstance(null, null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ namespace Microsoft.VisualStudio.ProjectSystem.LanguageServices.Handlers
[Export(typeof(IWorkspaceContextHandler))]
internal class DynamicItemHandler : AbstractWorkspaceContextHandler, IProjectEvaluationHandler
{
private const string DynamicFileExtension = ".cshtml";
private const string RazorPagesExtension = ".cshtml";
private const string RazorComponentsExtension = ".razor";
private readonly UnconfiguredProject _project;
private readonly HashSet<string> _paths = new HashSet<string>(StringComparers.Paths);

Expand Down Expand Up @@ -104,7 +105,8 @@ private void RemoveFromContextIfPresent(string includePath, IProjectLogger logge
private static bool IsDynamicFile(string includePath)
{
// Note a file called just '.cshtml' is still considered a Razor file
return includePath.EndsWith(DynamicFileExtension, StringComparison.OrdinalIgnoreCase);
return includePath.EndsWith(RazorPagesExtension, StringComparison.OrdinalIgnoreCase) ||
includePath.EndsWith(RazorComponentsExtension, StringComparison.OrdinalIgnoreCase);
}
}
}

0 comments on commit 50a3f40

Please sign in to comment.