提交 da717b8e 编写于 作者: C CyrusNajmabadi

Cleanup and simplify the viewmodels for boolean and enum code styles.

上级 985791b6
......@@ -715,39 +715,47 @@ private void AddExpressionBodyOptions(OptionSet optionSet, string expressionPref
new CodeStylePreference(CSharpVSResources.When_on_single_line, isChecked: false),
};
var enumValues = new[] { ExpressionBodyPreference.Never, ExpressionBodyPreference.WhenPossible, ExpressionBodyPreference.WhenOnSingleLine };
CodeStyleItems.Add(new EnumCodeStyleOptionViewModel<ExpressionBodyPreference>(
CSharpCodeStyleOptions.PreferExpressionBodiedMethods,
ServicesVSResources.Use_expression_body_for_methods,
enumValues,
new[] { s_preferBlockBodyForMethods, s_preferExpressionBodyForMethods, s_preferExpressionBodyForMethods },
this, optionSet, expressionPreferencesGroupTitle, expressionBodyPreferences));
CodeStyleItems.Add(new EnumCodeStyleOptionViewModel<ExpressionBodyPreference>(
CSharpCodeStyleOptions.PreferExpressionBodiedConstructors,
ServicesVSResources.Use_expression_body_for_constructors,
enumValues,
new[] { s_preferBlockBodyForConstructors, s_preferExpressionBodyForConstructors, s_preferExpressionBodyForConstructors },
this, optionSet, expressionPreferencesGroupTitle, expressionBodyPreferences));
CodeStyleItems.Add(new EnumCodeStyleOptionViewModel<ExpressionBodyPreference>(
CSharpCodeStyleOptions.PreferExpressionBodiedOperators,
ServicesVSResources.Use_expression_body_for_operators,
enumValues,
new[] { s_preferBlockBodyForOperators, s_preferExpressionBodyForOperators, s_preferExpressionBodyForOperators },
this, optionSet, expressionPreferencesGroupTitle, expressionBodyPreferences));
CodeStyleItems.Add(new EnumCodeStyleOptionViewModel<ExpressionBodyPreference>(
CSharpCodeStyleOptions.PreferExpressionBodiedProperties,
ServicesVSResources.Use_expression_body_for_properties,
enumValues,
new[] { s_preferBlockBodyForProperties, s_preferExpressionBodyForProperties, s_preferExpressionBodyForProperties },
this, optionSet, expressionPreferencesGroupTitle, expressionBodyPreferences));
CodeStyleItems.Add(new EnumCodeStyleOptionViewModel<ExpressionBodyPreference>(
CSharpCodeStyleOptions.PreferExpressionBodiedIndexers,
ServicesVSResources.Use_expression_body_for_indexers,
enumValues,
new[] { s_preferBlockBodyForIndexers, s_preferExpressionBodyForIndexers, s_preferExpressionBodyForIndexers },
this, optionSet, expressionPreferencesGroupTitle, expressionBodyPreferences));
CodeStyleItems.Add(new EnumCodeStyleOptionViewModel<ExpressionBodyPreference>(
CSharpCodeStyleOptions.PreferExpressionBodiedAccessors,
ServicesVSResources.Use_expression_body_for_accessors,
enumValues,
new[] { s_preferBlockBodyForAccessors, s_preferExpressionBodyForAccessors, s_preferExpressionBodyForAccessors },
this, optionSet, expressionPreferencesGroupTitle, expressionBodyPreferences));
}
......
// 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.Collections.Generic;
using System.Collections.Immutable;
using System.Linq;
using Microsoft.CodeAnalysis.CodeStyle;
using Microsoft.CodeAnalysis.Options;
......@@ -11,8 +10,7 @@
namespace Microsoft.VisualStudio.LanguageServices.Implementation.Options
{
/// <summary>
/// This class acts as a base for any view model that
/// binds to the codestyle options UI.
/// This class acts as a base for any view model that binds to the codestyle options UI.
/// </summary>
/// <remarks>
/// This supports databinding of:
......@@ -25,8 +23,6 @@ namespace Microsoft.VisualStudio.LanguageServices.Implementation.Options
/// </remarks>
internal abstract class AbstractCodeStyleOptionViewModel : AbstractNotifyPropertyChanged
{
protected readonly ImmutableArray<string> Previews;
protected AbstractOptionPreviewViewModel Info { get; }
public IOption Option { get; }
......@@ -37,7 +33,6 @@ internal abstract class AbstractCodeStyleOptionViewModel : AbstractNotifyPropert
public List<NotificationOptionViewModel> NotificationPreferences { get; set; }
public abstract CodeStylePreference SelectedPreference { get; set; }
public abstract bool NotificationsAvailable { get; }
public abstract string GetPreview();
public virtual NotificationOptionViewModel SelectedNotificationPreference
......@@ -49,15 +44,12 @@ public virtual NotificationOptionViewModel SelectedNotificationPreference
public AbstractCodeStyleOptionViewModel(
IOption option,
string description,
string[] previews,
AbstractOptionPreviewViewModel info,
OptionSet options,
string groupName,
List<CodeStylePreference> preferences = null,
List<NotificationOptionViewModel> notificationPreferences = null)
{
Previews = ImmutableArray.Create(previews);
Info = info;
Option = option;
Description = description;
......
......@@ -13,40 +13,11 @@ namespace Microsoft.VisualStudio.LanguageServices.Implementation.Options
/// </summary>
internal class BooleanCodeStyleOptionViewModel : AbstractCodeStyleOptionViewModel
{
private CodeStylePreference _selectedPreference;
public override CodeStylePreference SelectedPreference
{
get
{
return _selectedPreference;
}
set
{
if (SetProperty(ref _selectedPreference, value))
{
Info.SetOptionAndUpdatePreview(new CodeStyleOption<bool>(_selectedPreference.IsChecked, _selectedNotificationPreference.Notification), Option, GetPreview());
}
}
}
private readonly string _truePreview;
private readonly string _falsePreview;
private CodeStylePreference _selectedPreference;
private NotificationOptionViewModel _selectedNotificationPreference;
public override NotificationOptionViewModel SelectedNotificationPreference
{
get
{
return _selectedNotificationPreference;
}
set
{
if (SetProperty(ref _selectedNotificationPreference, value))
{
Info.SetOptionAndUpdatePreview(new CodeStyleOption<bool>(_selectedPreference.IsChecked, _selectedNotificationPreference.Notification), Option, GetPreview());
}
}
}
public override bool NotificationsAvailable => true;
public BooleanCodeStyleOptionViewModel(
IOption option,
......@@ -58,8 +29,11 @@ public override NotificationOptionViewModel SelectedNotificationPreference
string groupName,
List<CodeStylePreference> preferences = null,
List<NotificationOptionViewModel> notificationPreferences = null)
: base(option, description, new[] { truePreview, falsePreview }, info, options, groupName, preferences, notificationPreferences)
: base(option, description, info, options, groupName, preferences, notificationPreferences)
{
_truePreview = truePreview;
_falsePreview = falsePreview;
var codeStyleOption = ((CodeStyleOption<bool>)options.GetOption(new OptionKey(option, option.IsPerLanguage ? info.Language : null)));
_selectedPreference = Preferences.Single(c => c.IsChecked == codeStyleOption.Value);
......@@ -70,7 +44,33 @@ public override NotificationOptionViewModel SelectedNotificationPreference
NotifyPropertyChanged(nameof(SelectedNotificationPreference));
}
public override CodeStylePreference SelectedPreference
{
get => _selectedPreference;
set
{
if (SetProperty(ref _selectedPreference, value))
{
Info.SetOptionAndUpdatePreview(new CodeStyleOption<bool>(_selectedPreference.IsChecked, _selectedNotificationPreference.Notification), Option, GetPreview());
}
}
}
public override NotificationOptionViewModel SelectedNotificationPreference
{
get => _selectedNotificationPreference;
set
{
if (SetProperty(ref _selectedNotificationPreference, value))
{
Info.SetOptionAndUpdatePreview(new CodeStyleOption<bool>(_selectedPreference.IsChecked, _selectedNotificationPreference.Notification), Option, GetPreview());
}
}
}
public override string GetPreview()
=> SelectedPreference.IsChecked ? Previews[0] : Previews[1];
=> SelectedPreference.IsChecked ? _truePreview : _falsePreview;
}
}
\ No newline at end of file
......@@ -27,23 +27,60 @@ static EnumCodeStyleOptionViewModel()
Contract.ThrowIfFalse(typeof(T).IsEnum);
}
private static readonly ImmutableArray<T> s_enumValues =
ImmutableArray.Create((T[])Enum.GetValues(typeof(T)));
private readonly ImmutableArray<T> _enumValues;
private readonly ImmutableArray<string> _previews;
private CodeStylePreference _selectedPreference;
private NotificationOptionViewModel _selectedNotificationPreference;
public EnumCodeStyleOptionViewModel(
Option<CodeStyleOption<T>> option,
string description,
T[] enumValues,
string[] previews,
AbstractOptionPreviewViewModel info,
OptionSet options,
string groupName,
List<CodeStylePreference> preferences)
: base(option, description, info, options, groupName, preferences)
{
Debug.Assert(preferences.Count == enumValues.Length);
Debug.Assert(previews.Length == enumValues.Length);
var expectedEnumValues = Enum.GetValues(typeof(T));
Debug.Assert(expectedEnumValues.Length == enumValues.Length, "Enum was updated, but UI wasn't.");
_enumValues = enumValues.ToImmutableArray();
_previews = previews.ToImmutableArray();
var codeStyleOption = options.GetOption(option);
var enumIndex = _enumValues.IndexOf(codeStyleOption.Value);
_selectedPreference = Preferences[enumIndex];
var notificationViewModel = NotificationPreferences.Single(i => i.Notification.Value == codeStyleOption.Notification.Value);
_selectedNotificationPreference = NotificationPreferences.Single(p => p.Notification.Value == notificationViewModel.Notification.Value);
NotifyPropertyChanged(nameof(SelectedPreference));
NotifyPropertyChanged(nameof(SelectedNotificationPreference));
}
public override string GetPreview()
{
var index = Preferences.IndexOf(SelectedPreference);
return _previews[index];
}
public override CodeStylePreference SelectedPreference
{
get
{
return _selectedPreference;
}
get => _selectedPreference;
set
{
if (SetProperty(ref _selectedPreference, value))
{
var index = Preferences.IndexOf(value);
var enumValue = s_enumValues[index];
var enumValue = _enumValues[index];
Info.SetOptionAndUpdatePreview(
new CodeStyleOption<T>(
......@@ -53,20 +90,16 @@ public override CodeStylePreference SelectedPreference
}
}
private NotificationOptionViewModel _selectedNotificationPreference;
public override NotificationOptionViewModel SelectedNotificationPreference
{
get
{
return _selectedNotificationPreference;
}
get => _selectedNotificationPreference;
set
{
if (SetProperty(ref _selectedNotificationPreference, value))
{
var index = Preferences.IndexOf(SelectedPreference);
var enumValue = s_enumValues[index];
var enumValue = _enumValues[index];
Info.SetOptionAndUpdatePreview(
new CodeStyleOption<T>(
......@@ -75,38 +108,5 @@ public override NotificationOptionViewModel SelectedNotificationPreference
}
}
}
public override bool NotificationsAvailable => true;
public override string GetPreview()
{
var index = Preferences.IndexOf(SelectedPreference);
return Previews[index];
}
public EnumCodeStyleOptionViewModel(
Option<CodeStyleOption<T>> option,
string description,
string[] previews,
AbstractOptionPreviewViewModel info,
OptionSet options,
string groupName,
List<CodeStylePreference> preferences)
: base(option, description, previews, info, options, groupName, preferences)
{
Debug.Assert(preferences.Count == s_enumValues.Length);
Debug.Assert(previews.Length == s_enumValues.Length);
var codeStyleOption = options.GetOption(option);
var enumIndex = s_enumValues.IndexOf(codeStyleOption.Value);
_selectedPreference = Preferences[enumIndex];
var notificationViewModel = NotificationPreferences.Single(i => i.Notification.Value == codeStyleOption.Notification.Value);
_selectedNotificationPreference = NotificationPreferences.Single(p => p.Notification.Value == notificationViewModel.Notification.Value);
NotifyPropertyChanged(nameof(SelectedPreference));
NotifyPropertyChanged(nameof(SelectedNotificationPreference));
}
}
}
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册