Practical AST: Make your own AutoCompleters, KeyBindings and PSScriptAnalyzer Rules - Justin Gr…

Опубликовано: 15 Июль 2023
на канале: PowerShell Conference EU
430
10

I, Justin Grote, explain the concept of an Abstract Syntax Tree (AST) in PowerShell, which is an object representation of code that can be parsed and analyzed. I highlight that script blocks in PowerShell are also objects and have properties, such as the AST property, which allows you to access the parsed version of the script block. I briefly mention the various properties and methods available for working with AST objects, focusing on the find and find all methods for pattern matching. I give an example of using the find method to quickly search for specific code patterns in a script analyzer. I then move on to discussing PS read line keyboard shortcuts and show an example of defining a new shortcut using the set PS read line key handler function. I explain some of the unfamiliar concepts, such as initializing a variable and using reference variables in PowerShell.

I see a code snippet that uses the "git buffer state" command to retrieve the PSReadLineBuffer, which is the current input typed into the console. This command was created before Predictive IntelliSense, so it allowed users to see their command history by hitting F7. The reference variable, defined as "ref," is used to pass the pointer to the variable already defined so that the method can populate it. After retrieving the buffer state, a regex escape is performed to search the history for a matching pattern. The code also uses the PowerShell parser to parse the input and get the Abstract Syntax Tree (AST) object. Then, it goes through all the tokens in the AST and checks if any of them can be cast as a string expandable token, which indicates that the user wants to interpolate variables in a string. If a string expandable token is found, the script block changes single quotes to double quotes. Overall, this code demonstrates various techniques for manipulating input and using the AST.

We fixed the issue with the typing and quotes moving around when using the dollar name. I also demonstrated how to use the F7 key in PowerShell to bring up a grid view of all the git commands I have used. This is a handy alternative to predictive IntelliSense. I then shared an idea I had for a PSReadLine key handler that allows me to easily access a GUI version of help for a specific command. This works in VS Code as well as anywhere PowerShell runs. I showed how the key handler can detect if a web browser is installed and automatically open the Microsoft Docs page for the command. I also mentioned that I credit my sources and explained how I came up with the idea for the key handler. I discussed using the AST and the find all command to search for specific tokens and how PowerShell's IntelliSense feature can be helpful.

So, what I'm doing here is analyzing the delegate and looking for the command AST (Abstract Syntax Tree) in the PowerShell code. The delegate is not easily discoverable, so I need to know how to read it. The first step is to check if the AST represents a command. If it does, I also check if the cursor is on a word in the extent to enable the option of retrieving specific help for that command.

If the AST and cursor conditions are met, I filter out any null values. Then I check if the command has any special functions, which can be found in the documentation or by using the git member command. These functions can be debugged and analyzed in the VS Code environment. In this particular case, I use the git command name method to get the string representation of the git verb object, which requires some sanitization to handle special characters.

After performing a null check, I can determine if the command is a PowerShell command or if it comes from a module. To find out the module, I use a faster method compared to the get command approach. Speed is essential when it comes to keystrokes.


Смотрите видео Practical AST: Make your own AutoCompleters, KeyBindings and PSScriptAnalyzer Rules - Justin Gr… онлайн, длительностью часов минут секунд в хорошем качестве, которое загружено на канал PowerShell Conference EU 15 Июль 2023. Делитесь ссылкой на видео в социальных сетях, чтобы ваши подписчики и друзья так же посмотрели это видео. Данный видеоклип посмотрели 430 раз и оно понравилось 10 посетителям.