get razor syntax highlighting from workspace

This commit is contained in:
Matt Parker
2025-09-16 18:54:50 +10:00
parent cc7e766966
commit f446ef1655
8 changed files with 115 additions and 72 deletions

View File

@@ -13,24 +13,27 @@
<PackageVersion Include="Krafs.Publicizer" Version="2.3.0" /> <PackageVersion Include="Krafs.Publicizer" Version="2.3.0" />
<PackageVersion Include="Microsoft.AspNetCore.Components" Version="10.0.0-rc.1.25451.107" /> <PackageVersion Include="Microsoft.AspNetCore.Components" Version="10.0.0-rc.1.25451.107" />
<PackageVersion Include="Microsoft.AspNetCore.Components.WebView" Version="10.0.0-rc.1.25451.107" /> <PackageVersion Include="Microsoft.AspNetCore.Components.WebView" Version="10.0.0-rc.1.25451.107" />
<PackageVersion Include="Microsoft.Build" Version="17.15.0-preview-25462-116" /> <PackageVersion Include="Microsoft.Build" Version="17.15.0-preview-25465-103" />
<PackageVersion Include="Microsoft.Build.Framework" Version="17.15.0-preview-25462-116" /> <PackageVersion Include="Microsoft.Build.Framework" Version="17.15.0-preview-25465-103" />
<PackageVersion Include="Microsoft.Build.Locator" Version="1.9.1" /> <PackageVersion Include="Microsoft.Build.Locator" Version="1.9.1" />
<PackageVersion Include="Microsoft.Build.Tasks.Core" Version="17.15.0-preview-25462-116" /> <PackageVersion Include="Microsoft.Build.Tasks.Core" Version="17.15.0-preview-25465-103" />
<PackageVersion Include="Microsoft.Build.Utilities.Core" Version="17.15.0-preview-25462-116" /> <PackageVersion Include="Microsoft.Build.Utilities.Core" Version="17.15.0-preview-25465-103" />
<PackageVersion Include="Microsoft.CodeAnalysis.Common" Version="5.0.0-2.25462.116" /> <PackageVersion Include="Microsoft.CodeAnalysis.Common" Version="5.0.0-2.25465.103" />
<PackageVersion Include="Microsoft.CodeAnalysis.CSharp" Version="5.0.0-2.25462.116" /> <PackageVersion Include="Microsoft.CodeAnalysis.CSharp" Version="5.0.0-2.25465.103" />
<PackageVersion Include="Microsoft.CodeAnalysis.CSharp.Features" Version="5.0.0-2.25462.116" /> <PackageVersion Include="Microsoft.CodeAnalysis.CSharp.Features" Version="5.0.0-2.25465.103" />
<PackageVersion Include="Microsoft.CodeAnalysis.CSharp.Workspaces" Version="5.0.0-2.25462.116" /> <PackageVersion Include="Microsoft.CodeAnalysis.CSharp.Workspaces" Version="5.0.0-2.25465.103" />
<PackageVersion Include="Microsoft.Extensions.ObjectPool" Version="8.0.0" /> <PackageVersion Include="Microsoft.Extensions.ObjectPool" Version="8.0.0" />
<PackageVersion Include="Microsoft.AspNetCore.Razor.Utilities.Shared" Version="10.0.0-preview.25429.2" /> <PackageVersion Include="Microsoft.AspNetCore.Razor.Utilities.Shared" Version="10.0.0-preview.25465.103" />
<PackageVersion Include="Microsoft.CodeAnalysis.Features" Version="5.0.0-2.25462.116" /> <PackageVersion Include="Microsoft.CodeAnalysis.Features" Version="5.0.0-2.25465.103" />
<PackageVersion Include="Microsoft.CodeAnalysis.Razor.Compiler" Version="10.0.0-preview.25429.2" /> <PackageVersion Include="IgnoresAccessChecksToGenerator" Version="0.8.0" />
<PackageVersion Include="Microsoft.CodeAnalysis.Razor.Workspaces" Version="10.0.0-preview.25429.2" /> <PackageVersion Include="Microsoft.VisualStudioCode.RazorExtension" Version="10.0.0-preview.25465.103" />
<PackageVersion Include="Microsoft.CodeAnalysis.Workspaces.MSBuild" Version="5.0.0-2.25462.116" /> <PackageVersion Include="Microsoft.CodeAnalysis.Razor.Compiler" Version="10.0.0-preview.25465.103" />
<PackageVersion Include="Microsoft.CodeAnalysis.Remote.Razor" Version="10.0.0-preview.25465.103" />
<PackageVersion Include="Microsoft.CodeAnalysis.Razor.Workspaces" Version="10.0.0-preview.25465.103" />
<PackageVersion Include="Microsoft.CodeAnalysis.Workspaces.MSBuild" Version="5.0.0-2.25465.103" />
<PackageVersion Include="Microsoft.Diagnostics.NETCore.Client" Version="0.2.645901" /> <PackageVersion Include="Microsoft.Diagnostics.NETCore.Client" Version="0.2.645901" />
<PackageVersion Include="Microsoft.DotNet.Arcade.Sdk" Version="9.0.0-beta.24372.7" /> <PackageVersion Include="Microsoft.DotNet.Arcade.Sdk" Version="9.0.0-beta.24372.7" />
<PackageVersion Include="Microsoft.Net.Compilers.Razor.Toolset" Version="10.0.0-preview.25429.2" /> <PackageVersion Include="Microsoft.Net.Compilers.Razor.Toolset" Version="10.0.0-preview.25465.103" />
<PackageVersion Include="Microsoft.VisualStudio.Shared.VSCodeDebugProtocol" Version="18.0.10427.1" /> <PackageVersion Include="Microsoft.VisualStudio.Shared.VSCodeDebugProtocol" Version="18.0.10427.1" />
<PackageVersion Include="Microsoft.VisualStudio.SolutionPersistence" Version="1.0.52" /> <PackageVersion Include="Microsoft.VisualStudio.SolutionPersistence" Version="1.0.52" />
<PackageVersion Include="MudBlazor" Version="8.12.0" /> <PackageVersion Include="MudBlazor" Version="8.12.0" />
@@ -41,7 +44,8 @@
<PackageVersion Include="R3" Version="1.3.0" /> <PackageVersion Include="R3" Version="1.3.0" />
<PackageVersion Include="XtermBlazor" Version="2.1.2" /> <PackageVersion Include="XtermBlazor" Version="2.1.2" />
</ItemGroup> </ItemGroup>
<ItemGroup Label="Pinned Transitive Dependencies"> <ItemGroup Label="Pinned Transitive Dependencies">
<PackageVersion Include="Microsoft.CodeAnalysis.ExternalAccess.Razor.Features" Version="5.0.0-2.25462.116" /> <PackageVersion Include="Microsoft.CodeAnalysis.ExternalAccess.Razor.Features" Version="5.0.0-2.25465.103" />
</ItemGroup> <PackageVersion Include="Microsoft.CodeAnalysis.Remote.ServiceHub" Version="5.0.0-2.25465.103" />
</ItemGroup>
</Project> </Project>

View File

@@ -1,4 +1,5 @@
using System.Collections.Immutable; using System.Collections.Immutable;
using System.Composition.Hosting;
using System.Diagnostics; using System.Diagnostics;
using Ardalis.GuardClauses; using Ardalis.GuardClauses;
using Microsoft.CodeAnalysis; using Microsoft.CodeAnalysis;
@@ -9,6 +10,7 @@ using Microsoft.CodeAnalysis.CodeRefactorings;
using Microsoft.CodeAnalysis.Completion; using Microsoft.CodeAnalysis.Completion;
using Microsoft.CodeAnalysis.Host.Mef; using Microsoft.CodeAnalysis.Host.Mef;
using Microsoft.CodeAnalysis.MSBuild; using Microsoft.CodeAnalysis.MSBuild;
using Microsoft.CodeAnalysis.Remote.Razor.ProjectSystem;
using Microsoft.CodeAnalysis.Text; using Microsoft.CodeAnalysis.Text;
using NuGet.Packaging; using NuGet.Packaging;
using SharpIDE.Application.Features.SolutionDiscovery; using SharpIDE.Application.Features.SolutionDiscovery;
@@ -20,6 +22,7 @@ namespace SharpIDE.Application.Features.Analysis;
public static class RoslynAnalysis public static class RoslynAnalysis
{ {
public static MSBuildWorkspace? _workspace; public static MSBuildWorkspace? _workspace;
private static RemoteSnapshotManager? _snapshotManager;
private static SharpIdeSolutionModel? _sharpIdeSolutionModel; private static SharpIdeSolutionModel? _sharpIdeSolutionModel;
private static HashSet<CodeFixProvider> _codeFixProviders = []; private static HashSet<CodeFixProvider> _codeFixProviders = [];
private static HashSet<CodeRefactoringProvider> _codeRefactoringProviders = []; private static HashSet<CodeRefactoringProvider> _codeRefactoringProviders = [];
@@ -45,10 +48,18 @@ public static class RoslynAnalysis
var timer = Stopwatch.StartNew(); var timer = Stopwatch.StartNew();
if (_workspace is null) if (_workspace is null)
{ {
// is this hostServices necessary? test without it - just getting providers from assemblies instead var configuration = new ContainerConfiguration()
var host = MefHostServices.Create(MefHostServices.DefaultAssemblies); .WithAssemblies(MefHostServices.DefaultAssemblies)
_workspace ??= MSBuildWorkspace.Create(host); .WithAssembly(typeof(RemoteSnapshotManager).Assembly);
// TODO: dispose container at some point?
var container = configuration.CreateContainer();
var host = MefHostServices.Create(container);
_workspace = MSBuildWorkspace.Create(host);
_workspace.RegisterWorkspaceFailedHandler(o => throw new InvalidOperationException($"Workspace failed: {o.Diagnostic.Message}")); _workspace.RegisterWorkspaceFailedHandler(o => throw new InvalidOperationException($"Workspace failed: {o.Diagnostic.Message}"));
var snapshotManager = container.GetExports<RemoteSnapshotManager>().FirstOrDefault();
_snapshotManager = snapshotManager;
} }
var solution = await _workspace.OpenSolutionAsync(_sharpIdeSolutionModel.FilePath, new Progress()); var solution = await _workspace.OpenSolutionAsync(_sharpIdeSolutionModel.FilePath, new Progress());
timer.Stop(); timer.Stop();
@@ -57,7 +68,6 @@ public static class RoslynAnalysis
foreach (var assembly in MefHostServices.DefaultAssemblies) foreach (var assembly in MefHostServices.DefaultAssemblies)
{ {
//var assembly = analyzer.GetAssembly();
var fixers = CodeFixProviderLoader.LoadCodeFixProviders([assembly], LanguageNames.CSharp); var fixers = CodeFixProviderLoader.LoadCodeFixProviders([assembly], LanguageNames.CSharp);
_codeFixProviders.AddRange(fixers); _codeFixProviders.AddRange(fixers);
var refactoringProviders = CodeRefactoringProviderLoader.LoadCodeRefactoringProviders([assembly], LanguageNames.CSharp); var refactoringProviders = CodeRefactoringProviderLoader.LoadCodeRefactoringProviders([assembly], LanguageNames.CSharp);
@@ -137,6 +147,7 @@ public static class RoslynAnalysis
var cancellationToken = CancellationToken.None; var cancellationToken = CancellationToken.None;
var project = _workspace!.CurrentSolution.Projects.Single(s => s.FilePath == ((IChildSharpIdeNode)fileModel).GetNearestProjectNode()!.FilePath); var project = _workspace!.CurrentSolution.Projects.Single(s => s.FilePath == ((IChildSharpIdeNode)fileModel).GetNearestProjectNode()!.FilePath);
var document = project.Documents.SingleOrDefault(s => s.FilePath == fileModel.Path); var document = project.Documents.SingleOrDefault(s => s.FilePath == fileModel.Path);
if (document is null) return []; if (document is null) return [];
//var document = _workspace!.CurrentSolution.GetDocument(fileModel.Path); //var document = _workspace!.CurrentSolution.GetDocument(fileModel.Path);
Guard.Against.Null(document, nameof(document)); Guard.Against.Null(document, nameof(document));
@@ -155,6 +166,7 @@ public static class RoslynAnalysis
{ {
await _solutionLoadedTcs.Task; await _solutionLoadedTcs.Task;
var cancellationToken = CancellationToken.None; var cancellationToken = CancellationToken.None;
var timer = Stopwatch.StartNew();
var sharpIdeProjectModel = ((IChildSharpIdeNode) fileModel).GetNearestProjectNode()!; var sharpIdeProjectModel = ((IChildSharpIdeNode) fileModel).GetNearestProjectNode()!;
var project = _workspace!.CurrentSolution.Projects.Single(s => s.FilePath == sharpIdeProjectModel!.FilePath); var project = _workspace!.CurrentSolution.Projects.Single(s => s.FilePath == sharpIdeProjectModel!.FilePath);
if (!fileModel.Name.EndsWith(".razor", StringComparison.OrdinalIgnoreCase)) if (!fileModel.Name.EndsWith(".razor", StringComparison.OrdinalIgnoreCase))
@@ -162,20 +174,23 @@ public static class RoslynAnalysis
return []; return [];
//throw new InvalidOperationException("File is not a .razor file"); //throw new InvalidOperationException("File is not a .razor file");
} }
var importsFile = sharpIdeProjectModel.Files.Single(s => s.Name.Equals("_Imports.razor", StringComparison.OrdinalIgnoreCase));
var razorDocument = project.AdditionalDocuments.Single(s => s.FilePath == fileModel.Path); var razorDocument = project.AdditionalDocuments.Single(s => s.FilePath == fileModel.Path);
var importsDocument = project.AdditionalDocuments.Single(s => s.FilePath == importsFile.Path);
var razorProjectSnapshot = _snapshotManager!.GetSnapshot(project);
var documentSnapshot = razorProjectSnapshot.GetDocument(razorDocument);
var razorCodeDocument = await razorProjectSnapshot.GetRequiredCodeDocumentAsync(documentSnapshot, cancellationToken);
var razorCSharpDocument = razorCodeDocument.GetRequiredCSharpDocument();
var generatedDocument = await razorProjectSnapshot.GetRequiredGeneratedDocumentAsync(documentSnapshot, cancellationToken);
var generatedDocSyntaxRoot = await generatedDocument.GetSyntaxRootAsync(cancellationToken);
//var razorCsharpText = razorCSharpDocument.Text.ToString();
//var razorSyntaxRoot = razorCodeDocument.GetRequiredSyntaxRoot();
var razorText = await razorDocument.GetTextAsync(cancellationToken); var razorText = await razorDocument.GetTextAsync(cancellationToken);
var importsText = await importsDocument.GetTextAsync(cancellationToken);
var (razorSpans, razorGeneratedSourceText, sourceMappings) = RazorAccessors.GetClassifiedSpans(razorText, importsText, razorDocument.FilePath!, Path.GetDirectoryName(project.FilePath!)!);
var razorGeneratedDocument = project.AddDocument(fileModel.Name + ".g.cs", razorGeneratedSourceText); var (razorSpans, sourceMappings) = RazorAccessors.GetSpansAndMappingsForRazorCodeDocument(razorCodeDocument, razorCSharpDocument);
var razorSyntaxTree = await razorGeneratedDocument.GetSyntaxTreeAsync(cancellationToken);
var razorSyntaxRoot = await razorSyntaxTree!.GetRootAsync(cancellationToken); var classifiedSpans = await Classifier.GetClassifiedSpansAsync(generatedDocument, generatedDocSyntaxRoot!.FullSpan, cancellationToken);
var classifiedSpans = await Classifier.GetClassifiedSpansAsync(razorGeneratedDocument, razorSyntaxRoot.FullSpan, cancellationToken);
var roslynMappedSpans = classifiedSpans.Select(s => var roslynMappedSpans = classifiedSpans.Select(s =>
{ {
var genSpan = s.TextSpan; var genSpan = s.TextSpan;
@@ -205,11 +220,13 @@ public static class RoslynAnalysis
return null; return null;
}).Where(s => s is not null).ToList(); }).Where(s => s is not null).ToList();
razorSpans = [..razorSpans.Where(s => s.Kind is not SharpIdeRazorSpanKind.Code), ..roslynMappedSpans razorSpans = [
.Select(s => new SharpIdeRazorClassifiedSpan(s!.SourceSpanInRazor, SharpIdeRazorSpanKind.Code, s.CsharpClassificationType)) ..razorSpans.Where(s => s.Kind is not SharpIdeRazorSpanKind.Code),
..roslynMappedSpans.Select(s => new SharpIdeRazorClassifiedSpan(s!.SourceSpanInRazor, SharpIdeRazorSpanKind.Code, s.CsharpClassificationType))
]; ];
razorSpans = razorSpans.OrderBy(s => s.Span.AbsoluteIndex).ToImmutableArray(); razorSpans = razorSpans.OrderBy(s => s.Span.AbsoluteIndex).ToImmutableArray();
timer.Stop();
Console.WriteLine($"RoslynAnalysis: Razor syntax highlighting for {fileModel.Name} took {timer.ElapsedMilliseconds}ms");
return razorSpans; return razorSpans;
} }

View File

@@ -7,6 +7,14 @@
<Nullable>enable</Nullable> <Nullable>enable</Nullable>
</PropertyGroup> </PropertyGroup>
<ItemGroup>
<IgnoresAccessChecksTo Include="Microsoft.VisualStudioCode.RazorExtension" />
<IgnoresAccessChecksTo Include="Microsoft.CodeAnalysis.Remote.Razor.ProjectSystem" />
<IgnoresAccessChecksTo Include="Microsoft.CodeAnalysis.Remote.Razor" />
<IgnoresAccessChecksTo Include="Microsoft.CodeAnalysis.Razor.Workspaces" />
<IgnoresAccessChecksTo Include="Microsoft.CodeAnalysis.Razor.Compiler" />
</ItemGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="Microsoft.Diagnostics.NETCore.Client" PrivateAssets="all" /> <PackageReference Include="Microsoft.Diagnostics.NETCore.Client" PrivateAssets="all" />
<PackageReference Include="Microsoft.VisualStudio.Shared.VSCodeDebugProtocol" /> <PackageReference Include="Microsoft.VisualStudio.Shared.VSCodeDebugProtocol" />
@@ -16,9 +24,12 @@
<PackageReference Include="Microsoft.Build" ExcludeAssets="runtime" /> <PackageReference Include="Microsoft.Build" ExcludeAssets="runtime" />
<PackageReference Include="Microsoft.Build.Framework" ExcludeAssets="runtime" /> <PackageReference Include="Microsoft.Build.Framework" ExcludeAssets="runtime" />
<PackageReference Include="Microsoft.Build.Locator" /> <PackageReference Include="Microsoft.Build.Locator" />
<PackageReference Include="IgnoresAccessChecksToGenerator" PrivateAssets="All" />
<PackageReference Include="Microsoft.Build.Tasks.Core" ExcludeAssets="runtime" /> <PackageReference Include="Microsoft.Build.Tasks.Core" ExcludeAssets="runtime" />
<PackageReference Include="Microsoft.Build.Utilities.Core" ExcludeAssets="runtime" /> <PackageReference Include="Microsoft.Build.Utilities.Core" ExcludeAssets="runtime" />
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.Features" /> <PackageReference Include="Microsoft.CodeAnalysis.CSharp.Features" />
<!-- <PackageReference Include="Microsoft.VisualStudioCode.RazorExtension" />--> <!-- Either this or Microsoft.CodeAnalysis.Remote.Razor supplies RemoteSnapshotManager -->
<PackageReference Include="Microsoft.CodeAnalysis.Remote.Razor" />
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.Workspaces" /> <PackageReference Include="Microsoft.CodeAnalysis.CSharp.Workspaces" />
<PackageReference Include="Microsoft.CodeAnalysis.Features" /> <PackageReference Include="Microsoft.CodeAnalysis.Features" />
<PackageReference Include="Microsoft.CodeAnalysis.Workspaces.MSBuild" PrivateAssets="all" /> <PackageReference Include="Microsoft.CodeAnalysis.Workspaces.MSBuild" PrivateAssets="all" />

View File

@@ -139,6 +139,7 @@ public partial class CustomHighlighter : SyntaxHighlighter
"method name" => new Color("dcdcaa"), "method name" => new Color("dcdcaa"),
"extension method name" => new Color("dcdcaa"), "extension method name" => new Color("dcdcaa"),
"property name" => new Color("dcdcdc"), "property name" => new Color("dcdcdc"),
"field name" => new Color("dcdcdc"),
"static symbol" => new Color("dcdcaa"), "static symbol" => new Color("dcdcaa"),
"parameter name" => new Color("9cdcfe"), "parameter name" => new Color("9cdcfe"),
"local name" => new Color("9cdcfe"), "local name" => new Color("9cdcfe"),
@@ -150,7 +151,7 @@ public partial class CustomHighlighter : SyntaxHighlighter
// Misc // Misc
"excluded code" => new Color("a9a9a9"), "excluded code" => new Color("a9a9a9"),
_ => new Color("dcdcdc") _ => new Color("f27718") // orange, warning color for unhandled classifications
}; };
} }
} }

View File

@@ -96,7 +96,7 @@ public partial class IdeRoot : Control
var infraProject = solutionModel.AllProjects.Single(s => s.Name == "Infrastructure"); var infraProject = solutionModel.AllProjects.Single(s => s.Name == "Infrastructure");
var diFile = infraProject.Files.Single(s => s.Name == "DependencyInjection.cs"); var diFile = infraProject.Files.Single(s => s.Name == "DependencyInjection.cs");
await this.InvokeAsync(async () => await _sharpIdeCodeEdit.SetSharpIdeFile(diFile)); await this.InvokeDeferredAsync(async () => await _sharpIdeCodeEdit.SetSharpIdeFile(diFile));
//var runnableProject = solutionModel.AllProjects.First(s => s.IsRunnable); //var runnableProject = solutionModel.AllProjects.First(s => s.IsRunnable);
//await this.InvokeAsync(() => _runPanel.NewRunStarted(runnableProject)); //await this.InvokeAsync(() => _runPanel.NewRunStarted(runnableProject));

View File

@@ -162,6 +162,7 @@ public partial class SharpIdeCodeEdit : CodeEdit
}); });
} }
// TODO: Ensure not running on UI thread
public async Task SetSharpIdeFile(SharpIdeFile file) public async Task SetSharpIdeFile(SharpIdeFile file)
{ {
_currentFile = file; _currentFile = file;
@@ -169,11 +170,12 @@ public partial class SharpIdeCodeEdit : CodeEdit
_fileChangingSuppressBreakpointToggleEvent = true; _fileChangingSuppressBreakpointToggleEvent = true;
SetText(fileContents); SetText(fileContents);
_fileChangingSuppressBreakpointToggleEvent = false; _fileChangingSuppressBreakpointToggleEvent = false;
var syntaxHighlighting = await RoslynAnalysis.GetDocumentSyntaxHighlighting(_currentFile); var syntaxHighlighting = RoslynAnalysis.GetDocumentSyntaxHighlighting(_currentFile);
var razorSyntaxHighlighting = await RoslynAnalysis.GetRazorDocumentSyntaxHighlighting(_currentFile); var razorSyntaxHighlighting = RoslynAnalysis.GetRazorDocumentSyntaxHighlighting(_currentFile);
SetSyntaxHighlightingModel(syntaxHighlighting, razorSyntaxHighlighting); var diagnostics = RoslynAnalysis.GetDocumentDiagnostics(_currentFile);
var diagnostics = await RoslynAnalysis.GetDocumentDiagnostics(_currentFile); await Task.WhenAll(syntaxHighlighting, razorSyntaxHighlighting);
SetDiagnosticsModel(diagnostics); SetSyntaxHighlightingModel(await syntaxHighlighting, await razorSyntaxHighlighting);
SetDiagnosticsModel(await diagnostics);
} }
public void UnderlineRange(int line, int caretStartCol, int caretEndCol, Color color, float thickness = 1.5f) public void UnderlineRange(int line, int caretStartCol, int caretEndCol, Color color, float thickness = 1.5f)

View File

@@ -1,37 +1,55 @@
extern alias WorkspaceAlias; extern alias WorkspaceAlias;
using System.Collections.Immutable; using System.Collections.Immutable;
using Microsoft.AspNetCore.Razor.Language; using Microsoft.AspNetCore.Razor.Language;
using Microsoft.CodeAnalysis.Text;
using RazorCodeDocumentExtensions = WorkspaceAlias::Microsoft.AspNetCore.Razor.Language.RazorCodeDocumentExtensions; using RazorCodeDocumentExtensions = WorkspaceAlias::Microsoft.AspNetCore.Razor.Language.RazorCodeDocumentExtensions;
namespace SharpIDE.RazorAccess; namespace SharpIDE.RazorAccess;
public static class RazorAccessors public static class RazorAccessors
{ {
public static (ImmutableArray<SharpIdeRazorClassifiedSpan>, SourceText Text, List<SharpIdeRazorSourceMapping>) GetClassifiedSpans(SourceText sourceText, SourceText importsSourceText, string razorDocumentFilePath, string projectDirectory) //private static RazorProjectEngine? _razorProjectEngine;
public static (ImmutableArray<SharpIdeRazorClassifiedSpan>, List<SharpIdeRazorSourceMapping>) GetSpansAndMappingsForRazorCodeDocument(RazorCodeDocument razorCodeDocument, RazorCSharpDocument razorCSharpDocument)
{ {
var razorSourceDocument = RazorSourceDocument.Create(sourceText.ToString(), razorDocumentFilePath);
var importsRazorSourceDocument = RazorSourceDocument.Create(importsSourceText.ToString(), "_Imports.razor");
var projectEngine = RazorProjectEngine.Create(RazorConfiguration.Default, RazorProjectFileSystem.Create(projectDirectory),
builder => { /* configure features if needed */ });
//var razorCodeDocument = projectEngine.Process(razorSourceDocument, RazorFileKind.Component, [], []);
var razorCodeDocument = projectEngine.ProcessDesignTime(razorSourceDocument, RazorFileKind.Component, [importsRazorSourceDocument], []);
var razorCSharpDocument = razorCodeDocument.GetRequiredCSharpDocument();
//var generatedSourceText = razorCSharpDocument.Text;
//var filePath = razorCodeDocument.Source.FilePath.AssumeNotNull();
//var razorSourceText = razorCodeDocument.Source.Text;
var razorSpans = RazorCodeDocumentExtensions.GetClassifiedSpans(razorCodeDocument); var razorSpans = RazorCodeDocumentExtensions.GetClassifiedSpans(razorCodeDocument);
//var sharpIdeSpans = MemoryMarshal.Cast<RazorCodeDocumentExtensions.ClassifiedSpan, SharpIdeRazorClassifiedSpan>(razorSpans);
var sharpIdeSpans = razorSpans.Select(s => new SharpIdeRazorClassifiedSpan(s.Span.ToSharpIdeSourceSpan(), s.Kind.ToSharpIdeSpanKind())).ToList(); var sharpIdeSpans = razorSpans.Select(s => new SharpIdeRazorClassifiedSpan(s.Span.ToSharpIdeSourceSpan(), s.Kind.ToSharpIdeSpanKind())).ToList();
return (sharpIdeSpans.ToImmutableArray(), razorCSharpDocument.Text, razorCSharpDocument.SourceMappings.Select(s => s.ToSharpIdeSourceMapping()).ToList()); var result = (sharpIdeSpans.ToImmutableArray(), razorCSharpDocument.SourceMappings.Select(s => s.ToSharpIdeSourceMapping()).ToList());
return result;
} }
public static ImmutableArray<RazorCodeDocumentExtensions.ClassifiedSpan> GetClassifiedSpansForRazorCodeDocument(RazorCodeDocument razorCodeDocument)
{
var razorSpans = RazorCodeDocumentExtensions.GetClassifiedSpans(razorCodeDocument);
return razorSpans;
}
// public static (ImmutableArray<SharpIdeRazorClassifiedSpan>, SourceText Text, List<SharpIdeRazorSourceMapping>) GetClassifiedSpans(SourceText sourceText, SourceText importsSourceText, string razorDocumentFilePath, string projectDirectory)
// {
// var razorSourceDocument = RazorSourceDocument.Create(sourceText.ToString(), razorDocumentFilePath);
// var importsRazorSourceDocument = RazorSourceDocument.Create(importsSourceText.ToString(), "_Imports.razor");
//
// var razorProjectFileSystem = RazorProjectFileSystem.Create(projectDirectory);
// _razorProjectEngine ??= RazorProjectEngine.Create(RazorConfiguration.Default, razorProjectFileSystem,
// builder => { /* configure features if needed */ });
// //var projectItem = razorProjectFileSystem.GetItem(razorDocumentFilePath, RazorFileKind.Component);
//
// //var razorCodeDocument = projectEngine.Process(razorSourceDocument, RazorFileKind.Component, [], []);
// var razorCodeDocument = _razorProjectEngine.Process(razorSourceDocument, RazorFileKind.Component, [importsRazorSourceDocument], []);
// var razorCSharpDocument = razorCodeDocument.GetRequiredCSharpDocument();
// //var generatedSourceText = razorCSharpDocument.Text;
//
// //var filePath = razorCodeDocument.Source.FilePath.AssumeNotNull();
// //var razorSourceText = razorCodeDocument.Source.Text;
// var razorSpans = RazorCodeDocumentExtensions.GetClassifiedSpans(razorCodeDocument);
//
// //var sharpIdeSpans = MemoryMarshal.Cast<RazorCodeDocumentExtensions.ClassifiedSpan, SharpIdeRazorClassifiedSpan>(razorSpans);
// var sharpIdeSpans = razorSpans.Select(s => new SharpIdeRazorClassifiedSpan(s.Span.ToSharpIdeSourceSpan(), s.Kind.ToSharpIdeSpanKind())).ToList();
//
// var result = (sharpIdeSpans.ToImmutableArray(), razorCSharpDocument.Text, razorCSharpDocument.SourceMappings.Select(s => s.ToSharpIdeSourceMapping()).ToList());
// return result;
// }
// public static bool TryGetMappedSpans( // public static bool TryGetMappedSpans(
// TextSpan span, // TextSpan span,
// SourceText source, // SourceText source,
@@ -59,14 +77,4 @@ public static class RazorAccessors
// linePositionSpan = new LinePositionSpan(); // linePositionSpan = new LinePositionSpan();
// return false; // return false;
// } // }
// /// <summary>
// /// Wrapper to avoid <see cref="MissingMethodException"/>s in the caller during JITing
// /// even though the method is not actually called.
// /// </summary>
// [MethodImpl(MethodImplOptions.NoInlining)]
// private static object GetFileKindFromPath(string filePath)
// {
// return FileKinds.GetFileKindFromPath(filePath);
// }
} }

View File

@@ -19,7 +19,7 @@
<PackageReference Include="Microsoft.AspNetCore.Razor.Utilities.Shared" PrivateAssets="all" IncludeAssets="none" GeneratePathProperty="true" /> <PackageReference Include="Microsoft.AspNetCore.Razor.Utilities.Shared" PrivateAssets="all" IncludeAssets="none" GeneratePathProperty="true" />
<PackageReference Include="Microsoft.Extensions.ObjectPool" PrivateAssets="all" IncludeAssets="none" GeneratePathProperty="true" /> <PackageReference Include="Microsoft.Extensions.ObjectPool" PrivateAssets="all" IncludeAssets="none" GeneratePathProperty="true" />
<PackageReference Include="Microsoft.CodeAnalysis.Razor.Compiler" PrivateAssets="all" IncludeAssets="none" GeneratePathProperty="true" /> <PackageReference Include="Microsoft.CodeAnalysis.Razor.Compiler" PrivateAssets="all" IncludeAssets="none" GeneratePathProperty="true" />
<PackageReference Include="Microsoft.CodeAnalysis.Razor.Workspaces" PrivateAssets="all" IncludeAssets="none" GeneratePathProperty="true" /> <PackageReference Include="Microsoft.CodeAnalysis.Razor.Workspaces" Aliases="WorkspaceAlias" />
<PackageReference Include="Krafs.Publicizer"> <PackageReference Include="Krafs.Publicizer">
<PrivateAssets>all</PrivateAssets> <PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
@@ -28,7 +28,7 @@
<ItemGroup> <ItemGroup>
<!-- <Reference Include="$(PkgMicrosoft_Net_Compilers_Razor_Toolset)\source-generators\Microsoft.CodeAnalysis.Razor.Compiler.dll" />--> <!-- <Reference Include="$(PkgMicrosoft_Net_Compilers_Razor_Toolset)\source-generators\Microsoft.CodeAnalysis.Razor.Compiler.dll" />-->
<Reference Aliases="WorkspaceAlias" Include="$(PkgMicrosoft_CodeAnalysis_Razor_Workspaces)\lib\net9.0\Microsoft.CodeAnalysis.Razor.Workspaces.dll" /> <!-- <Reference Aliases="WorkspaceAlias" Include="$(PkgMicrosoft_CodeAnalysis_Razor_Workspaces)\lib\net9.0\Microsoft.CodeAnalysis.Razor.Workspaces.dll" />-->
<Reference Include="$(PkgMicrosoft_CodeAnalysis_Razor_Compiler)\lib\net9.0\Microsoft.CodeAnalysis.Razor.Compiler.dll" /> <Reference Include="$(PkgMicrosoft_CodeAnalysis_Razor_Compiler)\lib\net9.0\Microsoft.CodeAnalysis.Razor.Compiler.dll" />
<Reference Include="$(PkgMicrosoft_AspNetCore_Razor_Utilities_Shared)\lib\net9.0\Microsoft.AspNetCore.Razor.Utilities.Shared.dll" /> <Reference Include="$(PkgMicrosoft_AspNetCore_Razor_Utilities_Shared)\lib\net9.0\Microsoft.AspNetCore.Razor.Utilities.Shared.dll" />
<Reference Include="$(PkgMicrosoft_Extensions_ObjectPool)\lib\net8.0\Microsoft.Extensions.ObjectPool.dll" /> <Reference Include="$(PkgMicrosoft_Extensions_ObjectPool)\lib\net8.0\Microsoft.Extensions.ObjectPool.dll" />