未验证 提交 5e2bd9e5 编写于 作者: J Joey Robichaud 提交者: GitHub

Merge pull request #40513 from JoeRobich/workspace-registers-optionsprovider

Always try to register the EditorConfigDocumentOptionsProvider
......@@ -16,17 +16,17 @@ namespace Microsoft.VisualStudio.LanguageServices.Implementation.TaskList
[Export(typeof(IOptionPersister))]
internal class CommentTaskTokenSerializer : IOptionPersister
{
private readonly VisualStudioWorkspace _workspace;
private readonly ITaskList _taskList;
private readonly IGlobalOptionService _globalOptionService;
private string _lastCommentTokenCache = null;
[ImportingConstructor]
public CommentTaskTokenSerializer(
VisualStudioWorkspace workspace,
IGlobalOptionService globalOptionService,
[Import(typeof(SVsServiceProvider))] IServiceProvider serviceProvider)
{
_workspace = workspace;
_globalOptionService = globalOptionService;
// The SVsTaskList may not be available or doesn't actually implement ITaskList
// in the "devenv /build" scenario
......@@ -68,8 +68,7 @@ private void OnPropertyChanged(object sender, PropertyChangedEventArgs e)
var commentString = GetTaskTokenList(_taskList);
var optionSet = _workspace.Options;
var optionValue = optionSet.GetOption(TodoCommentOptions.TokenList);
var optionValue = _globalOptionService.GetOption(TodoCommentOptions.TokenList);
if (optionValue == commentString)
{
return;
......@@ -79,7 +78,7 @@ private void OnPropertyChanged(object sender, PropertyChangedEventArgs e)
_lastCommentTokenCache = commentString;
// let people to know that comment string has changed
_workspace.SetOptions(optionSet.WithChangedOption(TodoCommentOptions.TokenList, _lastCommentTokenCache));
_globalOptionService.RefreshOption(TodoCommentOptions.TokenList, _lastCommentTokenCache);
}
private static string GetTaskTokenList(ITaskList taskList)
......
......@@ -75,12 +75,12 @@ protected override async Task InitializeAsync(CancellationToken cancellationToke
var method = compilerFailFast.GetMethod(nameof(FailFast.OnFatalException), BindingFlags.Static | BindingFlags.NonPublic);
property.SetValue(null, Delegate.CreateDelegate(property.PropertyType, method));
_workspace = _componentModel.GetService<VisualStudioWorkspace>();
_workspace.Services.GetService<IExperimentationService>();
// Ensure the options persisters are loaded since we have to fetch options from the shell
_componentModel.GetExtensions<IOptionPersister>();
_workspace = _componentModel.GetService<VisualStudioWorkspace>();
_workspace.Services.GetService<IExperimentationService>();
RoslynTelemetrySetup.Initialize(this);
InitializeColors();
......
......@@ -6,7 +6,6 @@
using System;
using System.Collections.Immutable;
using System.Composition;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
......@@ -15,24 +14,11 @@
namespace Microsoft.CodeAnalysis.Options.EditorConfig
{
[Export(typeof(IDocumentOptionsProviderFactory)), Shared]
[ExportMetadata("Name", PredefinedDocumentOptionsProviderNames.EditorConfig)]
internal sealed class EditorConfigDocumentOptionsProviderFactory : IDocumentOptionsProviderFactory
internal static class EditorConfigDocumentOptionsProviderFactory
{
[ImportingConstructor]
public EditorConfigDocumentOptionsProviderFactory()
public static IDocumentOptionsProvider Create(Workspace workspace)
{
}
public IDocumentOptionsProvider? TryCreate(Workspace workspace)
{
if (!ShouldUseNativeEditorConfigSupport(workspace))
{
// Simply disable if the feature isn't on
return null;
}
return new EditorConfigDocumentOptionsProvider(workspace.Services.GetRequiredService<IErrorLoggerService>());
return new EditorConfigDocumentOptionsProvider(workspace.Services.GetService<IErrorLoggerService>());
}
private const string LocalRegistryPath = @"Roslyn\Internal\OnOff\Features\";
......@@ -48,15 +34,21 @@ public static bool ShouldUseNativeEditorConfigSupport(Workspace workspace)
private class EditorConfigDocumentOptionsProvider : IDocumentOptionsProvider
{
private readonly IErrorLoggerService _errorLogger;
private readonly IErrorLoggerService? _errorLogger;
public EditorConfigDocumentOptionsProvider(IErrorLoggerService errorLogger)
public EditorConfigDocumentOptionsProvider(IErrorLoggerService? errorLogger)
{
_errorLogger = errorLogger;
}
public async Task<IDocumentOptions?> GetOptionsForDocumentAsync(Document document, CancellationToken cancellationToken)
{
if (!ShouldUseNativeEditorConfigSupport(document.Project.Solution.Workspace))
{
// Simply disable if the feature isn't on
return null;
}
var options = await document.GetAnalyzerOptionsAsync(cancellationToken).ConfigureAwait(false);
return new DocumentOptions(options, _errorLogger);
......@@ -65,9 +57,9 @@ public EditorConfigDocumentOptionsProvider(IErrorLoggerService errorLogger)
private class DocumentOptions : IDocumentOptions
{
private readonly ImmutableDictionary<string, string> _options;
private readonly IErrorLoggerService _errorLogger;
private readonly IErrorLoggerService? _errorLogger;
public DocumentOptions(ImmutableDictionary<string, string> options, IErrorLoggerService errorLogger)
public DocumentOptions(ImmutableDictionary<string, string> options, IErrorLoggerService? errorLogger)
{
_options = options;
_errorLogger = errorLogger;
......
......@@ -14,9 +14,11 @@
using System.Threading.Tasks;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.Diagnostics;
using Microsoft.CodeAnalysis.ErrorLogger;
using Microsoft.CodeAnalysis.Host;
using Microsoft.CodeAnalysis.Internal.Log;
using Microsoft.CodeAnalysis.Options;
using Microsoft.CodeAnalysis.Options.EditorConfig;
using Microsoft.CodeAnalysis.Shared.Extensions;
using Microsoft.CodeAnalysis.Shared.Utilities;
using Microsoft.CodeAnalysis.Text;
......@@ -87,6 +89,8 @@ protected Workspace(HostServices host, string? workspaceKind)
var info = SolutionInfo.Create(SolutionId.CreateNewId(), VersionStamp.Create());
var emptyOptions = new SerializableOptionSet(languages: ImmutableHashSet<string>.Empty, _optionService, serializableOptions: ImmutableHashSet<IOption>.Empty, values: ImmutableDictionary<OptionKey, object?>.Empty);
_latestSolution = CreateSolution(info, emptyOptions);
_optionService.RegisterDocumentOptionsProvider(EditorConfigDocumentOptionsProviderFactory.Create(this));
}
internal void LogTestMessage(string message)
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册