提交 1754008c 编写于 作者: H Heejae Chang

Some code clean up.

First is cleanup some infobar code such as adding copyright header, adding internal, removing unused usings and etc.
Second is adding more info in exception log.
Last is making code action more resilient to null return value from user code.
上级 c8e8ad36
......@@ -71,7 +71,6 @@ public override void HandleException(object provider, Exception exception)
() => { EnableProvider(provider); IgnoreProvider(provider); });
}
}
else
{
if (_optionsService.GetOption(ExtensionManagerOptions.DisableCrashingExtensions))
......
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using Microsoft.CodeAnalysis.Extensions;
using Microsoft.CodeAnalysis.Internal.Log;
......
using System;
using System.Collections.Generic;
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System.Composition;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.Extensions;
using Microsoft.CodeAnalysis.Host;
using Microsoft.CodeAnalysis.Host.Mef;
......@@ -11,13 +8,13 @@
namespace Microsoft.CodeAnalysis.Editor.Implementation.Workspaces
{
[ExportWorkspaceServiceFactory(typeof(IErrorReportingService), ServiceLayer.Editor), Shared]
class EditorErrorReportingServiceFactory : IWorkspaceServiceFactory
internal class EditorErrorReportingServiceFactory : IWorkspaceServiceFactory
{
private Lazy<IErrorReportingService> _singleton = new Lazy<IErrorReportingService>(() => new EditorErrorReportingService());
private readonly IErrorReportingService _singleton = new EditorErrorReportingService();
public IWorkspaceService CreateService(HostWorkspaceServices workspaceServices)
{
return _singleton.Value;
return _singleton;
}
}
}
......@@ -34,5 +34,6 @@ internal partial class FeatureAttribute
public const string GlobalOperation = "GlobalOperation";
public const string DiagnosticService = "DiagnosticService";
public const string RuleSetEditor = "RuleSetEditor";
public const string InfoBar = "InfoBar";
}
}
using System;
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.Editor;
using Microsoft.CodeAnalysis.Extensions;
......@@ -11,24 +10,27 @@
using Microsoft.VisualStudio.LanguageServices.Implementation.ProjectSystem;
using Microsoft.VisualStudio.Shell;
using Microsoft.VisualStudio.Shell.Interop;
using VS = Microsoft.VisualStudio.Progression;
namespace Microsoft.VisualStudio.LanguageServices.Implementation
{
internal class VisualStudioErrorReportingService : IErrorReportingService
{
private readonly static InfoBarButton EnableItem = new InfoBarButton(ServicesVSResources.Enable);
private readonly static InfoBarButton EnableAndIgnoreItem = new InfoBarButton(ServicesVSResources.EnableAndIgnore);
private readonly VisualStudioWorkspaceImpl _workspace;
private readonly IForegroundNotificationService _foregroundNotificationService;
private readonly IAsynchronousOperationListener _listener;
private readonly IDocumentTrackingService _documentTrackingService;
private readonly static InfoBarButton EnableItem = new InfoBarButton(ServicesVSResources.Enable);
private readonly static InfoBarButton EnableAndIgnoreItem = new InfoBarButton(ServicesVSResources.EnableAndIgnore);
public VisualStudioErrorReportingService(VisualStudioWorkspaceImpl workspace, IForegroundNotificationService foregroundNotificationService)
public VisualStudioErrorReportingService(
VisualStudioWorkspaceImpl workspace, IForegroundNotificationService foregroundNotificationService, IAsynchronousOperationListener listener)
{
_workspace = workspace;
_foregroundNotificationService = foregroundNotificationService;
_documentTrackingService = workspace.Services.GetService<IDocumentTrackingService>();
_listener = listener;
_documentTrackingService = workspace.Services.GetService<IDocumentTrackingService>();
}
public void ShowErrorInfoForCodeFix(string codefixName, Action OnEnableClicked, Action OnEnableAndIgnoreClicked)
......@@ -36,7 +38,6 @@ public void ShowErrorInfoForCodeFix(string codefixName, Action OnEnableClicked,
var documentId = _documentTrackingService.GetActiveDocument();
// We can be called from any thread since errors can occur anywhere, however we can only construct and InfoBar from the UI thread.
var waiter = new InfoBarWaiter();
_foregroundNotificationService.RegisterNotification(() =>
{
IVsWindowFrame frame;
......@@ -45,7 +46,7 @@ public void ShowErrorInfoForCodeFix(string codefixName, Action OnEnableClicked,
{
CreateInfoBar(codefixName, OnEnableClicked, OnEnableAndIgnoreClicked, frame, factory);
}
}, waiter.BeginAsyncOperation("Show InfoBar"));
}, _listener.BeginAsyncOperation("Show InfoBar"));
}
private void CreateInfoBar(string name, Action onEnableClicked, Action onEnableAndIgnoreClicked, IVsWindowFrame frame, IVsInfoBarUIFactory factory)
......@@ -69,7 +70,7 @@ private void CreateInfoBar(string name, Action onEnableClicked, Action onEnableA
isCloseButtonVisible: true);
IVsInfoBarUIElement infoBarUI;
if (TryCreateInfoBarUI(infoBarModel, factory, out infoBarUI))
if (TryCreateInfoBarUI(factory, infoBarModel, out infoBarUI))
{
uint? infoBarCookie = null;
InfoBarEvents eventSink = new InfoBarEvents(onEnableClicked, onEnableAndIgnoreClicked, () =>
......@@ -79,16 +80,18 @@ private void CreateInfoBar(string name, Action onEnableClicked, Action onEnableA
infoBarUI.Unadvise(infoBarCookie.Value);
}
});
uint cookie;
infoBarUI.Advise(eventSink, out cookie);
infoBarCookie = cookie;
IVsInfoBarHost host = (IVsInfoBarHost)unknown;
host.AddInfoBar(infoBarUI);
}
}
}
private static bool TryCreateInfoBarUI(IVsInfoBar infoBar, IVsInfoBarUIFactory infoBarUIFactory, out IVsInfoBarUIElement uiElement)
private static bool TryCreateInfoBarUI(IVsInfoBarUIFactory infoBarUIFactory, IVsInfoBar infoBar, out IVsInfoBarUIElement uiElement)
{
uiElement = infoBarUIFactory.CreateInfoBar(infoBar);
return uiElement != null;
......@@ -127,7 +130,5 @@ public void OnClosed(IVsInfoBarUIElement infoBarUIElement)
onClosed();
}
}
internal class InfoBarWaiter : AsynchronousOperationListener { }
}
}
using System;
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.Collections.Generic;
using System.Composition;
using Microsoft.CodeAnalysis.Editor;
using Microsoft.CodeAnalysis.Extensions;
using Microsoft.CodeAnalysis.Host;
using Microsoft.CodeAnalysis.Host.Mef;
using Microsoft.CodeAnalysis.Shared.TestHooks;
using Microsoft.VisualStudio.LanguageServices.Implementation.ProjectSystem;
namespace Microsoft.VisualStudio.LanguageServices.Implementation
......@@ -11,17 +15,20 @@ namespace Microsoft.VisualStudio.LanguageServices.Implementation
[ExportWorkspaceServiceFactory(typeof(IErrorReportingService), ServiceLayer.Host), Shared]
internal sealed class VisualStudioErrorReportingServiceFactory : IWorkspaceServiceFactory
{
private Lazy<IErrorReportingService> _singleton;
private readonly IErrorReportingService _singleton;
[ImportingConstructor]
public VisualStudioErrorReportingServiceFactory(VisualStudioWorkspaceImpl workspace, IForegroundNotificationService foregroundNotificationService)
public VisualStudioErrorReportingServiceFactory(
VisualStudioWorkspaceImpl workspace,
IForegroundNotificationService foregroundNotificationService,
[ImportMany] IEnumerable<Lazy<IAsynchronousOperationListener, FeatureMetadata>> asyncListeners)
{
_singleton = new Lazy<IErrorReportingService>(() => new VisualStudioErrorReportingService(workspace, foregroundNotificationService));
_singleton = new VisualStudioErrorReportingService(workspace, foregroundNotificationService, new AggregateAsynchronousOperationListener(asyncListeners, FeatureAttribute.InfoBar));
}
public IWorkspaceService CreateService(HostWorkspaceServices workspaceServices)
{
return _singleton.Value;
return _singleton;
}
}
}
......@@ -11,6 +11,7 @@
using Microsoft.CodeAnalysis.CodeRefactorings;
using Microsoft.CodeAnalysis.Formatting;
using Microsoft.CodeAnalysis.Simplification;
using Roslyn.Utilities;
namespace Microsoft.CodeAnalysis.CodeActions
{
......@@ -84,6 +85,11 @@ public async Task<ImmutableArray<CodeActionOperation>> GetPreviewOperationsAsync
protected virtual async Task<IEnumerable<CodeActionOperation>> ComputeOperationsAsync(CancellationToken cancellationToken)
{
var changedSolution = await GetChangedSolutionAsync(cancellationToken).ConfigureAwait(false);
if (changedSolution == null)
{
return null;
}
return new CodeActionOperation[] { new ApplyChangesOperation(changedSolution) };
}
......@@ -103,6 +109,11 @@ protected virtual async Task<IEnumerable<CodeActionOperation>> ComputePreviewOpe
protected async virtual Task<Solution> GetChangedSolutionAsync(CancellationToken cancellationToken)
{
var changedDocument = await GetChangedDocumentAsync(cancellationToken).ConfigureAwait(false);
if (changedDocument == null)
{
return null;
}
return changedDocument.Project.Solution;
}
......@@ -121,6 +132,11 @@ protected virtual Task<Document> GetChangedDocumentAsync(CancellationToken cance
internal async Task<Solution> GetChangedSolutionInternalAsync(CancellationToken cancellationToken)
{
var solution = await GetChangedSolutionAsync(cancellationToken).ConfigureAwait(false);
if (solution == null)
{
return null;
}
return await this.PostProcessChangesAsync(solution, cancellationToken).ConfigureAwait(false);
}
......
......@@ -96,10 +96,15 @@ protected static T Cast<T>(object value)
protected static void CheckDeclarationNode<TDeclarationNode>(SyntaxNode destination) where TDeclarationNode : SyntaxNode
{
if (destination == null)
{
throw new ArgumentNullException("destination");
}
if (!(destination is TDeclarationNode))
{
throw new ArgumentException(
string.Format(WorkspacesResources.InvalidDestinationNode, typeof(TDeclarationNode).Name),
string.Format(WorkspacesResources.InvalidDestinationNode, typeof(TDeclarationNode).Name, destination.GetType().Name),
"destination");
}
}
......@@ -108,12 +113,17 @@ protected static T Cast<T>(object value)
where TDeclarationNode1 : SyntaxNode
where TDeclarationNode2 : SyntaxNode
{
if (destination == null)
{
throw new ArgumentNullException("destination");
}
if (!(destination is TDeclarationNode1) &&
!(destination is TDeclarationNode2))
{
throw new ArgumentException(
string.Format(WorkspacesResources.InvalidDestinationNode2,
typeof(TDeclarationNode1).Name, typeof(TDeclarationNode2).Name),
typeof(TDeclarationNode1).Name, typeof(TDeclarationNode2).Name, destination.GetType().Name),
"destination");
}
}
......@@ -123,13 +133,18 @@ protected static T Cast<T>(object value)
where TDeclarationNode2 : SyntaxNode
where TDeclarationNode3 : SyntaxNode
{
if (destination == null)
{
throw new ArgumentNullException("destination");
}
if (!(destination is TDeclarationNode1) &&
!(destination is TDeclarationNode2) &&
!(destination is TDeclarationNode3))
{
throw new ArgumentException(
string.Format(WorkspacesResources.InvalidDestinationNode3,
typeof(TDeclarationNode1).Name, typeof(TDeclarationNode2).Name, typeof(TDeclarationNode3).Name),
typeof(TDeclarationNode1).Name, typeof(TDeclarationNode2).Name, typeof(TDeclarationNode3).Name, destination.GetType().Name),
"destination");
}
}
......
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using Microsoft.CodeAnalysis.Host;
namespace Microsoft.CodeAnalysis.Extensions
{
interface IErrorReportingService : IWorkspaceService
internal interface IErrorReportingService : IWorkspaceService
{
void ShowErrorInfoForCodeFix(string codefixName, Action OnEnableClicked, Action OnEnableAndIgnoreClicked);
}
......
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.Collections.Generic;
using System.Composition;
using System.Runtime.CompilerServices;
using System.Threading;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.Host;
using Microsoft.CodeAnalysis.Host.Mef;
using Microsoft.CodeAnalysis.Shared.Collections;
using Microsoft.CodeAnalysis.Shared.Utilities;
using Microsoft.CodeAnalysis.Text;
namespace Microsoft.CodeAnalysis.Extensions
{
......
......@@ -467,7 +467,7 @@ internal class WorkspacesResources {
}
/// <summary>
/// Looks up a localized string similar to Destination type must be a {0}..
/// Looks up a localized string similar to Destination type must be a {0}. but given one is {1}..
/// </summary>
internal static string InvalidDestinationNode {
get {
......@@ -476,7 +476,7 @@ internal class WorkspacesResources {
}
/// <summary>
/// Looks up a localized string similar to Destination type must be a {0} or a {1}..
/// Looks up a localized string similar to Destination type must be a {0} or a {1}. but given one is {2}..
/// </summary>
internal static string InvalidDestinationNode2 {
get {
......@@ -485,7 +485,7 @@ internal class WorkspacesResources {
}
/// <summary>
/// Looks up a localized string similar to Destination type must be a {0}, {1} or {2}..
/// Looks up a localized string similar to Destination type must be a {0}, {1} or {2}. but given one is {3}..
/// </summary>
internal static string InvalidDestinationNode3 {
get {
......
......@@ -130,13 +130,13 @@
<value>Cycle detected in extensions</value>
</data>
<data name="InvalidDestinationNode" xml:space="preserve">
<value>Destination type must be a {0}.</value>
<value>Destination type must be a {0}. but given one is {1}.</value>
</data>
<data name="InvalidDestinationNode2" xml:space="preserve">
<value>Destination type must be a {0} or a {1}.</value>
<value>Destination type must be a {0} or a {1}. but given one is {2}.</value>
</data>
<data name="InvalidDestinationNode3" xml:space="preserve">
<value>Destination type must be a {0}, {1} or {2}.</value>
<value>Destination type must be a {0}, {1} or {2}. but given one is {3}.</value>
</data>
<data name="CouldNotFindLocationToGen" xml:space="preserve">
<value>Could not find location to generation symbol into.</value>
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册