/// <summary> /// Handles the tab key by calling all registered tab completion handlers. /// </summary> /// <param name="context">Context that can be used to inspect the current command line to perform tab completion</param> public void Handle(RichCommandLineContext context) { context.Intercept = true; context.RefreshTokenInfo(); try { foreach (var handler in TabCompletionHandlers) { if (handler.TryTabComplete(context)) { break; } } } catch (Exception ex) { if (ThrowOnTabCompletionHandlerException) { throw; } else { PowerLogger.LogLine("Tab completion handler threw exception: " + ex.ToString()); } } }
/// <summary> /// Handles the tab key by calling all registered tab completion handlers. /// </summary> /// <param name="context">Context that can be used to inspect the current command line to perform tab completion</param> public void Handle(RichCommandLineContext context) { context.Intercept = true; context.RefreshTokenInfo(); try { foreach (var handler in TabCompletionHandlers) { if (handler.TryTabComplete(context)) { break; } } } catch(Exception ex) { if (ThrowOnTabCompletionHandlerException) { throw; } else { PowerLogger.LogLine("Tab completion handler threw exception: " + ex.ToString()); } } }
/// <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); }
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> /// The implementation of ISyntaxHighlighter that uses the configuration you've created to perform syntax highlighting. /// </summary> /// <param name="readerContext">Context that is used internally</param> /// <returns>true if any highlighting changes were made, false otherwise</returns> public bool TryHighlight(RichCommandLineContext readerContext) { readerContext.RefreshTokenInfo(); bool didWork = false; for (int i = 0; i < readerContext.Tokens.Count; i++) { if (string.IsNullOrWhiteSpace(readerContext.Tokens[i].Value)) { continue; } var highlighterContext = new HighlighterContext() { CurrentToken = readerContext.Tokens[i], CurrentTokenIndex = i, IsLastToken = i == readerContext.Tokens.Count - 1, }; bool didWorkOnThisToken = false; bool shouldBeHighlightedByAtLeastOneHighlighter = false; foreach (var tokenHighlighter in TokenHighlighters) { bool shouldBeHighlightedByThisHighlighter = tokenHighlighter.ShouldBeHighlighted(readerContext, highlighterContext); shouldBeHighlightedByAtLeastOneHighlighter = shouldBeHighlightedByAtLeastOneHighlighter || shouldBeHighlightedByThisHighlighter; if (shouldBeHighlightedByThisHighlighter) { didWorkOnThisToken = EnsureHighlighted(highlighterContext.CurrentToken, readerContext, tokenHighlighter.HighlightForegroundColor, tokenHighlighter.HighlightBackgroundColor); if (didWorkOnThisToken) { break; } } } if (shouldBeHighlightedByAtLeastOneHighlighter == false) { didWorkOnThisToken = EnsureHighlighted(highlighterContext.CurrentToken, readerContext, null, null); } didWork = didWork || didWorkOnThisToken; } return(didWork); }
/// <summary> /// Handles the tab key by calling all registered tab completion handlers. /// </summary> /// <param name="context">Context that can be used to inspect the current command line to perform tab completion</param> public void Handle(RichCommandLineContext context) { context.Intercept = true; context.RefreshTokenInfo(); try { foreach (var handler in TabCompletionHandlers) { if (handler.TryTabComplete(context)) { break; } } } catch (Exception) { if (ThrowOnTabCompletionHandlerException) { throw; } } }
/// <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; }
/// <summary> /// The implementation of ISyntaxHighlighter that uses the configuration you've created to perform syntax highlighting. /// </summary> /// <param name="readerContext">Context that is used internally</param> /// <returns>true if any highlighting changes were made, false otherwise</returns> public bool TryHighlight(RichCommandLineContext readerContext) { readerContext.RefreshTokenInfo(); bool didWork = false; for (int i = 0; i < readerContext.Tokens.Count; i++) { if(string.IsNullOrWhiteSpace(readerContext.Tokens[i].Value)) { continue; } var highlighterContext = new HighlighterContext() { CurrentToken = readerContext.Tokens[i], CurrentTokenIndex = i, IsLastToken = i == readerContext.Tokens.Count-1, }; bool didWorkOnThisToken = false; bool shouldBeHighlightedByAtLeastOneHighlighter = false; foreach (var tokenHighlighter in TokenHighlighters) { bool shouldBeHighlightedByThisHighlighter = tokenHighlighter.ShouldBeHighlighted(readerContext, highlighterContext); shouldBeHighlightedByAtLeastOneHighlighter = shouldBeHighlightedByAtLeastOneHighlighter || shouldBeHighlightedByThisHighlighter; if (shouldBeHighlightedByThisHighlighter) { didWorkOnThisToken = EnsureHighlighted(highlighterContext.CurrentToken, readerContext, tokenHighlighter.HighlightForegroundColor, tokenHighlighter.HighlightBackgroundColor); if (didWorkOnThisToken) break; } } if(shouldBeHighlightedByAtLeastOneHighlighter == false) { didWorkOnThisToken = EnsureHighlighted(highlighterContext.CurrentToken, readerContext, null, null); } didWork = didWork || didWorkOnThisToken; } return didWork; }