Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: Demo: Offer Tokenizing Improvements for Highlighting #10295

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

msftrncs
Copy link
Contributor

@msftrncs msftrncs commented Aug 3, 2019

In function name and command name parsing, insure that an expandable string
does not exist as these are not to be expanded.
In command name parsing, insure that redirect tokens that will be accepted as
commands are flagged as such.
In command argument processing, arguments that originate as 'Operator' tokens have their TokenFlags reverted when switched from an 'operator' TokenKind to a 'Generic' TokenKind.
In command name processing, treat DashDash token as a command name and do not set in motion the 'no more parameters' mode.
In command argument processing, correct the 'no more parameters' mode so additional 'parameter' tokens are not marked as command names.
In command name processing, treat --% (verbatim argument operator) as a command and do not treat next argument verbatim.
In function definitions, correct function name token of -- to be generic, and fix assumptions that generic tokens are string tokens.

Reference #10275, #10347, #10348

to do

  • Find tests for --, add tests to make sure -- after a command of -- still works.
  • Find tests for --%, add tests to make sure --% after a command of --% still works.
  • Find tests for redirection operators, add tests to make sure redirection operators as commands do not interfere with redirection operators later in the command statement.

PR Summary

Adjust token polishing to benefit token based highlighting.

PR Context

The syntax highlighting offered by PSReadLine has always led me through some confusion and I am sure every once in a while it confuses some others as well as to what is really accepted as syntax. However the issue is not really with PSReadLine but with the tokens that PSReadLine uses for highlighting, rather than using the AST.

It would appear that some house keeping tasks are not performed on the token collection after the AST is produced from them. Some places clean up (set indicator flags) and some places do not. The most notable item is that tokens that ultimately become StringExpandableToken but when processed in to AST are treated as just StringToken remain in that form, which then incorrectly informs any downstream applications.

PR Checklist

Copy link
Member

@JamesWTruher JamesWTruher left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

definitely going to want to see a way to test the changes here

@ghost ghost added the Waiting on Author The PR was reviewed and requires changes or comments from the author before being accept label Sep 4, 2019
@msftrncs
Copy link
Contributor Author

@JamesWTruher, I think tests could be added to test\powershell\language\parser\parsing.tests.ps1, some ParseInput expressions could be constructed to test the tokenizer results of a few sample commands. I am referencing the ternary operator parsing tests that @daxian-dbw added. In some ways this is similar to how PSReadLine performs tests of its highlighting, but without simulating the input and output steps.

  • ParseInput a sample command,
  • verify select tokens are as expected (either by TokenKind or TokenFlag).

I'll work on getting this done.

@ghost ghost removed the Waiting on Author The PR was reviewed and requires changes or comments from the author before being accept label Sep 19, 2019
@msftrncs msftrncs changed the title Demo: Offer Tokenizing Improvements for Highlighting WIP: Demo: Offer Tokenizing Improvements for Highlighting Sep 19, 2019
In function name and command name parsing, insure that an expandable string
does not exist as these are not to be expanded.
In command name, insure that redirect tokens that will be accepted as
commands are flagged as such.

Reference PowerShell#10275
@msftrncs
Copy link
Contributor Author

I've included another commit to demonstrate all the potential highlighting improvements I think should be made within parsing/tokenizing. However, this also includes two changes to behavior, for -- and --% when used as command names (via invoke operators). The referenced issues are listed above the PR summary.

Copy link
Member

@lzybkr lzybkr left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changes look reasonable, but it's important to add tests that validate no semantic changes, e.g passing -- after -- or using a redirection operator as a command name.

@@ -5284,6 +5284,13 @@ private StatementAst FunctionDeclarationRule(Token functionToken)
// A function name that matches a keyword isn't really a keyword, so don't color it that way
functionNameToken.TokenFlags &= ~TokenFlags.Keyword;

// A function name is not an expandable token, so remove any nested tokens
var expandableToken = functionNameToken as StringExpandableToken;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pattern matching makes this a little easier to read:

if (functionNameToken is StringExpandableToken expandableToken)
{
    expandableToken.NestedTokens = null;
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ahhh… that's what that suggestion message means? I had copied that from a similar part in GetCommandArgument.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure what suggestion you're referring to, but pattern matching was not available in C# when the code you copied was written.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've addressed this code in revised commit, but have a ways to go on tests.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

VS Code suggests all over 'Use Pattern Matching'. The area of the code I copied was not one such area though, probably because its too complex for the pattern matching syntax.

@msftrncs
Copy link
Contributor Author

I've revised the most recent commit to address another -- highlighting issue, as a function name in a function definition. For this I used Token.SetAsCommandArgument. This method should probably be more aptly named SetAsGenericTokenKind. However, there was further code that assumed that a TokenKind.Generic token would be a StringToken, so I had to make some adjustments.

Correct tokenizing output so that `--` when used as a command name does not
instigate the 'stop processing parameters' function and is correctly
identified as a command.  Also correct further parameter tokens from being
incorrectly marked as commands while in that mode.
Correct tokenizing treatment of `--%` when it is a command name so as not
server as the VerbatimArgumentMode, and also to be indentified as a
command name token.
Address tokenizing of `--` (and others) as a function name, using
Token.SetIsCommandArgument.
This also addresses token flags being left set when operator tokens are
converted to generic tokens in Token.SetIsCommandArgument.
Also addressed assumptions that TokenKind.Generic is always StringToken.
@TravisEz13 TravisEz13 added this to the 7.1.0-preview.1 milestone Nov 23, 2019
@ghost ghost assigned daxian-dbw Nov 23, 2019
@ghost ghost added the Review - Needed The PR is being reviewed label May 27, 2020
@ghost
Copy link

ghost commented May 27, 2020

This pull request has been automatically marked as Review Needed because it has been there has not been any activity for 7 days.
Mainainer, Please provide feedback and/or mark it as Waiting on Author

@adityapatwardhan adityapatwardhan removed this from the 7.1.0-preview.4 milestone Jun 29, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Review - Needed The PR is being reviewed
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants