Skip to content

Commit

Permalink
Add GlobalOptions.SetBackgroundAnalysisScope and PythiaGlobalOptions …
Browse files Browse the repository at this point in the history
…External Access API (#59794)

* Add GlobalOptions.SetBackgroundAnalysisScope External Access API

* Add PythiaGlobalOptions

* Add IFSharpEditorFormattingServiceWithOptions
  • Loading branch information
tmat authored Feb 28, 2022
1 parent ce8c5c3 commit aad7e47
Show file tree
Hide file tree
Showing 10 changed files with 127 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
using Microsoft.CodeAnalysis.Completion;
using Microsoft.CodeAnalysis.Host.Mef;
using Microsoft.CodeAnalysis.Options;
using Microsoft.CodeAnalysis.Shared.Options;
using Microsoft.CodeAnalysis.SolutionCrawler;

namespace Microsoft.CodeAnalysis.ExternalAccess.VSTypeScript.Api
{
Expand All @@ -28,6 +30,22 @@ public bool BlockForCompletionItems
set => _globalOptions.SetGlobalOption(new OptionKey(CompletionViewOptions.BlockForCompletionItems, InternalLanguageNames.TypeScript), value);
}

#pragma warning disable CA1822 // Mark members as static - TODO: will set global options in future
public void SetBackgroundAnalysisScope(Workspace workspace, bool openFilesOnly)
#pragma warning restore
{
var solution = workspace.CurrentSolution;
workspace.TryApplyChanges(solution.WithOptions(solution.Options
.WithChangedOption(
SolutionCrawlerOptions.BackgroundAnalysisScopeOption,
InternalLanguageNames.TypeScript,
openFilesOnly ? BackgroundAnalysisScope.OpenFiles : BackgroundAnalysisScope.FullSolution)
.WithChangedOption(
ServiceFeatureOnOffOptions.RemoveDocumentDiagnosticsOnDocumentClose,
InternalLanguageNames.TypeScript,
openFilesOnly)));
}

internal IGlobalOptionService Service => _globalOptions;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// 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.Completion;
using System;
using Microsoft.CodeAnalysis.Options;
using Microsoft.CodeAnalysis.Shared.Options;
using Microsoft.CodeAnalysis.SolutionCrawler;
Expand All @@ -11,6 +11,7 @@ namespace Microsoft.CodeAnalysis.ExternalAccess.VSTypeScript.Api
{
internal static class VSTypeScriptOptions
{
[Obsolete("Use VSTypeScriptGlobalOptions.SetBackgroundAnalysisScope instead")]
public static OptionSet WithBackgroundAnalysisScope(this OptionSet options, bool openFilesOnly)
=> options.WithChangedOption(
SolutionCrawlerOptions.BackgroundAnalysisScopeOption,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// 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.Formatting;

namespace Microsoft.CodeAnalysis.ExternalAccess.FSharp.Editor
{
internal readonly struct AutoFormattingOptionsWrapper
{
internal readonly AutoFormattingOptions UnderlyingObject;

public AutoFormattingOptionsWrapper(AutoFormattingOptions underlyingObject)
=> UnderlyingObject = underlyingObject;

public FormattingOptions.IndentStyle IndentStyle
=> UnderlyingObject.IndentStyle;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// 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.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
Expand Down
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.

namespace Microsoft.CodeAnalysis.ExternalAccess.FSharp.Editor
{
internal interface IFSharpEditorFormattingServiceWithOptions : IFSharpEditorFormattingService
{
/// <summary>
/// True if this service would like to format the document based on the user typing the
/// provided character.
/// </summary>
bool SupportsFormattingOnTypedCharacter(Document document, AutoFormattingOptionsWrapper options, char ch);
}
}
20 changes: 19 additions & 1 deletion src/Tools/ExternalAccess/FSharp/FSharpGlobalOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,45 @@
using System;
using System.Composition;
using Microsoft.CodeAnalysis.Completion;
using Microsoft.CodeAnalysis.Formatting;
using Microsoft.CodeAnalysis.Host.Mef;
using Microsoft.CodeAnalysis.Options;
using Microsoft.CodeAnalysis.SolutionCrawler;
using Microsoft.VisualStudio.LanguageServices;

namespace Microsoft.CodeAnalysis.ExternalAccess.FSharp
{
[Export(typeof(FSharpGlobalOptions)), Shared]
internal sealed class FSharpGlobalOptions
{
private readonly IGlobalOptionService _globalOptions;
private readonly VisualStudioWorkspace _workspace;

[ImportingConstructor]
[Obsolete(MefConstruction.ImportingConstructorMessage, error: true)]
public FSharpGlobalOptions(IGlobalOptionService globalOptions)
public FSharpGlobalOptions(IGlobalOptionService globalOptions, VisualStudioWorkspace workspace)
{
_globalOptions = globalOptions;
_workspace = workspace;
}

public bool BlockForCompletionItems
{
get => _globalOptions.GetOption(CompletionViewOptions.BlockForCompletionItems, LanguageNames.FSharp);
set => _globalOptions.SetGlobalOption(new OptionKey(CompletionViewOptions.BlockForCompletionItems, LanguageNames.FSharp), value);
}

public void SetBackgroundAnalysisScope(bool openFilesOnly)
{
var solution = _workspace.CurrentSolution;

#pragma warning disable CS0618 // Type or member is obsolete
_workspace.TryApplyChanges(solution.WithOptions(solution.Options
.WithChangedOption(
SolutionCrawlerOptions.ClosedFileDiagnostic,
LanguageNames.FSharp,
!openFilesOnly)));
#pragma warning restore
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,9 @@ public Task<IList<TextChange>> GetFormattingChangesOnPasteAsync(Document documen

public bool SupportsFormattingOnTypedCharacter(Document document, AutoFormattingOptions options, char ch)
{
return _service.SupportsFormattingOnTypedCharacter(document, ch);
return _service is IFSharpEditorFormattingServiceWithOptions serviceWithOptions ?
serviceWithOptions.SupportsFormattingOnTypedCharacter(document, new AutoFormattingOptionsWrapper(options), ch) :
_service.SupportsFormattingOnTypedCharacter(document, ch);
}

async Task<ImmutableArray<TextChange>> IFormattingInteractionService.GetFormattingChangesAsync(Document document, TextSpan? textSpan, DocumentOptionSet? documentOptions, CancellationToken cancellationToken)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// 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 Microsoft.CodeAnalysis.Host.Mef;
using Microsoft.CodeAnalysis.Options;

namespace Microsoft.CodeAnalysis.ExternalAccess.Pythia.Api
{
[Export(typeof(PythiaGlobalOptions)), Shared]
internal sealed class PythiaGlobalOptions
{
private readonly IGlobalOptionService _globalOptions;

[ImportingConstructor]
[Obsolete(MefConstruction.ImportingConstructorMessage, error: true)]
public PythiaGlobalOptions(IGlobalOptionService globalOptions)
{
_globalOptions = globalOptions;
}

public bool ShowDebugInfo
{
get => _globalOptions.GetOption(s_showDebugInfoOption);
set => _globalOptions.SetGlobalOption(new OptionKey(s_showDebugInfoOption), value);
}

public bool RemoveRecommendationLimit
{
get => _globalOptions.GetOption(s_removeRecommendationLimitOption);
set => _globalOptions.SetGlobalOption(new OptionKey(s_removeRecommendationLimitOption), value);
}

public const string LocalRegistryPath = @"Roslyn\Internal\OnOff\Features\";

private static readonly Option2<bool> s_showDebugInfoOption = new(
"InternalFeatureOnOffOptions", "ShowDebugInfo", defaultValue: false,
storageLocation: new LocalUserProfileStorageLocation(LocalRegistryPath + "ShowDebugInfo"));

private static readonly Option2<bool> s_removeRecommendationLimitOption = new(
"InternalFeatureOnOffOptions", "RemoveRecommendationLimit", defaultValue: false,
storageLocation: new LocalUserProfileStorageLocation(LocalRegistryPath + "RemoveRecommendationLimit"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@
<RestrictedInternalsVisibleTo Include="Microsoft.CodeAnalysis.LiveUnitTesting.BuildManager.Core" Partner="UnitTesting" Key="$(UnitTestingKey)" />
<RestrictedInternalsVisibleTo Include="Microsoft.CodeAnalysis.UnitTesting.SourceBasedTestDiscovery" Partner="UnitTesting" Key="$(UnitTestingKey)" />
<RestrictedInternalsVisibleTo Include="Microsoft.CodeAnalysis.UnitTesting.SourceBasedTestDiscovery.Core" Partner="UnitTesting" Key="$(UnitTestingKey)" />
<RestrictedInternalsVisibleTo Include="Microsoft.VisualStudio.IntelliCode.CSharp" Partner="Pythia" Key="$(IntelliCodeCSharpKey)" />
<RestrictedInternalsVisibleTo Include="Microsoft.VisualStudio.IntelliCode.CSharp.Extraction" Partner="Pythia" Key="$(IntelliCodeCSharpKey)" />
<InternalsVisibleTo Include="Microsoft.VisualStudio.LanguageServices.CodeLens" />
<InternalsVisibleTo Include="Microsoft.VisualStudio.LanguageServices.CSharp" />
<InternalsVisibleTo Include="Microsoft.VisualStudio.LanguageServices.Implementation" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

namespace Microsoft.CodeAnalysis.ExternalAccess.Pythia.Api
{
[Obsolete("Use PythiaGlobalOptions instead")]
internal static class PythiaOptions
{
public const string LocalRegistryPath = @"Roslyn\Internal\OnOff\Features\";
Expand All @@ -26,6 +27,7 @@ internal static class PythiaOptions
storageLocation: new LocalUserProfileStorageLocation(LocalRegistryPath + nameof(RemoveRecommendationLimit)));
}

[Obsolete("Use PythiaGlobalOptions instead")]
[ExportSolutionOptionProvider, Shared]
internal class PythiaOptionsProvider : IOptionProvider
{
Expand Down

0 comments on commit aad7e47

Please sign in to comment.