in src/Engines/BaseEngine.cs [667:744]
public virtual async Task<CompletionResult?> Complete(string code, int cursorPos) =>
(await CompleteMagic(code, cursorPos))
.Concat(await CompleteMundane(code, cursorPos))
.AsCompletionResult(code, cursorPos);
#endregion
#region Command Execution
/// <summary>
/// Main entry point to execute a Jupyter cell.
///
/// It identifies if the cell contains a help or magic command and triggers ExecuteHelp
/// or ExecuteMagic accordingly. If no special symbols are found, it triggers ExecuteMundane.
/// </summary>
/// <param name="input">the cell's content.</param>
/// <param name="channel">the channel to generate messages or errors.</param>
/// <returns>An <c>ExecutionResult</c> instance with the results of </returns>
public async virtual Task<ExecutionResult> Execute(string input, IChannel channel)
=> await Execute(input, channel, CancellationToken.None);
/// <summary>
/// Main entry point to execute a Jupyter cell.
///
/// It identifies if the cell contains a help or magic command and triggers ExecuteHelp
/// or ExecuteMagic accordingly. If no special symbols are found, it triggers ExecuteMundane.
/// </summary>
/// <param name="input">the cell's content.</param>
/// <param name="channel">the channel to generate messages or errors.</param>
/// <param name="cancellationToken">the cancellation token used to request cancellation.</param>
/// <returns>An <c>ExecutionResult</c> instance with the results of </returns>
public async virtual Task<ExecutionResult> Execute(string input, IChannel channel, CancellationToken cancellationToken)
{
try
{
this.History.Add(input);
ExecutionResult result = ExecuteStatus.Ok.ToExecutionResult();
// Continue looping until we have processed all of the input
// or until we have a failure.
string currentInput = input;
while (result.Status == ExecuteStatus.Ok && !string.IsNullOrEmpty(currentInput))
{
// We first check to see if the first token is a help or magic command for this kernel.
var commandType = this.inputParser.GetNextCommand(currentInput, out ISymbol? symbol, out string commandInput, out string remainingInput);
if (commandType == InputParser.CommandType.MagicHelp || commandType == InputParser.CommandType.Help)
{
result = await ExecuteAndNotify(commandInput, symbol, channel, cancellationToken, ExecuteHelp, HelpExecuted);
currentInput = remainingInput;
}
else if (commandType == InputParser.CommandType.Magic)
{
result = await ExecuteAndNotify(commandInput, symbol, channel, cancellationToken, ExecuteMagic, MagicExecuted);
currentInput = remainingInput;
}
else
{
result = await ExecuteAndNotify(currentInput, channel, cancellationToken, ExecuteMundane, MundaneExecuted);
currentInput = string.Empty;
}
}
// Return the most recently-obtained result.
return result;
}
catch (TaskCanceledException)
{
// Do nothing; it's OK for a task to be cancelled.
return (null as object).ToExecutionResult();
}
catch (Exception e)
{
Logger.LogWarning(e, $"Exception encountered when executing input: ${input}");
channel.Stderr(e.Message);
return ExecuteStatus.Error.ToExecutionResult();
}
}