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
Expand Up @@ -41,8 +41,7 @@ private bool UseLSPEditor(string filePath)
}

// Otherwise, we just check for the lack of feature flag feature or project capability.
if (_lspEditorFeatureDetector.IsLspEditorEnabled() &&
_lspEditorFeatureDetector.IsLspEditorSupported(filePath))
if (_lspEditorFeatureDetector.IsLspEditorSupported(filePath))
{
return true;
}
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -4,44 +4,21 @@
using System;
using System.ComponentModel.Composition;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Internal.VisualStudio.Shell.Interop;
using Microsoft.VisualStudio.Razor.Extensions;
using Microsoft.VisualStudio.Razor.Logging;
using Microsoft.VisualStudio.Settings;
using Microsoft.VisualStudio.Threading;

namespace Microsoft.VisualStudio.Razor;

[Export(typeof(ILspEditorFeatureDetector))]
internal sealed class LspEditorFeatureDetector : ILspEditorFeatureDetector, IDisposable
[method: ImportingConstructor]
internal sealed class LspEditorFeatureDetector(
IUIContextService uiContextService,
IProjectCapabilityResolver projectCapabilityResolver,
RazorActivityLog activityLog) : ILspEditorFeatureDetector, IDisposable
{
private readonly IUIContextService _uiContextService;
private readonly IProjectCapabilityResolver _projectCapabilityResolver;
private readonly JoinableTaskFactory _jtf;
private readonly RazorActivityLog _activityLog;
private readonly CancellationTokenSource _disposeTokenSource;
private readonly AsyncLazy<bool> _lazyLegacyEditorEnabled;

[ImportingConstructor]
public LspEditorFeatureDetector(
IVsService<SVsSettingsPersistenceManager, ISettingsManager> vsSettingsManagerService,
IUIContextService uiContextService,
IProjectCapabilityResolver projectCapabilityResolver,
JoinableTaskContext joinableTaskContext,
RazorActivityLog activityLog)
{
_uiContextService = uiContextService;
_projectCapabilityResolver = projectCapabilityResolver;
_jtf = joinableTaskContext.Factory;
_activityLog = activityLog;

_disposeTokenSource = new();

_lazyLegacyEditorEnabled = new(() =>
ComputeUseLegacyEditorAsync(vsSettingsManagerService, activityLog, _disposeTokenSource.Token),
_jtf);
}
private readonly IUIContextService _uiContextService = uiContextService;
private readonly IProjectCapabilityResolver _projectCapabilityResolver = projectCapabilityResolver;
private readonly RazorActivityLog _activityLog = activityLog;
private readonly CancellationTokenSource _disposeTokenSource = new();

public void Dispose()
{
Expand All @@ -54,53 +31,14 @@ public void Dispose()
_disposeTokenSource.Dispose();
}

private static async Task<bool> ComputeUseLegacyEditorAsync(
IVsService<SVsSettingsPersistenceManager, ISettingsManager> vsSettingsManagerService,
RazorActivityLog activityLog,
CancellationToken cancellationToken)
{
var settingsManager = await vsSettingsManagerService.GetValueAsync(cancellationToken).ConfigureAwaitRunInline();
var useLegacyEditorSetting = settingsManager.GetValueOrDefault<bool>(WellKnownSettingNames.UseLegacyASPNETCoreEditor);

if (useLegacyEditorSetting)
{
activityLog.LogInfo($"Using legacy editor because the '{WellKnownSettingNames.UseLegacyASPNETCoreEditor}' setting is set to true.");
return true;
}

activityLog.LogInfo($"Using LSP editor.");
return false;
}

public bool IsLspEditorEnabled()
{
// This method is first called by our IFilePathToContentTypeProvider.TryGetContentTypeForFilePath(...) implementations.
// We call AsyncLazy<T>.GetValue() below to get the value. If the work hasn't yet completed, we guard against a hidden
// JTF.Run(...) on a background thread by asserting the UI thread.

if (!_lazyLegacyEditorEnabled.IsValueFactoryCompleted)
{
#pragma warning disable VSTHRD108 // Assert thread affinity unconditionally
_jtf.AssertUIThread();
#pragma warning restore VSTHRD108 // Assert thread affinity unconditionally
}

return !_lazyLegacyEditorEnabled.GetValue(_disposeTokenSource.Token);
return true;
}

public bool IsLspEditorSupported(string documentFilePath)
{
// Regardless of whether the LSP is enabled via tools/options, the document's project
// might not support it. For example, .NET Framework projects don't support the LSP Razor editor.

var useLegacyEditor = _projectCapabilityResolver.CheckCapability(WellKnownProjectCapabilities.LegacyRazorEditor, documentFilePath);

if (useLegacyEditor.HasCapability)
{
_activityLog.LogInfo($"'{documentFilePath}' does not support the LSP editor because it is associated with the '{WellKnownProjectCapabilities.LegacyRazorEditor}' capability.");
return false;
}

// .NET Framework projects don't support the LSP Razor editor.
if (!IsDotNetCoreProject(documentFilePath).HasCapability)
{
_activityLog.LogInfo($"'{documentFilePath}' does not support the LSP editor because it is not associated with the '{WellKnownProjectCapabilities.DotNetCoreCSharp}' capability.");
Expand Down
Loading