Просмотр исходного кода

feat: Enable Rule CA1851 (#16779)

* feat: Enable rule CA1851: Possible multiple enumerations of IEnumerable collection

* feat: Address rule CA1851

* fix: Address review

* fix: address review
workgroupengineering 1 год назад
Родитель
Сommit
36c9de9cf0

+ 2 - 0
.editorconfig

@@ -177,6 +177,8 @@ dotnet_diagnostic.CA1828.severity = warning
 dotnet_diagnostic.CA1829.severity = warning
 #CA1847: Use string.Contains(char) instead of string.Contains(string) with single characters
 dotnet_diagnostic.CA1847.severity = warning
+# CA1851: Possible multiple enumerations of IEnumerable collection
+dotnet_diagnostic.CA1851.severity = warning
 #CA1854: Prefer the IDictionary.TryGetValue(TKey, out TValue) method
 dotnet_diagnostic.CA1854.severity = warning
 #CA2211:Non-constant fields should not be visible

+ 1 - 1
src/Avalonia.Controls/NumericUpDown/NumericUpDown.cs

@@ -1137,7 +1137,7 @@ namespace Avalonia.Controls
                     {
                         // extract non-digit characters
                         var currentValueTextSpecialCharacters = currentValueText.Where(c => !char.IsDigit(c));
-                        var textSpecialCharacters = text.Where(c => !char.IsDigit(c));
+                        var textSpecialCharacters = text.Where(c => !char.IsDigit(c)).ToArray();
                         // same non-digit characters on currentValueText and new text => remove them on new Text to parse it again.
                         if (!currentValueTextSpecialCharacters.Except(textSpecialCharacters).Any())
                         {

+ 10 - 12
src/Avalonia.Controls/Primitives/TextSelectionCanvas.cs

@@ -1,10 +1,9 @@
 using System;
+using System.Collections.Generic;
 using System.Linq;
 using Avalonia.Controls.Presenters;
 using Avalonia.Input;
-using Avalonia.Interactivity;
 using Avalonia.Layout;
-using Avalonia.Media;
 using Avalonia.VisualTree;
 
 namespace Avalonia.Controls.Primitives
@@ -161,7 +160,7 @@ namespace Avalonia.Controls.Primitives
                 {
                     if (position >= _textBox.SelectionEnd)
                         position = _textBox.SelectionEnd - 1;
-                        _textBox.SelectionStart = position;
+                    _textBox.SelectionStart = position;
                 }
                 else
                 {
@@ -174,13 +173,12 @@ namespace Avalonia.Controls.Primitives
                 var selectionEnd = _textBox.SelectionEnd;
                 var start = Math.Min(selectionStart, selectionEnd);
                 var length = Math.Max(selectionStart, selectionEnd) - start;
+                var rects = new List<Rect>(_presenter.TextLayout.HitTestTextRange(start, length));
 
-                var rects = _presenter.TextLayout.HitTestTextRange(start, length);
-
-                if (rects.Any())
+                if (rects.Count > 0)
                 {
-                    var first = rects.First();
-                    var last = rects.Last();
+                    var first = rects[0];
+                    var last = rects[rects.Count -1];
 
                     if (handle.SelectionHandleType == SelectionHandleType.Start)
                         handle?.SetTopLeft(ToLayer(first.BottomLeft));
@@ -234,12 +232,12 @@ namespace Avalonia.Controls.Primitives
                 var start = Math.Min(selectionStart, selectionEnd);
                 var length = Math.Max(selectionStart, selectionEnd) - start;
 
-                var rects = _presenter.TextLayout.HitTestTextRange(start, length);
+                var rects = new List<Rect>(_presenter.TextLayout.HitTestTextRange(start, length));
 
-                if (rects.Any())
+                if (rects.Count > 0)
                 {
-                    var first = rects.First();
-                    var last = rects.Last();
+                    var first = rects[0];
+                    var last = rects[rects.Count - 1];
 
                     if (!_startHandle.IsDragging)
                     {

+ 34 - 12
src/Avalonia.Controls/TreeView.cs

@@ -11,7 +11,6 @@ using Avalonia.Collections;
 using Avalonia.Controls.Generators;
 using Avalonia.Controls.Primitives;
 using Avalonia.Input;
-using Avalonia.Input.Platform;
 using Avalonia.Interactivity;
 using Avalonia.Layout;
 using Avalonia.Threading;
@@ -966,20 +965,43 @@ namespace Avalonia.Controls
         /// </summary>
         /// <param name="items">The items collection.</param>
         /// <param name="desired">The desired items.</param>
-        private static void SynchronizeItems(IList items, IEnumerable<object> desired)
+        private static void SynchronizeItems(IList items, List<object> desired)
         {
-            var list = items.Cast<object>();
-            var toRemove = list.Except(desired).ToList();
-            var toAdd = desired.Except(list).ToList();
-
-            foreach (var i in toRemove)
+            var itemsCount = items.Count;
+            if (desired is not null)
             {
-                items.Remove(i);
-            }
+                var desiredCount = desired.Count;
+                if (itemsCount == 0 && desiredCount > 0)
+                {
+                    // Add all desired
+                    foreach (var item in desired)
+                    {
+                        items.Add(item);
+                    }
+                }
+                else if (itemsCount > 0 && desiredCount == 0)
+                {
+                    // Remove all
+                    items.Clear();
+                }
+                // Intersect
+                else
+                {
+                    var list = new object[items.Count];
+                    items.CopyTo(list, 0);
+                    var toRemove = list.Except(desired).ToArray();
+                    var toAdd = desired.Except(list).ToArray();
 
-            foreach (var i in toAdd)
-            {
-                items.Add(i);
+                    foreach (var i in toRemove)
+                    {
+                        items.Remove(i);
+                    }
+
+                    foreach (var i in toAdd)
+                    {
+                        items.Add(i);
+                    }
+                }
             }
         }
     }