Example #1
0
            internal SpellingError(ISpellingError error, SpellChecker spellChecker, string text, bool shouldSuppressCOMExceptions = true, bool shouldReleaseCOMObject = true)
            {
                if (error == null)
                {
                    throw new ArgumentNullException(nameof(error));
                }

                StartIndex       = error.StartIndex;
                Length           = error.Length;
                CorrectiveAction = (CorrectiveAction)error.CorrectiveAction;
                Replacement      = error.Replacement;

                PopulateSuggestions(error, spellChecker, text, shouldSuppressCOMExceptions, shouldReleaseCOMObject);
            }
Example #2
0
        /// <summary>
        /// Checks the spelling of the supplied text and returns a collection of spelling errors
        /// </summary>
        /// <param name="text">The text to check</param>
        /// <returns>The results of spell checking</returns>
        public IEnumerable <SpellingError> Check(string text)
        {
            var            errors       = this.spellChecker.Check(text);
            ISpellingError currentError = null;

            try
            {
                while ((currentError = errors.Next()) != null)
                {
                    var action = RecommendedAction.None;

                    switch (currentError.CorrectiveAction)
                    {
                    case CORRECTIVE_ACTION.CORRECTIVE_ACTION_DELETE:
                        action = RecommendedAction.Delete;
                        break;

                    case CORRECTIVE_ACTION.CORRECTIVE_ACTION_GET_SUGGESTIONS:
                        action = RecommendedAction.GetSuggestions;
                        break;

                    case CORRECTIVE_ACTION.CORRECTIVE_ACTION_REPLACE:
                        action = RecommendedAction.Replace;
                        break;
                    }

                    yield return(new SpellingError
                    {
                        StartIndex = currentError.StartIndex,
                        Length = currentError.Length,
                        RecommendedAction = action,
                        RecommendedReplacement = currentError.Replacement
                    });

                    Marshal.ReleaseComObject(currentError);
                }
            }
            finally
            {
                if (currentError != null)
                {
                    Marshal.ReleaseComObject(currentError);
                }

                Marshal.ReleaseComObject(errors);
            }
        }
        /// <summary>
        /// Determines whether an RCW.IEnumSpellingError instance has any errors,
        /// without asking for expensive details.
        /// </summary>
        internal static bool HasErrors(
            this IEnumSpellingError spellingErrors,
            bool shouldSuppressCOMExceptions = true,
            bool shouldReleaseCOMObject      = true)
        {
            if (spellingErrors == null)
            {
                throw new ArgumentNullException(nameof(spellingErrors));
            }

            bool result = false;

            try
            {
                while (!result)
                {
                    ISpellingError iSpellingError = spellingErrors.Next();

                    if (iSpellingError == null)
                    {
                        // no more ISpellingError objects left in the enum
                        break;
                    }

                    if ((CorrectiveAction)iSpellingError.CorrectiveAction != CorrectiveAction.None)
                    {
                        result = true;
                    }
                    Marshal.ReleaseComObject(iSpellingError);
                }
            }
            catch (COMException) when(shouldSuppressCOMExceptions)
            {
                // do nothing here
                // the exception filter does it all.
            }
            finally
            {
                if (shouldReleaseCOMObject)
                {
                    Marshal.ReleaseComObject(spellingErrors);
                }
            }

            return(result);
        }
Example #4
0
        internal static List <SpellingError> ToList(
            this IEnumSpellingError spellingErrors,
            SpellChecker spellChecker,
            string text,
            bool shouldSuppressCOMExceptions = true,
            bool shouldReleaseCOMObject      = true)
        {
            if (spellingErrors == null)
            {
                throw new ArgumentNullException(nameof(spellingErrors));
            }

            var result = new List <SpellingError>();

            try
            {
                while (true)
                {
                    ISpellingError iSpellingError = spellingErrors.Next();

                    if (iSpellingError == null)
                    {
                        // no more ISpellingError objects left in the enum
                        break;
                    }

                    var error = new SpellingError(iSpellingError, spellChecker, text, shouldSuppressCOMExceptions, true);
                    result.Add(error);
                }
            }
            catch (COMException) when(shouldSuppressCOMExceptions)
            {
                // do nothing here
                // the exception filter does it all.
            }
            finally
            {
                if (shouldReleaseCOMObject)
                {
                    Marshal.ReleaseComObject(spellingErrors);
                }
            }

            return(result);
        }
Example #5
0
 private void PopulateSuggestions(ISpellingError error, SpellChecker spellChecker, string text, bool shouldSuppressCOMExceptions, bool shouldReleaseCOMObject)
 {
     try
     {
         _suggestions = new List <string>();
         if (CorrectiveAction == CorrectiveAction.GetSuggestions)
         {
             var suggestions = spellChecker.Suggest(text, shouldSuppressCOMExceptions);
             _suggestions.AddRange(suggestions);
         }
         else if (CorrectiveAction == CorrectiveAction.Replace)
         {
             _suggestions.Add(Replacement);
         }
     }
     finally
     {
         if (shouldReleaseCOMObject)
         {
             Marshal.ReleaseComObject(error);
         }
     }
 }