A class that represents a result that an IContentAssistProvider returns to a parent reader.
        /// <summary>
        /// Creates a result that replaces the current token with the given selection.
        /// </summary>
        /// <param name="context">Context from the parent reader</param>
        /// <param name="selection">The selection string to insert</param>
        /// <returns>a result that replaces the current token with the given selection</returns>
        public static ContextAssistResult CreateInsertResult(RichCommandLineContext context, ConsoleString selection)
        {
            context.RefreshTokenInfo();
            var ret = new ContextAssistResult();

            bool hasInserted = false;
            var  newBuffer   = new List <ConsoleCharacter>();

            foreach (var token in context.Tokens)
            {
                if (context.IsCursorOnToken(token))
                {
                    if (string.IsNullOrWhiteSpace(token.Value))
                    {
                        newBuffer.AddRange(context.GetBufferSubstringFromToken(token));
                        ret.ConsoleRefreshLeftOffset = selection.Length;
                    }
                    else
                    {
                        var tokenOffset = context.BufferPosition - token.StartIndex;
                        ret.ConsoleRefreshLeftOffset = selection.Length - tokenOffset;
                    }

                    if (hasInserted == false)
                    {
                        hasInserted = true;
                        // cursor is on the current token
                        newBuffer.AddRange(selection);
                    }
                }
                else
                {
                    // this token not be modified
                    newBuffer.AddRange(context.GetBufferSubstringFromToken(token));
                }
            }

            if (hasInserted == false)
            {
                hasInserted = true;
                // cursor is on the current token
                newBuffer.AddRange(selection);
                ret.ConsoleRefreshLeftOffset = selection.Length;
            }

            ret.StatusCode = ContextAssistResultStatusCode.Success;
            ret.NewBuffer  = newBuffer;
            return(ret);
        }
Esempio n. 2
0
        public void Handle(RichCommandLineContext context)
        {
            if (context.KeyPressed.Modifiers.HasFlag(ConsoleModifiers.Control) == false)
            {
                return;
            }

            context.Intercept = true;
            context.RefreshTokenInfo();

            if (ContextAssistProvider == null || ContextAssistProvider.CanAssist(context) == false)
            {
                return;
            }

            int left = context.Console.CursorLeft;
            int top  = context.Console.CursorTop;

            ContextAssistResult result = ContextAssistResult.NoOp;

            try
            {
                context.Console.WriteLine("\n");
                result = ContextAssistProvider.DrawMenu(context);

                while (result.IsTerminal == false)
                {
                    var key = context.Console.ReadKey(true);
                    result = ContextAssistProvider.OnKeyboardInput(context, key);
                }
            }
            finally
            {
                ContextAssistProvider.ClearMenu(context);
                context.Console.CursorLeft = left;
                context.Console.CursorTop  = top;
            }

            if (result.StatusCode == ContextAssistResultStatusCode.Success)
            {
                context.ClearConsole();
                context.Console.CursorLeft = left;
                context.Console.CursorTop  = top;
                context.Buffer.Clear();
                context.Buffer.AddRange(result.NewBuffer);
                context.RefreshConsole(result.ConsoleRefreshLeftOffset, 0);
            }
        }
        /// <summary>
        /// Creates a result that replaces the current token with the given selection.
        /// </summary>
        /// <param name="context">Context from the parent reader</param>
        /// <param name="selection">The selection string to insert</param>
        /// <returns>a result that replaces the current token with the given selection</returns>
        public static ContextAssistResult CreateInsertResult(RichCommandLineContext context, ConsoleString selection)
        {
            context.RefreshTokenInfo();
            var ret = new ContextAssistResult();

            bool hasInserted = false;
            var newBuffer = new List<ConsoleCharacter>();
            foreach (var token in context.Tokens)
            {
                if (context.IsCursorOnToken(token))
                {
                    if (string.IsNullOrWhiteSpace(token.Value))
                    {
                        newBuffer.AddRange(context.GetBufferSubstringFromToken(token));
                        ret.ConsoleRefreshLeftOffset = selection.Length;
                    }
                    else
                    {
                        var tokenOffset = context.BufferPosition - token.StartIndex;
                        ret.ConsoleRefreshLeftOffset = selection.Length - tokenOffset;
                    }

                    if (hasInserted == false)
                    {
                        hasInserted = true;
                        // cursor is on the current token
                        newBuffer.AddRange(selection);
                    }
                }
                else
                {
                    // this token not be modified
                    newBuffer.AddRange(context.GetBufferSubstringFromToken(token));
                }
            }

            if (hasInserted == false)
            {
                hasInserted = true;
                // cursor is on the current token
                newBuffer.AddRange(selection);
                ret.ConsoleRefreshLeftOffset = selection.Length;
            }

            ret.StatusCode = ContextAssistResultStatusCode.Success;
            ret.NewBuffer = newBuffer;
            return ret;
        }