-
Notifications
You must be signed in to change notification settings - Fork 4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[LSP] Add Razor options provider to Roslyn (#53879)
- Loading branch information
1 parent
cb04321
commit 0e94007
Showing
10 changed files
with
133 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
15
src/Tools/ExternalAccess/Razor/IRazorDocumentOptionsService.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
75 changes: 75 additions & 0 deletions
75
src/Tools/ExternalAccess/Razor/RazorDocumentOptionsProviderFactory.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters