示例#1
0
        public HResult FindInText(string find, string replace, NiFindOptions options, string text, int offset, out int found, out int matchLength, out string replacementText, out bool isFound)
        {
            found = 0;
            matchLength = 0;
            replacementText = null;
            isFound = false;

            try
            {
                if (find == null)
                    throw new ArgumentNullException("find");
                if (text == null)
                    throw new ArgumentNullException("text");

                string pattern =
                    options.HasFlag(NiFindOptions.RegExp)
                    ? find
                    : Regex.Escape(find);

                if (options.HasFlag(NiFindOptions.WholeWord))
                    pattern = @"\b" + pattern + @"\b";

                var regexOptions = RegexOptions.Multiline;

                if (!options.HasFlag(NiFindOptions.MatchCase))
                    regexOptions |= RegexOptions.IgnoreCase;
                if (options.HasFlag(NiFindOptions.Backwards))
                    regexOptions |= RegexOptions.RightToLeft;

                var match = new Regex(pattern, regexOptions).Match(text, offset);

                isFound = match.Success;

                if (isFound)
                {
                    found = match.Index;
                    matchLength = match.Length;
                }

                if (replace != null)
                    throw new NotImplementedException();

                return HResult.OK;
            }
            catch (Exception ex)
            {
                return ErrorUtil.GetHResult(ex);
            }
        }
示例#2
0
        public void SetOptions(NiFindOptions options, NiFindOptions optionsMask)
        {
            _view.BeginUpdate();

            Options = (Options & ~optionsMask) | options;

            bool replace = options.HasFlag(NiFindOptions.Replace);

            _view.SetMode(replace ? FindMode.Replace : FindMode.Find);

            if ((optionsMask & NiFindOptions.TargetMask) != 0)
            {
                switch (Options & NiFindOptions.TargetMask)
                {
                case NiFindOptions.Document: _view.SetTarget(Finder.FindTarget.CurrentDocument); break;

                case NiFindOptions.OpenDocument: _view.SetTarget(Finder.FindTarget.AllOpenDocuments); break;

                default: _view.SetTarget(Finder.FindTarget.EntireProject); break;
                }
            }

            _view.SetIncludeSubFolders(Options.HasFlag(NiFindOptions.SubFolders));
            _view.SetMatchCase(Options.HasFlag(NiFindOptions.MatchCase));
            _view.SetMatchWholeWord(Options.HasFlag(NiFindOptions.WholeWord));
            _view.SetUseRegularExpressions(Options.HasFlag(NiFindOptions.RegExp));
            _view.SetKeepOpen(Options.HasFlag(NiFindOptions.KeepOpen));

            _view.EndUpdate();
        }
示例#3
0
        public HResult FindInText(string find, string replace, NiFindOptions options, string text, int offset, out int found, out int matchLength, out string replacementText, out bool isFound)
        {
            found           = 0;
            matchLength     = 0;
            replacementText = null;
            isFound         = false;

            try
            {
                if (find == null)
                {
                    throw new ArgumentNullException("find");
                }
                if (text == null)
                {
                    throw new ArgumentNullException("text");
                }

                string pattern =
                    options.HasFlag(NiFindOptions.RegExp)
                    ? find
                    : Regex.Escape(find);

                if (options.HasFlag(NiFindOptions.WholeWord))
                {
                    pattern = @"\b" + pattern + @"\b";
                }

                var regexOptions = RegexOptions.Multiline;

                if (!options.HasFlag(NiFindOptions.MatchCase))
                {
                    regexOptions |= RegexOptions.IgnoreCase;
                }
                if (options.HasFlag(NiFindOptions.Backwards))
                {
                    regexOptions |= RegexOptions.RightToLeft;
                }

                var match = new Regex(pattern, regexOptions).Match(text, offset);

                isFound = match.Success;

                if (isFound)
                {
                    found       = match.Index;
                    matchLength = match.Length;
                }

                if (replace != null)
                {
                    throw new NotImplementedException();
                }

                return(HResult.OK);
            }
            catch (Exception ex)
            {
                return(ErrorUtil.GetHResult(ex));
            }
        }
            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 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);
                }
            }
示例#6
0
        public void SetOptions(NiFindOptions options, NiFindOptions optionsMask)
        {
            _view.BeginUpdate();

            Options = (Options & ~optionsMask) | options;

            bool replace = options.HasFlag(NiFindOptions.Replace);

            _view.SetMode(replace ? FindMode.Replace : FindMode.Find);

            if ((optionsMask & NiFindOptions.TargetMask) != 0)
            {
                switch (Options & NiFindOptions.TargetMask)
                {
                    case NiFindOptions.Document: _view.SetTarget(Finder.FindTarget.CurrentDocument); break;
                    case NiFindOptions.OpenDocument: _view.SetTarget(Finder.FindTarget.AllOpenDocuments); break;
                    default: _view.SetTarget(Finder.FindTarget.EntireProject); break;
                }
            }

            _view.SetIncludeSubFolders(Options.HasFlag(NiFindOptions.SubFolders));
            _view.SetMatchCase(Options.HasFlag(NiFindOptions.MatchCase));
            _view.SetMatchWholeWord(Options.HasFlag(NiFindOptions.WholeWord));
            _view.SetUseRegularExpressions(Options.HasFlag(NiFindOptions.RegExp));
            _view.SetKeepOpen(Options.HasFlag(NiFindOptions.KeepOpen));

            _view.EndUpdate();
        }