Skip to content

Commit

Permalink
[LSP] Add Razor options provider to Roslyn (#53879)
Browse files Browse the repository at this point in the history
  • Loading branch information
allisonchou committed Jun 9, 2021
1 parent cb04321 commit 0e94007
Show file tree
Hide file tree
Showing 10 changed files with 133 additions and 13 deletions.
7 changes: 2 additions & 5 deletions src/Features/Core/Portable/Completion/CompletionProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,8 @@ public virtual Task<CompletionDescription> GetDescriptionAsync(Document document
/// <param name="item">The item to be committed.</param>
/// <param name="commitKey">The optional key character that caused the commit.</param>
/// <param name="cancellationToken"></param>
public virtual Task<CompletionChange> GetChangeAsync(
Document document, CompletionItem item, char? commitKey, CancellationToken cancellationToken)
{
return Task.FromResult(CompletionChange.Create(new TextChange(item.Span, item.DisplayText)));
}
public virtual Task<CompletionChange> GetChangeAsync(Document document, CompletionItem item, char? commitKey, CancellationToken cancellationToken)
=> Task.FromResult(CompletionChange.Create(new TextChange(item.Span, item.DisplayText)));

/// <summary>
/// True if the provider produces snippet items.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
using Microsoft.CodeAnalysis.Editing;
using Microsoft.CodeAnalysis.Formatting;
using Microsoft.CodeAnalysis.Formatting.Rules;
using Microsoft.CodeAnalysis.PooledObjects;
using Microsoft.CodeAnalysis.Shared.Naming;
using Microsoft.CodeAnalysis.Simplification;
using Microsoft.CodeAnalysis.Text;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

using System;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.Completion;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.Diagnostics;
using Microsoft.CodeAnalysis.Host;
using Microsoft.CodeAnalysis.Options;
using Microsoft.CodeAnalysis.PooledObjects;
using Microsoft.CodeAnalysis.Text;
Expand Down
24 changes: 24 additions & 0 deletions src/Tools/ExternalAccess/Razor/Extensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// 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.Host;

namespace Microsoft.CodeAnalysis.ExternalAccess.Razor
{
internal static class Extensions
{
private const string RazorCSharp = "RazorCSharp";

public static bool IsRazorDocument(this Document document)
{
var documentPropertiesService = document.Services.GetService<DocumentPropertiesService>();
if (documentPropertiesService != null && documentPropertiesService.DiagnosticsLspClientName == RazorCSharp)
{
return true;
}

return false;
}
}
}
13 changes: 13 additions & 0 deletions src/Tools/ExternalAccess/Razor/IRazorDocumentOptions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// 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.Options;

namespace Microsoft.CodeAnalysis.ExternalAccess.Razor
{
internal interface IRazorDocumentOptions
{
bool TryGetDocumentOption(OptionKey option, out object? value);
}
}
15 changes: 15 additions & 0 deletions src/Tools/ExternalAccess/Razor/IRazorDocumentOptionsService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// 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.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.Host;

namespace Microsoft.CodeAnalysis.ExternalAccess.Razor
{
internal interface IRazorDocumentOptionsService : IDocumentService
{
Task<IRazorDocumentOptions> GetOptionsForDocumentAsync(Document document, CancellationToken cancellationToken);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
// 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;
using System.Composition;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.Host.Mef;
using Microsoft.CodeAnalysis.Options;
using Microsoft.CodeAnalysis.Shared.Extensions;

namespace Microsoft.CodeAnalysis.ExternalAccess.Razor
{
[Shared]
[Export(typeof(IDocumentOptionsProviderFactory))]
internal sealed class RazorDocumentOptionsProviderFactory : IDocumentOptionsProviderFactory
{
private readonly Lazy<IRazorDocumentOptionsService> _innerRazorDocumentOptionsService;

[ImportingConstructor]
[Obsolete(MefConstruction.ImportingConstructorMessage, error: true)]
public RazorDocumentOptionsProviderFactory(
Lazy<IRazorDocumentOptionsService> innerRazorDocumentOptionsService)
{
if (innerRazorDocumentOptionsService is null)
{
throw new ArgumentNullException(nameof(innerRazorDocumentOptionsService));
}

_innerRazorDocumentOptionsService = innerRazorDocumentOptionsService;
}

public IDocumentOptionsProvider? TryCreate(Workspace workspace)
{
var optionsService = _innerRazorDocumentOptionsService.Value;
return new RazorDocumentOptionsProvider(optionsService);
}

private sealed class RazorDocumentOptionsProvider : IDocumentOptionsProvider
{
public readonly IRazorDocumentOptionsService RazorDocumentOptionsService;

public RazorDocumentOptionsProvider(IRazorDocumentOptionsService razorDocumentOptionsService)
{
RazorDocumentOptionsService = razorDocumentOptionsService;
}

public async Task<IDocumentOptions?> GetOptionsForDocumentAsync(Document document, CancellationToken cancellationToken)
{
if (!document.IsRazorDocument())
{
return null;
}

var options = await RazorDocumentOptionsService.GetOptionsForDocumentAsync(document, cancellationToken).ConfigureAwait(false);
return new RazorDocumentOptions(options);
}
}

// Used to convert IRazorDocumentOptions -> IDocumentOptions
private sealed class RazorDocumentOptions : IDocumentOptions
{
private readonly IRazorDocumentOptions _razorOptions;

public RazorDocumentOptions(IRazorDocumentOptions razorOptions)
{
_razorOptions = razorOptions;
}

public bool TryGetDocumentOption(OptionKey option, out object? value)
=> _razorOptions.TryGetDocumentOption(option, out value);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ public RazorDocumentServiceProviderWrapper(IRazorDocumentServiceProvider innerDo

public TService? GetService<TService>() where TService : class, IDocumentService
{
if (typeof(TService) == typeof(ISpanMappingService))
var serviceType = typeof(TService);
if (serviceType == typeof(ISpanMappingService))
{
if (_spanMappingService == null)
{
Expand All @@ -54,7 +55,7 @@ public RazorDocumentServiceProviderWrapper(IRazorDocumentServiceProvider innerDo
return (TService)(object)_spanMappingService;
}

if (typeof(TService) == typeof(IDocumentExcerptService))
if (serviceType == typeof(IDocumentExcerptService))
{
if (_excerptService == null)
{
Expand All @@ -78,7 +79,7 @@ public RazorDocumentServiceProviderWrapper(IRazorDocumentServiceProvider innerDo
return (TService)(object)_excerptService;
}

if (typeof(TService) == typeof(DocumentPropertiesService))
if (serviceType == typeof(DocumentPropertiesService))
{
if (_documentPropertiesService == null)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,7 @@
using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.Host;
using Microsoft.CodeAnalysis.PooledObjects;
using Microsoft.CodeAnalysis.Text;
using Roslyn.Utilities;

namespace Microsoft.CodeAnalysis.ExternalAccess.Razor
{
Expand Down

0 comments on commit 0e94007

Please sign in to comment.