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 @@ -34,15 +34,18 @@ internal sealed class SplitCommentCommandHandler : ICommandHandler<ReturnKeyComm
{
private readonly ITextUndoHistoryRegistry _undoHistoryRegistry;
private readonly IEditorOperationsFactoryService _editorOperationsFactoryService;
private readonly IGlobalOptionService _globalOptions;

[ImportingConstructor]
[Obsolete(MefConstruction.ImportingConstructorMessage, error: true)]
public SplitCommentCommandHandler(
ITextUndoHistoryRegistry undoHistoryRegistry,
IEditorOperationsFactoryService editorOperationsFactoryService)
IEditorOperationsFactoryService editorOperationsFactoryService,
IGlobalOptionService globalOptions)
{
_undoHistoryRegistry = undoHistoryRegistry;
_editorOperationsFactoryService = editorOperationsFactoryService;
_globalOptions = globalOptions;
}

public string DisplayName => EditorFeaturesResources.Split_comment;
Expand All @@ -65,6 +68,9 @@ public bool ExecuteCommand(ReturnKeyCommandArgs args, CommandExecutionContext co
if (document == null)
return false;

if (!_globalOptions.GetOption(SplitCommentOptions.Enabled, document.Project.Language))
return false;

var splitCommentService = document.GetLanguageService<ISplitCommentService>();
if (splitCommentService == null)
return false;
Expand Down Expand Up @@ -138,11 +144,6 @@ private static bool MatchesCommentStart(string commentStart, ITextSnapshotLine l
SnapshotSpan selectionSpan,
CancellationToken cancellationToken)
{
var options = await document.GetOptionsAsync(cancellationToken).ConfigureAwait(false);
var enabled = options.GetOption(SplitCommentOptions.Enabled);
if (!enabled)
return null;

var root = await document.GetRequiredSyntaxRootAsync(cancellationToken).ConfigureAwait(false);
var syntaxKinds = document.GetRequiredLanguageService<ISyntaxKindsService>();
var trivia = root.FindTrivia(selectionSpan.Start);
Expand All @@ -168,6 +169,7 @@ private static bool MatchesCommentStart(string commentStart, ITextSnapshotLine l
var textSnapshot = selectionSpan.Snapshot;
var triviaLine = textSnapshot.GetLineFromPosition(trivia.SpanStart);

var options = await document.GetOptionsAsync(cancellationToken).ConfigureAwait(false);
var replacementSpan = GetReplacementSpan(triviaLine, selectionSpan);
var replacementText = GetReplacementText(textView, options, triviaLine, trivia, selectionSpan.Start);
return (replacementSpan, replacementText);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ internal abstract partial class AbstractAsynchronousTaggerProvider<TTag> : Foreg
/// Options controlling this tagger. The tagger infrastructure will check this option
/// against the buffer it is associated with to see if it should tag or not.
///
/// An empty enumerable, or null, can be returned to indicate that this tagger should
/// An empty enumerable can be returned to indicate that this tagger should
/// run unconditionally.
/// </summary>
protected virtual IEnumerable<Option2<bool>> Options => SpecializedCollections.EmptyEnumerable<Option2<bool>>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,13 @@ private void TestWorker(
Assert.True(expectedOutputMarkup.Contains("\t"));
}

using var workspace = this.CreateWorkspace(inputMarkup);
using var workspace = CreateWorkspace(inputMarkup);

var globalOptions = workspace.ExportProvider.GetExportedValue<IGlobalOptionService>();
var language = workspace.Projects.Single().Language;
workspace.SetOptions(
workspace.Options.WithChangedOption(FormattingOptions.UseTabs, language, useTabs)
.WithChangedOption(SplitCommentOptions.Enabled, language, enabled));

globalOptions.SetGlobalOption(new OptionKey(SplitCommentOptions.Enabled, language), enabled);
workspace.SetOptions(workspace.Options.WithChangedOption(FormattingOptions.UseTabs, language, useTabs));

var document = workspace.Documents.Single();
var view = document.GetTextView();
Expand Down

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
using Microsoft.CodeAnalysis.Editor.UnitTests.Workspaces;
using Microsoft.CodeAnalysis.Host;
using Microsoft.CodeAnalysis.Host.Mef;
using Microsoft.CodeAnalysis.Options;
using Microsoft.CodeAnalysis.Remote;
using Microsoft.CodeAnalysis.Remote.Testing;
using Microsoft.CodeAnalysis.Test.Utilities;
Expand Down Expand Up @@ -58,6 +59,8 @@ public async Task Proxy(TestHost testHost)

using var localWorkspace = new TestWorkspace(composition: localComposition);

var globalOptions = localWorkspace.GetService<IGlobalOptionService>();

MockEditAndContinueWorkspaceService mockEncService;
var clientProvider = (InProcRemoteHostClientProvider?)localWorkspace.Services.GetService<IRemoteHostClientProvider>();
if (testHost == TestHost.InProcess)
Expand Down Expand Up @@ -240,7 +243,7 @@ void VerifyReanalyzeInvocation(ImmutableArray<DocumentId> documentIds)
},
emitDiagnosticsUpdated.Select(update =>
{
var d = update.GetPushDiagnostics(localWorkspace, InternalDiagnosticsOptions.NormalDiagnosticMode).Single();
var d = update.GetPushDiagnostics(globalOptions, InternalDiagnosticsOptions.NormalDiagnosticMode).Single();
return $"[{d.ProjectId}] {d.Severity} {d.Id}:" +
(d.DataLocation != null ? $" {d.DataLocation.OriginalFilePath}({d.DataLocation.OriginalStartLine}, {d.DataLocation.OriginalStartColumn}, {d.DataLocation.OriginalEndLine}, {d.DataLocation.OriginalEndColumn}):" : "") +
$" {d.Message}";
Expand Down
7 changes: 5 additions & 2 deletions src/EditorFeatures/Test/Preview/PreviewWorkspaceTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
using Microsoft.CodeAnalysis.Editor.UnitTests.Workspaces;
using Microsoft.CodeAnalysis.Host;
using Microsoft.CodeAnalysis.Host.Mef;
using Microsoft.CodeAnalysis.Options;
using Microsoft.CodeAnalysis.Shared.Extensions;
using Microsoft.CodeAnalysis.Shared.TestHooks;
using Microsoft.CodeAnalysis.SolutionCrawler;
Expand Down Expand Up @@ -136,9 +137,11 @@ public async Task TestPreviewServices()
public async Task TestPreviewDiagnostic()
{
var hostServices = EditorTestCompositions.EditorFeatures.GetHostServices();
var exportProvider = (IMefHostExportProvider)hostServices;

var diagnosticService = (IDiagnosticUpdateSource)((IMefHostExportProvider)hostServices).GetExportedValue<IDiagnosticAnalyzerService>();
var diagnosticService = (IDiagnosticUpdateSource)exportProvider.GetExportedValue<IDiagnosticAnalyzerService>();
RoslynDebug.AssertNotNull(diagnosticService);
var globalOptions = exportProvider.GetExportedValue<IGlobalOptionService>();

var taskSource = new TaskCompletionSource<DiagnosticsUpdatedArgs>();
diagnosticService.DiagnosticsUpdated += (s, a) => taskSource.TrySetResult(a);
Expand All @@ -164,7 +167,7 @@ public async Task TestPreviewDiagnostic()
Assert.True(taskSource.Task.IsCompleted);

var args = taskSource.Task.Result;
Assert.True(args.GetPushDiagnostics(previewWorkspace, InternalDiagnosticsOptions.NormalDiagnosticMode).Length > 0);
Assert.True(args.GetPushDiagnostics(globalOptions, InternalDiagnosticsOptions.NormalDiagnosticMode).Length > 0);
}

[WpfFact]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@
using Microsoft.CodeAnalysis.LanguageServer;
using Microsoft.CodeAnalysis.LanguageServer.Handler;
using Microsoft.CodeAnalysis.LanguageServer.Handler.CodeActions;
using Microsoft.CodeAnalysis.Options;
using Microsoft.CodeAnalysis.Shared.Extensions;
using Microsoft.CodeAnalysis.Test.Utilities;
using Microsoft.CodeAnalysis.Text;
using Microsoft.VisualStudio.Composition;
using Microsoft.VisualStudio.Text.Adornments;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Roslyn.Utilities;
Expand Down Expand Up @@ -404,8 +404,9 @@ private static RequestDispatcher CreateRequestDispatcher(TestWorkspace workspace

private static RequestExecutionQueue CreateRequestQueue(TestWorkspace workspace)
{
var registrationService = workspace.ExportProvider.GetExportedValue<ILspWorkspaceRegistrationService>();
return new RequestExecutionQueue(NoOpLspLogger.Instance, registrationService, ProtocolConstants.RoslynLspLanguages, serverName: "Tests", "TestClient");
var registrationService = workspace.GetService<ILspWorkspaceRegistrationService>();
var globalOptions = workspace.GetService<IGlobalOptionService>();
return new RequestExecutionQueue(NoOpLspLogger.Instance, registrationService, globalOptions, ProtocolConstants.RoslynLspLanguages, serverName: "Tests", "TestClient");
}

private static string GetDocumentFilePathFromName(string documentName)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
using Microsoft.CodeAnalysis.Editor.Implementation.Diagnostics;
using Microsoft.CodeAnalysis.Editor.UnitTests.Diagnostics;
using Microsoft.CodeAnalysis.Editor.UnitTests.Workspaces;
using Microsoft.CodeAnalysis.Options;
using Microsoft.CodeAnalysis.Text;
using Microsoft.CodeAnalysis.Text.Shared.Extensions;
using Microsoft.VisualStudio.Text.Tagging;
Expand All @@ -33,22 +34,22 @@ internal sealed class TestDiagnosticTagProducer<TProvider>

internal static async Task<IList<ITagSpan<IErrorTag>>> GetErrorsFromUpdateSource(TestWorkspace workspace, DiagnosticsUpdatedArgs updateArgs)
{
var source = new TestDiagnosticUpdateSource(workspace);
using (var wrapper = new DiagnosticTaggerWrapper<TProvider, IErrorTag>(workspace, updateSource: source))
{
var tagger = wrapper.TaggerProvider.CreateTagger<IErrorTag>(workspace.Documents.First().GetTextBuffer());
using (var disposable = tagger as IDisposable)
{
source.RaiseDiagnosticsUpdated(updateArgs);
var globalOptions = workspace.GetService<IGlobalOptionService>();
var source = new TestDiagnosticUpdateSource(globalOptions);

await wrapper.WaitForTags();
using var wrapper = new DiagnosticTaggerWrapper<TProvider, IErrorTag>(workspace, updateSource: source);

var snapshot = workspace.Documents.First().GetTextBuffer().CurrentSnapshot;
var spans = tagger.GetTags(snapshot.GetSnapshotSpanCollection()).ToImmutableArray();
var tagger = wrapper.TaggerProvider.CreateTagger<IErrorTag>(workspace.Documents.First().GetTextBuffer());
using var disposable = (IDisposable)tagger;

return spans;
}
}
source.RaiseDiagnosticsUpdated(updateArgs);

await wrapper.WaitForTags();

var snapshot = workspace.Documents.First().GetTextBuffer().CurrentSnapshot;
var spans = tagger.GetTags(snapshot.GetSnapshotSpanCollection()).ToImmutableArray();

return spans;
}

internal static DiagnosticData CreateDiagnosticData(TestHostDocument document, TextSpan span)
Expand All @@ -72,14 +73,14 @@ internal static DiagnosticData CreateDiagnosticData(TestHostDocument document, T
private class TestDiagnosticUpdateSource : IDiagnosticUpdateSource
{
private ImmutableArray<DiagnosticData> _diagnostics = ImmutableArray<DiagnosticData>.Empty;
private readonly Workspace _workspace;
private readonly IGlobalOptionService _globalOptions;

public TestDiagnosticUpdateSource(Workspace workspace)
=> _workspace = workspace;
public TestDiagnosticUpdateSource(IGlobalOptionService globalOptions)
=> _globalOptions = globalOptions;

public void RaiseDiagnosticsUpdated(DiagnosticsUpdatedArgs args)
{
_diagnostics = args.GetPushDiagnostics(_workspace, InternalDiagnosticsOptions.NormalDiagnosticMode);
_diagnostics = args.GetPushDiagnostics(_globalOptions, InternalDiagnosticsOptions.NormalDiagnosticMode);
DiagnosticsUpdated?.Invoke(this, args);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ Imports Microsoft.CodeAnalysis.Editor.UnitTests.Extensions
Imports Microsoft.CodeAnalysis.Editor.UnitTests.Utilities
Imports Microsoft.CodeAnalysis.Editor.UnitTests.Workspaces
Imports Microsoft.CodeAnalysis.Editor.VisualBasic.ImplementInterface
Imports Microsoft.CodeAnalysis.Options
Imports Microsoft.CodeAnalysis.Text
Imports Microsoft.VisualStudio.Commanding
Imports Microsoft.VisualStudio.Text
Expand Down Expand Up @@ -62,10 +63,10 @@ End Class
Interface IGoo
Sub TestSub()
End Interface")
Dim globalOptions = workspace.GetService(Of IGlobalOptionService)

Dim commandHandler = MoveCaretAndCreateCommandHandler(workspace)
workspace.TryApplyChanges(workspace.CurrentSolution.WithOptions(workspace.Options _
.WithChangedOption(FeatureOnOffOptions.AutomaticInsertionOfAbstractOrInterfaceMembers, LanguageNames.VisualBasic, False)))
globalOptions.SetGlobalOption(New OptionKey(FeatureOnOffOptions.AutomaticInsertionOfAbstractOrInterfaceMembers, LanguageNames.VisualBasic), False)

Dim nextHandlerCalled = False
Dim view = workspace.Documents.Single().GetTextView()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,24 +9,24 @@ namespace Microsoft.CodeAnalysis.Diagnostics
{
internal static class DiagnosticModeExtensions
{
private static DiagnosticMode GetDiagnosticMode(Workspace workspace, Option2<DiagnosticMode> option)
private static DiagnosticMode GetDiagnosticMode(IGlobalOptionService globalOptions, Option2<DiagnosticMode> option)
{
var diagnosticModeOption = workspace.Options.GetOption(option);
var diagnosticModeOption = globalOptions.GetOption(option);

// If the workspace diagnostic mode is set to Default, defer to the feature flag service.
if (diagnosticModeOption == DiagnosticMode.Default)
{
return workspace.Options.GetOption(DiagnosticOptions.LspPullDiagnosticsFeatureFlag) ? DiagnosticMode.Pull : DiagnosticMode.Push;
return globalOptions.GetOption(DiagnosticOptions.LspPullDiagnosticsFeatureFlag) ? DiagnosticMode.Pull : DiagnosticMode.Push;
}

// Otherwise, defer to the workspace+option to determine what mode we're in.
return diagnosticModeOption;
}

public static bool IsPullDiagnostics(this Workspace workspace, Option2<DiagnosticMode> option)
=> GetDiagnosticMode(workspace, option) == DiagnosticMode.Pull;
public static bool IsPullDiagnostics(this IGlobalOptionService globalOptions, Option2<DiagnosticMode> option)
=> GetDiagnosticMode(globalOptions, option) == DiagnosticMode.Pull;

public static bool IsPushDiagnostics(this Workspace workspace, Option2<DiagnosticMode> option)
=> GetDiagnosticMode(workspace, option) == DiagnosticMode.Push;
public static bool IsPushDiagnostics(this IGlobalOptionService globalOptions, Option2<DiagnosticMode> option)
=> GetDiagnosticMode(globalOptions, option) == DiagnosticMode.Push;
}
}
8 changes: 6 additions & 2 deletions src/Features/Core/Portable/Diagnostics/DiagnosticService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,19 @@ internal partial class DiagnosticService : IDiagnosticService
private readonly Dictionary<IDiagnosticUpdateSource, Dictionary<Workspace, Dictionary<object, Data>>> _map;

private readonly EventListenerTracker<IDiagnosticService> _eventListenerTracker;
private readonly IGlobalOptionService _globalOptions;

private ImmutableHashSet<IDiagnosticUpdateSource> _updateSources;

[ImportingConstructor]
[Obsolete(MefConstruction.ImportingConstructorMessage, error: true)]
public DiagnosticService(
IGlobalOptionService globalOptions,
IAsynchronousOperationListenerProvider listenerProvider,
[ImportMany] IEnumerable<Lazy<IEventListener, EventListenerMetadata>> eventListeners)
{
_globalOptions = globalOptions;

// we use registry service rather than doing MEF import since MEF import method can have race issue where
// update source gets created before aggregator - diagnostic service - is created and we will lose events fired before
// the aggregator is created.
Expand Down Expand Up @@ -233,7 +237,7 @@ private ValueTask<ImmutableArray<DiagnosticData>> GetDiagnosticsAsync(
{
// If this is a pull client, but pull diagnostics is not on, then they get nothing. Similarly, if this is a
// push client and pull diagnostics are on, they get nothing.
var isPull = workspace.IsPullDiagnostics(diagnosticMode);
var isPull = _globalOptions.IsPullDiagnostics(diagnosticMode);
if (forPullDiagnostics != isPull)
return new ValueTask<ImmutableArray<DiagnosticData>>(ImmutableArray<DiagnosticData>.Empty);

Expand Down Expand Up @@ -329,7 +333,7 @@ private ImmutableArray<DiagnosticBucket> GetDiagnosticBuckets(
{
// If this is a pull client, but pull diagnostics is not on, then they get nothing. Similarly, if this is a
// push client and pull diagnostics are on, they get nothing.
var isPull = workspace.IsPullDiagnostics(diagnosticMode);
var isPull = _globalOptions.IsPullDiagnostics(diagnosticMode);
if (forPullDiagnostics != isPull)
return ImmutableArray<DiagnosticBucket>.Empty;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,10 @@ public ImmutableArray<DiagnosticData> GetAllDiagnosticsRegardlessOfPushPullSetti
/// diagnostics in their scenario (or an empty array if not in their scenario).
/// </summary>
public ImmutableArray<DiagnosticData> GetPullDiagnostics(
Workspace workspace, Option2<DiagnosticMode> diagnosticMode)
IGlobalOptionService globalOptions, Option2<DiagnosticMode> diagnosticMode)
{
// If push diagnostics are on, they get nothing since they're asking for pull diagnostics.
if (workspace.IsPushDiagnostics(diagnosticMode))
if (globalOptions.IsPushDiagnostics(diagnosticMode))
return ImmutableArray<DiagnosticData>.Empty;

return _diagnostics;
Expand All @@ -66,10 +66,10 @@ public ImmutableArray<DiagnosticData> GetPullDiagnostics(
/// diagnostics in their scenario (or an empty array if not in their scenario).
/// </summary>
public ImmutableArray<DiagnosticData> GetPushDiagnostics(
Workspace workspace, Option2<DiagnosticMode> diagnosticMode)
IGlobalOptionService globalOptions, Option2<DiagnosticMode> diagnosticMode)
{
// If pull diagnostics are on, they get nothing since they're asking for push diagnostics.
if (workspace.IsPullDiagnostics(diagnosticMode))
if (globalOptions.IsPullDiagnostics(diagnosticMode))
return ImmutableArray<DiagnosticData>.Empty;

return _diagnostics;
Expand Down
Loading