From 4f3efc69306d691e348aeee5f18a20296c245c0d Mon Sep 17 00:00:00 2001 From: h3xds1nz Date: Sat, 20 Jul 2024 14:14:16 +0200 Subject: [PATCH 1/4] replace arraylist with list, remove unnecessary casts and iteration --- .../System/Windows/Documents/Speller.cs | 7 +++---- .../System/Windows/Documents/SpellerError.cs | 10 +--------- 2 files changed, 4 insertions(+), 13 deletions(-) diff --git a/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Documents/Speller.cs b/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Documents/Speller.cs index 3aceff72a9a..204313f6d6e 100644 --- a/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Documents/Speller.cs +++ b/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Documents/Speller.cs @@ -177,16 +177,15 @@ internal ITextPointer GetNextSpellingErrorPosition(ITextPointer position, Logica // for an error range. // This method actually runs the speller on the specified text, // re-evaluating the error from scratch. - internal IList GetSuggestionsForError(SpellingError error) + internal List GetSuggestionsForError(SpellingError error) { ITextPointer contextStart; ITextPointer contextEnd; ITextPointer contentStart; ITextPointer contentEnd; TextMap textMap; - ArrayList suggestions; - suggestions = new ArrayList(1); + List suggestions = new(); // // IMPORTANT!! @@ -915,7 +914,7 @@ private bool ScanErrorTextSegment(SpellerInteropBase.ISpellerSegment textSegment { if (textSegment.SubSegments.Count == 0) { - ArrayList suggestions = (ArrayList)data.Data; + List suggestions = (List)data.Data; if(textSegment.Suggestions.Count > 0) { foreach(string suggestion in textSegment.Suggestions) diff --git a/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Documents/SpellerError.cs b/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Documents/SpellerError.cs index b309aa8d32c..5761d3ff301 100644 --- a/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Documents/SpellerError.cs +++ b/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Documents/SpellerError.cs @@ -94,15 +94,7 @@ public void IgnoreAll() /// public IEnumerable Suggestions { - get - { - IList suggestions = _speller.GetSuggestionsForError(this); - - for (int i=0; i _speller.GetSuggestionsForError(this); } #endregion Public Properties From 844b414894d210841174e63db16cb014d90f5af6 Mon Sep 17 00:00:00 2001 From: h3xds1nz Date: Mon, 22 Jul 2024 08:55:40 +0200 Subject: [PATCH 2/4] simplify GetSuggestionsForError, use inline var declarations --- .../System/Windows/Documents/Speller.cs | 25 ++++++------------- 1 file changed, 7 insertions(+), 18 deletions(-) diff --git a/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Documents/Speller.cs b/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Documents/Speller.cs index 204313f6d6e..389c2e1b87a 100644 --- a/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Documents/Speller.cs +++ b/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Documents/Speller.cs @@ -179,14 +179,6 @@ internal ITextPointer GetNextSpellingErrorPosition(ITextPointer position, Logica // re-evaluating the error from scratch. internal List GetSuggestionsForError(SpellingError error) { - ITextPointer contextStart; - ITextPointer contextEnd; - ITextPointer contentStart; - ITextPointer contentEnd; - TextMap textMap; - - List suggestions = new(); - // // IMPORTANT!! // @@ -194,18 +186,15 @@ internal List GetSuggestionsForError(SpellingError error) // calculate the exact same error. Keep the two methods in sync! // - XmlLanguage language; - CultureInfo culture = GetCurrentCultureAndLanguage(error.Start, out language); - if (culture == null || !_spellerInterop.CanSpellCheck(culture)) - { - // Return an empty list. - } - else + List suggestions = new(); + CultureInfo culture = GetCurrentCultureAndLanguage(error.Start, out XmlLanguage language); + + if (culture is not null || _spellerInterop.CanSpellCheck(culture)) { - ExpandToWordBreakAndContext(error.Start, LogicalDirection.Backward, language, out contentStart, out contextStart); - ExpandToWordBreakAndContext(error.End, LogicalDirection.Forward, language, out contentEnd, out contextEnd); + ExpandToWordBreakAndContext(error.Start, LogicalDirection.Backward, language, out ITextPointer contentStart, out ITextPointer contextStart); + ExpandToWordBreakAndContext(error.End, LogicalDirection.Forward, language, out ITextPointer contentEnd, out ITextPointer contextEnd); - textMap = new TextMap(contextStart, contextEnd, contentStart, contentEnd); + TextMap textMap = new(contextStart, contextEnd, contentStart, contentEnd); SetCulture(culture); From 2c55fb2ececc430af52bf374436889ab38ee24b5 Mon Sep 17 00:00:00 2001 From: h3xds1nz Date: Mon, 22 Jul 2024 21:39:09 +0200 Subject: [PATCH 3/4] adjust the initial capacity to the typical number of suggestions --- .../PresentationFramework/System/Windows/Documents/Speller.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Documents/Speller.cs b/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Documents/Speller.cs index 389c2e1b87a..d7fd9aeac35 100644 --- a/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Documents/Speller.cs +++ b/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Documents/Speller.cs @@ -186,7 +186,7 @@ internal List GetSuggestionsForError(SpellingError error) // calculate the exact same error. Keep the two methods in sync! // - List suggestions = new(); + List suggestions = new(4); CultureInfo culture = GetCurrentCultureAndLanguage(error.Start, out XmlLanguage language); if (culture is not null || _spellerInterop.CanSpellCheck(culture)) From adcd2868dce1063f774b4fa48a3309d85c12719a Mon Sep 17 00:00:00 2001 From: h3xds1nz Date: Mon, 9 Sep 2024 08:42:44 +0200 Subject: [PATCH 4/4] properly evaluate whether we can spell-check --- .../PresentationFramework/System/Windows/Documents/Speller.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Documents/Speller.cs b/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Documents/Speller.cs index d7fd9aeac35..6d2df685dd0 100644 --- a/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Documents/Speller.cs +++ b/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Documents/Speller.cs @@ -189,7 +189,7 @@ internal List GetSuggestionsForError(SpellingError error) List suggestions = new(4); CultureInfo culture = GetCurrentCultureAndLanguage(error.Start, out XmlLanguage language); - if (culture is not null || _spellerInterop.CanSpellCheck(culture)) + if (culture is not null && _spellerInterop.CanSpellCheck(culture)) { ExpandToWordBreakAndContext(error.Start, LogicalDirection.Backward, language, out ITextPointer contentStart, out ITextPointer contextStart); ExpandToWordBreakAndContext(error.End, LogicalDirection.Forward, language, out ITextPointer contentEnd, out ITextPointer contextEnd);