Example #1
0
        /// <summary>
        /// Instructs the host to interrupt the currently running pipeline and start a new nested input loop.
        /// An input loop is the cycle of prompt, input, and execute.
        /// </summary>
        public override void EnterNestedPrompt()
        {
            // push the last
            IEditor keepNested = _nested;

            try
            {
                //! Far used to crash: Test-CallStack-.ps1 \ suspend \ type exit + enter
                //! This exception from Open() was removed, so don't try\catch all in here.
                //! SVN tag 4.2.26
                Interactive console = Interactive.Create(false);
                _nested = console.Editor;

                // Enter the modal editor. There are two ways to exit.
                // 1) User exits the editor ([Esc]/[F10]). _nested should be this editor, not null.
                // But PowerShell nested prompt is not yet exited, call 'exit', it triggers
                // ExitNestedPrompt(), it sets _nested to null.
                // 2) User types 'exit' in the editor. Then ExitNestedPrompt() is called first,
                // it sets _nested to null and closes the editor. Control gets here with null
                // _nested, so we do nothing but restoring the very first _nested.
                _nested.Open(OpenMode.Modal);

                // If _nested is not null then a user has closed the editor via UI, not by 'exit'.
                // Thus, we have to exit the nested prompt. IsRunning check is added for V3 CTP2.
                // It works fine in V2, too. Meaning: if there is no running pipeline (stepper)
                // then there is nothing to exit, so do not exit. Exit nothing hangs in V3 CTP2.
                if (_nested != null && A.Psf.IsRunning)
                {
                    using (var ps = A.Psf.NewPowerShell())
                        ps.AddScript("exit").Invoke();
                }
            }
            finally
            {
                // pop the last
                _nested = keepNested;
            }
        }