Skip to content

Commit

Permalink
Merge pull request #28 from halvarsson/fix-ps51-format-script
Browse files Browse the repository at this point in the history
fix Format() on Powershell 5.1
  • Loading branch information
adamdriscoll authored Feb 27, 2023
2 parents 76e35bc + 82dc474 commit 38a3fe1
Showing 1 changed file with 11 additions and 5 deletions.
16 changes: 11 additions & 5 deletions ShowEditorCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -192,12 +192,18 @@ private void Format()
{
var previousCursorPosition = textEditor.CursorPosition;
var previousTopRow = textEditor.TopRow;
var formatted = InvokeCommand.InvokeScript("Invoke-Formatter -ScriptDefinition $args[0]", formatValue).FirstOrDefault();
if (formatted != null)
using (var powerShell = PowerShell.Create(RunspaceMode.CurrentRunspace))
{
textEditor.Text = formatted.BaseObject as string;
textEditor.CursorPosition = previousCursorPosition;
textEditor.TopRow = previousTopRow;
powerShell.AddCommand("Invoke-Formatter");
powerShell.AddParameter("ScriptDefinition", formatValue);
var result = powerShell.Invoke();
var formatted = result.FirstOrDefault();
if (formatted != null)
{
textEditor.Text = formatted.BaseObject as string;
textEditor.CursorPosition = previousCursorPosition;
textEditor.TopRow = previousTopRow;
}
}
}
}
Expand Down

0 comments on commit 38a3fe1

Please sign in to comment.