Skip to content

Commit

Permalink
Merge pull request #40513 from JoeRobich/workspace-registers-optionsp…
Browse files Browse the repository at this point in the history
…rovider

Always try to register the EditorConfigDocumentOptionsProvider
  • Loading branch information
JoeRobich authored Mar 2, 2020
2 parents 43df70a + 48bf4d9 commit 5e2bd9e
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
Expand All @@ -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)
Expand Down
6 changes: 3 additions & 3 deletions src/VisualStudio/Core/Def/RoslynPackage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

using System;
using System.Collections.Immutable;
using System.Composition;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
Expand All @@ -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\";
Expand All @@ -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);
Expand All @@ -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;
Expand Down
4 changes: 4 additions & 0 deletions src/Workspaces/Core/Portable/Workspace/Workspace.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 5e2bd9e

Please sign in to comment.