From 820f1607d6edfca2a197c6ee2d1645aa336b6448 Mon Sep 17 00:00:00 2001 From: Carl Morris Date: Fri, 6 Sep 2019 15:37:28 -0500 Subject: [PATCH] Prioritize highlight of command names, prevent bleed from aligned nested tokens (#989) --- PSReadLine/Render.cs | 12 ++++++------ test/RenderTest.cs | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 39 insertions(+), 6 deletions(-) diff --git a/PSReadLine/Render.cs b/PSReadLine/Render.cs index aa85311..4c32a81 100644 --- a/PSReadLine/Render.cs +++ b/PSReadLine/Render.cs @@ -215,7 +215,7 @@ namespace Microsoft.PowerShell // use the tokens color otherwise use the initial color. var state = tokenStack.Peek(); var token = state.Tokens[state.Index]; - if (i == token.Extent.EndOffset) + while (i == token.Extent.EndOffset) { if (token == state.Tokens[state.Tokens.Length - 1]) { @@ -630,6 +630,11 @@ namespace Microsoft.PowerShell private string GetTokenColor(Token token) { + if ((token.TokenFlags & TokenFlags.CommandName) != 0) + { + return _options._commandColor; + } + switch (token.Kind) { case TokenKind.Comment: @@ -653,11 +658,6 @@ namespace Microsoft.PowerShell return _options._numberColor; } - if ((token.TokenFlags & TokenFlags.CommandName) != 0) - { - return _options._commandColor; - } - if ((token.TokenFlags & TokenFlags.Keyword) != 0) { return _options._keywordColor; diff --git a/test/RenderTest.cs b/test/RenderTest.cs index d2dac1e..82115f4 100644 --- a/test/RenderTest.cs +++ b/test/RenderTest.cs @@ -61,6 +61,39 @@ namespace Test InputAcceptedNow )); + // This tests for priority to highlight a command regardless of token kind and nested tokens potential to bleed the parent token color to the next token + Test("", Keys( + ". -abc def;. abc$name -def", + _.Home, + CheckThat(() => + AssertScreenIs(1, + TokenClassification.None, ". ", + TokenClassification.Command, "-abc", + TokenClassification.None, " def;. ", + TokenClassification.Command, "abc", + TokenClassification.Variable, "$name", + TokenClassification.None, " ", + TokenClassification.Parameter, "-def")), + _.Ctrl_c, + InputAcceptedNow + )); + + // Additional test for priority to highlight a command regardless of token kind and nested tokens potential to bleed the parent token color to the next token + Test("", Keys( + ". ++ abc$name -def", + _.Home, + CheckThat(() => + AssertScreenIs(1, + TokenClassification.None, ". ", + TokenClassification.Command, "++", + TokenClassification.None, " abc", + TokenClassification.Variable, "$name", + TokenClassification.None, " ", + TokenClassification.Parameter, "-def")), + _.Ctrl_c, + InputAcceptedNow + )); + Test("", Keys( "\"$([int];\"_$(1+2)\")\"", CheckThat(() =>