Ejemplo n.º 1
0
        private void InternalShowSearch(int width, bool update = false)
        {
            var ovl = GetOverlay();

            if (!ovl.Visible || update)
            {
                if (width > editor.Info.TextWidth)
                {
                    width = editor.Info.TextWidth;
                }

                var size = new Size(width, editor.Info.LineHeight + Dpi.GetHeight(8));
                var rect = new Rectangle(new Point(
                                             editor.Info.TextRight - size.Width - editor.Info.CharWidth,
                                             editor.Info.TextTop + editor.Info.CharWidth),
                                         size);
                ovl.Size     = size;
                ovl.Location = rect.Location;
                ovl.Visible  = true;
                ovl.Invalidate();
            }

            if (!editor.Buffer.Selections.Main.IsEmpty)
            {
                var txt = CopyCommand.GetTextRange(editor, editor.Buffer.Selections.Main);
                ovl.SearchBox.Text = txt;
                ovl.SearchBox.Buffer.Selections.Set(
                    new Selection(new Pos(0, 0), new Pos(0, txt.Length)));
            }

            if (!string.IsNullOrEmpty(ovl.SearchBox.Text))
            {
                Search();
            }

            ovl.SearchBox.Redraw();
            ovl.SearchBox.Focus();
        }
Ejemplo n.º 2
0
        private void Match()
        {
            if (!editor.EditorSettings.MatchWords || editor.Buffer == null)
            {
                return;
            }

            var caret = editor.Buffer.Selections.Main.Caret;
            var range = SelectWordCommand.SelectWord(editor, caret, SelectWordCommand.Strategy.Word);
            var txt   = range != null?CopyCommand.GetTextRange(editor, range) : null;

            if (txt == lastWord && finds.Count > 0)
            {
                return;
            }

            var needRedraw = InternalClearMatches();

            if (caret != requestCaret || (DateTime.Now - requestTime).TotalMilliseconds < 500)
            {
                if (needRedraw)
                {
                    editor.Buffer.RequestRedraw();
                }
                return;
            }

            lastWord = txt;

            if (range == null)
            {
                if (needRedraw)
                {
                    editor.Buffer.RequestRedraw();
                }
                return;
            }

            var grmId = editor.AffinityManager.GetAffinityId(caret);
            var grm   = grmId != 0 ? App.Ext.Grammars().GetGrammar(grmId) : null;
            var seps  = (" \t" + (grm?.NonWordSymbols ?? editor.EditorSettings.NonWordSymbols)).ToCharArray();
            var regex = new Regex("\\b" + Regex.Escape(txt) + "\\b");

            for (var i = 0; i < editor.Lines.Count; i++)
            {
                var line = editor.Lines[i];
                var ln   = line.Text;

                foreach (Match m in regex.Matches(ln))
                {
                    if (m.Success && editor.AffinityManager.GetAffinityId(i, m.Index) == grmId)
                    {
                        var aps = new AppliedStyle(StandardStyle.MatchedWord, m.Index, m.Index + m.Length - 1);
                        line.AppliedStyles.Add(aps);
                        finds.Add(new SearchResult(i, aps));
                    }
                }
            }

            if (finds.Count == 1)
            {
                editor.Lines[finds[0].Line].AppliedStyles.Remove(finds[0].Style);
                finds.Clear();
            }

            if (finds.Count > 0 || needRedraw)
            {
                editor.Buffer.RequestRedraw();
            }

            requestCaret = Pos.Empty;
        }