Skip to content

Commit

Permalink
Add Razor External Access APIs to allow passing classification option…
Browse files Browse the repository at this point in the history
…s through ExcerptService (#59394)
  • Loading branch information
tmat committed Feb 9, 2022
1 parent f9e0fa7 commit 4489bfe
Show file tree
Hide file tree
Showing 7 changed files with 89 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@
<RestrictedInternalsVisibleTo Include="Microsoft.VisualStudio.IntelliCode" Key="$(IntelliCodeKey)" Partner="IntelliCode" />
<RestrictedInternalsVisibleTo Include="Microsoft.CodeAnalysis.LiveUnitTesting.BuildManager" Partner="UnitTesting" Key="$(UnitTestingKey)" />
<RestrictedInternalsVisibleTo Include="Microsoft.CodeAnalysis.LiveUnitTesting.BuildManager.Core" Partner="UnitTesting" Key="$(UnitTestingKey)" />
<RestrictedInternalsVisibleTo Include="Microsoft.CodeAnalysis.LiveUnitTesting.Orchestrator" Partner="UnitTesting" Key="$(UnitTestingKey)" />
<RestrictedInternalsVisibleTo Include="Microsoft.CodeAnalysis.UnitTesting.SourceBasedTestDiscovery" Partner="UnitTesting" Key="$(UnitTestingKey)" />
<RestrictedInternalsVisibleTo Include="Microsoft.CodeAnalysis.UnitTesting.SourceBasedTestDiscovery.Core" Partner="UnitTesting" Key="$(UnitTestingKey)" />
</ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,21 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.Text;

namespace Microsoft.CodeAnalysis.ExternalAccess.Razor
{
[Obsolete("Use IRazorDocumentExcerptServiceImplementation instead")]
internal interface IRazorDocumentExcerptService
{
Task<RazorExcerptResult?> TryExcerptAsync(Document document, TextSpan span, RazorExcerptMode mode, CancellationToken cancellationToken);
}

internal interface IRazorDocumentExcerptServiceImplementation
{
Task<RazorExcerptResult?> TryExcerptAsync(Document document, TextSpan span, RazorExcerptMode mode, RazorClassificationOptionsWrapper options, CancellationToken cancellationToken);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
<InternalsVisibleTo Include="Microsoft.AspNetCore.Razor.LanguageServer" Key="$(RazorKey)" />
<InternalsVisibleTo Include="Microsoft.AspNetCore.Razor.LanguageServer.Test" Key="$(RazorKey)" />
<InternalsVisibleTo Include="Microsoft.CodeAnalysis.Razor.Workspaces" Key="$(RazorKey)" />
<InternalsVisibleTo Include="Microsoft.CodeAnalysis.Razor.Workspaces.Test" Key="$(RazorKey)" />
<InternalsVisibleTo Include="Microsoft.CodeAnalysis.Remote.Razor" Key="$(RazorKey)" />
<InternalsVisibleTo Include="Microsoft.VisualStudio.Editor.Razor" Key="$(RazorKey)" />
<InternalsVisibleTo Include="Microsoft.VisualStudio.LanguageServices.Razor" Key="$(RazorKey)" />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using Microsoft.CodeAnalysis.Classification;

namespace Microsoft.CodeAnalysis.ExternalAccess.Razor
{
internal readonly struct RazorClassificationOptionsWrapper
{
public static RazorClassificationOptionsWrapper Default = new(ClassificationOptions.Default);

internal readonly ClassificationOptions UnderlyingObject;

public RazorClassificationOptionsWrapper(ClassificationOptions underlyingObject)
=> UnderlyingObject = underlyingObject;
}
}
22 changes: 22 additions & 0 deletions src/Tools/ExternalAccess/Razor/RazorClassifierAccessor.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.Classification;
using Microsoft.CodeAnalysis.Shared.Extensions;
using Microsoft.CodeAnalysis.Text;

namespace Microsoft.CodeAnalysis.ExternalAccess.Razor
{
internal static class RazorClassifierAccessor
{
public static async Task<IEnumerable<ClassifiedSpan>> GetClassifiedSpansAsync(Document document, TextSpan textSpan, RazorClassificationOptionsWrapper options, CancellationToken cancellationToken)
{
var semanticModel = await document.GetRequiredSemanticModelAsync(cancellationToken).ConfigureAwait(false);
return Classifier.GetClassifiedSpans(document.Project.Solution.Workspace.Services, semanticModel, textSpan, options.UnderlyingObject, cancellationToken);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,37 +6,52 @@
using System.ComponentModel;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.Classification;
using Microsoft.CodeAnalysis.Host;
using Microsoft.CodeAnalysis.Text;
using Roslyn.Utilities;

namespace Microsoft.CodeAnalysis.ExternalAccess.Razor
{
internal sealed class RazorDocumentExcerptServiceWrapper : IDocumentExcerptService
{
private readonly IRazorDocumentExcerptService _razorDocumentExcerptService;
[Obsolete]
private readonly IRazorDocumentExcerptService? _legacyRazorDocumentExcerptService;

private readonly IRazorDocumentExcerptServiceImplementation? _impl;

[Obsolete]
public RazorDocumentExcerptServiceWrapper(IRazorDocumentExcerptService razorDocumentExcerptService)
{
_razorDocumentExcerptService = razorDocumentExcerptService ?? throw new ArgumentNullException(nameof(razorDocumentExcerptService));
}
=> _legacyRazorDocumentExcerptService = razorDocumentExcerptService;

public RazorDocumentExcerptServiceWrapper(IRazorDocumentExcerptServiceImplementation impl)
=> _impl = impl;

public async Task<ExcerptResult?> TryExcerptAsync(Document document, TextSpan span, ExcerptMode mode, CancellationToken cancellationToken)
{
var razorMode = mode switch
{
ExcerptMode.SingleLine => RazorExcerptMode.SingleLine,
ExcerptMode.Tooltip => RazorExcerptMode.Tooltip,
_ => throw new InvalidEnumArgumentException($"Unsupported enum type {mode}."),
_ => throw ExceptionUtilities.UnexpectedValue(mode),
};
var nullableRazorExcerpt = await _razorDocumentExcerptService.TryExcerptAsync(document, span, razorMode, cancellationToken).ConfigureAwait(false);
if (nullableRazorExcerpt == null)

RazorExcerptResult? result;
if (_impl != null)
{
var options = ClassificationOptions.From(document.Project);
result = await _impl.TryExcerptAsync(document, span, razorMode, new RazorClassificationOptionsWrapper(options), cancellationToken).ConfigureAwait(false);
}
else
{
return null;
#pragma warning disable CS0612 // Type or member is obsolete
Contract.ThrowIfNull(_legacyRazorDocumentExcerptService);
result = await _legacyRazorDocumentExcerptService.TryExcerptAsync(document, span, razorMode, cancellationToken).ConfigureAwait(false);
#pragma warning restore
}

var razorExcerpt = nullableRazorExcerpt.Value;
var roslynExcerpt = new ExcerptResult(razorExcerpt.Content, razorExcerpt.MappedSpan, razorExcerpt.ClassifiedSpans, razorExcerpt.Document, razorExcerpt.Span);
return roslynExcerpt;
var razorExcerpt = result.Value;
return new ExcerptResult(razorExcerpt.Content, razorExcerpt.MappedSpan, razorExcerpt.ClassifiedSpans, razorExcerpt.Document, razorExcerpt.Span);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,20 @@ public RazorDocumentServiceProviderWrapper(IRazorDocumentServiceProvider innerDo
ref _lazyExcerptService,
static documentServiceProvider =>
{
var razorExcerptService = documentServiceProvider.GetService<IRazorDocumentExcerptService>();
return razorExcerptService is not null ? new RazorDocumentExcerptServiceWrapper(razorExcerptService) : null;
var impl = documentServiceProvider.GetService<IRazorDocumentExcerptServiceImplementation>();
if (impl != null)
{
return new RazorDocumentExcerptServiceWrapper(impl);
}
#pragma warning disable CS0612, CS0618 // Type or member is obsolete
var legacyImpl = documentServiceProvider.GetService<IRazorDocumentExcerptService>();
if (legacyImpl != null)
{
return new RazorDocumentExcerptServiceWrapper(legacyImpl);
}
#pragma warning restore
return null;
},
_innerDocumentServiceProvider);

Expand Down

0 comments on commit 4489bfe

Please sign in to comment.