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

The History view in PowerShell Pro Tools for VS Code is malformed #92

Open
TMA-2 opened this issue Nov 9, 2024 · 0 comments
Open

The History view in PowerShell Pro Tools for VS Code is malformed #92

TMA-2 opened this issue Nov 9, 2024 · 0 comments

Comments

@TMA-2
Copy link

TMA-2 commented Nov 9, 2024

Describe the bug

The History view reads from the start of the PSReadLine history instead of the end. This causes it to only ever show the first 25 lines going by the command run in the log. This would be super-easy to fix if -First was changed to -Last. It might also be nice to have a setting to show more than 25 items, e.g. "poshProTools.historyMaxItems": 100 or something thereabouts. Lastly, lines ending with a continuation backtick are attempted to be combined rather than left as-is. This wouldn't be a huge deal except that they aren't combined properly, leaving out most of any command ending in a line continuation backtick. See attached screenshot for an example of this.

Log output on refresh

[19:33:55.337] Scheduling command for main runspace: Get-Content (Get-PSReadLineOption).HistorySavePath | Select-Object -First 25

Expected Behavior

By order of importance (in my opinion), the view should:

  1. Read from the end of the file, whether it sorts descending or ascending in the view afterwards.
  2. Correctly concatenate lines that end with a backtick
  3. Ideally, read more than 25 lines, perhaps with a related setting, or just by using the pre-existing ⚙️terminal.integrated.shellIntegration.history.
  4. Trim extension host injection commands from the history, e.g. Import-Module, Start-PoshToolsServer, and Clear-Host. I'm sure there's a way to leave these out of the history to start with, as you wind up with a lot after some time, a minimum of six-eight lines per launch, more if the extension needs to restart.

See below for ideas on how to fix these. If I learn enough about Github and VSCode extensions I might attempt a PR myself?

Actual Behavior

The view reads from the beginning of the PSReadLine history, showing the first 25 items and remaining static forever, unless the PSReadLine history save path is changed. See attached screenshot for this.

Not a huge deal, but given the extension requires that persistent terminal sessions be disabled, it's not a good replacement. Reading from the end of the history, fixing line concatenation, and expanding the history size from a meager 25 items are the three issues I've found here.

Issue 1

All that needs to happen to fix this is to change the Select-Object parameter from -First to -Last.

Issue 2

This issue lies in the method that does this in PoshToolsServer.cs - GetHistory(). In my short skimming of the code, I think there needs to be a third condition, as I think what's happening is that each time a line of text is detected as ending with a backtick, the StringBuilder object is re-instantiated before appending the line without regard to whether it's still in the middle of a multi-line statement. Then on the loop after the last line detected (multiLine != null), only a single line is added to the result list from the multiLine variable, usually leaving the last line and perhaps a closing bracket in the history view where a full multi-line statement ought to be (see screenshot). Excusing my crappy C#, adding a check for multiLine being null or not along with a fourth condition in the foreach loop might solve this, i.e.:

// the first line ending in `
if (item.EndsWith('`') && multiLine == null)
{
    multiLine = new StringBuilder();
    multiLine.AppendLine(item.TrimEnd('`'));
}
// subsequent lines ending in `
else if (item.EndsWith('`') && multiLine != null)
{
    multiLine.AppendLine(item.TrimEnd('`'));
}
// the next line after
else if (multiLine != null)
{
	// continued as usual...
}

Issue 3

I think this could easily be added by just using the ⚙️terminal.integrated.shellIntegration.history setting for the number of lines, whether in the Select-Object call, or by reading in the entire history, and limiting in the JS extension code that refreshes the History view.

Issue 4

Adding the following regex exclusion (whether by adding Select-String <pattern> -NotMatch to the get history call, or in the C# code while processing the lines) would prune any startup injection commands from the history:

Import-Module '.*PowerShellProTools(?:\.VSCode)?\.psd1'|Start-PoshToolsServer -PipeName '\w+'(?:\nClear-Host)?

Version

  • PowerShell v7.4.6
  • VS Code 1.95.2
  • Extension: PowerShell Extension v2024.5.0 (pre-release)
  • Extension: PowerShell Pro Tools 2024.7.6
  • Windows 10 Enterprise 10.0.19045

Steps to Reproduce

  1. Open VSCode
  2. Verify the History view is enabled: setting ⚙️poshProTools.sideBar.historyVisibility is set to True.
  3. Run any command in the Terminal, and observe that the History view contains the same 25 lines.
  4. Compare the items in History to your PSReadLine history file (should be in $env:appdata\Microsoft\Windows\PowerShell\PSReadLine\Visual Studio Code Host_history.txt -- run code (Get-PSReadLineOption).HistorySavePath to open it). The first 25 items should be the same, albeit missing lines ending in backticks, and with their order reversed in the view due to result.Reverse(); in the associated code.

Screenshots

As can be seen, the first 25 lines are shown. And, only the last line ending in a backtick for the multi-line statement was added.
PSProTools-HistoryView-Comparison2-edit

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant