public HResult Replace(string search, string replace, NiFindOptions options, bool resetStartPoint, INiFindHelper helper, out NiFindResult result)
            {
                result = NiFindResult.NotFound;

                try
                {
                    if (search == null)
                    {
                        throw new ArgumentNullException("search");
                    }
                    if (helper == null)
                    {
                        throw new ArgumentNullException("helper");
                    }

                    var    document = _textAreaControl.Document;
                    string text     = document.TextContent;

                    int offset;

                    if (options.HasFlag(NiFindOptions.Backwards))
                    {
                        if (resetStartPoint)
                        {
                            offset = text.Length;
                        }
                        else
                        {
                            offset = _textAreaControl.Caret.Offset;
                        }
                    }
                    else
                    {
                        if (resetStartPoint)
                        {
                            offset = 0;
                        }
                        else if (_textAreaControl.SelectionManager.HasSomethingSelected)
                        {
                            offset = _textAreaControl.SelectionManager.SelectionCollection.Last().EndOffset;
                        }
                        else
                        {
                            offset = _textAreaControl.Caret.Offset;
                        }
                    }

                    int    matchLength;
                    string replacement;
                    bool   found;
                    ErrorUtil.ThrowOnFailure(helper.FindInText(
                                                 search,
                                                 replace,
                                                 options,
                                                 text,
                                                 offset,
                                                 out offset,
                                                 out matchLength,
                                                 out replacement,
                                                 out found
                                                 ));

                    if (found)
                    {
                        MarkSpan(GetTextSpan(document, offset, matchLength));
                        result = replace == null ? NiFindResult.Found : NiFindResult.Replaced;
                    }
                    else
                    {
                        result = NiFindResult.NotFound;
                    }

                    return(HResult.OK);
                }
                catch (Exception ex)
                {
                    return(ErrorUtil.GetHResult(ex));
                }
            }
 public HResult Find(string search, NiFindOptions options, bool resetStartPoint, INiFindHelper helper, out NiFindResult result)
 {
     return(Replace(search, null, options, resetStartPoint, helper, out result));
 }
 public HResult Replace(string search, string replace, NiFindOptions options, bool resetStartPoint, INiFindHelper helper, out NiFindResult result)
 {
     return Control.FindTarget.Replace(search, replace, options, resetStartPoint, helper, out result);
 }
Example #4
0
 public HResult Replace(string search, string replace, NiFindOptions options, bool resetStartPoint, INiFindHelper helper, out NiFindResult result)
 {
     return(Control.FindTarget.Replace(search, replace, options, resetStartPoint, helper, out result));
 }
 public HResult Find(string search, NiFindOptions options, bool resetStartPoint, INiFindHelper helper, out NiFindResult result)
 {
     return Replace(search, null, options, resetStartPoint, helper, out result);
 }
            public HResult Replace(string search, string replace, NiFindOptions options, bool resetStartPoint, INiFindHelper helper, out NiFindResult result)
            {
                result = NiFindResult.NotFound;

                try
                {
                    if (search == null)
                        throw new ArgumentNullException("search");
                    if (helper == null)
                        throw new ArgumentNullException("helper");

                    var document = _textAreaControl.Document;
                    string text = document.TextContent;

                    int offset;

                    if (options.HasFlag(NiFindOptions.Backwards))
                    {
                        if (resetStartPoint)
                            offset = text.Length;
                        else
                            offset = _textAreaControl.Caret.Offset;
                    }
                    else
                    {
                        if (resetStartPoint)
                            offset = 0;
                        else if (_textAreaControl.SelectionManager.HasSomethingSelected)
                            offset = _textAreaControl.SelectionManager.SelectionCollection.Last().EndOffset;
                        else
                            offset = _textAreaControl.Caret.Offset;
                    }

                    int matchLength;
                    string replacement;
                    bool found;
                    ErrorUtil.ThrowOnFailure(helper.FindInText(
                        search,
                        replace,
                        options,
                        text,
                        offset,
                        out offset,
                        out matchLength,
                        out replacement,
                        out found
                    ));

                    if (found)
                    {
                        MarkSpan(GetTextSpan(document, offset, matchLength));
                        result = replace == null ? NiFindResult.Found : NiFindResult.Replaced;
                    }
                    else
                    {
                        result = NiFindResult.NotFound;
                    }

                    return HResult.OK;
                }
                catch (Exception ex)
                {
                    return ErrorUtil.GetHResult(ex);
                }
            }