Пример #1
0
        public static async Task <InputResult <TValue> > Run <TGetter>(Editor editor, TOptions options) where TGetter : EditorGetter <TOptions, TValue>
        {
            using (var getter = Activator.CreateInstance <TGetter>())
            {
                getter.Editor = editor;

                getter.Completion = new TaskCompletionSource <InputResult <TValue> >();

                getter.Editor.InputMode = true;
                getter.Editor.DoPrompt("");

                getter.Options = options;

                var initArgs = new InitArgs <TValue>();
                getter.Init(initArgs);
                if (!initArgs.ContinueAsync)
                {
                    if (initArgs.InputValid)
                    {
                        getter.Completion.SetResult(InputResult <TValue> .AcceptResult(initArgs.Value, AcceptReason.Init));
                    }
                    else
                    {
                        getter.Completion.SetResult(InputResult <TValue> .CancelResult(CancelReason.Init));
                    }
                }

                if (initArgs.ContinueAsync)
                {
                    getter.Editor.CursorMove  += getter.Editor_CursorMove;
                    getter.Editor.CursorClick += getter.Editor_CursorClick;
                    getter.Editor.KeyDown     += getter.Editor_KeyDown;
                    getter.Editor.KeyPress    += getter.Editor_KeyPress;
                }

                getter.SetCursorText("");
                return(await getter.Completion.Task);
            }
        }
Пример #2
0
        private void Editor_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Escape)
            {
                Editor.DoPrompt("");
                CancelInput();
                var result = InputResult <TValue> .CancelResult(CancelReason.Escape);

                Completion.SetResult(result);
            }
            else if (e.KeyCode == Keys.Enter || e.KeyCode == Keys.Return || (SpaceAccepts && e.KeyCode == Keys.Space))
            {
                string keyword = Options.MatchKeyword(CurrentText);

                if (!string.IsNullOrEmpty(keyword))
                {
                    Editor.DoPrompt("");
                    var result = InputResult <TValue> .KeywordResult(keyword);

                    Completion.SetResult(result);
                }
                else if (!string.IsNullOrEmpty(CurrentText))
                {
                    var args = new InputArgs <string, TValue>(CurrentText);
                    AcceptTextInput(args);
                    if (args.InputValid)
                    {
                        Editor.DoPrompt("");
                        var result = InputResult <TValue> .AcceptResult(args.Value, AcceptReason.Text);

                        if (args.InputCompleted)
                        {
                            Completion.SetResult(result);
                        }
                    }
                    else
                    {
                        CurrentText = "";
                        Editor.DoPrompt(Options.GetFullPrompt() + args.ErrorMessage);
                    }
                }
                else
                {
                    Editor.DoPrompt("");
                    CancelInput();
                    var result = InputResult <TValue> .CancelResult(e.KeyCode == Keys.Space?CancelReason.Space : CancelReason.Enter);

                    Completion.SetResult(result);
                }
            }
            else if (e.KeyCode == Keys.Tab)
            {
                if (e.Shift)
                {
                    Editor.SnapPoints.Next();
                }
                else
                {
                    Editor.SnapPoints.Previous();
                }
                Editor.Document.ActiveView.Redraw();
            }
        }