StyleViewModel.cs 27.2 KB
Newer Older
S
Sam Harwell 已提交
1
// Copyright (c) Microsoft.  All Rights Reserved.  Licensed under the Apache License, Version 2.0.  See License.txt in the project root for license information.
2 3

using System;
4
using System.Collections.Generic;
5
using System.Windows.Data;
6
using Microsoft.CodeAnalysis;
7
using Microsoft.CodeAnalysis.CodeStyle;
8 9 10 11 12 13
using Microsoft.CodeAnalysis.CSharp.CodeStyle;
using Microsoft.CodeAnalysis.Options;
using Microsoft.VisualStudio.LanguageServices.Implementation.Options;

namespace Microsoft.VisualStudio.LanguageServices.CSharp.Options.Formatting
{
14 15 16 17 18 19
    /// <summary>
    /// This is the view model for CodeStyle options page.
    /// </summary>
    /// <remarks>
    /// The codestyle options page is defined in <see cref="CodeStylePage"/>
    /// </remarks>
20 21
    internal class StyleViewModel : AbstractOptionPreviewViewModel
    {
B
Balaji Krishnan 已提交
22 23
        #region "Preview Text"

24
        private const string s_fieldDeclarationPreviewTrue = @"
25
class C{
B
Balaji Krishnan 已提交
26
    int capacity;
B
Balaji Krishnan 已提交
27
    void Method()
28 29
    {
//[
B
Balaji Krishnan 已提交
30
        this.capacity = 0;
31 32 33 34
//]
    }
}";

35
        private const string s_fieldDeclarationPreviewFalse = @"
36
class C{
B
Balaji Krishnan 已提交
37
    int capacity;
B
Balaji Krishnan 已提交
38
    void Method()
39 40
    {
//[
B
Balaji Krishnan 已提交
41 42 43 44 45
        capacity = 0;
//]
    }
}";

46
        private const string s_propertyDeclarationPreviewTrue = @"
B
Balaji Krishnan 已提交
47 48
class C{
    public int Id { get; set; }
B
Balaji Krishnan 已提交
49
    void Method()
B
Balaji Krishnan 已提交
50 51 52 53 54 55 56
    {
//[
        this.Id = 0;
//]
    }
}";

57
        private const string s_propertyDeclarationPreviewFalse = @"
B
Balaji Krishnan 已提交
58 59
class C{
    public int Id { get; set; }
B
Balaji Krishnan 已提交
60
    void Method()
B
Balaji Krishnan 已提交
61 62 63 64 65 66 67
    {
//[
        Id = 0;
//]
    }
}";

68
        private const string s_eventDeclarationPreviewTrue = @"
B
Balaji Krishnan 已提交
69 70
using System;
class C{
B
Balaji Krishnan 已提交
71
    event EventHandler Elapsed;
B
Balaji Krishnan 已提交
72 73 74
    void Handler(object sender, EventArgs args)
    {
//[
B
Balaji Krishnan 已提交
75
        this.Elapsed += Handler;
B
Balaji Krishnan 已提交
76 77 78 79
//]
    }
}";

80
        private const string s_eventDeclarationPreviewFalse = @"
B
Balaji Krishnan 已提交
81 82
using System;
class C{
B
Balaji Krishnan 已提交
83
    event EventHandler Elapsed;
B
Balaji Krishnan 已提交
84 85 86
    void Handler(object sender, EventArgs args)
    {
//[
B
Balaji Krishnan 已提交
87
        Elapsed += Handler;
B
Balaji Krishnan 已提交
88 89 90 91
//]
    }
}";

92
        private const string s_methodDeclarationPreviewTrue = @"
B
Balaji Krishnan 已提交
93 94 95 96 97 98 99 100 101 102
using System;
class C{
    void Display()
    {
//[
        this.Display();
//]
    }
}";

103
        private const string s_methodDeclarationPreviewFalse = @"
B
Balaji Krishnan 已提交
104 105 106 107 108 109
using System;
class C{
    void Display()
    {
//[
        Display();
110 111 112 113
//]
    }
}";

114
        private const string s_intrinsicPreviewDeclarationTrue = @"
115 116 117 118 119 120 121 122 123 124 125
class Program
{
//[
    private int _member;
    static void M(int argument)
    {
        int local;
    }
//]
}";

126
        private const string s_intrinsicPreviewDeclarationFalse = @"
127 128 129 130 131 132 133 134 135 136 137 138
using System;
class Program
{
//[
    private Int32 _member;
    static void M(Int32 argument)
    {
        Int32 local;
    }
//]
}";

139
        private const string s_intrinsicPreviewMemberAccessTrue = @"
140 141 142 143 144 145 146 147 148 149
class Program
{
//[
    static void M()
    {
        var local = int.MaxValue;
    }
//]
}";

150
        private const string s_intrinsicPreviewMemberAccessFalse = @"
151 152 153 154 155 156 157 158 159 160 161
using System;
class Program
{
//[
    static void M()
    {
        var local = Int32.MaxValue;
    }
//]
}";

162
        private static readonly string s_varForIntrinsicsPreviewFalse = $@"
B
Balaji Krishnan 已提交
163
using System;
164
class C{{
B
Balaji Krishnan 已提交
165
    void Method()
166
    {{
167
//[
168
        int x = 5; // {ServicesVSResources.built_in_types}
169
//]
170 171
    }}
}}";
172

173
        private static readonly string s_varForIntrinsicsPreviewTrue = $@"
B
Balaji Krishnan 已提交
174
using System;
175
class C{{
B
Balaji Krishnan 已提交
176
    void Method()
177
    {{
178
//[
179
        var x = 5; // {ServicesVSResources.built_in_types}
180
//]
181 182
    }}
}}";
183

184
        private static readonly string s_varWhereApparentPreviewFalse = $@"
B
Balaji Krishnan 已提交
185
using System;
186
class C{{
B
Balaji Krishnan 已提交
187
    void Method()
188
    {{
189
//[
190
        C cobj = new C(); // {ServicesVSResources.type_is_apparent_from_assignment_expression}
191
//]
192 193
    }}
}}";
194

195
        private static readonly string s_varWhereApparentPreviewTrue = $@"
B
Balaji Krishnan 已提交
196
using System;
197
class C{{
B
Balaji Krishnan 已提交
198
    void Method()
199
    {{
200
//[
201
        var cobj = new C(); // {ServicesVSResources.type_is_apparent_from_assignment_expression}
B
Balaji Krishnan 已提交
202
//]
203 204
    }}
}}";
B
Balaji Krishnan 已提交
205

206
        private static readonly string s_varWherePossiblePreviewFalse = $@"
B
Balaji Krishnan 已提交
207
using System;
208
class C{{
B
Balaji Krishnan 已提交
209
    void Init()
210
    {{
B
Balaji Krishnan 已提交
211
//[
212
        Action f = this.Init(); // {ServicesVSResources.everywhere_else}
B
Balaji Krishnan 已提交
213
//]
214 215
    }}
}}";
B
Balaji Krishnan 已提交
216

217
        private static readonly string s_varWherePossiblePreviewTrue = $@"
B
Balaji Krishnan 已提交
218
using System;
219
class C{{
B
Balaji Krishnan 已提交
220
    void Init()
221
    {{
B
Balaji Krishnan 已提交
222
//[
223
        var f = this.Init(); // {ServicesVSResources.everywhere_else}
224
//]
225 226
    }}
}}";
227

228
        private static readonly string s_preferThrowExpression = $@"
229 230 231
using System;

class C
232
{{
233 234 235
    private string s;

    public C(string s)
236
    {{
237
//[
238
        // {ServicesVSResources.Prefer_colon}
239 240
        this.s = s ?? throw new ArgumentNullException(nameof(s));

241
        // {ServicesVSResources.Over_colon}
242
        if (s == null)
243
        {{
244
            throw new ArgumentNullException(nameof(s));
245
        }}
246 247 248

        this.s = s;
//]
249 250
    }}
}}
251 252
";

253
        private static readonly string s_preferCoalesceExpression = $@"
254 255 256
using System;

class C
257
{{
258 259 260
    private string s;

    public C(string s)
261
    {{
262
//[
263
        // {ServicesVSResources.Prefer_colon}
264 265
        var v = x ?? y;

266 267
        // {ServicesVSResources.Over_colon}
        var v = x != null ? x : y; // {ServicesVSResources.or}
268 269
        var v = x == null ? y : x;
//]
270 271
    }}
}}
272 273
";

274
        private static readonly string s_preferConditionalDelegateCall = $@"
275 276 277
using System;

class C
278
{{
279 280 281
    private string s;

    public C(string s)
282
    {{
283
//[
284
        // {ServicesVSResources.Prefer_colon}
285 286
        func?.Invoke(args);

287
        // {ServicesVSResources.Over_colon}
288
        if (func != null)
289
        {{
290
            func(args);
291
        }}
292
//]
293 294
    }}
}}
C
CyrusNajmabadi 已提交
295 296
";

C
Cyrus Najmabadi 已提交
297
        private static readonly string s_preferNullPropagation = $@"
C
CyrusNajmabadi 已提交
298 299 300
using System;

class C
301
{{
C
CyrusNajmabadi 已提交
302
    public C(object o)
303
    {{
C
CyrusNajmabadi 已提交
304
//[
305
        // {ServicesVSResources.Prefer_colon}
C
CyrusNajmabadi 已提交
306 307
        var v = o?.ToString();

308 309
        // {ServicesVSResources.Over_colon}
        var v = o == null ? null : o.ToString(); // {ServicesVSResources.or}
C
CyrusNajmabadi 已提交
310 311
        var v = o != null ? o.ToString() : null;
//]
312 313
    }}
}}
314 315
";

316
        private static readonly string s_preferPatternMatchingOverAsWithNullCheck = $@"
317
class C
318
{{
319
    void M()
320
    {{
321
//[
322
        // {ServicesVSResources.Prefer_colon}
323
        if (o is string s)
324 325
        {{
        }}
326

327
        // {ServicesVSResources.Over_colon}
328 329
        var s = o as string;
        if (s != null)
330 331
        {{
        }}
332
//]
333 334
    }}
}}
335 336
";

337
        private static readonly string s_preferPatternMatchingOverIsWithCastCheck = $@"
338
class C
339
{{
340
    void M()
341
    {{
342
//[
343
        // {ServicesVSResources.Prefer_colon}
344
        if (o is int i)
345 346
        {{
        }}
347

348
        // {ServicesVSResources.Over_colon}
349
        if (o is int)
350
        {{
351
            var i = (int)o;
352
        }}
353
//]
354 355
    }}
}}
C
CyrusNajmabadi 已提交
356 357
";

358
        private static readonly string s_preferObjectInitializer = $@"
C
CyrusNajmabadi 已提交
359 360 361
using System;

class Customer
362
{{
C
CyrusNajmabadi 已提交
363 364 365
    private int Age;

    public Customer()
366
    {{
C
CyrusNajmabadi 已提交
367
//[
368
        // {ServicesVSResources.Prefer_colon}
C
CyrusNajmabadi 已提交
369
        var c = new Customer()
370
        {{
C
CyrusNajmabadi 已提交
371
            Age = 21
372
        }};
C
CyrusNajmabadi 已提交
373

374
        // {ServicesVSResources.Over_colon}
C
CyrusNajmabadi 已提交
375 376 377
        var c = new Customer();
        c.Age = 21;
//]
378 379
    }}
}}
380 381
";

382
        private static readonly string s_preferCollectionInitializer = $@"
383 384 385
using System.Collections.Generic;

class Customer
386
{{
387 388 389
    private int Age;

    public Customer()
390
    {{
391
//[
392
        // {ServicesVSResources.Prefer_colon}
393
        var list = new List<int>
394
        {{
395 396 397
            1,
            2,
            3
398
        }};
399

C
CyrusNajmabadi 已提交
400
        // {ServicesVSResources.Over_colon}
401 402 403 404 405
        var list = new List<int>();
        list.Add(1);
        list.Add(2);
        list.Add(3);
//]
406 407
    }}
}}
C
CyrusNajmabadi 已提交
408 409
";

410
        private static readonly string s_preferExplicitTupleName = $@"
C
CyrusNajmabadi 已提交
411
class Customer
412
{{
C
CyrusNajmabadi 已提交
413
    public Customer()
414
    {{
C
CyrusNajmabadi 已提交
415
//[
416
        // {ServicesVSResources.Prefer_colon}
C
CyrusNajmabadi 已提交
417 418 419 420
        (string name, int age) customer = GetCustomer();
        var name = customer.name;
        var age = customer.age;

421
        // {ServicesVSResources.Over_colon}
C
CyrusNajmabadi 已提交
422 423 424 425
        (string name, int age) customer = GetCustomer();
        var name = customer.Item1;
        var age = customer.Item2;
//]
426 427
    }}
}}
428 429
";

430
        private static readonly string s_preferSimpleDefaultExpression = $@"
431 432 433 434 435 436 437 438 439 440 441 442
using System.Threading;

class Customer
{{
//[
    // {ServicesVSResources.Prefer_colon}
    void DoWork(CancellationToken cancellationToken = default) {{ }}

    // {ServicesVSResources.Over_colon}
    void DoWork(CancellationToken cancellationToken = default(CancellationToken)) {{ }}
//]
}}
443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478
";

        private static readonly string s_preferInferredTupleName = $@"
using System.Threading;

class Customer
{{
    public Customer(int age, string name)
    {{
//[
        // {ServicesVSResources.Prefer_colon}
        var tuple = (age, name);

        // {ServicesVSResources.Over_colon}
        var tuple = (age: age, name: name);
//]
    }}
}}
";

        private static readonly string s_preferInferredAnonymousTypeMemberName = $@"
using System.Threading;

class Customer
{{
    public Customer(int age, string name)
    {{
//[
        // {ServicesVSResources.Prefer_colon}
        var anon = new {{ age, name }};

        // {ServicesVSResources.Over_colon}
        var anon = new {{ age = age, name = name }};
//]
    }}
}}
479
";
480

481
        private static readonly string s_preferInlinedVariableDeclaration = $@"
482 483 484
using System;

class Customer
485
{{
486
    public Customer(string value)
487
    {{
488
//[
489
        // {ServicesVSResources.Prefer_colon}
490
        if (int.TryParse(value, out int i))
491 492
        {{
        }}
493

494
        // {ServicesVSResources.Over_colon}
495 496
        int i;
        if (int.TryParse(value, out i))
497 498
        {{
        }}
499
//]
500 501
    }}
}}
C
Cyrus Najmabadi 已提交
502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527
";

        private static readonly string s_preferDeconstructedVariableDeclaration = $@"
using System;

class Customer
{{
    public Customer(string value)
    {{
//[
        // {ServicesVSResources.Prefer_colon}
        var (name, age) = GetPersonTuple();
        Console.WriteLine($""{{name}} {{age}}"");

        (int x, int y) = GetPointTuple();
        Console.WriteLine($""{{x}} {{y}}"");

        // {ServicesVSResources.Over_colon}
        var person = GetPersonTuple();
        Console.WriteLine($""{{person.name}} {{person.age}}"");

        (int x, int y) point = GetPointTuple();
        Console.WriteLine($""{{point.x}} {{point.y}}"");
//]
    }}
}}
528 529
";

530
        private static readonly string s_preferBraces = $@"
531 532 533
using System;

class Customer
534
{{
535 536 537
    private int Age;

    public int GetAge()
538
    {{
539
//[
540
        // {ServicesVSResources.Prefer_colon}
541
        if (test)
542
        {{
543
            this.Display();
544
        }}
545
        
546
        // {ServicesVSResources.Over_colon}
547 548 549
        if (test)
            this.Display();
//]
550 551
    }}
}}
552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574
";

        private static readonly string s_preferAutoProperties = $@"
using System;

class Customer
{{
//[
    // {ServicesVSResources.Prefer_colon}
    public int Age {{ get; }}

    // {ServicesVSResources.Over_colon}
    private int age;

    public int Age
    {{
        get
        {{
            return age;
        }}
    }}
//]
}}
C
CyrusNajmabadi 已提交
575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599
";

        private static readonly string s_preferLocalFunctionOverAnonymousFunction = $@"
using System;

class Customer
{{
    public Customer(string value)
    {{
//[
        // {ServicesVSResources.Prefer_colon}
        int fibonacci(int n)
        {{
            return n <= 1 ? 1 : fibonacci(n - 1) + fibonacci(n - 2);
        }}

        // {ServicesVSResources.Over_colon}
        Func<int, int> fibonacci = null;
        fibonacci = (int n) =>
        {{
            return n <= 1 ? 1 : fibonacci(n - 1) + fibonacci(n - 2);
        }};
//]
    }}
}}
600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619
";

        private static readonly string s_preferIsNullOverReferenceEquals = $@"
using System;

class Customer
{{
    public Customer(string value)
    {{
//[
        // {ServicesVSResources.Prefer_colon}
        if (value is null)
            return;

        // {ServicesVSResources.Over_colon}
        if (object.ReferenceEquals(value, null))
            return;
//]
    }}
}}
620 621
";

622
        private const string s_preferExpressionBodyForMethods = @"
623 624 625 626 627 628 629 630 631 632 633 634
using System;

//[
class Customer
{
    private int Age;

    public int GetAge() => this.Age;
}
//]
";

635
        private const string s_preferBlockBodyForMethods = @"
636 637 638 639 640 641 642 643 644 645 646 647 648 649 650
using System;

//[
class Customer
{
    private int Age;

    public int GetAge()
    {
        return this.Age;
    }
}
//]
";

651
        private const string s_preferExpressionBodyForConstructors = @"
652 653 654 655 656 657 658 659 660 661 662 663
using System;

//[
class Customer
{
    private int Age;

    public Customer(int age) => Age = age;
}
//]
";

664
        private const string s_preferBlockBodyForConstructors = @"
665 666 667 668 669 670 671 672 673 674 675 676 677 678 679
using System;

//[
class Customer
{
    private int Age;

    public Customer(int age)
    {
        Age = age;
    }
}
//]
";

680
        private const string s_preferExpressionBodyForOperators = @"
681 682 683 684 685 686 687 688 689 690 691
using System;

struct ComplexNumber
{
//[
    public static ComplexNumber operator +(ComplexNumber c1, ComplexNumber c2)
        => new ComplexNumber(c1.Real + c2.Real, c1.Imaginary + c2.Imaginary);
//]
}
";

692
        private const string s_preferBlockBodyForOperators = @"
693 694 695 696 697 698 699 700 701 702 703 704 705
using System;

struct ComplexNumber
{
//[
    public static ComplexNumber operator +(ComplexNumber c1, ComplexNumber c2)
    {
        return new ComplexNumber(c1.Real + c2.Real, c1.Imaginary + c2.Imaginary);
    }
//]
}
";

706
        private const string s_preferExpressionBodyForProperties = @"
707 708 709 710 711 712 713 714 715 716 717
using System;

//[
class Customer
{
    private int _age;
    public int Age => _age;
}
//]
";

718
        private const string s_preferBlockBodyForProperties = @"
719 720 721 722 723 724 725 726 727 728 729
using System;

//[
class Customer
{
    private int _age;
    public int Age { get { return _age; } }
}
//]
";

730
        private const string s_preferExpressionBodyForAccessors = @"
731 732 733 734 735 736 737 738 739 740 741 742 743 744 745
using System;

//[
class Customer
{
    private int _age;
    public int Age
    {
        get => _age;
        set => _age = value;
    }
}
//]
";

746
        private const string s_preferBlockBodyForAccessors = @"
747 748 749 750 751 752 753 754 755 756 757 758 759 760 761
using System;

//[
class Customer
{
    private int _age;
    public int Age
    {
        get { return _age; }
        set { _age = value; }
    }
}
//]
";

762
        private const string s_preferExpressionBodyForIndexers = @"
763 764 765 766 767 768 769 770 771 772 773
using System;

//[
class List<T>
{
    private T[] _values;
    public T this[int i] => _values[i];
}
//]
";

774
        private const string s_preferBlockBodyForIndexers = @"
775 776 777 778 779 780 781 782 783
using System;

//[
class List<T>
{
    private T[] _values;
    public T this[int i] { get { return _values[i]; } }
}
//]
784 785
";

B
Balaji Krishnan 已提交
786 787
        #endregion

788 789
        internal StyleViewModel(OptionSet optionSet, IServiceProvider serviceProvider) : base(optionSet, serviceProvider, LanguageNames.CSharp)
        {
B
Balaji Krishnan 已提交
790
            var collectionView = (ListCollectionView)CollectionViewSource.GetDefaultView(CodeStyleItems);
B
Balaji Krishnan 已提交
791
            collectionView.GroupDescriptions.Add(new PropertyGroupDescription(nameof(AbstractCodeStyleOptionViewModel.GroupName)));
792

793 794 795
            var qualifyGroupTitle = CSharpVSResources.this_preferences_colon;
            var predefinedTypesGroupTitle = CSharpVSResources.predefined_type_preferences_colon;
            var varGroupTitle = CSharpVSResources.var_preferences_colon;
C
CyrusNajmabadi 已提交
796
            var nullCheckingGroupTitle = CSharpVSResources.null_checking_colon;
797
            var codeBlockPreferencesGroupTitle = ServicesVSResources.Code_block_preferences_colon;
C
CyrusNajmabadi 已提交
798
            var expressionPreferencesGroupTitle = ServicesVSResources.Expression_preferences_colon;
799
            var variablePreferencesGroupTitle = ServicesVSResources.Variable_preferences_colon;
800

B
Balaji Krishnan 已提交
801 802
            var qualifyMemberAccessPreferences = new List<CodeStylePreference>
            {
803 804
                new CodeStylePreference(CSharpVSResources.Prefer_this, isChecked: true),
                new CodeStylePreference(CSharpVSResources.Do_not_prefer_this, isChecked: false),
B
Balaji Krishnan 已提交
805 806 807 808
            };

            var predefinedTypesPreferences = new List<CodeStylePreference>
            {
809 810
                new CodeStylePreference(ServicesVSResources.Prefer_predefined_type, isChecked: true),
                new CodeStylePreference(ServicesVSResources.Prefer_framework_type, isChecked: false),
B
Balaji Krishnan 已提交
811
            };
812

813
            var typeStylePreferences = new List<CodeStylePreference>
814
            {
815 816
                new CodeStylePreference(CSharpVSResources.Prefer_var, isChecked: true),
                new CodeStylePreference(CSharpVSResources.Prefer_explicit_type, isChecked: false),
817
            };
B
Balaji Krishnan 已提交
818

819 820 821 822
            CodeStyleItems.Add(new BooleanCodeStyleOptionViewModel(CodeStyleOptions.QualifyFieldAccess, CSharpVSResources.Qualify_field_access_with_this, s_fieldDeclarationPreviewTrue, s_fieldDeclarationPreviewFalse, this, optionSet, qualifyGroupTitle, qualifyMemberAccessPreferences));
            CodeStyleItems.Add(new BooleanCodeStyleOptionViewModel(CodeStyleOptions.QualifyPropertyAccess, CSharpVSResources.Qualify_property_access_with_this, s_propertyDeclarationPreviewTrue, s_propertyDeclarationPreviewFalse, this, optionSet, qualifyGroupTitle, qualifyMemberAccessPreferences));
            CodeStyleItems.Add(new BooleanCodeStyleOptionViewModel(CodeStyleOptions.QualifyMethodAccess, CSharpVSResources.Qualify_method_access_with_this, s_methodDeclarationPreviewTrue, s_methodDeclarationPreviewFalse, this, optionSet, qualifyGroupTitle, qualifyMemberAccessPreferences));
            CodeStyleItems.Add(new BooleanCodeStyleOptionViewModel(CodeStyleOptions.QualifyEventAccess, CSharpVSResources.Qualify_event_access_with_this, s_eventDeclarationPreviewTrue, s_eventDeclarationPreviewFalse, this, optionSet, qualifyGroupTitle, qualifyMemberAccessPreferences));
B
Balaji Krishnan 已提交
823

824 825
            CodeStyleItems.Add(new BooleanCodeStyleOptionViewModel(CodeStyleOptions.PreferIntrinsicPredefinedTypeKeywordInDeclaration, ServicesVSResources.For_locals_parameters_and_members, s_intrinsicPreviewDeclarationTrue, s_intrinsicPreviewDeclarationFalse, this, optionSet, predefinedTypesGroupTitle, predefinedTypesPreferences));
            CodeStyleItems.Add(new BooleanCodeStyleOptionViewModel(CodeStyleOptions.PreferIntrinsicPredefinedTypeKeywordInMemberAccess, ServicesVSResources.For_member_access_expressions, s_intrinsicPreviewMemberAccessTrue, s_intrinsicPreviewMemberAccessFalse, this, optionSet, predefinedTypesGroupTitle, predefinedTypesPreferences));
826

827
            // Use var
828 829 830
            CodeStyleItems.Add(new BooleanCodeStyleOptionViewModel(CSharpCodeStyleOptions.UseImplicitTypeForIntrinsicTypes, CSharpVSResources.For_built_in_types, s_varForIntrinsicsPreviewTrue, s_varForIntrinsicsPreviewFalse, this, optionSet, varGroupTitle, typeStylePreferences));
            CodeStyleItems.Add(new BooleanCodeStyleOptionViewModel(CSharpCodeStyleOptions.UseImplicitTypeWhereApparent, CSharpVSResources.When_variable_type_is_apparent, s_varWhereApparentPreviewTrue, s_varWhereApparentPreviewFalse, this, optionSet, varGroupTitle, typeStylePreferences));
            CodeStyleItems.Add(new BooleanCodeStyleOptionViewModel(CSharpCodeStyleOptions.UseImplicitTypeWherePossible, CSharpVSResources.Elsewhere, s_varWherePossiblePreviewTrue, s_varWherePossiblePreviewFalse, this, optionSet, varGroupTitle, typeStylePreferences));
831

832
            // Code block
833
            CodeStyleItems.Add(new BooleanCodeStyleOptionViewModel(CSharpCodeStyleOptions.PreferBraces, ServicesVSResources.Prefer_braces, s_preferBraces, s_preferBraces, this, optionSet, codeBlockPreferencesGroupTitle));
C
Cyrus Najmabadi 已提交
834
            CodeStyleItems.Add(new BooleanCodeStyleOptionViewModel(CodeStyleOptions.PreferAutoProperties, ServicesVSResources.analyzer_Prefer_auto_properties, s_preferAutoProperties, s_preferAutoProperties, this, optionSet, codeBlockPreferencesGroupTitle));
835

836
            // Expression preferences
837 838 839 840 841
            CodeStyleItems.Add(new BooleanCodeStyleOptionViewModel(CodeStyleOptions.PreferObjectInitializer, ServicesVSResources.Prefer_object_initializer, s_preferObjectInitializer, s_preferObjectInitializer, this, optionSet, expressionPreferencesGroupTitle));
            CodeStyleItems.Add(new BooleanCodeStyleOptionViewModel(CodeStyleOptions.PreferCollectionInitializer, ServicesVSResources.Prefer_collection_initializer, s_preferCollectionInitializer, s_preferCollectionInitializer, this, optionSet, expressionPreferencesGroupTitle));
            CodeStyleItems.Add(new BooleanCodeStyleOptionViewModel(CSharpCodeStyleOptions.PreferPatternMatchingOverIsWithCastCheck, CSharpVSResources.Prefer_pattern_matching_over_is_with_cast_check, s_preferPatternMatchingOverIsWithCastCheck, s_preferPatternMatchingOverIsWithCastCheck, this, optionSet, expressionPreferencesGroupTitle));
            CodeStyleItems.Add(new BooleanCodeStyleOptionViewModel(CSharpCodeStyleOptions.PreferPatternMatchingOverAsWithNullCheck, CSharpVSResources.Prefer_pattern_matching_over_as_with_null_check, s_preferPatternMatchingOverAsWithNullCheck, s_preferPatternMatchingOverAsWithNullCheck, this, optionSet, expressionPreferencesGroupTitle));
            CodeStyleItems.Add(new BooleanCodeStyleOptionViewModel(CodeStyleOptions.PreferExplicitTupleNames, ServicesVSResources.Prefer_explicit_tuple_name, s_preferExplicitTupleName, s_preferExplicitTupleName, this, optionSet, expressionPreferencesGroupTitle));
842
            CodeStyleItems.Add(new BooleanCodeStyleOptionViewModel(CSharpCodeStyleOptions.PreferSimpleDefaultExpression, ServicesVSResources.Prefer_simple_default_expression, s_preferSimpleDefaultExpression, s_preferSimpleDefaultExpression, this, optionSet, expressionPreferencesGroupTitle));
843 844
            CodeStyleItems.Add(new BooleanCodeStyleOptionViewModel(CSharpCodeStyleOptions.PreferInferredTupleNames, ServicesVSResources.Prefer_inferred_tuple_names, s_preferInferredTupleName, s_preferInferredTupleName, this, optionSet, expressionPreferencesGroupTitle));
            CodeStyleItems.Add(new BooleanCodeStyleOptionViewModel(CSharpCodeStyleOptions.PreferInferredAnonymousTypeMemberNames, ServicesVSResources.Prefer_inferred_anonymous_type_member_names, s_preferInferredAnonymousTypeMemberName, s_preferInferredAnonymousTypeMemberName, this, optionSet, expressionPreferencesGroupTitle));
C
CyrusNajmabadi 已提交
845
            CodeStyleItems.Add(new BooleanCodeStyleOptionViewModel(CSharpCodeStyleOptions.PreferLocalOverAnonymousFunction, ServicesVSResources.Prefer_local_function_over_anonymous_function, s_preferLocalFunctionOverAnonymousFunction, s_preferLocalFunctionOverAnonymousFunction, this, optionSet, expressionPreferencesGroupTitle));
846

C
CyrusNajmabadi 已提交
847
            AddExpressionBodyOptions(optionSet, expressionPreferencesGroupTitle);
848

849
            // Variable preferences
850
            CodeStyleItems.Add(new BooleanCodeStyleOptionViewModel(CodeStyleOptions.PreferInlinedVariableDeclaration, ServicesVSResources.Prefer_inlined_variable_declaration, s_preferInlinedVariableDeclaration, s_preferInlinedVariableDeclaration, this, optionSet, variablePreferencesGroupTitle));
C
Cyrus Najmabadi 已提交
851
            CodeStyleItems.Add(new BooleanCodeStyleOptionViewModel(CodeStyleOptions.PreferDeconstructedVariableDeclaration, ServicesVSResources.Prefer_deconstructed_variable_declaration, s_preferDeconstructedVariableDeclaration, s_preferDeconstructedVariableDeclaration, this, optionSet, variablePreferencesGroupTitle));
852

853
            // Null preferences.
854 855 856 857
            CodeStyleItems.Add(new BooleanCodeStyleOptionViewModel(CodeStyleOptions.PreferThrowExpression, CSharpVSResources.Prefer_throw_expression, s_preferThrowExpression, s_preferThrowExpression, this, optionSet, nullCheckingGroupTitle));
            CodeStyleItems.Add(new BooleanCodeStyleOptionViewModel(CSharpCodeStyleOptions.PreferConditionalDelegateCall, CSharpVSResources.Prefer_conditional_delegate_call, s_preferConditionalDelegateCall, s_preferConditionalDelegateCall, this, optionSet, nullCheckingGroupTitle));
            CodeStyleItems.Add(new BooleanCodeStyleOptionViewModel(CodeStyleOptions.PreferCoalesceExpression, ServicesVSResources.Prefer_coalesce_expression, s_preferCoalesceExpression, s_preferCoalesceExpression, this, optionSet, nullCheckingGroupTitle));
            CodeStyleItems.Add(new BooleanCodeStyleOptionViewModel(CodeStyleOptions.PreferNullPropagation, ServicesVSResources.Prefer_null_propagation, s_preferNullPropagation, s_preferNullPropagation, this, optionSet, nullCheckingGroupTitle));
858
            CodeStyleItems.Add(new BooleanCodeStyleOptionViewModel(CodeStyleOptions.PreferIsNullCheckOverReferenceEqualityMethod, CSharpVSResources.Prefer_is_null_over_ReferenceEquals, s_preferIsNullOverReferenceEquals, s_preferIsNullOverReferenceEquals, this, optionSet, nullCheckingGroupTitle));
859
        }
860

C
CyrusNajmabadi 已提交
861
        private void AddExpressionBodyOptions(OptionSet optionSet, string expressionPreferencesGroupTitle)
862
        {
C
CyrusNajmabadi 已提交
863 864 865 866 867 868 869
            var expressionBodyPreferences = new List<CodeStylePreference>
            {
                new CodeStylePreference(CSharpVSResources.Never, isChecked: false),
                new CodeStylePreference(CSharpVSResources.When_possible, isChecked: false),
                new CodeStylePreference(CSharpVSResources.When_on_single_line, isChecked: false),
            };

870 871
            var enumValues = new[] { ExpressionBodyPreference.Never, ExpressionBodyPreference.WhenPossible, ExpressionBodyPreference.WhenOnSingleLine };

872 873 874
            CodeStyleItems.Add(new EnumCodeStyleOptionViewModel<ExpressionBodyPreference>(
                CSharpCodeStyleOptions.PreferExpressionBodiedMethods,
                ServicesVSResources.Use_expression_body_for_methods,
875
                enumValues,
876 877 878 879 880 881
                new[] { s_preferBlockBodyForMethods, s_preferExpressionBodyForMethods, s_preferExpressionBodyForMethods },
                this, optionSet, expressionPreferencesGroupTitle, expressionBodyPreferences));

            CodeStyleItems.Add(new EnumCodeStyleOptionViewModel<ExpressionBodyPreference>(
                CSharpCodeStyleOptions.PreferExpressionBodiedConstructors,
                ServicesVSResources.Use_expression_body_for_constructors,
882
                enumValues,
883 884 885 886 887 888
                new[] { s_preferBlockBodyForConstructors, s_preferExpressionBodyForConstructors, s_preferExpressionBodyForConstructors },
                this, optionSet, expressionPreferencesGroupTitle, expressionBodyPreferences));

            CodeStyleItems.Add(new EnumCodeStyleOptionViewModel<ExpressionBodyPreference>(
                CSharpCodeStyleOptions.PreferExpressionBodiedOperators,
                ServicesVSResources.Use_expression_body_for_operators,
889
                enumValues,
890 891 892 893 894 895
                new[] { s_preferBlockBodyForOperators, s_preferExpressionBodyForOperators, s_preferExpressionBodyForOperators },
                this, optionSet, expressionPreferencesGroupTitle, expressionBodyPreferences));

            CodeStyleItems.Add(new EnumCodeStyleOptionViewModel<ExpressionBodyPreference>(
                CSharpCodeStyleOptions.PreferExpressionBodiedProperties,
                ServicesVSResources.Use_expression_body_for_properties,
896
                enumValues,
897 898 899 900 901 902
                new[] { s_preferBlockBodyForProperties, s_preferExpressionBodyForProperties, s_preferExpressionBodyForProperties },
                this, optionSet, expressionPreferencesGroupTitle, expressionBodyPreferences));

            CodeStyleItems.Add(new EnumCodeStyleOptionViewModel<ExpressionBodyPreference>(
                CSharpCodeStyleOptions.PreferExpressionBodiedIndexers,
                ServicesVSResources.Use_expression_body_for_indexers,
903
                enumValues,
904 905 906 907 908
                new[] { s_preferBlockBodyForIndexers, s_preferExpressionBodyForIndexers, s_preferExpressionBodyForIndexers },
                this, optionSet, expressionPreferencesGroupTitle, expressionBodyPreferences));

            CodeStyleItems.Add(new EnumCodeStyleOptionViewModel<ExpressionBodyPreference>(
                CSharpCodeStyleOptions.PreferExpressionBodiedAccessors,
C
CyrusNajmabadi 已提交
909
                ServicesVSResources.Use_expression_body_for_accessors,
910
                enumValues,
911 912 913
                new[] { s_preferBlockBodyForAccessors, s_preferExpressionBodyForAccessors, s_preferExpressionBodyForAccessors },
                this, optionSet, expressionPreferencesGroupTitle, expressionBodyPreferences));
        }
914
    }
S
Sam Harwell 已提交
915
}