Roslyn accomplishes this by having LeadingTrivia and TrailingTrivia collections for each token in the Syntax Tree. PowerShell doesn't maintain this type of trivia in the AST so refactoring algorithms have to take the AST and parsed tokens into account when processing and reordering a script.
I feel like I talked to @lzybkr about this at one point so there may be a good reason for not doing this that I don't remember but I figure it'd be a cool discussion.
Yeah, we've discussed it. Refactoring is a major design point for Roslyn, so *Trivia collections are pretty much required.
It might make sense to add *Trivia collections to PowerShell, but probably as a build time option in a stand-alone dll for PowerShell editor services.
The reason PowerShell doesn't have whitespace trivia yet is that it's not needed at runtime (really the most important scenario for PowerShell) and computing the trivia has a non-zero cost. It took a lot of work from Roslyn to reach parity with the native compiler, so adding such a feature to PowerShell is expected to be a lot of work to do well.
Now it's certainly reasonable to build the parser as a standalone dll as a tools such as PowerShell editor services - in that build configuration, having *Trivia makes a lot of sense.
@bergmeister Perhaps it can be interesting for you.
Having the parser as a standalone DLL would be pretty cool
I'd can investigate this but currently we have over 45 open PRs - slow reviews from MSFT team slow down community works 馃槙
No no, it's just a daydream for now. Tooling would benefit, but we should focus on what's being worked on currently for now.
The slow reviews are a consequence of driving other work at the moment -- hopefully it will pay dividends so that we can spend more time working on PowerShell itself.
Most helpful comment
Yeah, we've discussed it. Refactoring is a major design point for Roslyn, so *Trivia collections are pretty much required.
It might make sense to add *Trivia collections to PowerShell, but probably as a build time option in a stand-alone dll for PowerShell editor services.
The reason PowerShell doesn't have whitespace trivia yet is that it's not needed at runtime (really the most important scenario for PowerShell) and computing the trivia has a non-zero cost. It took a lot of work from Roslyn to reach parity with the native compiler, so adding such a feature to PowerShell is expected to be a lot of work to do well.
Now it's certainly reasonable to build the parser as a standalone dll as a tools such as PowerShell editor services - in that build configuration, having *Trivia makes a lot of sense.