Skip to content

Commit

Permalink
populate allowed filetypes on startup instead of each time we are dis…
Browse files Browse the repository at this point in the history
…playing the dialogs
  • Loading branch information
halvarsson committed May 2, 2024
1 parent d809055 commit 8b04015
Showing 1 changed file with 9 additions and 14 deletions.
23 changes: 9 additions & 14 deletions ShowEditorCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ public class ShowEditorCommand : PSCmdlet
private bool _matchCase;
private bool _matchWholeWord;
#endregion
private List<string> _allowedFileTypes = new List<string>();

[Parameter(ParameterSetName = "Path", ValueFromPipeline = true, Position = 0)]
public string Path { get; set; }
Expand All @@ -43,6 +44,12 @@ protected override void BeginProcessing()
_runspace = RunspaceFactory.CreateRunspace();
_runspace.Open();
currentDirectory = SessionState.Path.CurrentLocation.Path;
// populate the allowed file types for dialogs on startup
_allowedFileTypes.Add(".ps1");
_allowedFileTypes.Add(".psm1");
_allowedFileTypes.Add(".psd1");
_allowedFileTypes.Add(".json");
_allowedFileTypes.Add(".txt");
}

protected override void ProcessRecord()
Expand Down Expand Up @@ -245,13 +252,7 @@ private void Open()
{
return;
}
List<string> allowedFileTypes = new List<string>();
allowedFileTypes.Add(".ps1");
allowedFileTypes.Add(".psm1");
allowedFileTypes.Add(".psd1");
allowedFileTypes.Add(".json");
allowedFileTypes.Add(".txt");
var dialog = new OpenDialog("Open file", "", allowedFileTypes);
var dialog = new OpenDialog("Open file", "", _allowedFileTypes);
dialog.CanChooseDirectories = false;
dialog.CanChooseFiles = true;
dialog.AllowsMultipleSelection = false;
Expand Down Expand Up @@ -405,13 +406,7 @@ private bool Save(bool saveAs)
{
if (string.IsNullOrEmpty(Path) || saveAs)
{
List<string> allowedFileTypes = new List<string>();
allowedFileTypes.Add(".ps1");
allowedFileTypes.Add(".psm1");
allowedFileTypes.Add(".psd1");
allowedFileTypes.Add(".json");
allowedFileTypes.Add(".txt");
var dialog = new SaveDialog(saveAs ? "Save file as" : "Save file", "", allowedFileTypes);
var dialog = new SaveDialog(saveAs ? "Save file as" : "Save file", "", _allowedFileTypes);
dialog.DirectoryPath = currentDirectory;
Application.Run(dialog);

Expand Down

0 comments on commit 8b04015

Please sign in to comment.