Exemplo n.º 1
0
        private void LiveSearchEvents(MainFormUi ui)
        {
            var ctrl = ui.searchControl;
            var rb   = ui.resultBox;

            ctrl.searchTextBox.TextChanged += (o, e) => SciUtil.SearchAndMove(rb, ctrl.searchTextBox.Text);
            ctrl.btnNext.Click             += (o, e) => SciUtil.SearchAndMove(rb, ctrl.searchTextBox.Text);
            ctrl.btnPrev.Click             += (o, e) => SciUtil.SearchAndMove(rb, ctrl.searchTextBox.Text, reverse: true);
            ctrl.searchTextBox.KeyDown     += (o, e) =>
            {
                if (e.KeyCode == Keys.Enter)
                {
                    if (e.Shift)
                    {
                        SciUtil.SearchAndMove(rb, ctrl.searchTextBox.Text, reverse: true);
                    }
                    else
                    {
                        SciUtil.SearchAndMove(rb, ctrl.searchTextBox.Text);
                    }
                    e.SuppressKeyPress = true;
                    e.Handled          = true;
                }
            };
        }
Exemplo n.º 2
0
        public static void StartSearch(MainFormUi ui)
        {
            var text = ui.searchTextBox.Text;

            if (text == null || text.Trim().Length == 0)
            {
                return;
            }
            WorkDir = ui.dirSelector.Text;
            if (!Directory.Exists(WorkDir))
            {
                ui.resultBox.Text = $"Directory does not exist: '{WorkDir}'";
                return;
            }
            text = text.Replace("\"", "\\\"");
            var p = new Process();

            var args = CreateArgsForRg(text);

            Debugger.Log(0, "", args);
            ui.statusLabelCurrentArgs.Text = args;
            AssignStartInfo(p.StartInfo, "rg.exe", args);
            p.StartInfo.RedirectStandardOutput = true;
            p.StartInfo.RedirectStandardError  = true;
            ui.resultBox.ReadOnly = false;
            ui.resultBox.ClearAll();
            Lines.Clear();
            p.EnableRaisingEvents = true;
            var toFlush   = new List <string>();
            var flushlock = new Object();
            // do not decorate
            var emitCommentsForFiles = !RgExtraArgs.StartsWith("--files");

            void RichFlush(IEnumerable <string> lines)
            {
                var sb = new StringBuilder();

                foreach (var line in lines)
                {
                    if (line.Length == 0)
                    {
                        sb.Append("\r\n");
                        continue;
                    }

                    if (char.IsDigit(line[0]))
                    {
                        var parts = line.Split(new[] { ":", "-" }, 2, StringSplitOptions.None);
                        // context
                        if (parts.Length == 1)
                        {
                            sb.Append(line + "\r\n");
                        }
                        else
                        {
                            sb.Append(" ").Append(parts[1].Trim()).Append("\r\n");
                        }
                    }
                    else
                    {
                        if (emitCommentsForFiles)
                        {
                            sb.Append("//- ");
                        }
                        sb.Append(line).Append("\r\n");
                    }
                }
                ui.resultBox.AppendText(sb.ToString());
            }

            Action doFlush = () =>
            {
                var fl = toFlush;
                toFlush = new List <string>();
                RichFlush(fl);
            };
            Action debouncedFlush = Debounce(100, doFlush);

            p.OutputDataReceived += (o, ev) =>
            {
                if (ev.Data == null)
                {
                    return;
                }
                lock (flushlock)
                {
                    Lines.Add(ev.Data);
                    toFlush.Add(ev.Data);
                    ui.resultBox.Invoke(debouncedFlush);
                }
            };
            Action hideAbort   = () => ui.btnAbort.Visible = false;
            Action searchReady = () =>
            {
                ui.resultBox.SelectionStart = 0;
                ui.resultBox.SelectionEnd   = 0;
                ui.resultBox.ReadOnly       = true;
            };

            p.ErrorDataReceived += (o, e) =>
            {
                if (e.Data == null)
                {
                    return;
                }
                toFlush.Add(e.Data);

                ui.resultBox.Invoke(doFlush);
            };
            p.Exited += (o, ev) =>
            {
                CurrentSearchProcess = null;
                ui.btnAbort.Invoke(hideAbort);
                ui.resultBox.Invoke(doFlush);
                ui.resultBox.Invoke(searchReady);
            };
            p.Start();
            p.BeginOutputReadLine();
            p.BeginErrorReadLine();

            CurrentSearchProcess = p;
            ui.btnAbort.Visible  = true;
            PrependIfNew(Logic.DirHistory, WorkDir);
            PrependIfNew(Logic.SearchHistory, text);
            ui.dirSelector.SelectedIndex   = 0;
            ui.searchTextBox.SelectedIndex = 0;
        }
Exemplo n.º 3
0
 private static void SearchStartEvent(MainFormUi ui, KeyEventArgs e)
 {
     e.SuppressKeyPress = true;
     e.Handled          = true;
     Logic.StartSearch(ui);
 }
Exemplo n.º 4
0
        public MainFormBehind(MainFormUi ui)
        {
            void SetupScintilla()
            {
                var scintilla = SciUtil.CreateScintilla();

                scintilla.UpdateUI += (o, e) =>
                {
                    if (e.Change == UpdateChange.Selection)
                    {
                        UpdateStatusBar();
                    }
                };
                //sci.ReadOnly = true;
                ui.resultBox = scintilla;
                ui.form.Controls.Add(ui.resultBox);
                ui.resultBox.Dock = DockStyle.Fill;
                ui.searchTextBox.Select();
                ui.tableLayout.Controls.Add(ui.resultBox, 0, 1);
            }

            this.Ui = ui;
            SetupScintilla();
            ui.searchTextBox.KeyDown += (o, e) =>
            {
                if (e.KeyCode == Keys.Enter)
                {
                    SearchStartEvent(ui, e);
                }
            };
            ui.dirSelector.KeyDown += (o, e) =>
            {
                if (e.KeyCode == Keys.Enter)
                {
                    SearchStartEvent(ui, e);
                }
            };

            ui.resultBox.KeyDown += (o, e) =>
            {
                HandleKeyDownOnResults(e, ui.resultBox.CurrentLine);
            };
            ui.resultBox.KeyPress += (o, e) =>
            {
                // prevent PLING sound
                e.Handled = true;
            };
            SciUtil.SetAllText(ui.resultBox, Logic.Tutorial);

            ui.dirSelector.DataSource   = Logic.DirHistory;
            ui.searchTextBox.DataSource = Logic.SearchHistory;
            ui.rgArgsComboBox.Items.AddRange(new[]
            {
                Logic.RgExtraArgs,
                "--files",
                "-m 5 --smart-case",
                "-M 1000",
                "-g *.cs -g *.csproj",
                "--no-ignore",
                "--context 2"
            });
            ui.rgArgsComboBox.TextChanged += (o, e) =>
            {
                Logic.RgExtraArgs = ui.rgArgsComboBox.Text;
                UpdateStatusBar();
            };

            ui.dirSelector.Text = Logic.WorkDir;
            ui.btnAbort.Click  += (o, e) =>
            {
                ui.btnAbort.Visible = false;
                Logic.KillSearch();
            };
            LiveSearchEvents(ui);

            if (Logic.InitialSearchString != null)
            {
                ui.searchTextBox.Text     = Logic.InitialSearchString;
                Logic.InitialSearchString = null;
                Logic.StartSearch(ui);
            }
            UpdateStatusBar();
        }