Browse Source

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
pull/17171/head
workgroupengineering 2 years ago
committed by GitHub
parent
commit
36c9de9cf0
No known key found for this signature in database GPG Key ID: B5690EEEBB952194
  1. 2
      .editorconfig
  2. 2
      src/Avalonia.Controls/NumericUpDown/NumericUpDown.cs
  3. 22
      src/Avalonia.Controls/Primitives/TextSelectionCanvas.cs
  4. 46
      src/Avalonia.Controls/TreeView.cs

2
.editorconfig

@ -177,6 +177,8 @@ dotnet_diagnostic.CA1828.severity = warning
dotnet_diagnostic.CA1829.severity = warning dotnet_diagnostic.CA1829.severity = warning
#CA1847: Use string.Contains(char) instead of string.Contains(string) with single characters #CA1847: Use string.Contains(char) instead of string.Contains(string) with single characters
dotnet_diagnostic.CA1847.severity = warning 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 #CA1854: Prefer the IDictionary.TryGetValue(TKey, out TValue) method
dotnet_diagnostic.CA1854.severity = warning dotnet_diagnostic.CA1854.severity = warning
#CA2211:Non-constant fields should not be visible #CA2211:Non-constant fields should not be visible

2
src/Avalonia.Controls/NumericUpDown/NumericUpDown.cs

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

22
src/Avalonia.Controls/Primitives/TextSelectionCanvas.cs

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

46
src/Avalonia.Controls/TreeView.cs

@ -11,7 +11,6 @@ using Avalonia.Collections;
using Avalonia.Controls.Generators; using Avalonia.Controls.Generators;
using Avalonia.Controls.Primitives; using Avalonia.Controls.Primitives;
using Avalonia.Input; using Avalonia.Input;
using Avalonia.Input.Platform;
using Avalonia.Interactivity; using Avalonia.Interactivity;
using Avalonia.Layout; using Avalonia.Layout;
using Avalonia.Threading; using Avalonia.Threading;
@ -966,20 +965,43 @@ namespace Avalonia.Controls
/// </summary> /// </summary>
/// <param name="items">The items collection.</param> /// <param name="items">The items collection.</param>
/// <param name="desired">The desired items.</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 itemsCount = items.Count;
var toRemove = list.Except(desired).ToList(); if (desired is not null)
var toAdd = desired.Except(list).ToList();
foreach (var i in toRemove)
{ {
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) foreach (var i in toRemove)
{ {
items.Add(i); items.Remove(i);
}
foreach (var i in toAdd)
{
items.Add(i);
}
}
} }
} }
} }

Loading…
Cancel
Save