/// <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);
}