Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
// 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.Collections.Generic;
using System.Text;
using Microsoft.AspNetCore.Mvc.Razor.Extensions;
using Microsoft.AspNetCore.Razor.Language;
using Microsoft.AspNetCore.Razor.PooledObjects;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.Razor;

namespace Microsoft.NET.Sdk.Razor.SourceGenerators
{
public partial class RazorSourceGenerator
{
internal static string GetIdentifierFromPath(string filePath)
internal static string GetIdentifierFromPath(ReadOnlySpan<char> filePath)
{
var builder = new StringBuilder(filePath.Length);
using var _ = StringBuilderPool.GetPooledObject(out var builder);

for (var i = 0; i < filePath.Length; i++)
{
Expand All @@ -31,6 +32,7 @@ internal static string GetIdentifierFromPath(string filePath)
}

builder.Append(".g.cs");

return builder.ToString();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System;
using System.Diagnostics.CodeAnalysis;
using System.IO;
using Microsoft.AspNetCore.Razor;
using Microsoft.NET.Sdk.Razor.SourceGenerators;

Expand All @@ -25,8 +25,18 @@ public static bool TryComputeHintNameFromRazorDocument(this TextDocument razorDo
return false;
}

var projectBasePath = Path.GetDirectoryName(razorDocument.Project.FilePath).AssumeNotNull();
var relativeDocumentPath = razorDocument.FilePath[projectBasePath.Length..].TrimStart('/', '\\');
var filePath = razorDocument.FilePath.AsSpanOrDefault();
var projectFilePath = razorDocument.Project.FilePath.AsSpanOrDefault();
var projectBasePath = PathUtilities.GetDirectoryName(projectFilePath);
if (filePath.Length <= projectBasePath.Length)
{
// File must be from outside the project directory
hintName = null;
return false;
}

var relativeDocumentPath = filePath[projectBasePath.Length..].TrimStart(['/', '\\']);

hintName = RazorSourceGenerator.GetIdentifierFromPath(relativeDocumentPath);

return hintName is not null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,10 @@ public void Execute()
// ret
Marshal.WriteByte(functionPointer, 0xC3);
break;

case Architecture.Arm64:
// This place is not a place of honor. No highly esteemed deed is commemorated here.
Marshal.WriteInt64(functionPointer, 0xD65F03C0);
break;
default:
throw new NotSupportedException();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.IO;
using Microsoft.AspNetCore.Razor.Test.Common;
using Microsoft.AspNetCore.Razor.Test.Common.VisualStudio;
using Microsoft.CodeAnalysis;
using Xunit;
using Xunit.Abstractions;

namespace Microsoft.VisualStudio.LanguageServices.Razor.Test.Cohost;

public class TextDocumentExtensionsTest(ITestOutputHelper testOutput) : VisualStudioWorkspaceTestBase(testOutput)
{
[Theory]
[InlineData(@"Pages\Index.razor")]
[InlineData(@"Pages/Index.razor")]
[InlineData(@"Pages.Index.razor")]
public void TryComputeHintNameFromRazorDocument(string razorFilePath)
{
var projectId = ProjectId.CreateNewId();
var projectInfo = ProjectInfo
.Create(
projectId,
VersionStamp.Create(),
name: "Project",
assemblyName: "Project",
LanguageNames.CSharp,
TestProjectData.SomeProject.FilePath);

var documentId = DocumentId.CreateNewId(projectId);
var solution = Workspace.CurrentSolution
.AddProject(projectInfo)
.AddAdditionalDocument(documentId, "File.razor", "", filePath: Path.Combine(TestProjectData.SomeProjectPath, razorFilePath));

var document = solution.GetAdditionalDocument(documentId);

Assert.NotNull(document);
Assert.True(document.TryComputeHintNameFromRazorDocument(out var hintName));
// This tests TryComputeHintNameFromRazorDocument and also neatly demonstrates a bug: https://github.com/dotnet/razor/issues/11578
Assert.Equal("Pages_Index_razor.g.cs", hintName);
}
}